<?php
// 仪表盘统计逻辑
$invoiceCount = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as count FROM invoice"))['count'];
$supplierCount = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as count FROM supplier WHERE status=1"))['count'];
$customerCount = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as count FROM customer WHERE status=1"))['count'];
?>

<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; margin-bottom: 30px;">
    <div style="background-color: #f8f9fa; padding: 20px; border-radius: 8px; text-align: center;">
        <h3 style="color: #3498db; font-size: 24px; margin-bottom: 10px;"><?= $invoiceCount ?></h3>
        <p style="color: #666;">总发票数量</p>
    </div>
    <div style="background-color: #f8f9fa; padding: 20px; border-radius: 8px; text-align: center;">
        <h3 style="color: #2ecc71; font-size: 24px; margin-bottom: 10px;"><?= $supplierCount ?></h3>
        <p style="color: #666;">有效供应商</p>
    </div>
    <div style="background-color: #f8f9fa; padding: 20px; border-radius: 8px; text-align: center;">
        <h3 style="color: #e74c3c; font-size: 24px; margin-bottom: 10px;"><?= $customerCount ?></h3>
        <p style="color: #666;">有效客户</p>
    </div>
</div>

<div>
    <h4>最近添加的发票</h4>
    <table class="table">
        <thead>
            <tr>
                <th>发票号码</th>
                <th>金额</th>
                <th>供应商</th>
                <th>添加时间</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $sql = "SELECT i.invoice_no, i.amount, s.name as supplier_name, i.create_time 
                    FROM invoice i 
                    LEFT JOIN supplier s ON i.supplier_id = s.id 
                    ORDER BY i.create_time DESC LIMIT 10";
            $res = mysqli_query($conn, $sql);
            if (mysqli_num_rows($res) > 0) {
                while ($row = mysqli_fetch_assoc($res)) {
                    echo "<tr>
                            <td>{$row['invoice_no']}</td>
                            <td>{$row['amount']}</td>
                            <td>{$row['supplier_name']}</td>
                            <td>{$row['create_time']}</td>
                          </tr>";
                }
            } else {
                echo "<tr><td colspan='4' style='text-align: center;'>暂无发票数据</td></tr>";
            }
            ?>
        </tbody>
    </table>
</div>