<?php
requireAdmin();

$page = isset($_GET['p']) ? intval($_GET['p']) : 1;
$status = isset($_GET['status']) ? $_GET['status'] : '';
$perpage = 20;

// 构建查询条件
$where = "1=1";
if ($status) {
    $where .= " AND t.status = '$status'";
}

// 获取总数
$total_result = $db->query("SELECT COUNT(*) as cnt FROM tickets t WHERE $where");
$total = $total_result->fetch_assoc()['cnt'];
$offset = ($page - 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
");
?>

<div class="tickets-manager">
    <div class="page-header">
        <h2>工单管理</h2>
    </div>
    
    <!-- 筛选栏 -->
    <div class="filter-bar">
        <select id="statusFilter" onchange="location.href='?page=tickets&status='+this.value">
            <option value="">全部状态</option>
            <option value="open" <?php echo $status == 'open' ? 'selected' : ''; ?>>待处理</option>
            <option value="processing" <?php echo $status == 'processing' ? 'selected' : ''; ?>>处理中</option>
            <option value="closed" <?php echo $status == 'closed' ? 'selected' : ''; ?>>已关闭</option>
        </select>
    </div>
    
    <div class="table-container">
        <table class="data-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>标题</th>
                    <th>用户</th>
                    <th>状态</th>
                    <th>优先级</th>
                    <th>创建时间</th>
                    <th>更新时间</th>
                    <th>操作</th>
                </thead>
            <tbody>
                <?php if (empty($tickets)): ?>
                    <tr><td colspan="8" style="text-align: center;">暂无工单</td></tr>
                <?php else: ?>
                    <?php foreach ($tickets as $ticket): ?>
                        <tr>
                            <td>#<?php echo $ticket['id']; ?></td>
                            <td><strong><?php echo htmlspecialchars($ticket['title']); ?></strong></td>
                            <td><?php echo htmlspecialchars($ticket['username']); ?></td>
                            <td>
                                <span class="status-badge status-<?php echo $ticket['status']; ?>">
                                    <?php 
                                    $status_map = ['open' => '待处理', 'processing' => '处理中', 'closed' => '已关闭'];
                                    echo $status_map[$ticket['status']] ?? $ticket['status'];
                                    ?>
                                </span>
                            </td>
                            <td>
                                <span class="priority-<?php echo $ticket['priority']; ?>">
                                    <?php 
                                    $priority_map = ['low' => '低', 'medium' => '中', 'high' => '高'];
                                    echo $priority_map[$ticket['priority']] ?? $ticket['priority'];
                                    ?>
                                </span>
                            </td>
                            <td><?php echo $ticket['created_at']; ?></td>
                            <td><?php echo $ticket['updated_at']; ?></td>
                            <td>
                                <a href="?page=ticket_view&id=<?php echo $ticket['id']; ?>" class="btn-small">处理</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
    <!-- 分页 -->
    <?php if ($total > $perpage): ?>
        <div class="pagination">
            <?php
            $total_pages = ceil($total / $perpage);
            for ($i = 1; $i <= $total_pages; $i++):
            ?>
                <a href="?page=tickets&p=<?php echo $i; ?>&status=<?php echo $status; ?>" class="page-link <?php echo $i == $page ? 'active' : ''; ?>">
                    <?php echo $i; ?>
                </a>
            <?php endfor; ?>
        </div>
    <?php endif; ?>
</div>

<style>
.filter-bar {
    margin-bottom: 20px;
}
.filter-bar select {
    padding: 8px 16px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-primary);
}
.status-badge {
    padding: 3px 8px;
    border-radius: 20px;
    font-size: 12px;
}
.status-open, .status-processing {
    background: #d1fae5;
    color: #065f46;
}
.status-closed {
    background: #fee2e2;
    color: #991b1b;
}
.priority-low {
    color: #6b7280;
}
.priority-medium {
    color: #f59e0b;
}
.priority-high {
    color: #ef4444;
}
.btn-small {
    padding: 5px 12px;
    background: var(--primary-color);
    color: white;
    border-radius: 5px;
    text-decoration: none;
    font-size: 12px;
}
.btn-small:hover {
    background: var(--primary-hover);
}
.pagination {
    display: flex;
    gap: 8px;
    justify-content: center;
    margin-top: 20px;
}
.page-link {
    padding: 6px 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    text-decoration: none;
    color: var(--text-primary);
}
.page-link.active {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}
</style>