<?php
require_once __DIR__ . '/header.php';
$pdo = db();

// Get counts for dashboard
$case_count = $pdo->query("SELECT COUNT(*) FROM cases")->fetchColumn();
$service_count = $pdo->query("SELECT COUNT(*) FROM services")->fetchColumn();
$lead_count = $pdo->query("SELECT COUNT(*) FROM leads")->fetchColumn();
$tech_count = $pdo->query("SELECT COUNT(*) FROM tech_stack")->fetchColumn();

// Get recent leads
$recent_leads = $pdo->query("SELECT * FROM leads ORDER BY created_at DESC LIMIT 5")->fetchAll();
?>

<div class="row mb-4">
    <div class="col-md-3">
        <div class="card stats-card h-100">
            <div class="stats-icon icon-blue">
                <i class="fas fa-briefcase"></i>
            </div>
            <div>
                <h3 class="mb-0 fw-bold"><?= $case_count ?></h3>
                <p class="text-muted mb-0">声誉作品</p>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card stats-card h-100">
            <div class="stats-icon icon-green">
                <i class="fas fa-cube"></i>
            </div>
            <div>
                <h3 class="mb-0 fw-bold"><?= $service_count ?></h3>
                <p class="text-muted mb-0">核心能力</p>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card stats-card h-100">
            <div class="stats-icon icon-orange">
                <i class="fas fa-headset"></i>
            </div>
            <div>
                <h3 class="mb-0 fw-bold"><?= $lead_count ?></h3>
                <p class="text-muted mb-0">咨询记录</p>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card stats-card h-100">
            <div class="stats-icon icon-red">
                <i class="fas fa-layer-group"></i>
            </div>
            <div>
                <h3 class="mb-0 fw-bold"><?= $tech_count ?></h3>
                <p class="text-muted mb-0">技术底座</p>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span>最新咨询记录</span>
                <a href="leads.php" class="btn btn-sm btn-link">查看全部</a>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th>客户姓名</th>
                                <th>联系方式</th>
                                <th>咨询时间</th>
                                <th>状态</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($recent_leads as $lead): ?>
                            <tr>
                                <td><?= htmlspecialchars($lead['name']) ?></td>
                                <td><?= htmlspecialchars($lead['email'] ?: $lead['phone']) ?></td>
                                <td><?= date('Y-m-d H:i', strtotime($lead['created_at'])) ?></td>
                                <td><span class="badge badge-soft-primary">待处理</span></td>
                            </tr>
                            <?php endforeach; if (empty($recent_leads)): ?>
                            <tr>
                                <td colspan="4" class="text-center py-4">暂无数据</td>
                            </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-4">
        <div class="card">
            <div class="card-header">系统信息</div>
            <div class="card-body">
                <ul class="list-group list-group-flush">
                    <li class="list-group-item d-flex justify-content-between align-items-center px-0">
                        <span>PHP 版本</span>
                        <span class="badge bg-light text-dark"><?= PHP_VERSION ?></span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center px-0">
                        <span>数据库</span>
                        <span class="badge bg-light text-dark">MySQL 5.7+</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center px-0">
                        <span>服务器时间</span>
                        <span class="badge bg-light text-dark"><?= date('Y-m-d H:i:s') ?></span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center px-0">
                        <span>登录用户</span>
                        <span class="badge bg-light text-dark"><?= htmlspecialchars($admin['username']) ?></span>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>

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