<?php
require_once 'auth.php'; 

if (isLogged()) {
    header('Location: ../index.php');
    exit;
}

$erreur = '';

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

    if (!empty($email) && !empty($password)) {
        $resultat_connexion = loginUser($email, $password);

        if ($resultat_connexion === true) {
            header('Location: ../index.php');
            exit;
        } elseif ($resultat_connexion === "non_valide") {
            $erreur = "Votre compte est en attente de validation par la mairie.";
        } else {
            $erreur = "Identifiants incorrects.";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Connexion - Saint Vaast en Chaussée</title>
    <link rel="stylesheet" href="../css/global.css">
    <style>
        /* On ne garde ici QUE ce qui concerne le formulaire de connexion */
        .login-box {
            max-width: 450px;
            margin: 0 auto;
            padding: 10px;
        }
        
        .login-box h2 { text-align: center; color: var(--nature-vert, #4a7c44); margin-top: 0; margin-bottom: 25px; }
        
        .login-box label { display: block; margin-bottom: 5px; font-size: 0.9em; font-weight: bold; color: var(--nature-texte, #333); }
        .login-box input { width: 100%; padding: 12px; margin-bottom: 20px; border: 2px solid var(--nature-clair, #f0f4f0); border-radius: 15px; box-sizing: border-box; transition: 0.3s; font-family: inherit; }
        .login-box input:focus { border-color: var(--nature-vert, #4a7c44); outline: none; }
        
        .login-box button { background: var(--nature-vert, #4a7c44); color: white; border: none; padding: 12px; width: 100%; border-radius: 30px; font-weight: bold; cursor: pointer; font-size: 1.1em; transition: opacity 0.2s; margin-top: 5px; }
        .login-box button:hover { opacity: 0.9; }
        
        .erreur { color: #c33; background: #fee; border: 1px solid #fcc; padding: 15px; border-radius: 15px; margin-bottom: 20px; text-align: center; font-weight: bold; }
        
        .liens-bas { text-align: center; margin-top: 25px; font-size: 0.95em; color: #666; }
        .liens-bas a { color: var(--nature-vert, #4a7c44); text-decoration: underline; font-weight: bold; transition: 0.2s; display: inline-block; margin-top: 10px; }
        .liens-bas a:hover { color: #365c32; }
    </style>
</head>
<body>
<div class="site-container">
    <img src="../images/banniere.png" alt="Bannière" class="banner-img">
    <nav><?php include '../includes/menu.php'; ?></nav>
    
    <main class="main-wrapper">
        <div class="login-box">
            <h2>Connexion</h2>
            
            <?php if($erreur) echo "<div class='erreur'>$erreur</div>"; ?>
            
            <form method="POST">
                <div>
                    <label for="email">Adresse Email</label>
                    <input type="email" id="email" name="email" required>
                </div>
                
                <div>
                    <label for="password">Mot de passe</label>
                    <input type="password" id="password" name="password" required>
                </div>
                
                <button type="submit">Se connecter</button>
            </form>
            
            <div class="liens-bas">
                Vous n'avez pas encore de compte ? <br>
                <a href="register.php">Créer un compte habitant</a>
            </div>
        </div>
    </main>
</div>
</body>
</html>