<?php
require_once __DIR__ . '/config/app.php';

if (isLoggedIn()) {
    redirect('/dashboard.php');
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    if (login($username, $password)) {
        redirect('/dashboard.php');
    } else {
        $error = 'Identifiants incorrects ou compte désactivé.';
    }
}
?>
<!DOCTYPE html>
<html lang="fr" dir="ltr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Connexion - <?= APP_NAME ?></title>
    <link href="<?= APP_URL ?>/assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="<?= APP_URL ?>/assets/css/bootstrap-icons.min.css" rel="stylesheet">
    <style>
        body {
            min-height: 100vh;
            background: linear-gradient(135deg, #0c4a6e 0%, #065f46 50%, #1e3a5f 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        .login-container {
            width: 100%;
            max-width: 440px;
            padding: 1rem;
        }
        .login-card {
            background: rgba(255,255,255,0.97);
            border-radius: 20px;
            box-shadow: 0 25px 60px rgba(0,0,0,0.3);
            overflow: hidden;
        }
        .login-header {
            background: linear-gradient(135deg, #0c4a6e, #065f46);
            color: white;
            padding: 2.5rem 2rem 2rem;
            text-align: center;
        }
        .login-header h1 { font-size: 1.5rem; margin: 0.5rem 0 0.25rem; font-weight: 700; }
        .login-header p { opacity: 0.85; font-size: 0.9rem; margin: 0; }
        .login-icon { font-size: 3rem; }
        .login-body { padding: 2rem; }
        .form-floating > .form-control { border-radius: 12px; border: 2px solid #e2e8f0; }
        .form-floating > .form-control:focus { border-color: #065f46; box-shadow: 0 0 0 0.2rem rgba(6,95,70,0.15); }
        .btn-login {
            background: linear-gradient(135deg, #0c4a6e, #065f46);
            border: none;
            border-radius: 12px;
            padding: 0.8rem;
            font-size: 1.1rem;
            font-weight: 600;
            transition: all 0.3s;
        }
        .btn-login:hover { transform: translateY(-2px); box-shadow: 0 8px 25px rgba(6,95,70,0.3); }
    </style>
</head>
<body>
<div class="login-container">
    <div class="login-card">
        <div class="login-header">
            <div class="login-icon">🕌</div>
            <h1><?= APP_NAME ?></h1>
            <p>Système Intégré de Gestion – Hajj & Omra</p>
        </div>
        <div class="login-body">
            <?php if ($error): ?>
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <i class="bi bi-exclamation-triangle-fill me-2"></i><?= $error ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            <?php endif; ?>
            <form method="POST" autocomplete="off">
                <div class="form-floating mb-3">
                    <input type="text" class="form-control" id="username" name="username" placeholder="Nom d'utilisateur" required autofocus>
                    <label for="username"><i class="bi bi-person me-2"></i>Nom d'utilisateur</label>
                </div>
                <div class="form-floating mb-4">
                    <input type="password" class="form-control" id="password" name="password" placeholder="Mot de passe" required>
                    <label for="password"><i class="bi bi-lock me-2"></i>Mot de passe</label>
                </div>
                <button type="submit" class="btn btn-login btn-primary w-100">
                    <i class="bi bi-box-arrow-in-right me-2"></i>Se connecter
                </button>
            </form>
            <div class="text-center mt-4 text-muted small">
                <p class="mb-0">&copy; <?= date('Y') ?> <?= APP_NAME ?> v<?= APP_VERSION ?></p>
            </div>
        </div>
    </div>
</div>
<script src="<?= APP_URL ?>/assets/js/bootstrap.bundle.min.js"></script>
</body>
</html>
