<?php
require_once 'main/initial.php';

$error_message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = check($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    if (empty($email) || empty($password)) {
        $error_message = 'جميع الحقول مطلوبة.';
    } else {
        $stmt = $con->prepare('SELECT * FROM admins WHERE email = ?');
        $stmt->execute([$email]);
        $admin = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($admin && password_verify($password, $admin['password'])) {
            $_SESSION['admin_id'] = $admin['id'];
            $_SESSION['admin_email'] = $admin['email'];
            header('Location: admin_dashboard.php');
            exit;
        } else {
            $error_message = 'البريد الإلكتروني أو كلمة المرور غير صحيحة.';
        }
    }
}
$page_title = 'تسجيل دخول المشرف';
include 'includes/header.php';
?>
<div class="container" style="max-width:400px;margin:3rem auto;">
    <div class="card p-4 shadow">
        <h3 class="mb-3 text-center">تسجيل دخول المشرف</h3>
        <?php if ($error_message): ?>
            <div class="alert alert-danger"><?php echo htmlspecialchars($error_message); ?></div>
        <?php endif; ?>
        <form method="POST" action="">
            <div class="mb-3">
                <label for="email" class="form-label">البريد الإلكتروني</label>
                <input type="email" class="form-control" id="email" name="email" required autofocus>
            </div>
            <div class="mb-3">
                <label for="password" class="form-label">كلمة المرور</label>
                <input type="password" class="form-control" id="password" name="password" required>
            </div>
            <div class="d-grid">
                <button type="submit" class="btn btn-primary">تسجيل الدخول</button>
            </div>
        </form>
    </div>
</div>
<?php include 'includes/footer.php'; ?>