<?php
session_start();
if (isset($_SESSION['admin_logged_in'])) {
    header('Location: dashboard.php');
    exit;
}

require_once '../src/config/db.php';
require_once '../src/includes/functions.php';
require_once 'includes/lang.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);

    if (!empty($username) && !empty($password)) {
        $user = get_user_by_username($pdo, $username);
        if ($user && password_verify($password, $user['password'])) {
            $_SESSION['admin_logged_in']  = true;
            $_SESSION['admin_username']   = $user['username'];
            $_SESSION['admin_name']       = $user['full_name'];
            header('Location: dashboard.php');
            exit;
        } else {
            $error = $t['admin_invalid_login'];
        }
    } else {
        $error = $isRTL ? 'يرجى ملء جميع الحقول.' : 'Please fill in all fields.';
    }
}
?>
<!DOCTYPE html>
<html lang="<?php echo $t['lang']; ?>" dir="<?php echo $t['dir']; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $t['admin_login_title']; ?> - VAULTIXAI</title>
    <link rel="stylesheet" href="<?php echo $bootstrapCSS; ?>" crossorigin="anonymous">
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body class="login-page">

    <div class="login-card">

        <span class="brand-shield">🛡️</span>
        <h1 class="text-center fw-black mb-1" style="font-size:1.8rem; color:#080f1e; letter-spacing:2px;">VAULTIXAI</h1>
        <p class="text-center text-muted mb-4" style="font-size:.9rem;"><?php echo $t['admin_login_subtitle']; ?></p>

        <?php if (isset($error)): ?>
            <div class="alert alert-danger fw-bold small mb-4">
                ⚠️ <?php echo htmlspecialchars($error); ?>
            </div>
        <?php endif; ?>

        <form method="POST" novalidate>
            <div class="mb-3">
                <label for="username" class="form-label"><?php echo $t['admin_username']; ?></label>
                <input type="text" id="username" name="username"
                       class="form-control form-control-lg"
                       placeholder="<?php echo $t['admin_username']; ?>"
                       required autocomplete="username">
            </div>
            <div class="mb-4">
                <label for="password" class="form-label"><?php echo $t['admin_password']; ?></label>
                <input type="password" id="password" name="password"
                       class="form-control form-control-lg"
                       placeholder="••••••••"
                       required autocomplete="current-password">
            </div>
            <button type="submit" class="btn btn-gold w-100 py-2 fw-bold fs-6">
                🔐 <?php echo $t['admin_login_btn']; ?>
            </button>
        </form>

        <!-- Language Switcher -->
        <div class="text-center mt-4">
            <a href="<?php echo lang_switch_url($t['lang_switch_code']); ?>" class="text-muted text-decoration-none small fw-bold">
                🌐 <?php echo $t['lang_switch_label']; ?>
            </a>
        </div>

    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
</body>
</html>
