<?php
require_once __DIR__ . '/../config/database.php';
$settings = getAllSettings($pdo);

if (isAdmin()) redirect(BASE_URL . '/admin/index.php');

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $phone = trim($_POST['phone'] ?? '');
    $password = $_POST['password'] ?? '';
    $stmt = $pdo->prepare("SELECT * FROM users WHERE phone = ? AND role = 'admin'");
    $stmt->execute([$phone]);
    $user = $stmt->fetch();
    if ($user && password_verify($password, $user['password'])) {
        $_SESSION['user_id'] = $user['id'];
        $_SESSION['user_name'] = $user['name'];
        $_SESSION['role'] = 'admin';
        redirect(BASE_URL . '/admin/index.php');
    } else {
        $error = 'ফোন নম্বর বা পাসওয়ার্ড সঠিক নয়।';
    }
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>অ্যাডমিন লগইন</title>
    <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { font-family: 'Hind Siliguri', sans-serif; background: #1A2A4F; min-height: 100vh; display: flex; align-items: center; justify-content: center; }
        .login-box { background: white; border-radius: 16px; padding: 48px 40px; width: 100%; max-width: 420px; box-shadow: 0 20px 60px rgba(0,0,0,0.3); }
        .login-logo { text-align: center; margin-bottom: 28px; }
        .login-logo .icon { font-size: 3rem; color: #D4AF37; }
        .login-logo h2 { font-size: 1.5rem; font-weight: 700; color: #1A2A4F; margin-top: 8px; }
        .login-logo p { color: #6b6860; font-size: 0.9rem; }
        .form-group { margin-bottom: 18px; }
        .form-label { display: block; font-weight: 600; color: #1A2A4F; margin-bottom: 6px; font-size: 0.9rem; }
        .form-control { width: 100%; padding: 11px 14px; border: 1.5px solid #e8e6e1; border-radius: 8px; font-family: inherit; font-size: 0.95rem; outline: none; transition: all 0.2s; }
        .form-control:focus { border-color: #D4AF37; box-shadow: 0 0 0 3px rgba(212,175,55,0.15); }
        .btn { display: block; width: 100%; background: #1A2A4F; color: #D4AF37; border: none; padding: 13px; border-radius: 8px; font-family: inherit; font-size: 1rem; font-weight: 700; cursor: pointer; transition: all 0.2s; }
        .btn:hover { background: #D4AF37; color: #1A2A4F; }
        .alert { padding: 12px 16px; border-radius: 8px; margin-bottom: 16px; font-size: 0.9rem; background: rgba(229,62,62,0.1); color: #c53030; border: 1px solid rgba(229,62,62,0.2); }
    </style>
</head>
<body>
    <div class="login-box">
        <div class="login-logo">
            <div class="icon"><i class="fas fa-user-shield"></i></div>
            <h2>অ্যাডমিন প্যানেল</h2>
            <p>শুধুমাত্র অ্যাডমিনদের জন্য</p>
        </div>
        <?php if ($error): ?>
            <div class="alert"><i class="fas fa-exclamation-circle"></i> <?= e($error) ?></div>
        <?php endif; ?>
        <form method="POST">
            <div class="form-group">
                <label class="form-label">মোবাইল নম্বর</label>
                <input type="tel" class="form-control" name="phone" placeholder="০১XXXXXXXXX" required>
            </div>
            <div class="form-group">
                <label class="form-label">পাসওয়ার্ড</label>
                <input type="password" class="form-control" name="password" required>
            </div>
            <button type="submit" class="btn">লগইন করুন</button>
        </form>
    </div>
</body>
</html>
