<?php
require_once('includes/functions.php');

if (isset($_SESSION['user_id'])) {
    header("Location: " . ($_SESSION['role'] == 'admin' ? 'admin/dashboard.php' : 'employee/dashboard.php'));
    exit();
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = strtolower(trim($_POST['username']));
    $password = strtolower(trim($_POST['password']));
    
    $user = authenticate($username, $password);
    if ($user) {
        $isActive = isset($user['is_active']) ? $user['is_active'] : true;
        if (!$isActive) {
            $error = "Your account has been deactivated. Please contact the Purser.";
        } else {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['role'] = $user['role'];
            $_SESSION['full_name'] = $user['full_name'];
            header("Location: " . ($user['role'] == 'admin' ? 'admin/dashboard.php' : 'employee/dashboard.php'));
            exit();
        }
    } else {
        $error = "Invalid username or password. Please try again.";
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    <title>RxAero Portal – Crew Login</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Georgia', 'Times New Roman', serif;
            background: linear-gradient(135deg, #6b1d2f 0%, #3b1a24 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        .login-card {
            background: #faf3e8;
            padding: 30px 25px;
            border-radius: 32px;
            width: 100%;
            max-width: 400px;
            box-shadow: 0 20px 35px rgba(0,0,0,0.2);
            border: 1px solid #c9a87c;
            text-align: center;
        }
        h1 {
            font-size: 1.8rem;
            color: #6b1d2f;
            margin-bottom: 8px;
            letter-spacing: 2px;
        }
        .subhead {
            color: #8b6b42;
            font-style: italic;
            margin-bottom: 24px;
            border-bottom: 1px solid #e2cfb3;
            display: inline-block;
            padding-bottom: 4px;
        }
        input {
            width: 100%;
            padding: 14px;
            margin: 10px 0;
            border: 1px solid #d4bc9a;
            border-radius: 40px;
            font-size: 1rem;
            background: white;
            font-family: inherit;
        }
        button {
            width: 100%;
            padding: 14px;
            background: #6b1d2f;
            color: #f9e4c2;
            border: none;
            border-radius: 40px;
            font-size: 1rem;
            font-weight: bold;
            cursor: pointer;
            margin-top: 10px;
            font-family: 'Georgia', serif;
            transition: 0.2s;
        }
        button:hover {
            background: #8b2635;
        }
        .error {
            background: #f2dbdb;
            color: #a03e4b;
            padding: 10px;
            border-radius: 30px;
            margin-bottom: 15px;
            font-size: 0.85rem;
        }
        .info-note {
            margin-top: 20px;
            font-size: 0.7rem;
            color: #8b7355;
        }
        @media (max-width: 480px) {
            .login-card { padding: 25px 20px; }
            h1 { font-size: 1.5rem; }
        }
    </style>
</head>
<body>
    <div class="login-card">
        <h1>✈️ RxAero Portal</h1>
        <div class="subhead">Crew Login</div>
        <?php if($error): ?>
            <div class="error"><?php echo $error; ?></div>
        <?php endif; ?>
        <form method="POST">
            <input type="text" name="username" placeholder="Username (lowercase)" required autocomplete="off">
            <input type="password" name="password" placeholder="Password (lowercase)" required>
            <button type="submit">Sign In ✈️</button>
        </form>
        <div class="info-note">🔒 All credentials are automatically lowercased.</div>
    </div>
</body>
</html>