<?php
session_start();
require_once 'config/database.php';

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    $stmt = $pdo->prepare("SELECT * FROM associations WHERE username = ?");
    $stmt->execute([$username]);
    $asso = $stmt->fetch();
    
    if ($asso && password_verify($password, $asso['password_hash'])) {
        $_SESSION['asso_id'] = $asso['id'];
        $_SESSION['asso_name'] = $asso['name'];
        header("Location: asso-admin.php");
        exit;
    } else {
        $error = "Identifiants incorrects.";
    }
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <title>Connexion Association - Kutzenhausen</title>
    <script src="js/tailwind.min.js"></script>
    <script src="js/lucide.min.js"></script>
</head>
<body class="bg-alsace-cream min-h-screen flex items-center justify-center font-sans p-4">
    <div class="bg-white p-10 rounded-2xl shadow-2xl w-full max-w-md border-t-8 border-[#7b2c2d]">
        <div class="text-center mb-10">
            <h1 class="text-3xl font-serif font-bold text-alsace-red">Espace Association</h1>
            <p class="text-sm text-gray-500 mt-2 font-medium">Gestion de vos manifestations</p>
        </div>

        <?php if($error): ?>
            <div class="p-4 bg-red-100 text-red-700 rounded-lg mb-6 font-bold shadow-sm flex items-center">
                <i data-lucide="alert-circle" class="w-5 h-5 mr-2"></i> <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <form action="" method="POST" class="space-y-6">
            <div>
                <label class="block text-sm font-bold text-gray-700 mb-2">Identifiant</label>
                <div class="relative">
                    <i data-lucide="user" class="absolute left-3 top-1/2 -translate-y-1/2 w-5 h-5 text-gray-400"></i>
                    <input type="text" name="username" required class="w-full pl-10 pr-4 py-3 border border-gray-300 rounded-lg outline-none focus:border-[#7b2c2d] focus:ring-1 focus:ring-[#7b2c2d] transition">
                </div>
            </div>
            <div>
                <label class="block text-sm font-bold text-gray-700 mb-2">Mot de passe</label>
                <div class="relative">
                    <i data-lucide="lock" class="absolute left-3 top-1/2 -translate-y-1/2 w-5 h-5 text-gray-400"></i>
                    <input type="password" name="password" required class="w-full pl-10 pr-4 py-3 border border-gray-300 rounded-lg outline-none focus:border-[#7b2c2d] focus:ring-1 focus:ring-[#7b2c2d] transition">
                </div>
            </div>
            <button type="submit" class="w-full bg-[#7b2c2d] text-white font-bold py-4 rounded-lg hover:bg-[#5c1a1b] transition shadow-lg transform active:scale-95">
                Se connecter
            </button>
            <div class="text-center pt-4">
                <a href="index.php" class="text-sm text-gray-500 hover:text-[#7b2c2d] flex items-center justify-center transition">
                    <i data-lucide="arrow-left" class="w-4 h-4 mr-1"></i> Retour au site
                </a>
            </div>
        </form>
    </div>
    <script>lucide.createIcons();</script>
</body>
</html>
