<?php
session_start();
require_once '../db.php';

$erreur = '';

if (isset($_SESSION['user_id']) && isset($_SESSION['role']) && $_SESSION['role'] === 'admin') {
    header('Location: modifsparcswait.php');
    exit;
}

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

    if (!empty($email) && !empty($password)) {
        try {
            $stmt = $pdo->prepare("SELECT * FROM utilisateurs WHERE email = ?");
            $stmt->execute([$email]);
            $user = $stmt->fetch();

            if ($user && password_verify($password, $user['password'])) {
                if ($user['role'] === 'admin') {
                    $_SESSION['user_id'] = $user['id'];
                    $_SESSION['nom'] = $user['nom'];
                    $_SESSION['role'] = $user['role'];

                    header('Location: modifsparcswait.php');
                    exit;
                } else {
                    $erreur = "Accès refusé. Réservé aux administrateurs.";
                }
            } else {
                $erreur = "Identifiants incorrects.";
            }
        } catch (PDOException $e) {
            $erreur = "Erreur système : " . $e->getMessage();
        }
    } else {
        $erreur = "Veuillez remplir tous les champs.";
    }
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Connexion Administration | ParcoTrip</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="../css/style.css">
    <style>
        .admin-login-body { background: #f1f2f6; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; font-family: 'Plus Jakarta Sans', sans-serif; }
        .login-card { background: #ffffff; padding: 40px; border-radius: 20px; box-shadow: 0 15px 35px rgba(0,0,0,0.05); width: 100%; max-width: 420px; border: 1px solid #e4e7eb; box-sizing: border-box; }
        .login-card .logo { text-align: center; margin-bottom: 30px; font-size: 1.8rem; font-weight: 800; }
        .login-card .logo span { color: #4a51ff; }
        .login-card h2 { font-size: 1.3rem; font-weight: 700; color: #1a202c; margin-bottom: 24px; text-align: center; }
        .form-group { margin-bottom: 20px; }
        .form-group label { display: block; font-size: 0.85rem; font-weight: 600; color: #4a5568; margin-bottom: 8px; }
        .form-control { width: 100%; padding: 12px 16px; border-radius: 10px; border: 1px solid #cbd5e1; font-family: inherit; font-size: 0.95rem; box-sizing: border-box; }
        .form-control:focus { outline: none; border-color: #4a51ff; box-shadow: 0 0 0 3px rgba(74, 81, 255, 0.1); }
        .btn-admin-submit { width: 100%; padding: 14px; background: #1a202c; color: #ffffff; border: none; border-radius: 10px; font-weight: 700; font-size: 0.95rem; cursor: pointer; margin-top: 10px; }
        .btn-admin-submit:hover { background: #4a51ff; }
        .alert-danger { background: #ffebee; color: #c62828; padding: 12px 16px; border-radius: 10px; font-size: 0.85rem; font-weight: 600; margin-bottom: 20px; border: 1px solid #ffcdd2; }
    </style>
</head>
<body class="admin-login-body">
    <div class="login-card">
        <div class="logo">PARCO<span>TRIP</span></div>
        <h2>Espace Connexion Admin 🔐</h2>
        <?php if (!empty($erreur)): ?><div class="alert-danger"><?php echo $erreur; ?></div><?php endif; ?>
        <form action="login.php" method="POST">
            <div class="form-group">
                <label for="email">Adresse Email</label>
                <input type="email" name="email" id="email" class="form-control" required placeholder="admin@parcotrip.com">
            </div>
            <div class="form-group">
                <label for="password">Mot de passe</label>
                <input type="password" name="password" id="password" class="form-control" required placeholder="••••••••">
            </div>
            <button type="submit" class="btn-admin-submit">Se connecter</button>
        </form>
    </div>
</body>
</html>