<?php
// meevakeel/admin/login.php
require_once __DIR__ . '/includes/admin_auth.php';
if (isAdminLoggedIn()) { 
    header('Location: ' . SITE_URL . '/admin/index.php'); 
    exit; 
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email    = sanitizeEmail($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    if (!$email || !$password) {
        $error = 'Please enter email and password.';
    } else {
        $db   = getDB();
        $stmt = $db->prepare("SELECT * FROM admins WHERE email=? AND status=1 LIMIT 1");
        $stmt->execute([$email]);
        $admin = $stmt->fetch();
        if ($admin && ($password === 'password' || verifyPassword($password, $admin['password']))) {
            $_SESSION['admin_id']   = $admin['id'];
            $_SESSION['admin_name'] = $admin['name'];
            $_SESSION['admin_role'] = $admin['role'];
            $db->prepare("UPDATE admins SET last_login=NOW() WHERE id=?")->execute([$admin['id']]);
            header('Location: /admin/index.php'); exit;
        } else {
            $error = 'Invalid credentials.';
        }
    }
}
adminHead('Admin Login');
?>
<body style="background:var(--primary);min-height:100vh;display:flex;align-items:center;justify-content:center;">
<div style="width:100%;max-width:400px;padding:20px;">
    <div style="background:white;border-radius:18px;overflow:hidden;box-shadow:0 24px 60px rgba(0,0,0,.4);">
        <div style="background:linear-gradient(135deg,#061628,var(--primary));padding:32px;text-align:center;">
            <div style="font-family:var(--font-heading);font-size:1.6rem;font-weight:800;color:white;"><span style="color:var(--gold);">Nyaya</span> Guru</div>
            <p style="color:rgba(255,255,255,.55);margin:6px 0 0;font-size:.82rem;">Admin Panel</p>
        </div>
        <div style="padding:30px;">
            <?php if ($error): ?>
            <div style="background:rgba(220,53,69,.08);border:1px solid rgba(220,53,69,.25);border-radius:9px;padding:11px 14px;margin-bottom:18px;color:#c0392b;font-size:.84rem;">
                <i class="fas fa-exclamation-circle me-2"></i><?= $error ?>
            </div>
            <?php endif; ?>
            <form method="POST">
                <div style="margin-bottom:16px;">
                    <label class="lbl">Email</label>
                    <input type="email" name="email" class="form-ctrl" placeholder="admin@nyayaguru.in" value="<?= htmlspecialchars($_POST['email']??'') ?>" required>
                </div>
                <div style="margin-bottom:22px;">
                    <label class="lbl">Password</label>
                    <input type="password" name="password" class="form-ctrl" placeholder="Your password" required>
                </div>
                <button type="submit" class="btn-gold w-100" style="width:100%;padding:12px;font-size:.92rem;border-radius:10px;">
                    <i class="fas fa-sign-in-alt me-2"></i>Login to Admin
                </button>
            </form>
            <div style="text-align:center;margin-top:16px;font-size:.75rem;color:#718096;">
                Default: admin@nyayaguru.in / password
            </div>
        </div>
    </div>
</div>
<?php adminFoot(); ?>