<?php
session_start();
$config = require __DIR__ . '/../config.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']);

$msg = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');

    $new_password = $_POST['new_password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';

    if (!$email || !$new_password || !$confirm_password) {
        $msg = 'Preencha todos os campos.';
    } elseif ($new_password !== $confirm_password) {
        $msg = 'As senhas não coincidem.';
    } else {
        // Verificar se o usuário existe
        $stmt = $pdo->prepare('SELECT id FROM labs WHERE email = ? LIMIT 1');
        $stmt->execute([$email]);
        $lab = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$lab) {
            $msg = 'Usuário não encontrado.';
        } else {
            // Atualizar senha
            $hash = password_hash($new_password, PASSWORD_DEFAULT);
            $stmt = $pdo->prepare('UPDATE labs SET senha_hash = ? WHERE id = ?');
            $stmt->execute([$hash, $lab['id']]);
            $msg = 'Senha alterada com sucesso!';
        }
    }
}
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Trocar Senha</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 flex items-center justify-center">
<div class="bg-white p-8 rounded shadow w-full max-w-md">
    <h1 class="text-xl font-semibold mb-4">Troca de Senha</h1>

    <?php if($msg): ?>
        <div class="mb-4 p-2 rounded <?= strpos($msg, 'sucesso') !== false ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' ?>">
            <?= htmlspecialchars($msg) ?>
        </div>
    <?php endif; ?>

    <form method="post" class="space-y-4">
        <div>
            <label class="block text-sm text-gray-700 mb-1">Email</label>
            <input type="email" name="email" required class="w-full border p-2 rounded">
        </div>
  
        <div>
            <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>
            <label class="block text-sm text-gray-700 mb-1">Confirmar senha</label>
            <input type="password" name="confirm_password" required class="w-full border p-2 rounded">
        </div>
        <button type="submit" class="w-full bg-blue-600 text-white px-4 py-2 rounded">Alterar Senha</button>
    </form>

    <div class="mt-4 text-sm text-gray-500">
        <a href="index.php" class="text-blue

