<?php
session_start();

// Si déjà connecté, rediriger vers le dashboard
if (isset($_SESSION['user_id'])) {
    header("Location: index.php");
    exit();
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Faux login pour test si la DB n'est pas encore montée : (admin / admin)
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';

    // En production, vous feriez :
    // require '../config/database.php';
    // $stmt = $pdo->prepare("SELECT id, username, password_hash FROM users WHERE username = ?");
    // $stmt->execute([$username]);
    // $user = $stmt->fetch();
    // if ($user && password_verify($password, $user['password_hash'])) { ... }

    if ($username === 'admin' && $password === 'admin') {
        $_SESSION['user_id'] = 1; // Faux ID
        $_SESSION['username'] = 'admin';
        $_SESSION['role'] = 'admin';
        header("Location: index.php");
        exit();
    } else {
        $error = "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 Administration - Kutzenhausen</title>
    <script src="../js/tailwind.min.js"></script>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center font-sans">
    
    <div class="bg-white p-8 rounded-xl shadow-lg w-full max-w-md border-t-4 border-[#7b2c2d]">
        <div class="text-center mb-8">
            <h1 class="text-2xl font-bold text-gray-800">Espace Administration</h1>
            <p class="text-sm text-gray-500 mt-2">Commune de Kutzenhausen</p>
        </div>

        <?php if($error): ?>
            <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 mb-6 rounded" role="alert">
                <p><?php echo $error; ?></p>
            </div>
        <?php endif; ?>

        <form action="login.php" method="POST" class="space-y-6">
            <div>
                <label for="username" class="block text-sm font-medium text-gray-700">Nom d'utilisateur</label>
                <input type="text" name="username" id="username" required 
                       class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-md shadow-sm focus:ring-[#7b2c2d] focus:border-[#7b2c2d]">
            </div>

            <div>
                <label for="password" class="block text-sm font-medium text-gray-700">Mot de passe</label>
                <input type="password" name="password" id="password" required 
                       class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-md shadow-sm focus:ring-[#7b2c2d] focus:border-[#7b2c2d]">
            </div>

            <div>
                <button type="submit" 
                        class="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-[#7b2c2d] hover:bg-[#5c1a1b] focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-[#7b2c2d] transition">
                    Se connecter
                </button>
            </div>
            
            <div class="text-center text-xs text-gray-400 mt-4">
                Test : identifiant "admin", mot de passe "admin"
            </div>
        </form>
    </div>

</body>
</html>
