<?php
// File: /artist/tickets.php
include 'header.php'; // Includes security check and DB connection

$artist_id = $_SESSION['user_id'];
$tickets = [];

// Fetch all support tickets for the current artist
$stmt = $conn->prepare("SELECT id, subject, status, updated_at FROM support_tickets WHERE user_id = ? ORDER BY updated_at DESC");
$stmt->bind_param("i", $artist_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result) {
    $tickets = $result->fetch_all(MYSQLI_ASSOC);
}
$stmt->close();
?>

<div class="p-6 md:p-8">
    <div class="flex justify-between items-center mb-6">
        <h1 class="text-3xl font-bold text-ucf-charcoal">Support Tickets</h1>
        <a href="ticket_new.php" class="bg-ucf-green text-white font-bold py-2 px-4 rounded-lg hover:bg-ucf-green-dark transition-colors flex items-center">
            <i class="fas fa-plus-circle mr-2"></i> Create New Ticket
        </a>
    </div>

    <div class="bg-white p-6 rounded-lg shadow-md">
        <h2 class="text-2xl font-bold text-ucf-charcoal mb-4">Your 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">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>
                                <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">
                                    <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</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="5" class="px-6 py-12 text-center text-gray-500">You have not created any support tickets yet.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
