<?php
// File: /admin/tickets.php
include 'header.php'; // Includes admin security check and DB connection

$tickets = [];
// This query fetches all tickets from all users, joining to get the user's name
$sql = "SELECT 
            st.id, 
            st.subject, 
            st.status, 
            st.updated_at, 
            u.username 
        FROM support_tickets st
        JOIN users u ON st.user_id = u.id
        ORDER BY st.status = 'open' DESC, st.updated_at DESC";

$result = $conn->query($sql);
if ($result) {
    $tickets = $result->fetch_all(MYSQLI_ASSOC);
}
?>

<div class="p-6 md:p-8">
    <h1 class="text-3xl font-bold text-ucf-charcoal mb-6">Manage Support Tickets</h1>

    <div class="bg-white p-6 rounded-lg shadow-md">
        <h2 class="text-2xl font-bold text-ucf-charcoal mb-4">All Tickets</h2>
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Ticket ID</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Subject</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Submitted By</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Last Updated</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    <?php if (!empty($tickets)): ?>
                        <?php foreach ($tickets as $ticket): ?>
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">#<?php echo $ticket['id']; ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"><?php echo htmlspecialchars($ticket['subject']); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo htmlspecialchars($ticket['username']); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm">
                                   <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full <?php 
                                        if ($ticket['status'] == 'open') echo 'bg-yellow-100 text-yellow-800';
                                        elseif ($ticket['status'] == 'answered') echo 'bg-blue-100 text-blue-800';
                                        else echo 'bg-gray-100 text-gray-800';
                                    ?>">
                                        <?php echo ucfirst($ticket['status']); ?>
                                    </span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo date('M d, Y, h:i A', strtotime($ticket['updated_at'])); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                    <a href="ticket_view.php?id=<?php echo $ticket['id']; ?>" class="text-indigo-600 hover:text-indigo-900">View / Reply</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                         <tr>
                            <td colspan="6" class="px-6 py-12 text-center text-gray-500">No support tickets have been submitted yet.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>

