<?php
// public/index.php
@session_start();
$config = require __DIR__ . '/../config.php';
require_once __DIR__ . '/../src/Auth.php';
$auth = new Auth($config);

$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $senha = $_POST['senha'] ?? '';
    $lab = $auth->loginLab($email, $senha);
    if ($lab) {
        $_SESSION['lab_id'] = $lab['id'];
        $_SESSION['lab_nome'] = $lab['nome'];
        header('Location: dashboard.php');
        exit;
    } else {
        $error = 'Credenciais inválidas';
    }
}
?>
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Login - Assinador</title>
  <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<script>
function openPasswordModal(labId) {
    const iframe = document.getElementById('passwordIframe');
    iframe.src = 'troca_senha.php'; // passa ID do lab
    document.getElementById('passwordModal').classList.remove('hidden');
}

function closePasswordModal() {
    const iframe = document.getElementById('passwordIframe');
    iframe.src = ''; // limpa conteúdo ao fechar
    document.getElementById('passwordModal').classList.add('hidden');
}
</script>

<body class="bg-gray-50">
  <div class="max-w-md mx-auto mt-20 bg-white p-8 rounded shadow">
    <h1 class="text-2xl mb-4">Entrar</h1>
    <?php if ($error): ?><div class="bg-red-100 text-red-700 p-2 mb-3"><?=htmlspecialchars($error)?></div><?php endif; ?>
    <form method="post" class="space-y-3">
      <input name="email" type="email" placeholder="Email" required class="w-full border p-2 rounded" />
      <input name="senha" type="password" placeholder="Senha" required class="w-full border p-2 rounded" />
      <div class="flex justify-between items-center">
<button type="button" onclick="openPasswordModal()" 
        class="bg-blue-600 text-white px-2 py-1 rounded text-sm">
    Trocar senha
</button>
          <!--<a href="register.php" class="text-blue-600">Cadastrar laboratório</a>-->
        <button class="bg-green-600 text-white px-4 py-2 rounded">Entrar</button>
      </div>
    </form>
  </div>

<!-- Modal -->
<div id="passwordModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden">
    <div class="bg-white rounded shadow w-11/12 max-w-2xl h-4/5 flex flex-col">
        <div class="flex justify-between items-center p-4 border-b">
            <h3 class="text-lg font-semibold">Troca de Senha</h3>
            <button type="button" onclick="closePasswordModal()" class="text-gray-500">&times;</button>
        </div>
        <iframe id="passwordIframe" src="" class="flex-1 w-full border-0"></iframe>
    </div>
</div>
</body>
</html>
