<?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
");

// 统计数据
$stats = [
    'total' => $db->fetchOne("SELECT COUNT(*) as cnt FROM tickets")['cnt'],
    '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'],
    'closed' => $db->fetchOne("SELECT COUNT(*) as cnt FROM tickets WHERE status = 'closed'")['cnt'],
];
?>

<div class="tickets-manager">
    <!-- 页面头部 -->
    <div class="page-header">
        <h2>
            <img src="https://api.iconify.design/mdi:ticket.svg?color=%23667eea" width="26" height="26" style="vertical-align: middle; margin-right: 10px;" alt="">
            工单管理
        </h2>
        <p class="text-muted">管理所有用户提交的工单</p>
    </div>
    
    <!-- 统计卡片 -->
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-icon">
                <img src="https://api.iconify.design/mdi:ticket.svg?color=%23667eea" width="28" height="28" alt="">
            </div>
            <div class="stat-content">
                <span class="stat-value"><?php echo $stats['total']; ?></span>
                <span class="stat-label">总工单数</span>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">
                <img src="https://api.iconify.design/mdi:alert-circle.svg?color=%23f59e0b" width="28" height="28" alt="">
            </div>
            <div class="stat-content">
                <span class="stat-value"><?php echo $stats['open']; ?></span>
                <span class="stat-label">待处理</span>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">
                <img src="https://api.iconify.design/mdi:progress-clock.svg?color=%238b5cf6" width="28" height="28" alt="">
            </div>
            <div class="stat-content">
                <span class="stat-value"><?php echo $stats['processing']; ?></span>
                <span class="stat-label">处理中</span>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">
                <img src="https://api.iconify.design/mdi:check-circle.svg?color=%2310b981" width="28" height="28" alt="">
            </div>
            <div class="stat-content">
                <span class="stat-value"><?php echo $stats['closed']; ?></span>
                <span class="stat-label">已关闭</span>
            </div>
        </div>
    </div>
    
    <!-- 筛选标签 -->
    <div class="filter-bar">
        <div class="filter-tabs">
            <a href="?page=tickets" class="filter-tab <?php echo $status == '' ? 'active' : ''; ?>">
                <img src="https://api.iconify.design/mdi:ticket.svg?color=<?php echo $status == '' ? 'white' : '%236b7280'; ?>" width="16" height="16" alt="">
                全部
                <span class="count"><?php echo $stats['total']; ?></span>
            </a>
            <a href="?page=tickets&status=open" class="filter-tab <?php echo $status == 'open' ? 'active' : ''; ?>">
                <img src="https://api.iconify.design/mdi:alert-circle.svg?color=<?php echo $status == 'open' ? 'white' : '%23f59e0b'; ?>" width="16" height="16" alt="">
                待处理
                <span class="count"><?php echo $stats['open']; ?></span>
            </a>
            <a href="?page=tickets&status=processing" class="filter-tab <?php echo $status == 'processing' ? 'active' : ''; ?>">
                <img src="https://api.iconify.design/mdi:progress-clock.svg?color=<?php echo $status == 'processing' ? 'white' : '%238b5cf6'; ?>" width="16" height="16" alt="">
                处理中
                <span class="count"><?php echo $stats['processing']; ?></span>
            </a>
            <a href="?page=tickets&status=closed" class="filter-tab <?php echo $status == 'closed' ? 'active' : ''; ?>">
                <img src="https://api.iconify.design/mdi:check-circle.svg?color=<?php echo $status == 'closed' ? 'white' : '%2310b981'; ?>" width="16" height="16" alt="">
                已关闭
                <span class="count"><?php echo $stats['closed']; ?></span>
            </a>
        </div>
    </div>
    
    <!-- 工单列表 -->
    <div class="table-wrapper">
        <table class="data-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>标题</th>
                    <th>用户</th>
                    <th>状态</th>
                    <th>优先级</th>
                    <th>创建时间</th>
                    <th>更新时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($tickets)): ?>
                    <tr>
                        <td colspan="8">
                            <div class="empty-state">
                                <img src="https://api.iconify.design/mdi:ticket-outline.svg?color=%239ca3af" width="48" height="48" alt="">
                                <p>暂无工单</p>
                            </div>
                        </td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($tickets as $ticket): ?>
                        <tr>
                            <td class="id-cell">#<?php echo $ticket['id']; ?></td>
                            <td>
                                <div class="ticket-title">
                                    <img src="https://api.iconify.design/mdi:ticket.svg?color=%23667eea" width="14" height="14" alt="">
                                    <strong><?php echo htmlspecialchars($ticket['title']); ?></strong>
                                </div>
                            </td>
                            <td>
                                <div class="user-info">
                                    <img src="https://api.iconify.design/mdi:account.svg?color=%236b7280" width="14" height="14" alt="">
                                    <?php echo htmlspecialchars($ticket['username']); ?>
                                </div>
                            </td>
                            <td>
                                <span class="status-badge status-<?php echo $ticket['status']; ?>">
                                    <span class="status-dot"></span>
                                    <?php 
                                    $status_map = ['open' => '待处理', 'processing' => '处理中', 'closed' => '已关闭'];
                                    echo $status_map[$ticket['status']] ?? $ticket['status'];
                                    ?>
                                </span>
                            </td>
                            <td>
                                <span class="priority-badge priority-<?php echo $ticket['priority']; ?>">
                                    <?php 
                                    $priority_map = [
                                        'low' => ['icon' => 'chevron-down', 'label' => '低', 'color' => '10b981'],
                                        'medium' => ['icon' => 'minus', 'label' => '中', 'color' => 'f59e0b'],
                                        'high' => ['icon' => 'chevron-up', 'label' => '高', 'color' => 'ef4444']
                                    ];
                                    $p = $priority_map[$ticket['priority']] ?? ['icon' => 'circle', 'label' => $ticket['priority'], 'color' => '6b7280'];
                                    ?>
                                    <img src="https://api.iconify.design/mdi:<?php echo $p['icon']; ?>.svg?color=%23<?php echo $p['color']; ?>" width="14" height="14" alt="">
                                    <?php echo $p['label']; ?>
                                </span>
                            </td>
                            <td><?php echo date('Y-m-d H:i', strtotime($ticket['created_at'])); ?></td>
                            <td><?php echo date('Y-m-d H:i', strtotime($ticket['updated_at'])); ?></td>
                            <td>
                                <a href="?page=ticket_view&id=<?php echo $ticket['id']; ?>" class="btn-small">
                                    <img src="https://api.iconify.design/mdi:eye.svg?color=white" width="14" height="14" style="margin-right: 4px;" alt="">
                                    处理
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
    <!-- 分页 -->
    <?php if ($total > $perpage): ?>
        <div class="pagination">
            <?php if ($page > 1): ?>
                <a href="?page=tickets&p=<?php echo $page - 1; ?>&status=<?php echo $status; ?>" class="page-link page-prev">
                    <img src="https://api.iconify.design/mdi:chevron-left.svg?color=currentColor" width="16" height="16" alt="">
                </a>
            <?php endif; ?>
            
            <?php
            $total_pages = ceil($total / $perpage);
            $start_page = max(1, $page - 2);
            $end_page = min($total_pages, $page + 2);
            
            if ($start_page > 1): ?>
                <a href="?page=tickets&p=1&status=<?php echo $status; ?>" class="page-link">1</a>
                <?php if ($start_page > 2): ?>
                    <span class="page-ellipsis">...</span>
                <?php endif; ?>
            <?php endif; ?>
            
            <?php for ($i = $start_page; $i <= $end_page; $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; ?>
            
            <?php if ($end_page < $total_pages): ?>
                <?php if ($end_page < $total_pages - 1): ?>
                    <span class="page-ellipsis">...</span>
                <?php endif; ?>
                <a href="?page=tickets&p=<?php echo $total_pages; ?>&status=<?php echo $status; ?>" class="page-link"><?php echo $total_pages; ?></a>
            <?php endif; ?>
            
            <?php if ($page < $total_pages): ?>
                <a href="?page=tickets&p=<?php echo $page + 1; ?>&status=<?php echo $status; ?>" class="page-link page-next">
                    <img src="https://api.iconify.design/mdi:chevron-right.svg?color=currentColor" width="16" height="16" alt="">
                </a>
            <?php endif; ?>
        </div>
    <?php endif; ?>
</div>

<style>
/* 基础变量 */
:root {
    --primary-color: #667eea;
    --primary-hover: #5a67d8;
    --danger-color: #ef4444;
    --success-color: #10b981;
    --warning-color: #f59e0b;
    --bg-primary: #ffffff;
    --bg-secondary: #f9fafb;
    --text-primary: #1f2937;
    --text-secondary: #6b7280;
    --text-muted: #9ca3af;
    --border-color: #e5e7eb;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
}

.tickets-manager {
    max-width: 100%;
    padding: 4px;
}

/* 页面头部 */
.page-header {
    margin-bottom: 20px;
}
.page-header h2 {
    display: flex;
    align-items: center;
    font-size: 24px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0 0 6px;
}
.text-muted {
    color: var(--text-muted);
    font-size: 13px;
}

/* 统计卡片 */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
    margin-bottom: 24px;
}
.stat-card {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    padding: 20px;
    border: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 16px;
    transition: all 0.2s;
    box-shadow: var(--shadow-sm);
}
.stat-card:hover {
    box-shadow: var(--shadow-md);
    transform: translateY(-2px);
}
.stat-icon {
    width: 52px;
    height: 52px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(102, 126, 234, 0.05) 100%);
}
.stat-content {
    display: flex;
    flex-direction: column;
}
.stat-value {
    font-size: 26px;
    font-weight: 700;
    color: var(--text-primary);
    line-height: 1.2;
}
.stat-label {
    font-size: 13px;
    color: var(--text-muted);
    margin-top: 4px;
}

/* 筛选标签 */
.filter-bar {
    margin-bottom: 24px;
}
.filter-tabs {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}
.filter-tab {
    display: inline-flex;
    align-items: center;
    padding: 10px 18px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 30px;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    text-decoration: none;
    transition: all 0.2s;
}
.filter-tab:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
    background: rgba(102, 126, 234, 0.05);
}
.filter-tab.active {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}
.filter-tab .count {
    margin-left: 6px;
    padding: 2px 8px;
    background: rgba(0, 0, 0, 0.1);
    border-radius: 20px;
    font-size: 12px;
}
.filter-tab.active .count {
    background: rgba(255, 255, 255, 0.2);
}

/* 表格 */
.table-wrapper {
    overflow-x: auto;
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
}
.data-table {
    width: 100%;
    border-collapse: collapse;
    min-width: 900px;
}
.data-table th {
    padding: 16px 16px;
    text-align: left;
    font-weight: 600;
    font-size: 13px;
    color: var(--text-secondary);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    white-space: nowrap;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}
.data-table td {
    padding: 16px;
    border-bottom: 1px solid var(--border-color);
    font-size: 14px;
    vertical-align: middle;
}
.data-table tbody tr {
    transition: background 0.2s;
}
.data-table tbody tr:hover {
    background: var(--bg-secondary);
}
.data-table tbody tr:last-child td {
    border-bottom: none;
}

/* 单元格样式 */
.id-cell {
    font-family: 'Monaco', 'Menlo', monospace;
    font-size: 12px;
    color: var(--text-muted);
    font-weight: 500;
}
.ticket-title {
    display: flex;
    align-items: center;
    gap: 8px;
}
.user-info {
    display: flex;
    align-items: center;
    gap: 6px;
}

/* 状态徽章 */
.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 12px;
    border-radius: 30px;
    font-size: 12px;
    font-weight: 500;
    white-space: nowrap;
}
.status-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
}
.status-open {
    background: #fef3c7;
    color: #92400e;
}
.status-open .status-dot {
    background: #f59e0b;
}
.status-processing {
    background: #ede9fe;
    color: #6d28d9;
}
.status-processing .status-dot {
    background: #8b5cf6;
}
.status-closed {
    background: #e5e7eb;
    color: #4b5563;
}
.status-closed .status-dot {
    background: #6b7280;
}

/* 优先级徽章 */
.priority-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    border-radius: 30px;
    font-size: 12px;
    font-weight: 500;
}
.priority-low {
    background: #d1fae5;
    color: #065f46;
}
.priority-medium {
    background: #fef3c7;
    color: #92400e;
}
.priority-high {
    background: #fee2e2;
    color: #991b1b;
}

/* 按钮 */
.btn-small {
    display: inline-flex;
    align-items: center;
    padding: 7px 16px;
    background: var(--primary-color);
    color: white;
    border-radius: var(--radius-sm);
    text-decoration: none;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.2s;
}
.btn-small:hover {
    background: var(--primary-hover);
    transform: translateY(-1px);
    box-shadow: var(--shadow-sm);
}

/* 空状态 */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    text-align: center;
}
.empty-state img {
    opacity: 0.4;
    margin-bottom: 16px;
}
.empty-state p {
    color: var(--text-muted);
    font-size: 15px;
    margin: 0;
}

/* 分页 */
.pagination {
    display: flex;
    gap: 6px;
    justify-content: center;
    margin-top: 28px;
}
.page-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 38px;
    height: 38px;
    padding: 0 8px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    text-decoration: none;
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 500;
    background: var(--bg-primary);
    transition: all 0.2s;
}
.page-link:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
    background: rgba(102, 126, 234, 0.05);
}
.page-link.active {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}
.page-ellipsis {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 38px;
    height: 38px;
    color: var(--text-muted);
}

/* 响应式 */
@media (max-width: 1024px) {
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}
@media (max-width: 768px) {
    .stats-grid {
        grid-template-columns: 1fr;
    }
    .filter-tabs {
        flex-direction: column;
    }
    .filter-tab {
        justify-content: center;
    }
}
</style>