<?php
// rec_form.php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = $_POST['email'];
    $token = $_POST['token'];
    $new_password = md5($_POST['new_password']); // A senha será armazenada como MD5

    // Conexão ao banco
    $conn = new mysqli('91.239.208.209', 'root', 'jotapega1x', 'dbo_acc');

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Verifica o token
    $result = $conn->query("SELECT token FROM password_recovery_tokens WHERE email = '$email'");
    $row = $result->fetch_assoc();
    
    if ($row && $row['token'] === $token) {
        // Atualiza a senha
        $conn->query("UPDATE accounts SET Password_hash = '$new_password' WHERE email = '$email'");

        // Remove o token usado
        $conn->query("DELETE FROM password_recovery_tokens WHERE email = '$email'");

        $message = "<p style='color: green;'>Senha alterada com sucesso!<br><a href='/index.html'>Clique aqui para acessar o site</a></p>";
    } else {
        $message = "<p style='color: red;'>Token inválido ou não encontrado.</p>";
    }

    $conn->close();
}
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Alterar Senha</title>
    <style>
        body {
            background-image: url('fundo.jpg');
            background-size: cover;
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .form-container {
            background-color: rgba(255, 255, 255, 0.9); /* Branco com 90% de opacidade */
            padding: 20px; /* Reduzido */
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 300px; /* Reduzido */
            text-align: left; /* Alinhado à esquerda */
            font-size: 14px; /* Reduzido */
        }
        h2 {
            color: #333;
            font-size: 16px; /* Reduzido */
            text-align: center;
        }
        label {
            display: block; /* Cada label em linha separada */
            margin-bottom: 5px;
            font-weight: bold;
        }
        input {
            width: calc(100% - 20px); /* Reduzido */
            padding: 8px; /* Reduzido */
            margin-bottom: 10px; /* Reduzido */
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 14px; /* Reduzido */
        }
        button {
            background-color: orange;
            color: white;
            padding: 8px 15px; /* Reduzido */
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px; /* Reduzido */
            display: block;
            width: 100%; /* Botão ocupa toda a largura */
        }
        button:hover {
            background-color: #e67e22;
        }
        .message {
            margin-top: 10px; /* Reduzido */
            font-size: 12px; /* Reduzido */
            text-align: center;
        }
        .success {
            color: green;
        }
        .error {
            color: red;
        }
    </style>
</head>
<body>

<div class="form-container">
    <h2>Alterar Senha</h2>
    <form method="POST">
        <label for="email">Email:</label>
        <input type="email" name="email" required>
        
        <label for="token">Token:</label>
        <input type="text" name="token" required>
        
        <label for="new_password">Nova Senha:</label>
        <input type="password" name="new_password" required>
        
        <button type="submit">Alterar Senha</button>
    </form>
    
    <!-- Exibe mensagem de sucesso ou erro -->
    <?php
    if (isset($message)) {
        echo "<div class='message'>$message</div>";
    }
    ?>
</div>

</body>
</html>
