<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-5-20
 */


$licenseInfo = $license->getStatus();
$levelNames = ['体验版', '免费版', '基础版', '专业版'];
$currentLevel = $licenseInfo['level'] ?? 0;
$stats = [
    'article' => $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('article')}")['cnt'],
    'product' => $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('product')}")['cnt'],
    'member' => $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('member')}")['cnt'],
    'guestbook' => $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('guestbook')} WHERE status = 0")['cnt'],
    'channel' => $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('channel')}")['cnt'],
];
$latestArticles = $db->fetchAll("SELECT id, title, create_time FROM {$db->table('article')} ORDER BY id DESC LIMIT 3");
$latestGuestbook = $db->fetchAll("SELECT id, name, title, create_time FROM {$db->table('guestbook')} ORDER BY id DESC LIMIT 3");
$latestMembers = $db->fetchAll("SELECT id, username, reg_time FROM {$db->table('member')} ORDER BY id DESC LIMIT 3");
$latestProducts = $db->fetchAll("SELECT id, title, create_time FROM {$db->table('product')} ORDER BY id DESC LIMIT 3");
$todayStart = strtotime(date('Y-m-d'));
$todayArticle = $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('article')} WHERE create_time >= ?", [$todayStart])['cnt'];
$todayMember = $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('member')} WHERE reg_time >= ?", [$todayStart])['cnt'];
$todayGuestbook = $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('guestbook')} WHERE create_time >= ?", [$todayStart])['cnt'];
$todayProduct = $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('product')} WHERE create_time >= ?", [$todayStart])['cnt'];
// 订单统计
$todayOrder = 0;
$todaySales = 0;
$totalOrder = 0;
$totalSales = 0;
if ($licenseInfo['can_use_order']) {
    $todayOrder = $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('order')} WHERE create_time >= ?", [$todayStart])['cnt'];
    $todaySalesData = $db->fetch("SELECT COALESCE(SUM(total_amount), 0) as total FROM {$db->table('order')} WHERE create_time >= ? AND status IN (1,2,3)", [$todayStart]);
    $todaySales = $todaySalesData['total'] ?? 0;
    $totalOrder = $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('order')}")['cnt'];
    $totalSalesData = $db->fetch("SELECT COALESCE(SUM(total_amount), 0) as total FROM {$db->table('order')} WHERE status IN (1,2,3)");
    $totalSales = $totalSalesData['total'] ?? 0;
}
// 站点配置
$siteConfig = [];
$configs = $db->fetchAll("SELECT config_key, config_value FROM {$db->table('config')}");
foreach ($configs as $cfg) {
    $siteConfig[$cfg['config_key']] = $cfg['config_value'];
}
// 今日浏览量
$viewsData = getSiteViews();
$todayViews = $viewsData['today_views'];
$totalViews = $viewsData['total_views'];
// 获取模板列表
$templates = [];
$apiBase = defined('LICENSE_CENTER_URL') ? LICENSE_CENTER_URL : 'http://yutou.wtbidc.cn/license-center';
$apiUrl = $apiBase . '/api.php?action=template_list';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['level' => $currentLevel]));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($ch);
curl_close($ch);
if ($resp) {
    $result = json_decode($resp, true);
    if ($result && isset($result['code']) && $result['code'] == 200) {
        $templates = $result['templates'] ?? [];
    }
}
// 本地模板目录
$localTemplateDir = dirname(__DIR__, 2) . '/template/';
$installedTemplates = [];
if (is_dir($localTemplateDir)) {
    foreach (glob($localTemplateDir . '*', GLOB_ONLYDIR) as $dir) {
        $installedTemplates[] = basename($dir);
    }
}
// 服务器信息
$serverInfo = [
    'php_version' => PHP_VERSION,
    'mysql_version' => $db->fetch("SELECT VERSION() as v")['v'] ?? '未知',
    'server_os' => PHP_OS,
    'server_ip' => $_SERVER['SERVER_ADDR'] ?? gethostbyname(gethostname()),
    'web_root' => dirname(__DIR__, 2),
    'disk_free' => function_exists('disk_free_space') ? round(disk_free_space(dirname(__DIR__, 2)) / 1024 / 1024 / 1024, 2) . ' GB' : '未知',
    'upload_max' => ini_get('upload_max_filesize'),
    'memory_limit' => ini_get('memory_limit'),
];

// 当前模板
$currentTemplateKey = $db->fetch("SELECT config_value FROM {$db->table('config')} WHERE config_key = 'site_template'")['config_value'] ?? 'default';
$dashboardQuickLinks = [
    ['label' => '发布文章', 'desc' => '进入文章管理快速发布内容', 'url' => '/admin/main.php?act=article', 'tone' => 'blue'],
    ['label' => '管理产品', 'desc' => '维护产品、分类与展示信息', 'url' => '/admin/main.php?act=product', 'tone' => 'green'],
    ['label' => '模板中心', 'desc' => '切换模板并查看授权模板资源', 'url' => '/admin/main.php?act=template', 'tone' => 'gold'],
    ['label' => '授权升级', 'desc' => '查看版本权益与授权状态', 'url' => '/admin/main.php?act=auth', 'tone' => 'purple'],
];
$dashboardOps = [
    ['label' => '内容总量', 'value' => $stats['article'] + $stats['product'], 'meta' => '文章 + 产品'],
    ['label' => '待处理事项', 'value' => $stats['guestbook'], 'meta' => '未回复留言'],
    ['label' => '模板资源', 'value' => count($templates), 'meta' => '当前可用模板'],
    ['label' => '站点栏目', 'value' => $stats['channel'], 'meta' => '已创建栏目数'],
];
?>
<div class="page-container fade-in">
    <!-- 页面标题 -->
    <div class="page-header">
        <div>
            <h2 style="font-size:16px;font-weight:600;color:var(--dark);margin:0;">
                <svg style="width:22px;height:22px;color:var(--primary);margin-right:8px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/>
                    <rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/>
                </svg> 网站管理控制台
            </h2>
            <p style="font-size:12px;color:var(--text-light);margin:4px 0 0 0;">一站式企业数字化运营平台</p>
        </div>
        <div>
            <span class="badge badge-<?php echo $currentLevel >= 2 ? 'success' : 'warning'; ?>">
                <svg style="width:14px;height:14px;margin-right:4px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M2 4l3 12h14l3-12-6 7-4-7-4 7-6-7z"/><path d="M5 16v4h14v-4"/><path d="M9 20h6"/>
                </svg>
                <?php echo $levelNames[$currentLevel] ?? '体验版'; ?>
            </span>
        </div>
    </div>
    <div class="dashboard-hero">
        <div class="dashboard-hero-main">
            <div class="dashboard-hero-kicker">yutouCMS Admin Console</div>
            <h3 class="dashboard-hero-title">统一查看站点运营、内容、模板与授权状态</h3>
            <p class="dashboard-hero-desc">当前首页聚合核心经营指标、内容动态、模板资源与运行环境信息，适合作为日常管理总入口。</p>
            <div class="dashboard-hero-actions">
                <?php foreach ($dashboardQuickLinks as $link): ?>
                <a href="<?php echo $link['url']; ?>" class="dashboard-quick-link tone-<?php echo $link['tone']; ?>">
                    <span class="dashboard-quick-label"><?php echo htmlspecialchars($link['label']); ?></span>
                    <span class="dashboard-quick-desc"><?php echo htmlspecialchars($link['desc']); ?></span>
                </a>
                <?php endforeach; ?>
            </div>
        </div>
        <div class="dashboard-hero-side">
            <?php foreach ($dashboardOps as $item): ?>
            <div class="dashboard-hero-stat">
                <div class="dashboard-hero-stat-label"><?php echo htmlspecialchars($item['label']); ?></div>
                <div class="dashboard-hero-stat-value"><?php echo htmlspecialchars((string) $item['value']); ?></div>
                <div class="dashboard-hero-stat-meta"><?php echo htmlspecialchars($item['meta']); ?></div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php if ($licenseInfo['level'] < 2): ?>
    <div class="alert alert-warning">
        <svg style="width:18px;height:18px;flex-shrink:0;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <circle cx="12" cy="12" r="10"/>
            <line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/>
        </svg>
        <div>
            <strong>当前版本：<?php echo $levelNames[$licenseInfo['level']]; ?></strong>
            <p style="margin:4px 0 0 0;font-size:13px;">
                可用功能：基础文章管理、基础产品管理、留言板功能
                | 可使用模板：<?php echo $licenseInfo['level'] == 0 ? '1个' : '2个'; ?>免费模板
                | <a href="/admin/main.php?act=auth" style="color:#c2690c;font-weight:600;">点击升级授权→</a>
            </p>
        </div>
    </div>
    <?php endif; ?>
    <!-- 统计卡片 -->
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-header">
                <span class="stat-label">文章总数</span>
                <div class="stat-icon blue">
                    <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
                        <polyline points="14 2 14 8 20 8"/>
                        <line x1="16" y1="13" x2="8" y2="13"/>
                        <line x1="16" y1="17" x2="8" y2="17"/>
                        <polyline points="10 9 9 9 8 9"/>
                    </svg>
                </div>
            </div>
            <div class="stat-value"><?php echo $stats['article']; ?></div>
            <div class="stat-change">今日新增 <?php echo $todayArticle; ?> 篇</div>
        </div>
        <div class="stat-card">
            <div class="stat-header">
                <span class="stat-label">产品总数</span>
                <div class="stat-icon green">
                    <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/>
                        <polyline points="3.27 6.96 12 12.01 20.73 6.96"/>
                        <line x1="12" y1="22.08" x2="12" y2="12"/>
                    </svg>
                </div>
            </div>
            <div class="stat-value"><?php echo $stats['product']; ?></div>
            <div class="stat-change">今日新增 <?php echo $todayProduct; ?> 个</div>
        </div>
        <div class="stat-card">
            <div class="stat-header">
                <span class="stat-label">会员总数</span>
                <div class="stat-icon" style="background:#8b5cf6;color:#fff;">
                    <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/>
                        <circle cx="9" cy="7" r="4"/>
                        <path d="M23 21v-2a4 4 0 0 0-3-3.87"/>
                        <path d="M16 3.13a4 4 0 0 1 0 7.75"/>
                    </svg>
                </div>
            </div>
            <div class="stat-value"><?php echo $stats['member']; ?></div>
            <div class="stat-change">今日注册 <?php echo $todayMember; ?> 人</div>
        </div>
        <div class="stat-card">
            <div class="stat-header">
                <span class="stat-label">待回复留言</span>
                <div class="stat-icon <?php echo $stats['guestbook'] > 0 ? 'orange' : 'blue'; ?>">
                    <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>
                    </svg>
                </div>
            </div>
            <div class="stat-value" style="color:<?php echo $stats['guestbook'] > 0 ? 'var(--warning)' : 'var(--dark)'; ?>">
                <?php echo $stats['guestbook']; ?>
            </div>
            <div class="stat-change">今日新增 <?php echo $todayGuestbook; ?> 条</div>
        </div>
    </div>
    <?php if ($licenseInfo['can_use_order']): ?>
    <div class="stats-grid" style="margin-top:0;">
        <div class="stat-card">
            <div class="stat-header">
                <span class="stat-label">今日订单</span>
                <div class="stat-icon" style="background:#06b6d4;color:#fff;">
                    <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <circle cx="9" cy="21" r="1"/><circle cx="20" cy="21" r="1"/>
                        <path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"/>
                    </svg>
                </div>
            </div>
            <div class="stat-value"><?php echo $todayOrder; ?></div>
            <div class="stat-change">订单总数：<?php echo $totalOrder; ?></div>
        </div>
        <div class="stat-card">
            <div class="stat-header">
                <span class="stat-label">今日销售额</span>
                <div class="stat-icon red">
                    <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <rect x="1" y="4" width="22" height="16" rx="2" ry="2"/>
                        <line x1="1" y1="10" x2="23" y2="10"/>
                    </svg>
                </div>
            </div>
            <div class="stat-value">¥<?php echo number_format($todaySales, 0); ?></div>
            <div class="stat-change">销售总额：¥<?php echo number_format($totalSales, 2); ?></div>
        </div>
        <div class="stat-card">
            <div class="stat-header">
                <span class="stat-label">今日浏览</span>
                <div class="stat-icon" style="background:#8b5cf6;color:#fff;">
                    <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/>
                        <circle cx="12" cy="12" r="3"/>
                    </svg>
                </div>
            </div>
            <div class="stat-value"><?php echo $todayViews ?: '-'; ?></div>
            <div class="stat-change">总浏览量：<?php echo $totalViews ?: '-'; ?></div>
        </div>
    </div>
    <?php endif; ?>
    <!-- 最新内容 -->
    <div class="dashboard-content-grid" style="display:grid;grid-template-columns:repeat(4,1fr);gap:20px;margin-top:20px;">
        <div class="content-card">
            <div class="card-header">
                <h2><svg style="width:18px;height:18px;margin-right:6px;color:var(--primary);" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/></svg> 最新文章</h2>
                <a href="/admin/main.php?act=article" class="btn btn-sm btn-gray">更多</a>
            </div>
            <div class="card-body" style="padding:0;">
                <?php if (empty($latestArticles)): ?>
                <div class="empty-state" style="padding:30px;">
                    <p style="margin:0;">暂无文章</p>
                </div>
                <?php else: ?>
                <table class="data-table" style="margin:0;">
                    <?php foreach ($latestArticles as $item): ?>
                    <tr>
                        <td>
                            <a href="/public/index.php?m=article&id=<?php echo $item['id']; ?>" target="_blank" style="color:var(--text);display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;text-decoration:none;">
                                <?php echo htmlspecialchars($item['title']); ?>
                            </a>
                        </td>
                        <td style="width:70px;color:var(--text-light);font-size:12px;"><?php echo date('m-d', $item['create_time']); ?></td>
                    </tr>
                    <?php endforeach; ?>
                </table>
                <?php endif; ?>
            </div>
        </div>
        <div class="content-card">
            <div class="card-header">
                <h2><svg style="width:18px;height:18px;margin-right:6px;color:var(--primary);" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/></svg> 最新产品</h2>
                <a href="/admin/main.php?act=product" class="btn btn-sm btn-gray">更多</a>
            </div>
            <div class="card-body" style="padding:0;">
                <?php if (empty($latestProducts)): ?>
                <div class="empty-state" style="padding:30px;">
                    <p style="margin:0;">暂无产品</p>
                </div>
                <?php else: ?>
                <table class="data-table" style="margin:0;">
                    <?php foreach ($latestProducts as $item): ?>
                    <tr>
                        <td>
                            <a href="/public/index.php?m=product&id=<?php echo $item['id']; ?>" target="_blank" style="color:var(--text);display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;text-decoration:none;">
                                <?php echo htmlspecialchars($item['title']); ?>
                            </a>
                        </td>
                        <td style="width:70px;color:var(--text-light);font-size:12px;"><?php echo date('m-d', $item['create_time']); ?></td>
                    </tr>
                    <?php endforeach; ?>
                </table>
                <?php endif; ?>
            </div>
        </div>
        <div class="content-card">
            <div class="card-header">
                <h2><svg style="width:18px;height:18px;margin-right:6px;color:var(--primary);" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg> 最新留言</h2>
                <a href="/admin/main.php?act=guestbook" class="btn btn-sm btn-gray">更多</a>
            </div>
            <div class="card-body" style="padding:0;">
                <?php if (empty($latestGuestbook)): ?>
                <div class="empty-state" style="padding:30px;">
                    <p style="margin:0;">暂无留言</p>
                </div>
                <?php else: ?>
                <table class="data-table" style="margin:0;">
                    <?php foreach ($latestGuestbook as $item): ?>
                    <tr>
                        <td>
                            <a href="/public/index.php?m=guestbook&id=<?php echo $item['id']; ?>" target="_blank" style="color:var(--text);display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;text-decoration:none;">
                                <?php echo htmlspecialchars($item['name']); ?>：<?php echo htmlspecialchars($item['title']); ?>
                            </a>
                        </td>
                        <td style="width:70px;color:var(--text-light);font-size:12px;"><?php echo date('m-d', $item['create_time']); ?></td>
                    </tr>
                    <?php endforeach; ?>
                </table>
                <?php endif; ?>
            </div>
        </div>
        <div class="content-card">
            <div class="card-header">
                <h2><svg style="width:18px;height:18px;margin-right:6px;color:var(--primary);" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/></svg> 最新会员</h2>
                <a href="/admin/main.php?act=member" class="btn btn-sm btn-gray">更多</a>
            </div>
            <div class="card-body" style="padding:0;">
                <?php if (empty($latestMembers)): ?>
                <div class="empty-state" style="padding:30px;">
                    <p style="margin:0;">暂无会员</p>
                </div>
                <?php else: ?>
                <table class="data-table" style="margin:0;">
                    <?php foreach ($latestMembers as $item): ?>
                    <tr>
                        <td>
                            <a href="/member/" target="_blank" style="color:var(--text);display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;text-decoration:none;">
                                <?php echo htmlspecialchars($item['username']); ?>
                            </a>
                        </td>
                        <td style="width:70px;color:var(--text-light);font-size:12px;"><?php echo date('m-d', $item['reg_time']); ?></td>
                    </tr>
                    <?php endforeach; ?>
                </table>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <!-- 系统信息 + 模板展示 -->
    <div class="dashboard-secondary-grid" style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-top:20px;">
        <!-- 系统信息 -->
        <div class="content-card">
            <div class="card-header">
                <h2><svg style="width:18px;height:18px;margin-right:6px;color:var(--primary);" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg> 系统信息</h2>
            </div>
            <div class="card-body" style="padding:16px;">
                <div style="display:grid;grid-template-columns:1fr 1fr;gap:12px;">
                    <div style="padding:14px 16px;background:linear-gradient(135deg,#eef4ff,#f5f8ff);border-radius:var(--radius);border:1px solid #c7d9ff;display:flex;align-items:center;justify-content:space-between;gap:8px;">
                        <div>
                            <div style="font-size:11px;color:var(--text-light);margin-bottom:4px;">系统版本</div>
                            <div style="font-size:14px;font-weight:600;color:var(--dark);">芋头CMS V1.0</div>
                        </div>
                        <button id="checkVersionBtn" type="button" onclick="checkNewVersion()" style="padding:5px 12px;background:#fff;border:1px solid #c7d9ff;border-radius:6px;font-size:12px;color:var(--brand);cursor:pointer;white-space:nowrap;transition:all 0.2s;">
                            <svg style="width:12px;height:12px;vertical-align:middle;margin-right:3px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="1 4 1 10 7 10"/><path d="M3.51 15a9 9 0 1 0 .49-4.5"/></svg>检查更新
                        </button>
                    </div>
                    <div style="padding:14px 16px;background:linear-gradient(135deg,#f0fdf4,#f5fbf7);border-radius:var(--radius);border:1px solid #bbf7d0;">
                        <div style="font-size:11px;color:var(--text-light);margin-bottom:4px;">授权级别</div>
                        <div style="font-size:14px;font-weight:600;color:<?php echo $licenseInfo['level'] >= 2 ? 'var(--success)' : 'var(--warning)'; ?>;"><?php echo $levelNames[$licenseInfo['level']]; ?></div>
                    </div>
                    <div style="padding:14px 16px;background:linear-gradient(135deg,#fffbeb,#fffdf5);border-radius:var(--radius);border:1px solid #fde68a;">
                        <div style="font-size:11px;color:var(--text-light);margin-bottom:4px;">授权KEY</div>
                        <div style="font-size:11px;font-weight:600;color:var(--dark);font-family:monospace;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">
                            <?php echo $licenseInfo['license_key'] ? htmlspecialchars($licenseInfo['license_key']) : '未激活'; ?>
                        </div>
                    </div>
                    <div style="padding:14px 16px;background:linear-gradient(135deg,#f0f9ff,#f5fbff);border-radius:var(--radius);border:1px solid #bfdbfe;">
                        <div style="font-size:11px;color:var(--text-light);margin-bottom:4px;">授权域名</div>
                        <div style="font-size:12px;font-weight:600;color:var(--dark);overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">
                            <?php echo htmlspecialchars($licenseInfo['domain'] ?? $_SERVER['HTTP_HOST']); ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 模板缩略图展示 -->
        <div class="content-card">
            <div class="card-header">
                <h2><svg style="width:18px;height:18px;margin-right:6px;color:var(--primary);" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 19l7-7 3 3-7 7-3-3z"/><path d="M18 13l-1.5-7.5L2 2l3.5 14.5L13 18l5-5z"/><path d="M2 2l7.586 7.586"/><circle cx="11" cy="11" r="2"/></svg> 模板中心</h2>
                <a href="/admin/main.php?act=template" class="btn btn-sm btn-gray">更多模板</a>
            </div>
            <div class="card-body" style="padding:12px;">
                <?php if (empty($templates)): ?>
                <div style="padding:20px;text-align:center;color:var(--text-light);">暂无可用模板</div>
                <?php else: ?>
                <div style="display:grid;grid-template-columns:repeat(4,1fr);gap:10px;">
                    <?php
                    $showTemplates = array_slice($templates, 0, 8);
                    foreach ($showTemplates as $t):
                        $isInstalled = in_array($t['template_key'], $installedTemplates);
                        $isActive = $currentTemplateKey == $t['template_key'];
                        $thumbUrl = $t['thumbnail'] ?? '';
                        if (strpos($thumbUrl, 'http') !== 0) {
                            $thumbUrl = 'http://yutou.wtbidc.cn/license-center/' . ltrim($thumbUrl, '/');
                        }
                    ?>
                    <div style="border:2px solid <?php echo $isActive ? 'var(--primary)' : 'var(--border)'; ?>;border-radius:var(--radius);overflow:hidden;background:var(--gray);position:relative;cursor:pointer;" onclick="location.href='/admin/main.php?act=template'" title="<?php echo htmlspecialchars($t['name']); ?>">
                        <?php if ($isActive): ?>
                        <div style="position:absolute;top:4px;right:4px;background:var(--primary);color:#fff;font-size:9px;padding:2px 5px;border-radius:10px;z-index:2;">使用中</div>
                        <?php endif; ?>
                        <?php if ($thumbUrl): ?>
                        <img src="<?php echo htmlspecialchars($thumbUrl); ?>" style="width:100%;height:56px;object-fit:cover;" onerror="this.style.display='none';this.nextElementSibling.style.display='flex';">
                        <?php endif; ?>
                        <div style="width:100%;height:56px;display:<?php echo $thumbUrl ? 'none' : 'flex'; ?>;align-items:center;justify-content:center;background:linear-gradient(135deg,#eef4ff,#f5f8ff);">
                            <svg style="width:18px;height:18px;color:var(--primary);" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <path d="M12 19l7-7 3 3-7 7-3-3z"/><path d="M18 13l-1.5-7.5L2 2l3.5 14.5L13 18l5-5z"/>
                            </svg>
                        </div>
                        <div style="padding:6px 8px;background:#fff;">
                            <div style="font-size:11px;font-weight:600;color:var(--dark);overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"><?php echo htmlspecialchars($t['name']); ?></div>
                            <?php if (!$isInstalled && !$isActive): ?>
                            <div style="font-size:9px;color:var(--text-light);">未安装</div>
                            <?php else: ?>
                            <div style="font-size:9px;color:var(--success);">✓ 已安装</div>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <!-- 服务器信息 -->
    <div class="dashboard-server-grid" style="display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:12px;margin-top:20px;">
        <div class="server-card">
            <div class="server-icon" style="background:#ede9fe;color:#7c3aed;">
                <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <polyline points="4 17 10 11 4 5"/><line x1="12" y1="19" x2="20" y2="19"/>
                </svg>
            </div>
            <div>
                <div class="server-label">PHP版本</div>
                <div class="server-value"><?php echo $serverInfo['php_version']; ?></div>
            </div>
        </div>
        <div class="server-card">
            <div class="server-icon" style="background:#dcfce7;color:#16a34a;">
                <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M21 12c0 1.66-4 3-9 3s-9-1.34-9-3"/><path d="M3 5v14c0 1.66 4 3 9 3s9-1.34 9-3V5"/>
                </svg>
            </div>
            <div>
                <div class="server-label">MySQL版本</div>
                <div class="server-value"><?php echo htmlspecialchars($serverInfo['mysql_version']); ?></div>
            </div>
        </div>
        <div class="server-card">
            <div class="server-icon" style="background:#fef3c7;color:#d97706;">
                <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <rect x="2" y="3" width="20" height="14" rx="2" ry="2"/><line x1="8" y1="21" x2="16" y2="21"/><line x1="12" y1="17" x2="12" y2="21"/>
                </svg>
            </div>
            <div>
                <div class="server-label">服务器系统</div>
                <div class="server-value"><?php echo htmlspecialchars($serverInfo['server_os']); ?></div>
            </div>
        </div>
        <div class="server-card">
            <div class="server-icon" style="background:#fee2e2;color:#dc2626;">
                <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <rect x="2" y="2" width="20" height="8" rx="2" ry="2"/><rect x="2" y="14" width="20" height="8" rx="2" ry="2"/><line x1="6" y1="6" x2="6.01" y2="6"/><line x1="6" y1="18" x2="6.01" y2="18"/>
                </svg>
            </div>
            <div>
                <div class="server-label">服务器IP</div>
                <div class="server-value"><?php echo htmlspecialchars($serverInfo['server_ip']); ?></div>
            </div>
        </div>
        <div class="server-card">
            <div class="server-icon" style="background:#e0f2fe;color:#0284c7;">
                <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"/>
                </svg>
            </div>
            <div>
                <div class="server-label">网站根目录</div>
                <div class="server-value" title="<?php echo $serverInfo['web_root']; ?>"><?php echo htmlspecialchars($serverInfo['web_root']); ?></div>
            </div>
        </div>
        <div class="server-card">
            <div class="server-icon" style="background:#fce7f3;color:#db2777;">
                <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <line x1="22" y1="12" x2="2" y2="12"/><path d="M5.45 5.11L2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z"/>
                </svg>
            </div>
            <div>
                <div class="server-label">磁盘剩余</div>
                <div class="server-value"><?php echo $serverInfo['disk_free']; ?></div>
            </div>
        </div>
        <div class="server-card">
            <div class="server-icon" style="background:#ffedd5;color:#ea580c;">
                <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/>
                </svg>
            </div>
            <div>
                <div class="server-label">最大上传</div>
                <div class="server-value"><?php echo $serverInfo['upload_max']; ?></div>
            </div>
        </div>
        <div class="server-card">
            <div class="server-icon" style="background:#f0fdf4;color:#16a34a;">
                <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <rect x="4" y="4" width="16" height="16" rx="2" ry="2"/><rect x="9" y="9" width="6" height="6"/><line x1="9" y1="1" x2="9" y2="4"/><line x1="15" y1="1" x2="15" y2="4"/><line x1="9" y1="20" x2="9" y2="23"/><line x1="15" y1="20" x2="15" y2="23"/>
                </svg>
            </div>
            <div>
                <div class="server-label">运行内存</div>
                <div class="server-value"><?php echo $serverInfo['memory_limit']; ?></div>
            </div>
        </div>
    </div>
</div>
<script>
function checkNewVersion() {
    var btn = document.getElementById('checkVersionBtn');
    btn.disabled = true;
    btn.innerHTML = '<svg style="width:12px;height:12px;vertical-align:middle;margin-right:3px;animation:spin 1s linear infinite;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>检查中...';
    var apiBase = 'http://yutou.wtbidc.cn/license-center';
    var xhr = new XMLHttpRequest();
    xhr.open('GET', apiBase + '/api.php?action=check_update', true);
    xhr.timeout = 10000;
    xhr.onload = function() {
        btn.disabled = false;
        btn.innerHTML = '<svg style="width:12px;height:12px;vertical-align:middle;margin-right:3px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="1 4 1 10 7 10"/><path d="M3.51 15a9 9 0 1 0 .49-4.5"/></svg>检查更新';
        if (xhr.status === 200) {
            try {
                var res = JSON.parse(xhr.responseText);
                if (res.code === 200 && res.data && res.data.latest_version) {
                    var latest = res.data.latest_version;
                    var current = res.data.current_version || '1.0.0';
                    if (compareVersion(latest, current) > 0) {
                        var msg = '发现新版本 v' + latest + '，请前往下载更新！\n\n更新内容：\n' + (res.data.changelog || '暂无更新说明');
                        alert(msg);
                    } else {
                        alert('当前已是最新版本 (v' + current + ')，无需更新。');
                    }
                } else {
                    alert('检查更新失败，请稍后重试。');
                }
            } catch(e) {
                alert('检查更新失败，请稍后重试。');
            }
        } else {
            alert('检查更新失败，请稍后重试。');
        }
    };
    xhr.ontimeout = function() {
        btn.disabled = false;
        btn.innerHTML = '<svg style="width:12px;height:12px;vertical-align:middle;margin-right:3px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="1 4 1 10 7 10"/><path d="M3.51 15a9 9 0 1 0 .49-4.5"/></svg>检查更新';
        alert('检查更新超时，请稍后重试。');
    };
    xhr.onerror = function() {
        btn.disabled = false;
        btn.innerHTML = '<svg style="width:12px;height:12px;vertical-align:middle;margin-right:3px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="1 4 1 10 7 10"/><path d="M3.51 15a9 9 0 1 0 .49-4.5"/></svg>检查更新';
        alert('检查更新失败，请确保授权中心正常连接。');
    };
    xhr.send();
}
function compareVersion(v1, v2) {
    var a1 = v1.split('.'), a2 = v2.split('.');
    for (var i = 0; i < Math.max(a1.length, a2.length); i++) {
        var n1 = parseInt(a1[i] || 0), n2 = parseInt(a2[i] || 0);
    if (n1 > n2) return 1;
        if (n1 < n2) return -1;
    }
    return 0;
}
function checkNewVersion() {
    var btn = document.getElementById('checkVersionBtn');
    if (btn) { btn.disabled = true; btn.innerHTML = '<svg style="width:12px;height:12px;vertical-align:middle;margin-right:3px;animation:spin 1s linear infinite;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>检查中...'; }
    var apiBase = 'http://yutou.wtbidc.cn/license-center';
    var xhr = new XMLHttpRequest();
    xhr.open('GET', apiBase + '/api.php?action=check_update', true);
    xhr.timeout = 10000;
    xhr.onload = function() {
        if (btn) { btn.disabled = false; btn.innerHTML = '<svg style="width:12px;height:12px;vertical-align:middle;margin-right:3px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="1 4 1 10 7 10"/><path d="M3.51 15a9 9 0 1 0 .49-4.5"/></svg>检查更新'; }
        if (xhr.status === 200) {
            try {
                var res = JSON.parse(xhr.responseText);
                if (res.code === 200 && res.data && res.data.latest_version) {
                    var latest = res.data.latest_version;
                    var current = res.data.current_version || '1.0.0';
                    if (compareVersion(latest, current) > 0) {
                        var msg = '发现新版本 v' + latest + '，请前往下载更新！\n\n更新内容：\n' + (res.data.changelog || '暂无更新说明');
                        alert(msg);
                    }
                }
            } catch(e) {}
        }
    };
    xhr.onerror = function() {};
    xhr.send();
}
(function(){
    var last = localStorage.getItem('version_check_time') || 0;
    var now = Date.now();
    if (now - last > 2 * 60 * 60 * 1000) {
        localStorage.setItem('version_check_time', now);
        setTimeout(checkNewVersion, 2000);
    }
})();
</script>
<style>
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
.page-container.fade-in {
    background:
        radial-gradient(circle at top right, rgba(37, 99, 235, 0.08), transparent 24%),
        radial-gradient(circle at top left, rgba(15, 118, 110, 0.07), transparent 22%);
}
.page-header {
    padding: 22px 26px;
    border-radius: 24px;
    background: linear-gradient(135deg, #0f172a, #1e3a8a 52%, #0f766e);
    box-shadow: 0 24px 48px rgba(15, 23, 42, 0.18);
    margin-bottom: 22px;
}
.page-header h2,
.page-header p,
.page-header .badge {
    color: #fff;
}
.page-header p {
    opacity: 0.84;
}
.page-header .badge {
    background: rgba(255, 255, 255, 0.14);
    border: 1px solid rgba(255, 255, 255, 0.18);
    backdrop-filter: blur(8px);
}
.dashboard-hero {
    display: grid;
    grid-template-columns: minmax(0, 1.45fr) minmax(320px, 0.95fr);
    gap: 18px;
    margin-bottom: 22px;
}
.dashboard-hero-main,
.dashboard-hero-side {
    border-radius: 24px;
    overflow: hidden;
}
.dashboard-hero-main {
    padding: 26px 28px;
    background: linear-gradient(145deg, #ffffff, #f6faff);
    border: 1px solid rgba(148, 163, 184, 0.18);
    box-shadow: 0 22px 44px rgba(15, 23, 42, 0.08);
}
.dashboard-hero-kicker {
    display: inline-flex;
    align-items: center;
    padding: 6px 10px;
    border-radius: 999px;
    background: #e0ecff;
    color: #1d4ed8;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}
.dashboard-hero-title {
    margin: 16px 0 10px;
    font-size: 28px;
    line-height: 1.25;
    color: #0f172a;
}
.dashboard-hero-desc {
    margin: 0;
    max-width: 760px;
    color: #475569;
    font-size: 14px;
    line-height: 1.85;
}
.dashboard-hero-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
    margin-top: 22px;
}
.dashboard-quick-link {
    display: block;
    padding: 18px 18px 16px;
    border-radius: 18px;
    border: 1px solid rgba(148, 163, 184, 0.18);
    background: #fff;
    text-decoration: none;
    transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease;
}
.dashboard-quick-link:hover {
    transform: translateY(-3px);
    box-shadow: 0 18px 32px rgba(15, 23, 42, 0.08);
}
.dashboard-quick-link.tone-blue { border-color: rgba(59, 130, 246, 0.18); background: linear-gradient(135deg, #eff6ff, #ffffff); }
.dashboard-quick-link.tone-green { border-color: rgba(16, 185, 129, 0.18); background: linear-gradient(135deg, #ecfdf5, #ffffff); }
.dashboard-quick-link.tone-gold { border-color: rgba(245, 158, 11, 0.2); background: linear-gradient(135deg, #fffbeb, #ffffff); }
.dashboard-quick-link.tone-purple { border-color: rgba(139, 92, 246, 0.2); background: linear-gradient(135deg, #f5f3ff, #ffffff); }
.dashboard-quick-label {
    display: block;
    font-size: 15px;
    font-weight: 700;
    color: #0f172a;
}
.dashboard-quick-desc {
    display: block;
    margin-top: 8px;
    font-size: 12px;
    line-height: 1.7;
    color: #64748b;
}
.dashboard-hero-side {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
}
.dashboard-hero-stat {
    padding: 22px 18px;
    border-radius: 22px;
    background: linear-gradient(145deg, #0f172a, #111827);
    color: #fff;
    box-shadow: 0 20px 40px rgba(15, 23, 42, 0.14);
}
.dashboard-hero-stat-label {
    font-size: 12px;
    color: rgba(255,255,255,0.72);
}
.dashboard-hero-stat-value {
    margin-top: 12px;
    font-size: 30px;
    font-weight: 800;
    letter-spacing: -0.03em;
}
.dashboard-hero-stat-meta {
    margin-top: 8px;
    font-size: 12px;
    color: rgba(255,255,255,0.66);
}
.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 18px;
    margin-bottom: 18px;
}
.stat-card {
    position: relative;
    overflow: hidden;
    border-radius: 22px;
    border: 1px solid rgba(148, 163, 184, 0.18);
    background: linear-gradient(180deg, #ffffff, #f8fbff);
    box-shadow: 0 20px 38px rgba(15, 23, 42, 0.06);
}
.stat-card::after {
    content: "";
    position: absolute;
    inset: auto -40px -46px auto;
    width: 120px;
    height: 120px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(37, 99, 235, 0.11), transparent 70%);
}
.stat-header,
.stat-card,
.content-card,
.server-card {
    transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease;
}
.stat-card:hover,
.content-card:hover,
.server-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 24px 44px rgba(15, 23, 42, 0.09);
}
.stat-value {
    font-size: 34px;
    font-weight: 800;
    letter-spacing: -0.04em;
    color: #0f172a;
}
.stat-label {
    font-size: 13px;
    letter-spacing: 0.02em;
    color: #475569;
}
.stat-change {
    font-size: 12px;
    color: #64748b;
}
.content-card {
    border-radius: 22px;
    border: 1px solid rgba(148, 163, 184, 0.16);
    box-shadow: 0 18px 38px rgba(15, 23, 42, 0.05);
    overflow: hidden;
}
.card-header {
    padding: 18px 18px 14px;
    border-bottom: 1px solid rgba(226, 232, 240, 0.78);
    background: linear-gradient(180deg, rgba(248, 250, 252, 0.9), rgba(255,255,255,0.92));
}
.card-header h2 {
    color: #0f172a;
    font-size: 15px;
    font-weight: 700;
}
.card-body table tr td {
    padding-top: 12px;
    padding-bottom: 12px;
}
.dashboard-content-grid .content-card,
.dashboard-secondary-grid .content-card {
    min-width: 0;
}
.dashboard-server-grid {
    margin-top: 22px !important;
}
.server-card {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 18px 16px;
    border-radius: 18px;
    border: 1px solid rgba(148, 163, 184, 0.16);
    background: linear-gradient(145deg, #ffffff, #f8fafc);
    box-shadow: 0 16px 30px rgba(15, 23, 42, 0.05);
}
.server-label {
    font-size: 12px;
    color: #64748b;
}
.server-value {
    margin-top: 4px;
    font-size: 14px;
    font-weight: 700;
    color: #0f172a;
    word-break: break-all;
}
.server-icon {
    flex: 0 0 auto;
    width: 42px;
    height: 42px;
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.alert {
    margin-bottom: 20px;
    border-radius: 18px;
    box-shadow: 0 14px 28px rgba(245, 158, 11, 0.1);
}
.btn.btn-sm.btn-gray {
    border-radius: 999px;
    border: 1px solid rgba(148, 163, 184, 0.26);
    background: #fff;
    color: #1d4ed8;
    font-weight: 700;
}
.btn.btn-sm.btn-gray:hover {
    color: #1e3a8a;
    border-color: rgba(59, 130, 246, 0.34);
    background: #eff6ff;
}
@media (max-width: 1280px) {
    .dashboard-hero,
    .dashboard-secondary-grid {
        grid-template-columns: 1fr !important;
    }
    .dashboard-content-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    }
}
@media (max-width: 900px) {
    .page-header {
        padding: 20px 18px;
    }
    .dashboard-hero-actions,
    .dashboard-hero-side,
    .stats-grid,
    .dashboard-content-grid {
        grid-template-columns: 1fr !important;
    }
    .dashboard-hero-title {
        font-size: 22px;
    }
}
</style>
