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

// Redirect if already logged in
if (!empty($_SESSION[ADMIN_SESSION])) {
    header('Location: ' . SITE_URL . '/admin/index.php');
    exit;
}

$error = '';

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

    if ($username && $password) {
        $db = getDB();
        $stmt = $db->prepare("SELECT * FROM admin_users WHERE username = ? LIMIT 1");
        $stmt->execute([$username]);
        $admin = $stmt->fetch();

        if ($admin && password_verify($password, $admin['password'])) {
            $_SESSION[ADMIN_SESSION] = true;
            $_SESSION['admin_username'] = $admin['username'];
            header('Location: ' . SITE_URL . '/admin/index.php');
            exit;
        } else {
            $error = 'Invalid username or password.';
        }
    } else {
        $error = 'Please enter username and password.';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login | <?= sanitize(getSetting('site_name')) ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/all.css"/>
    <style>
        body { background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%); min-height: 100vh; display: flex; align-items: center; }
        .login-card { background: white; border-radius: 20px; box-shadow: 0 20px 60px rgba(0,0,0,0.4); overflow: hidden; max-width: 420px; width: 100%; }
        .login-header { background: linear-gradient(135deg, #0d6efd, #0a58ca); padding: 40px 30px; text-align: center; }
        .login-body { padding: 40px 35px; }
        .form-control:focus { border-color: #0d6efd; box-shadow: 0 0 0 0.2rem rgba(13,110,253,.2); }
        .btn-login { background: linear-gradient(135deg, #0d6efd, #0a58ca); border: none; padding: 12px; font-size: 1rem; font-weight: 600; letter-spacing: 0.5px; border-radius: 8px; }
        .btn-login:hover { background: linear-gradient(135deg, #0b5ed7, #0a53be); }
    </style>
</head>
<body>
<div class="container d-flex justify-content-center">
    <div class="login-card">
        <div class="login-header">
            <i class="fas fa-shopping-bag text-white fa-3x mb-3"></i>
            <h4 class="text-white fw-bold mb-1"><?= sanitize(getSetting('site_name')) ?></h4>
            <p class="text-white-50 mb-0">Admin Panel</p>
        </div>
        <div class="login-body">
            <h5 class="fw-bold mb-4 text-center">Sign In</h5>
            <?php if ($error): ?>
                <div class="alert alert-danger py-2"><i class="fas fa-exclamation-circle me-2"></i><?= sanitize($error) ?></div>
            <?php endif; ?>
            <form method="POST" action="">
                <div class="mb-3">
                    <label class="form-label fw-semibold">Username</label>
                    <div class="input-group">
                        <span class="input-group-text"><i class="fas fa-user text-muted"></i></span>
                        <input type="text" name="username" class="form-control" placeholder="Enter username" value="<?= sanitize($_POST['username'] ?? '') ?>" required autofocus>
                    </div>
                </div>
                <div class="mb-4">
                    <label class="form-label fw-semibold">Password</label>
                    <div class="input-group">
                        <span class="input-group-text"><i class="fas fa-lock text-muted"></i></span>
                        <input type="password" name="password" class="form-control" placeholder="Enter password" required>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary btn-login w-100 text-white">
                    <i class="fas fa-sign-in-alt me-2"></i>Login
                </button>
            </form>
            <div class="text-center mt-3">
                <small class="text-muted">Default: admin / Admin@123</small>
            </div>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
