<?php
/**
 * 站点维护页面 — 后台开启维护时显示此页面
 */
require_once __DIR__ . '/config.php';

$db = getDB();

// 获取维护状态和原因
$stmt = $db->query("SELECT key_name, value FROM settings WHERE group_name = 'site' AND key_name IN ('site_closed', 'site_closed_reason', 'site_logo', 'site_title')");
$siteInfo = [];
foreach ($stmt->fetchAll() as $row) {
    $siteInfo[$row['key_name']] = $row['value'];
}

// 如果不是维护状态，重定向到首页
if ($siteInfo['site_closed'] !== '1') {
    header('Location: ' . SITE_URL . '/index.php');
    exit;
}

$reason = $siteInfo['site_closed_reason'] ?? '系统升级维护中，请稍后再访问';
$siteTitle = $siteInfo['site_title'] ?? '一课';
$siteLogo = $siteInfo['site_logo'] ?? '';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>站点维护 - <?php echo htmlspecialchars($siteTitle); ?></title>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: #f5f7fa;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        .maintain-container {
            text-align: center;
            max-width: 380px;
            padding: 40px 24px;
        }
        .maintain-icon {
            font-size: 64px;
            color: #d1d5db;
            margin-bottom: 24px;
        }
        .maintain-title {
            font-size: 20px;
            font-weight: 600;
            color: #1f2937;
            margin-bottom: 16px;
        }
        .maintain-reason {
            font-size: 15px;
            color: #6b7280;
            line-height: 1.6;
            margin-bottom: 32px;
            background: white;
            padding: 16px;
            border-radius: 12px;
            border: 1px solid #e5e7eb;
        }
        .maintain-refresh {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 12px 24px;
            background: #374151;
            color: white;
            border-radius: 8px;
            font-size: 14px;
            cursor: pointer;
            transition: background 0.15s;
        }
        .maintain-refresh:hover { background: #1f2937; }
        .maintain-site-name {
            margin-top: 40px;
            font-size: 12px;
            color: #9ca3af;
        }
    </style>
</head>
<body>
    <div class="maintain-container">
        <?php if ($siteLogo): ?>
        <div style="margin-bottom:24px;">
            <img src="<?php echo htmlspecialchars($siteLogo); ?>" style="max-width:80px;max-height:80px;border-radius:12px;">
        </div>
        <?php else: ?>
        <div class="maintain-icon">
            <i class="fas fa-tools"></i>
        </div>
        <?php endif; ?>

        <div class="maintain-title">站点维护中</div>
        <div class="maintain-reason"><?php echo htmlspecialchars($reason); ?></div>
        <button class="maintain-refresh" onclick="location.reload()">
            <i class="fas fa-sync-alt"></i> 刷新页面
        </button>
        <div class="maintain-site-name"><?php echo htmlspecialchars($siteTitle); ?></div>
    </div>
</body>
</html>
