<?php
/**
 * 首页 - 中奖概览
 */
require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/functions.php';

requireLogin();

$pageTitle = '彩票助手 - 首页';

// 统计数据
$totalTickets = db()->query("SELECT COUNT(*) FROM my_tickets")->fetchColumn();
$totalFixed = db()->query("SELECT COUNT(*) FROM fixed_numbers WHERE is_active = 1")->fetchColumn();
$totalPrize = db()->query("SELECT COALESCE(SUM(prize_amount), 0) FROM prize_records")->fetchColumn();
$prizeCount = db()->query("SELECT COUNT(*) FROM prize_records")->fetchColumn();

// 最近中奖记录
$recentPrizes = db()->query("SELECT * FROM prize_records ORDER BY created_at DESC LIMIT 10")->fetchAll();

// 最新开奖
$latestSSQ = db()->query("SELECT * FROM lottery_results WHERE lottery_type = 'ssq' ORDER BY issue_no DESC LIMIT 1")->fetch();
$latestDLT = db()->query("SELECT * FROM lottery_results WHERE lottery_type = 'dlt' ORDER BY issue_no DESC LIMIT 1")->fetch();

require_once __DIR__ . '/includes/header.php';
?>

<div class="stats">
    <div class="stat-item">
        <div class="stat-value"><?php echo $totalTickets; ?></div>
        <div class="stat-label">已录入号码</div>
    </div>
    <div class="stat-item">
        <div class="stat-value"><?php echo $totalFixed; ?></div>
        <div class="stat-label">守号数量</div>
    </div>
    <div class="stat-item">
        <div class="stat-value"><?php echo $prizeCount; ?></div>
        <div class="stat-label">中奖次数</div>
    </div>
    <div class="stat-item">
        <div class="stat-value">¥<?php echo number_format($totalPrize, 0); ?></div>
        <div class="stat-label">累计奖金</div>
    </div>
</div>

<div class="card">
    <h2>最新开奖</h2>
    <table>
        <tr>
            <th>彩种</th>
            <th>期号</th>
            <th>开奖号码</th>
            <th>开奖日期</th>
        </tr>
        <?php if ($latestSSQ): ?>
        <tr>
            <td><?php echo getLotteryTag('ssq'); ?></td>
            <td><?php echo $latestSSQ['issue_no']; ?></td>
            <td><?php echo formatNumbers('ssq', $latestSSQ['numbers']); ?></td>
            <td><?php echo $latestSSQ['draw_date']; ?></td>
        </tr>
        <?php endif; ?>
        <?php if ($latestDLT): ?>
        <tr>
            <td><?php echo getLotteryTag('dlt'); ?></td>
            <td><?php echo $latestDLT['issue_no']; ?></td>
            <td><?php echo formatNumbers('dlt', $latestDLT['numbers']); ?></td>
            <td><?php echo $latestDLT['draw_date']; ?></td>
        </tr>
        <?php endif; ?>
        <?php if (!$latestSSQ && !$latestDLT): ?>
        <tr><td colspan="4" style="text-align:center;color:#999;">暂无开奖数据，请先<a href="results.php">采集开奖结果</a></td></tr>
        <?php endif; ?>
    </table>
</div>

<div class="card">
    <h2 style="display:flex;justify-content:space-between;align-items:center;">
        最近中奖记录
        <?php if ($prizeCount > 10): ?>
        <a href="prizes.php" style="font-size:12px;font-weight:normal;">查看全部 &raquo;</a>
        <?php endif; ?>
    </h2>
    <?php if (empty($recentPrizes)): ?>
        <p style="color:#999;text-align:center;">暂无中奖记录</p>
    <?php else: ?>
    <table>
        <tr>
            <th>彩种</th>
            <th>期号</th>
            <th>我的号码</th>
            <th>奖级</th>
            <th>奖金</th>
            <th>类型</th>
        </tr>
        <?php foreach ($recentPrizes as $prize): ?>
        <tr>
            <td><?php echo getLotteryTag($prize['lottery_type']); ?></td>
            <td><?php echo $prize['issue_no']; ?></td>
            <td><?php echo formatNumbers($prize['lottery_type'], $prize['my_numbers']); ?></td>
            <td><?php echo $prize['prize_level']; ?>等奖</td>
            <td style="color:#e53935;font-weight:bold;">¥<?php echo number_format($prize['prize_amount'], 0); ?></td>
            <td><?php echo $prize['is_fixed'] ? '<span class="tag tag-fixed">守号</span>' : '单期'; ?></td>
        </tr>
        <?php endforeach; ?>
    </table>
    <?php endif; ?>
</div>

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