<?php
require_once __DIR__ . '/../db/config.php';
require_once __DIR__ . '/../includes/lang.php';
$lang = 'zh'; // Force Simplified Chinese for admin login

if (session_status() === PHP_SESSION_NONE) session_start();

// If already logged in as admin, redirect to dashboard
if (isset($_SESSION['admin_id'])) {
    header('Location: /admin/index.php');
    exit;
}

// Get site settings
$site_logo = getSetting('site_logo', '/assets/images/logo.png');
$site_favicon = getSetting('site_favicon', $site_logo);
$site_name = getSetting('site_name', 'Byro') . ' 管理后台';

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
        if (empty($username) || empty($password)) {
            $error = __("fill_full_info");
        } else {
            $stmt = db()->prepare("SELECT * FROM admins WHERE username = ?");
            $stmt->execute([$username]);
            $admin = $stmt->fetch();
            
            if ($admin && password_verify($password, $admin["password_hash"])) {
                $_SESSION["admin_id"] = $admin["id"];
                $_SESSION["admin_username"] = $admin["username"];
                $_SESSION["admin_role"] = $admin["role"];
                header("Location: /admin/index.php");
                exit;
            } else {
                $error = __("invalid_account_pwd");
            }
        }
    }
?>
<!DOCTYPE html>
<html lang="<?= $lang ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= __("login") ?> - <?= $site_name ?></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.11.1/font/bootstrap-icons.css">
    <?php if ($site_favicon): ?>
    <link rel="icon" href="<?= $site_favicon ?>">
    <?php endif; ?>
    <style>
        body {
            background: #f4f7f6;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'PingFang SC', sans-serif;
        }
        .login-card {
            width: 100%;
            max-width: 400px;
            background: #fff;
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.05);
        }
        .logo {
            text-align: center;
            margin-bottom: 30px;
            color: #0062ff;
            font-size: 24px;
            font-weight: bold;
        }
        .btn-primary {
            background: #0062ff;
            border: none;
            padding: 12px;
            font-weight: bold;
        }
    </style>
</head>
<body>
        <div class="logo">
            <?php if ($site_logo): ?>
                <img src="<?= $site_logo ?>?v=<?= time() ?>" height="40" class="mb-2 d-block mx-auto">
            <?php else: ?>
                <i class="bi bi-shield-lock-fill me-2"></i>
            <?php endif; ?>
            <div><?= __("admin_panel") ?></div>
        </div>
        
        <?php if ($error): ?>
            <div class="alert alert-danger small py-2 px-3 mb-4">
                <i class="bi bi-exclamation-circle me-2"></i><?= $error ?>
            </div>
        <?php endif; ?>

        <form method="POST">
            <div class="mb-3">
                <label class="form-label small text-muted"><?= __("account") ?></label>
                <input type="text" name="username" class="form-control" required>
            </div>
            <div class="mb-4">
                <label class="form-label small text-muted"><?= __("password") ?></label>
                <input type="password" name="password" class="form-control" required>
            </div>
            <button type="submit" class="btn btn-primary w-100 rounded-pill"><?= __("login") ?></button>
        </form>
    </div>
</body>
</html>
