<?php
session_start();
require_once '../includes/db.php';
require_once '../includes/functions.php';

// Zaten admin girişi yapmışsa doğrudan panele yönlendir
if (isset($_SESSION['admin_id'])) {
    header("Location: index.php");
    exit;
}

$hata = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['admin_login'])) {
    $email = trim($_POST['email']);
    $sifre = $_POST['password'];

    if (empty($email) || empty($sifre)) {
        $hata = 'Lütfen tüm alanları doldurun.';
    } else {
        // Sadece 'admin' rolüne sahip olanları sorguluyoruz
        $stmt = $db->prepare("SELECT id, name, surname, password FROM users WHERE email = ? AND role = 'admin' AND status = 1");
        $stmt->execute([$email]);
        $admin = $stmt->fetch();

        if ($admin && password_verify($sifre, $admin['password'])) {
            // GÜVENLİK: Oturum kimliğini yenile (Session Fixation koruması)
            session_regenerate_id(true);

            // Admin session'larını müşteri session'larından farklı isimlendiriyoruz
            $_SESSION['admin_id'] = $admin['id'];
            $_SESSION['admin_name'] = $admin['name'] . ' ' . $admin['surname'];
            $_SESSION['admin_role'] = 'admin';

            header("Location: index.php");
            exit;
        } else {
            $hata = 'Geçersiz e-posta veya şifre, ya da admin yetkiniz yok.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Girişi - Sınav Koçu</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        body { background: #f4f7f6; height: 100vh; display: flex; align-items: center; justify-content: center; }
        .login-card { width: 100%; max-width: 400px; border: none; border-radius: 15px; }
        .admin-logo { font-size: 3rem; color: #0d6efd; }
    </style>
</head>
<body>

<div class="card login-card shadow-lg">
    <div class="card-body p-5">
        <div class="text-center mb-4">
            <div class="admin-logo"><i class="bi bi-shield-lock-fill"></i></div>
            <h4 class="fw-bold">Yönetim Paneli</h4>
            <p class="text-muted small">Lütfen yönetici bilgilerinizi girin.</p>
        </div>

        <?php if ($hata): ?>
            <div class="alert alert-danger py-2 small"><i class="bi bi-exclamation-circle me-2"></i><?= $hata ?></div>
        <?php endif; ?>

        <form action="" method="POST">
            <div class="mb-3">
                <label class="form-label small fw-bold">E-Posta</label>
                <div class="input-group">
                    <span class="input-group-text bg-light"><i class="bi bi-envelope"></i></span>
                    <input type="email" name="email" class="form-control" required autofocus>
                </div>
            </div>
            <div class="mb-4">
                <label class="form-label small fw-bold">Şifre</label>
                <div class="input-group">
                    <span class="input-group-text bg-light"><i class="bi bi-key"></i></span>
                    <input type="password" name="password" class="form-control" required>
                </div>
            </div>
            <button type="submit" name="admin_login" class="btn btn-primary w-100 fw-bold py-2 shadow-sm">
                Giriş Yap <i class="bi bi-arrow-right-short"></i>
            </button>
        </form>
        
        <div class="text-center mt-4">
            <a href="../index.php" class="text-decoration-none small text-muted"><i class="bi bi-house"></i> Siteye Dön</a>
        </div>
    </div>
</div>

</body>
</html>