<?php
// public/dashboard.php
session_start();
if (empty($_SESSION['lab_id'])) { header('Location: index.php'); exit; }
$config = require __DIR__ . '/../config.php';
require_once __DIR__ . '/../src/Auth.php';
$auth = new Auth($config);
$lab = $auth->getLabById((int)$_SESSION['lab_id']);
?>
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Dashboard - Assinador</title>
  <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-50 min-h-screen">
  <header class="bg-white shadow">
    <div class="max-w-5xl mx-auto p-4 flex justify-between items-center">
      <div>
        <h1 class="text-lg font-semibold">Assinador de Documentos — <?=htmlspecialchars($lab['nome'] ?? 'Laboratório')?></h1>

    
      </div>
      <div>
        <a href="logout.php" class="text-red-600">Sair</a>
      </div>
    </div>
  </header>

  <main class="max-w-4xl mx-auto p-6">
    <div class="bg-white p-6 rounded shadow">
      <form action="upload.php" method="post" enctype="multipart/form-data" class="space-y-4">
        <label class="block">
          <span class="text-sm text-gray-700">Arquivos PDF (múltiplos)</span>
          <input id="pdfInput"  name="pdfs[]" type="file" accept="application/pdf" multiple required class="mt-1" />
            <div id="fileList" class="mt-1 text-sm text-gray-600"></div>
        </label>

        <label class="block">
          <span class="text-sm text-gray-700">Enviar PFX</span>
          <input name="pfx" type="file" accept=".pfx" class="mt-1" />
        </label>

        <label class="block">
          <span class="text-sm text-gray-700">Senha do PFX </span>
          <input name="pfx_password" type="password" class="w-full border p-2 rounded mt-1"/>
        </label>

        <div class="flex justify-between items-center">

          <button class="bg-blue-600 text-white px-4 py-2 rounded">Enviar e Processar</button>
        </div>
      </form>
    </div>

    <div class="mt-6">
      <h2 class="text-lg mb-2">Últimos uploads</h2>
      <div class="bg-white p-4 rounded shadow">
        <?php
        // listar últimos 10 uploads
        $pdo = new PDO("mysql:host={$config->db['host']};dbname={$config->db['name']};charset={$config->db['charset']}", $config->db['user'], $config->db['pass']);
        $stmt = $pdo->prepare('SELECT * FROM uploads WHERE lab_id = ? ORDER BY created_at DESC LIMIT 10');
        $stmt->execute([$_SESSION['lab_id']]);
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        if (!$rows) {
            echo '<div class="text-sm text-gray-500">Nenhum upload ainda.</div>';
        } else {
            echo '<ul class="space-y-2">';
            foreach ($rows as $r) {
               
                $signed = $r['signed_filename'] ? '<span class="text-green-600">Assinado</span>' : '<span class="text-orange-600">Processado</span>';

                 $downloadLink = 'download.php?file=' . urlencode(basename($r['signed_filename']));

        // Nome do arquivo de download forçado como PDF
        $downloadName = pathinfo($r['original_filename'], PATHINFO_FILENAME) . '.pdf';

        echo '<li class="flex justify-between items-center border-b pb-2">';
        echo '<div>';
        echo '<strong><a href="'.htmlspecialchars($downloadLink).'" download="'.htmlspecialchars($downloadName).'" class="text-blue-600 text-sm">'
            . htmlspecialchars($r['original_filename']) . '</a></strong>';
        echo '<div class="text-xs text-gray-500">Enviado em '.$r['created_at'].'</div>';
        echo '</div>';

        echo '<div>'.$signed.'</div>';
        echo '</li>';
            }
            echo '</ul>';
        }
        ?>
      </div>
    </div>
  </main>
</body>
<script>
const input = document.getElementById('pdfInput');
const fileList = document.getElementById('fileList');

input.addEventListener('change', () => {
    const files = Array.from(input.files);
    if (files.length === 0) {
        fileList.textContent = '';
        return;
    }
    fileList.innerHTML = files.map(f => f.name).join('<br>');
});
</script>
</html>