<?php
$page_num = isset($_GET['p']) ? intval($_GET['p']) : 1;
$perpage = 15;
$status = isset($_GET['status']) ? $_GET['status'] : '';

$where = "1=1";
if ($status) $where .= " AND t.status = '$status'";

$total = $db->fetchOne("SELECT COUNT(*) as cnt FROM tickets t WHERE $where")['cnt'];
$offset = ($page_num - 1) * $perpage;

$tickets = $db->fetchAll("
    SELECT t.*, u.username 
    FROM tickets t 
    JOIN users u ON t.user_id = u.id 
    WHERE $where 
    ORDER BY CASE t.status WHEN 'open' THEN 1 WHEN 'processing' THEN 2 ELSE 3 END, t.id DESC 
    LIMIT $offset, $perpage
");

$stats = [
    'open' => $db->fetchOne("SELECT COUNT(*) as cnt FROM tickets WHERE status='open'")['cnt'],
    'processing' => $db->fetchOne("SELECT COUNT(*) as cnt FROM tickets WHERE status='processing'")['cnt'],
];
?>

<!-- 统计 -->
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 12px;">
    <div class="card" style="text-align: center; background: #fef3c7;">
        <div style="font-size: 28px; font-weight: 700; color: #92400e;"><?php echo $stats['open']; ?></div>
        <div style="color: #92400e;">待处理</div>
    </div>
    <div class="card" style="text-align: center; background: #ede9fe;">
        <div style="font-size: 28px; font-weight: 700; color: #6d28d9;"><?php echo $stats['processing']; ?></div>
        <div style="color: #6d28d9;">处理中</div>
    </div>
</div>

<!-- 筛选 -->
<div style="display: flex; gap: 8px; margin: 12px 0;">
    <a href="?page=tickets" class="btn" style="background:<?php echo $status==''?'#667eea':'#f5f5f5'; ?>; color:<?php echo $status==''?'white':'#666'; ?>;">全部</a>
    <a href="?page=tickets&status=open" class="btn" style="background:<?php echo $status=='open'?'#667eea':'#f5f5f5'; ?>; color:<?php echo $status=='open'?'white':'#666'; ?>;">待处理</a>
    <a href="?page=tickets&status=processing" class="btn" style="background:<?php echo $status=='processing'?'#667eea':'#f5f5f5'; ?>; color:<?php echo $status=='processing'?'white':'#666'; ?>;">处理中</a>
</div>

<!-- 工单列表 -->
<?php foreach ($tickets as $ticket): ?>
<div class="card">
    <div style="display: flex; justify-content: space-between; margin-bottom: 8px;">
        <span style="font-weight: 600;">#<?php echo $ticket['id']; ?> <?php echo htmlspecialchars($ticket['title']); ?></span>
        <span style="padding: 4px 10px; border-radius: 20px; font-size: 12px;
                     <?php 
                     if ($ticket['status'] == 'open') echo 'background:#fef3c7;color:#92400e;';
                     elseif ($ticket['status'] == 'processing') echo 'background:#ede9fe;color:#6d28d9;';
                     else echo 'background:#e5e7eb;color:#4b5563;';
                     ?>">
            <?php echo $ticket['status'] == 'open' ? '待处理' : ($ticket['status'] == 'processing' ? '处理中' : '已关闭'); ?>
        </span>
    </div>
    <div style="display: flex; align-items: center; gap: 8px; margin-bottom: 10px;">
        <img src="https://api.iconify.design/mdi:account.svg?color=%23666" width="16" height="16" alt="">
        <span style="font-size: 13px;"><?php echo htmlspecialchars($ticket['username']); ?></span>
        <span style="font-size: 12px; color: #999;"><?php echo date('m-d H:i', strtotime($ticket['created_at'])); ?></span>
    </div>
    <a href="?page=ticket_view&id=<?php echo $ticket['id']; ?>" class="btn btn-primary" style="text-decoration: none;">
    <img src="https://api.iconify.design/mdi:eye.svg?color=white" width="16" height="16" alt="">
    查看详情
</a>
</div>
<?php endforeach; ?>

<?php if (empty($tickets)): ?>
<div class="card" style="text-align: center; padding: 40px;">
    <img src="https://api.iconify.design/mdi:ticket-outline.svg?color=%23999" width="48" height="48" alt="">
    <p style="color: #999;">暂无工单</p>
</div>
<?php endif; ?>