<?php
require_once __DIR__ . '/../config/master_database.php';
session_start();

if (!empty($_SESSION['super_admin_id'])) {
    header('Location: dashboard.php');
    exit;
}

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

    $stmt = $pdoMaster->prepare("SELECT * FROM super_admins WHERE email = ? AND status = 1");
    $stmt->execute([$email]);
    $admin = $stmt->fetch();

    if ($admin && password_verify($password, $admin['password'])) {
        $_SESSION['super_admin_id']   = $admin['id'];
        $_SESSION['super_admin_name'] = $admin['name'];
        header('Location: dashboard.php');
        exit;
    }
    $error = 'Invalid email or password.';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Super Admin - Seder Core</title>
<link rel="icon" type="image/png" sizes="32x32" href="../assets/img/favicon-32.png?v=5">
<link rel="icon" type="image/png" sizes="16x16" href="../assets/img/favicon-16.png?v=5">
<link rel="apple-touch-icon" href="../assets/img/apple-touch-icon.png?v=5">
<link href="https://fonts.googleapis.com/css2?family=Fraunces:wght@600;700&family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="../assets/css/style.css?v=5">
</head>
<body>
<div class="login-page">
    <div class="login-card">
        <img src="../assets/img/logo.png?v=5" alt="Seder Core" class="login-logo-img">
        <div class="logo-sub">Super Admin Control Panel</div>

        <?php if ($error): ?>
        <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
        <?php endif; ?>

        <form method="POST">
            <div class="form-group">
                <label>Email Address</label>
                <input type="email" name="email" required autofocus>
            </div>
            <div class="form-group">
                <label>Password</label>
                <input type="password" name="password" required>
            </div>
            <button type="submit" class="btn btn-primary btn-block">Sign In</button>
        </form>
    </div>
</div>
</body>
</html>