<?php
ob_start();
/**
 * 2026企业CMS - 后台仪表盘
 */
require_once __DIR__ . '/header.php';
$pageTitle = '后台仪表盘';
$todayStart = date('Y-m-d 00:00:00');
$stats = [
    'articles'  => db_count('articles', 'status=1'),
    'products'  => db_count('products', 'status=1'),
    'cases'     => db_count('cases', 'status=1'),
    'contacts'  => db_count('contacts', 'is_read=0'),
    'admins'    => db_count('admins', 'status=1'),
    'today_visits' => 0, // 需要访问统计功能
];
$latestArticles = db_query("SELECT `id`,`title`,`slug`,`created_at` FROM `articles` ORDER BY `id` DESC LIMIT 5");
$latestContacts = db_query("SELECT `id`,`name`,`phone`,`created_at` FROM `contacts` ORDER BY `id` DESC LIMIT 5");
$recentLogs   = db_query("SELECT l.*,a.username FROM `logs` l LEFT JOIN `admins` a ON l.`admin_id`=a.`id` ORDER BY l.`id` DESC LIMIT 8");
?>
<div class="page-header">
    <h2>后台仪表盘</h2>
    <div>
        <button class="btn btn-primary" onclick="location.reload()"><i class="fas fa-sync-alt"></i> 刷新</button>
        <a href="/" target="_blank" class="btn btn-success"><i class="fas fa-external-link-alt"></i> 查看网站</a>
    </div>
</div>

<!-- 统计卡片 -->
<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-icon" style="background:#2c82e0;"><i class="fas fa-newspaper"></i></div>
        <div class="stat-info"><h3><?php echo $stats['articles']; ?></h3><p>文章总数</p></div>
    </div>
    <div class="stat-card">
        <div class="stat-icon" style="background:#52c41a;"><i class="fas fa-box"></i></div>
        <div class="stat-info"><h3><?php echo $stats['products']; ?></h3><p>产品总数</p></div>
    </div>
    <div class="stat-card">
        <div class="stat-icon" style="background:#faad14;"><i class="fas fa-images"></i></div>
        <div class="stat-info"><h3><?php echo $stats['cases']; ?></h3><p>案例总数</p></div>
    </div>
    <div class="stat-card">
        <div class="stat-icon" style="background:#f5222d;"><i class="fas fa-envelope"></i></div>
        <div class="stat-info"><h3><?php echo $stats['contacts']; ?></h3><p>未读留言</p></div>
    </div>
</div>

<!-- 快捷操作 -->
<div style="display:flex;gap:20px;margin-bottom:30px;">
    <a href="/admin/articles.php?action=add" class="btn btn-primary"><i class="fas fa-plus"></i> 发布文章</a>
    <a href="/admin/products.php?action=add" class="btn btn-success"><i class="fas fa-plus"></i> 添加产品</a>
    <a href="/admin/cases.php?action=add" class="btn btn-warning"><i class="fas fa-plus"></i> 添加案例</a>
    <a href="/admin/settings.php" class="btn"><i class="fas fa-cog"></i> 网站设置</a>
</div>

<!-- 最新文章 + 最新留言 -->
<div style="display:flex;gap:20px;">
    <div style="flex:1;background:#fff;border-radius:8px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,0.1);">
        <h3 style="margin-bottom:15px;font-size:16px;">最新文章</h3>
        <ul style="list-style:none;">
            <?php foreach ($latestArticles as $a): ?>
            <li style="padding:8px 0;border-bottom:1px solid #f0f0f0;display:flex;justify-content:space-between;">
                <a href="/article/<?php echo $a['slug']; ?>.html" target="_blank"><?php echo xss_clean(str_cut($a['title'],30)); ?></a>
                <span style="color:#999;font-size:12px;"><?php echo date('m-d',strtotime($a['created_at'])); ?></span>
            </li>
            <?php endforeach; ?>
        </ul>
        <div style="margin-top:10px;"><a href="/admin/articles.php" style="font-size:13px;color:#999;">查看全部 →</a></div>
    </div>
    
    <div style="flex:1;background:#fff;border-radius:8px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,0.1);">
        <h3 style="margin-bottom:15px;font-size:16px;">最新留言</h3>
        <ul style="list-style:none;">
            <?php foreach ($latestContacts as $c): ?>
            <li style="padding:8px 0;border-bottom:1px solid #f0f0f0;">
                <div style="font-weight:bold;font-size:13px;"><?php echo xss_clean($c['name']); ?> <span style="color:#999;font-weight:normal;"><?php echo $c['phone']; ?></span></div>
                <div style="color:#999;font-size:12px;"><?php echo date('m-d H:i',strtotime($c['created_at'])); ?></div>
            </li>
            <?php endforeach; ?>
        </ul>
        <div style="margin-top:10px;"><a href="/admin/contacts.php" style="font-size:13px;color:#999;">查看全部 →</a></div>
    </div>
</div>

<!-- 操作日志 -->
<div style="margin-top:20px;background:#fff;border-radius:8px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,0.1);">
    <h3 style="margin-bottom:15px;font-size:16px;">最近操作日志</h3>
    <table>
        <thead><tr><th>管理员</th><th>操作</th><th>模块</th><th>说明</th><th>时间</th></tr></thead>
        <tbody>
            <?php foreach ($recentLogs as $log): ?>
            <tr>
                <td><?php echo xss_clean($log['username'] ?: '系统'); ?></td>
                <td><?php echo xss_clean($log['action']); ?></td>
                <td><?php echo xss_clean($log['module']); ?></td>
                <td><?php echo xss_clean(str_cut($log['description'],30)); ?></td>
                <td><?php echo date('m-d H:i',strtotime($log['created_at'])); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<?php require_once __DIR__ . '/footer.php'; ?>
