<?php
// public/register.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') {
    $nome = trim($_POST['nome'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $senha = $_POST['senha'] ?? '';
    // tratar upload do PFX
    $pfxPathDb = null;
    if (!empty($_FILES['pfx']['tmp_name'])) {
        $destDir = __DIR__ . '/../storage/uploads';
        if (!is_dir($destDir)) mkdir($destDir, 0755, true);
        $dest = $destDir . '/pfx_' . time() . '_' . basename($_FILES['pfx']['name']);
        if (move_uploaded_file($_FILES['pfx']['tmp_name'], $dest)) {
            $pfxPathDb = $dest;
        }
    }
    try {
        $auth->createLab($nome, $email, $senha, $pfxPathDb);
        header('Location: index.php');
        exit;
    } catch (Exception $e) {
        $error = $e->getMessage();
    }
}
?>
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Cadastro - 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">
  <div class="max-w-lg mx-auto mt-16 bg-white p-8 rounded shadow">
    <h1 class="text-2xl mb-4">Cadastro do Laboratório</h1>
    <?php if ($error): ?>
      <div class="bg-red-100 text-red-700 p-2 mb-3"><?=htmlspecialchars($error)?></div>
    <?php endif; ?>
    <form method="post" enctype="multipart/form-data" class="space-y-3">
      <input name="nome" required placeholder="Nome do laboratório" class="w-full border p-2 rounded" />
      <input name="email" type="email" required placeholder="Email" class="w-full border p-2 rounded" />
      <input name="senha" type="password" required placeholder="Senha" class="w-full border p-2 rounded" />
      <label class="block">
        <span class="text-sm text-gray-600">Certificado A1 (.pfx) — opcional</span>
        <input name="pfx" type="file" accept=".pfx" class="w-full mt-1" />
      </label>
      <div class="flex justify-between items-center">
        <a href="index.php" class="text-blue-600">Voltar ao login</a>
        <button class="bg-blue-600 text-white px-4 py-2 rounded">Cadastrar</button>
      </div>
    </form>
  </div>
</body>
</html>

