<?php
declare(strict_types=1);
require_once __DIR__ . '/queue_bootstrap.php';

if (!empty($_SESSION['user_id'])) {
    qh_redirect('index.php');
}

$error = '';

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

    if ($username === '' || $password === '') {
        $error = qh_t('Please enter your username and password.', 'يرجى إدخال اسم المستخدم وكلمة المرور.');
    } else {
        try {
            $stmt = db()->prepare("SELECT id, password, role FROM users WHERE username = :username LIMIT 1");
            $stmt->execute(['username' => $username]);
            $user = $stmt->fetch();

            if ($user && password_verify($password, $user['password'])) {
                $_SESSION['user_id'] = (int) $user['id'];
                $_SESSION['username'] = $username;
                $_SESSION['role'] = $user['role'] ?? 'admin';
                qh_redirect('index.php');
            } else {
                $error = qh_t('Invalid username or password.', 'اسم المستخدم أو كلمة المرور غير صحيحة.');
            }
        } catch (Throwable $e) {
            $error = qh_t('Login failed due to a system error.', 'فشل تسجيل الدخول بسبب خطأ في النظام.');
        }
    }
}

qh_page_start(
    'login',
    qh_t('Sign In', 'تسجيل الدخول'),
    qh_t('Sign in to the hospital queue system.', 'تسجيل الدخول إلى نظام طوابير المستشفى.')
);
?>
<style>
.login-wrapper {
    min-height: calc(100vh - 200px);
    display: flex;
    align-items: center;
    justify-content: center;
}
.login-card {
    width: 100%;
    max-width: 420px;
    border: none;
    border-radius: 12px;
    box-shadow: 0 8px 30px rgba(0,0,0,0.08);
    overflow: hidden;
}
.login-header {
    background: var(--accent, #0F8B8D);
    color: white;
    padding: 2rem 1.5rem;
    text-align: center;
}
.login-body {
    padding: 2rem;
    background: #fff;
}
</style>

<div class="container-fluid container-xxl px-3 px-lg-4">
    <div class="login-wrapper">
        <div class="card login-card">
            <div class="login-header">
                <h3 class="mb-0 fw-bold"><?= qh_h(qh_hospital_name()) ?></h3>
                <p class="text-white-50 mt-2 mb-0"><?= qh_h(qh_t('Secure Access', 'الوصول الآمن')) ?></p>
            </div>
            <div class="login-body">
                <?php if ($error !== ''): ?>
                    <div class="alert alert-danger mb-4"><?= qh_h($error) ?></div>
                <?php endif; ?>
                
                <form method="POST" action="<?= qh_h(qh_url('login.php')) ?>">
                    <div class="mb-4">
                        <label class="form-label text-secondary fw-semibold"><?= qh_h(qh_t('Username', 'اسم المستخدم')) ?></label>
                        <input type="text" name="username" class="form-control form-control-lg bg-light" required autofocus>
                    </div>
                    <div class="mb-4">
                        <label class="form-label text-secondary fw-semibold"><?= qh_h(qh_t('Password', 'كلمة المرور')) ?></label>
                        <input type="password" name="password" class="form-control form-control-lg bg-light" required>
                    </div>
                    <button type="submit" class="btn btn-primary w-100 py-3 rounded-pill fw-bold" style="font-size: 1.1rem;">
                        <?= qh_h(qh_t('Sign In', 'تسجيل الدخول')) ?>
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>

<?php qh_page_end(); ?>