<?php
$config = require __DIR__ . '/../config.php';
$config = require __DIR__ . '/../config.php';
require_once __DIR__ . '/../src/Auth.php';
// Conexão PDO
$pdo = new PDO("mysql:host={$config->db['host']};dbname={$config->db['name']};charset={$config->db['charset']}", $config->db['user'], $config->db['pass']);
// Deletar laboratório
if (isset($_POST['delete_id'])) {
    $stmt = $pdo->prepare('DELETE FROM labs WHERE id = ?');
    $stmt->execute([$_POST['delete_id']]);
    header('Location: dashboard_labs.php');
    exit;
}

// Troca de senha
if (isset($_POST['change_id'], $_POST['new_password'])) {
    $hash = password_hash($_POST['new_password'], PASSWORD_DEFAULT);
    $stmt = $pdo->prepare('UPDATE labs SET senha = ? WHERE id = ?');
    $stmt->execute([$hash, $_POST['change_id']]);
    header('Location: dashboard_labs.php');
    exit;
}

// Buscar laboratórios
$stmt = $pdo->query('SELECT id, nome, email FROM labs ORDER BY nome');
$labs = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Gerenciar Laboratórios</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 p-4 flex justify-between items-center">
    <h1 class="text-lg font-semibold">Gerenciar Laboratórios</h1>
    <a href="logout.php" class="text-red-600">Sair</a>
  </header>

  <main class="max-w-5xl mx-auto p-6">
    <div class="bg-white p-6 rounded shadow">
      <h2 class="text-lg font-semibold mb-4">Lista de Laboratórios</h2>
      <?php if (!$labs): ?>
        <p class="text-gray-500">Nenhum laboratório cadastrado.</p>
      <?php else: ?>
        <table class="w-full border-collapse">
          <thead>
            <tr class="bg-gray-100 text-left">
              <th class="p-2 border">ID</th>
              <th class="p-2 border">Nome</th>
              <th class="p-2 border">Email</th>
              <th class="p-2 border">Ações</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($labs as $lab): ?>
              <tr class="hover:bg-gray-50">
                <td class="p-2 border"><?= $lab['id'] ?></td>
                <td class="p-2 border"><?= htmlspecialchars($lab['nome']) ?></td>
                <td class="p-2 border"><?= htmlspecialchars($lab['email']) ?></td>
                <td class="p-2 border space-x-2">
                  <!-- Troca de senha -->
                  <button onclick="openPasswordModal(<?= $lab['id'] ?>, '<?= htmlspecialchars(addslashes($lab['nome'])) ?>')" class="bg-blue-600 text-white px-2 py-1 rounded text-sm">Trocar senha</button>
                  <!-- Deletar laboratório -->
                  <form action="" method="post" style="display:inline;" onsubmit="return confirm('Deseja realmente deletar <?= htmlspecialchars($lab['nome']) ?>?');">
                    <input type="hidden" name="delete_id" value="<?= $lab['id'] ?>">
                    <button type="submit" class="bg-red-600 text-white px-2 py-1 rounded text-sm">Deletar</button>
                  </form>
                </td>
              </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      <?php endif; ?>
    </div>
  </main>

  <!-- Modal de troca de senha -->
  <div id="passwordModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden">
    <div class="bg-white rounded p-6 w-96">
      <h3 class="text-lg font-semibold mb-4">Trocar senha</h3>
      <form id="passwordForm" method="post">
        <input type="hidden" name="change_id" id="change_id">
        <div class="mb-4">
          <label class="block text-sm text-gray-700 mb-1">Nova senha</label>
          <input type="password" name="new_password" required class="w-full border p-2 rounded">
        </div>
        <div class="flex justify-end space-x-2">
          <button type="button" onclick="closePasswordModal()" class="px-4 py-2 rounded border">Cancelar</button>
          <button type="submit" class="bg-blue-600 text-white px-4 py-2 rounded">Salvar</button>
        </div>
      </form>
    </div>
  </div>

  <script>
    function openPasswordModal(id, name) {
      document.getElementById('change_id').value = id;
      document.getElementById('passwordModal').classList.remove('hidden');
    }
    function closePasswordModal() {
      document.getElementById('passwordModal').classList.add('hidden');
    }
  </script>
</body>
</html>