<?php
/**
 * Admin Support Tickets
 */
require_once 'includes/header.php';

$filter = $_GET['status'] ?? 'open';
$where = $filter !== 'all' ? "t.status = '$filter'" : "1=1";

$tickets = dbFetchAll("SELECT t.*, u.email, u.first_name FROM support_tickets t LEFT JOIN users u ON t.user_id = u.id WHERE $where ORDER BY t.created_at DESC LIMIT 100");

$stats = [
    'open' => dbFetch("SELECT COUNT(*) as c FROM support_tickets WHERE status = 'open'")['c'] ?? 0,
    'pending' => dbFetch("SELECT COUNT(*) as c FROM support_tickets WHERE status = 'pending'")['c'] ?? 0,
    'closed' => dbFetch("SELECT COUNT(*) as c FROM support_tickets WHERE status = 'closed'")['c'] ?? 0
];
?>

<div class="page-header">
    <h1 class="page-title">Support Tickets</h1>
</div>

<div class="row mb-4">
    <div class="col-md-4">
        <div class="stat-card">
            <div class="stat-icon red"><i class="fas fa-exclamation-circle"></i></div>
            <div class="stat-value"><?= number_format($stats['open']) ?></div>
            <div class="stat-label">Open Tickets</div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="stat-card">
            <div class="stat-icon yellow"><i class="fas fa-clock"></i></div>
            <div class="stat-value"><?= number_format($stats['pending']) ?></div>
            <div class="stat-label">Pending</div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="stat-card">
            <div class="stat-icon green"><i class="fas fa-check-circle"></i></div>
            <div class="stat-value"><?= number_format($stats['closed']) ?></div>
            <div class="stat-label">Closed</div>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <div class="d-flex gap-2">
            <a href="?status=open" class="btn btn-sm <?= $filter === 'open' ? 'btn-primary' : 'btn-secondary' ?>">Open</a>
            <a href="?status=pending" class="btn btn-sm <?= $filter === 'pending' ? 'btn-primary' : 'btn-secondary' ?>">Pending</a>
            <a href="?status=closed" class="btn btn-sm <?= $filter === 'closed' ? 'btn-primary' : 'btn-secondary' ?>">Closed</a>
            <a href="?status=all" class="btn btn-sm <?= $filter === 'all' ? 'btn-primary' : 'btn-secondary' ?>">All</a>
        </div>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive" style="max-height: 500px; overflow-y: auto;">
            <table class="table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>User</th>
                        <th>Subject</th>
                        <th>Priority</th>
                        <th>Status</th>
                        <th>Date</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($tickets)): ?>
                    <tr><td colspan="7" class="text-center py-4">No tickets found</td></tr>
                    <?php else: ?>
                    <?php foreach ($tickets as $ticket): ?>
                    <tr style="cursor:pointer" onclick="window.location='ticket-detail.php?id=<?= $ticket['id'] ?>'">
                        <td>#<?= $ticket['id'] ?></td>
                        <td><?= htmlspecialchars($ticket['email'] ?? 'Unknown') ?></td>
                        <td><?= htmlspecialchars($ticket['subject'] ?? 'No Subject') ?></td>
                        <td>
                            <span class="badge badge-<?= $ticket['priority'] === 'high' ? 'danger' : ($ticket['priority'] === 'medium' ? 'warning' : 'secondary') ?>">
                                <?= ucfirst($ticket['priority'] ?? 'low') ?>
                            </span>
                        </td>
                        <td>
                            <span class="badge badge-<?= $ticket['status'] === 'open' ? 'danger' : ($ticket['status'] === 'pending' ? 'warning' : 'success') ?>">
                                <?= ucfirst($ticket['status']) ?>
                            </span>
                        </td>
                        <td><?= date('d M Y H:i', strtotime($ticket['created_at'])) ?></td>
                        <td><a href="ticket-detail.php?id=<?= $ticket['id'] ?>" class="btn btn-sm btn-primary" onclick="event.stopPropagation()"><i class="fas fa-eye"></i> View</a></td>
                    </tr>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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