<?php
require_once __DIR__ . "/../config/db.php";
session_start();

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $email = $_POST['email'] ?? '';
    $pass  = $_POST['password'] ?? '';

    $stmt = $pdo->prepare("SELECT * FROM users WHERE email=? LIMIT 1");
    $stmt->execute([$email]);
    $user = $stmt->fetch();

    if ($user && password_verify($pass, $user['password'])) {

        $_SESSION['admin_id'] = $user['id'];
        header("Location: dashboard.php");
        exit;

    } else {
        $error = "Email və ya şifrə yanlışdır";
    }
}
?>

<h2>Admin Login</h2>

<form method="post">
    <input type="email" name="email" placeholder="Email" required>
    <br><br>
    <input type="password" name="password" placeholder="Şifrə" required>
    <br><br>
    <button>Giriş</button>
</form>

<?php if($error): ?>
<p><?=$error?></p>
<?php endif; ?>