{% extends "index.html" %}
{% block title %}Upload de Faturas{% endblock %}
{% block content %}

<h1 style="font-size: 1.5rem; margin-bottom: 1rem;">📤 Upload de Faturas</h1>

<div class="upload-box" id="upload-box">
  <h3>Arraste faturas em PDF aqui ou clique para selecionar</h3>
  <p style="color: gray; font-size: 0.9rem;">Apenas PDFs textuais (não escaneados)</p>
  <br />
  <button class="btn btn-primary" onclick="document.getElementById('file-input').click()">Selecionar Arquivos</button>
  <input type="file" id="file-input" accept=".pdf" multiple onchange="handleFiles(this.files)" style="display:none;" />
</div>

<div class="buttons">
  <button class="btn btn-primary" onclick="processar(this)">Processar Faturas</button>
  <button class="btn btn-danger" onclick="limpar()">Limpar Tudo</button>
  <button class="btn btn-success" onclick="baixarPlanilha()">📅 Abrir Planilha</button>
  <button class="btn btn-success" onclick="gerarRelatorio()">📊 Gerar Relatório</button>
</div>

<table>
  <thead>
    <tr>
      <th>Arquivo</th>
      <th>Status</th>
      <th>Mensagem</th>
    </tr>
  </thead>
  <tbody id="file-table">
    <tr><td colspan="3" style="text-align: center; color: #aaa;">Nenhum arquivo selecionado.</td></tr>
  </tbody>
</table>

<script>
  let arquivos = [];
  let statusInterval = null;
  const fileTable = document.getElementById('file-table');

  function handleFiles(files) {
    arquivos = [...arquivos, ...files];
    renderTable();
  }

  function renderTable(statusList = []) {
    const rows = statusList.length ? statusList : arquivos.map(file => ({ nome: file.name, status: 'Aguardando', mensagem: '' }));
    fileTable.innerHTML = rows.length
      ? rows.map(file => `
        <tr>
          <td>${file.nome}</td>
          <td class="${file.status === 'Concluido' ? 'status-ok' : file.status === 'Erro' ? 'status-error' : 'status-warn'}">${file.status}</td>
          <td>${file.mensagem || '---'}</td>
        </tr>
      `).join('')
      : '<tr><td colspan="3" style="text-align:center; color: #aaa;">Nenhum arquivo selecionado.</td></tr>';
  }

  async function processar(btn) {
    if (arquivos.length === 0) return alert("Nenhum arquivo selecionado.");
    btn.disabled = true;
    btn.innerText = "⏳ Processando...";

    const formData = new FormData();
    arquivos.forEach(file => formData.append("files", file));

    try {
      await fetch("/upload-files", { method: "POST", body: formData });
      await fetch("/process-queue", { method: "POST" });
      arquivos = [];
      statusInterval = setInterval(updateStatus, 1000);
    } catch (err) {
      alert("Erro ao processar faturas.");
    } finally {
      setTimeout(() => {
        btn.innerText = "Processar Faturas";
        btn.disabled = false;
      }, 1500);
    }
  }

  async function updateStatus() {
    const res = await fetch("/get-status");
    const data = await res.json();
    renderTable(data.files);

    if (!data.is_processing && statusInterval) {
      clearInterval(statusInterval);
    }
  }

  function limpar() {
    fetch("/clear-all", { method: "POST" });
    arquivos = [];
    renderTable();
  }

  function baixarPlanilha() {
    window.open('/export-excel', '_blank');
  }

  function gerarRelatorio() {
    window.open('/generate-report', '_blank');
  }

  const dropZone = document.getElementById('upload-box');
  dropZone.addEventListener('dragover', (e) => {
    e.preventDefault();
    dropZone.classList.add('dragover');
  });
  dropZone.addEventListener('dragleave', () => {
    dropZone.classList.remove('dragover');
  });
  dropZone.addEventListener('drop', (e) => {
    e.preventDefault();
    dropZone.classList.remove('dragover');
    handleFiles(e.dataTransfer.files);
  });

  window.addEventListener('DOMContentLoaded', updateStatus);
</script>

<style>
  .upload-box {
    background: #fff;
    border: 2px dashed #ccc;
    padding: 2rem;
    text-align: center;
    border-radius: 10px;
    margin-bottom: 2rem;
  }
  .upload-box.dragover {
    background-color: #eef2ff;
    border-color: #4361ee;
  }

  .buttons {
    display: flex;
    gap: 1rem;
    justify-content: center;
    flex-wrap: wrap;
    margin-bottom: 2rem;
  }

  .btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease-in-out;
  }
  .btn:hover { opacity: 0.9; }
  .btn-primary { background-color: #4361ee; color: white; }
  .btn-success { background-color: #198754; color: white; }
  .btn-danger { background-color: #dc3545; color: white; }

  table {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 0 10px rgba(0,0,0,0.05);
  }

  th, td {
    padding: 1rem;
    text-align: left;
    border-bottom: 1px solid #eee;
  }
  th {
    background-color: #f2f2f2;
    font-size: 0.85rem;
    color: #555;
    text-transform: uppercase;
    letter-spacing: 1px;
  }

  .status-ok { color: #198754; }
  .status-error { color: #dc3545; }
  .status-warn { color: #ffc107; }
</style>

{% endblock %}