<?php
$apps_count = $db->fetchOne("SELECT COUNT(*) as cnt FROM apps WHERE status = 1")['cnt'] ?? 0;
$licenses_count = $db->fetchOne("SELECT COUNT(*) as cnt FROM licenses")['cnt'] ?? 0;
$users_count = $db->fetchOne("SELECT COUNT(*) as cnt FROM users WHERE role = 'user'")['cnt'] ?? 0;
$today_income = $db->fetchOne("SELECT SUM(amount) as total FROM orders WHERE status = 'paid' AND DATE(paid_at) = CURDATE()")['total'] ?? 0;
$pending_tickets = $db->fetchOne("SELECT COUNT(*) as cnt FROM tickets WHERE status IN ('open','processing')")['cnt'] ?? 0;

require_once __DIR__ . '/../../includes/LicenseClient.php';
try {
    $client = new LicenseClient();
    $current_version = $client->getCurrentVersion();
    $update_info = $client->checkUpdate();
    $has_update = $update_info['has_update'] ?? false;
} catch (Exception $e) {
    $current_version = defined('SYS_VERSION') ? SYS_VERSION : '1.0.0';
    $has_update = false;
}
?>

<div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px;">
    <div class="card" style="text-align: center;">
        <img src="https://api.iconify.design/mdi:account-group.svg?color=%23667eea" width="32" height="32" alt="">
        <div style="font-size: 28px; font-weight: 700; color: #667eea;"><?php echo number_format($users_count); ?></div>
        <div style="color: #999; font-size: 13px;">总用户</div>
    </div>
    <div class="card" style="text-align: center;">
        <img src="https://api.iconify.design/mdi:application.svg?color=%2310b981" width="32" height="32" alt="">
        <div style="font-size: 28px; font-weight: 700; color: #10b981;"><?php echo number_format($apps_count); ?></div>
        <div style="color: #999; font-size: 13px;">应用</div>
    </div>
    <div class="card" style="text-align: center;">
        <img src="https://api.iconify.design/mdi:key.svg?color=%23f59e0b" width="32" height="32" alt="">
        <div style="font-size: 28px; font-weight: 700; color: #f59e0b;"><?php echo number_format($licenses_count); ?></div>
        <div style="color: #999; font-size: 13px;">授权码</div>
    </div>
    <div class="card" style="text-align: center;">
        <img src="https://api.iconify.design/mdi:cash.svg?color=%23ef4444" width="32" height="32" alt="">
        <div style="font-size: 28px; font-weight: 700; color: #ef4444;">¥<?php echo number_format($today_income, 2); ?></div>
        <div style="color: #999; font-size: 13px;">今日收入</div>
    </div>
</div>

<div class="card">
    <h3 style="margin-bottom: 16px; font-size: 16px; display: flex; align-items: center; gap: 8px;">
        <img src="https://api.iconify.design/mdi:flash.svg?color=%23667eea" width="20" height="20" alt="">
        快捷入口
    </h3>
    <div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;">
        <a href="?page=users" style="text-decoration: none;">
            <div style="background: #667eea15; border-radius: 12px; padding: 12px; text-align: center;">
                <img src="https://api.iconify.design/mdi:account-group.svg?color=%23667eea" width="28" height="28" alt="">
                <div style="font-size: 12px; margin-top: 6px; color: #333;">用户</div>
            </div>
        </a>
        <a href="?page=apps" style="text-decoration: none;">
            <div style="background: #10b98115; border-radius: 12px; padding: 12px; text-align: center;">
                <img src="https://api.iconify.design/mdi:application.svg?color=%2310b981" width="28" height="28" alt="">
                <div style="font-size: 12px; margin-top: 6px; color: #333;">应用</div>
            </div>
        </a>
        <a href="?page=orders" style="text-decoration: none;">
            <div style="background: #f59e0b15; border-radius: 12px; padding: 12px; text-align: center;">
                <img src="https://api.iconify.design/mdi:package.svg?color=%23f59e0b" width="28" height="28" alt="">
                <div style="font-size: 12px; margin-top: 6px; color: #333;">订单</div>
            </div>
        </a>
        <a href="?page=card_codes" style="text-decoration: none;">
            <div style="background: #8b5cf615; border-radius: 12px; padding: 12px; text-align: center;">
                <img src="https://api.iconify.design/mdi:credit-card.svg?color=%238b5cf6" width="28" height="28" alt="">
                <div style="font-size: 12px; margin-top: 6px; color: #333;">卡密</div>
            </div>
        </a>
        <a href="?page=tickets" style="text-decoration: none;">
            <div style="background: #ef444415; border-radius: 12px; padding: 12px; text-align: center;">
                <img src="https://api.iconify.design/mdi:ticket.svg?color=%23ef4444" width="28" height="28" alt="">
                <div style="font-size: 12px; margin-top: 6px; color: #333;">工单</div>
            </div>
        </a>
        <a href="?page=announcements" style="text-decoration: none;">
            <div style="background: #06b6d415; border-radius: 12px; padding: 12px; text-align: center;">
                <img src="https://api.iconify.design/mdi:bullhorn.svg?color=%2306b6d4" width="28" height="28" alt="">
                <div style="font-size: 12px; margin-top: 6px; color: #333;">公告</div>
            </div>
        </a>
    </div>
</div>

<div class="card">
    <h3 style="margin-bottom: 12px; font-size: 16px; display: flex; align-items: center; gap: 8px;">
        <img src="https://api.iconify.design/mdi:information.svg?color=%23667eea" width="20" height="20" alt="">
        系统状态
    </h3>
    <div style="display: flex; flex-direction: column; gap: 10px;">
        <div style="display: flex; justify-content: space-between;">
            <span style="color: #999;">当前版本</span>
            <span>v<?php echo htmlspecialchars($current_version); ?></span>
        </div>
        <div style="display: flex; justify-content: space-between;">
            <span style="color: #999;">PHP版本</span>
            <span><?php echo PHP_VERSION; ?></span>
        </div>
        <div style="display: flex; justify-content: space-between;">
            <span style="color: #999;">待处理工单</span>
            <span style="color: #ef4444;"><?php echo $pending_tickets; ?></span>
        </div>
        <?php if ($has_update): ?>
            <a href="?page=update" class="btn btn-primary" style="text-decoration: none; margin-top: 8px;">
                <img src="https://api.iconify.design/mdi:cloud-download.svg?color=white" width="18" height="18" alt="">
                有新版本，立即更新
            </a>
        <?php endif; ?>
    </div>
</div>