<?php ob_start(); ?>

<div class="flex items-center justify-between mb-6">
    <div class="flex items-center gap-4">
        <a href="<?= BASE_URL ?>/agent/tickets" class="px-4 py-2 rounded-lg <?= $filter === 'all' ? 'bg-indigo-600 text-white' : 'bg-white text-gray-700 border' ?>">
            Tous les tickets
        </a>
        <a href="<?= BASE_URL ?>/agent/tickets?filter=mes" class="px-4 py-2 rounded-lg <?= $filter === 'mes' ? 'bg-indigo-600 text-white' : 'bg-white text-gray-700 border' ?>">
            Mes tickets
        </a>
    </div>
</div>

<div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
    <div class="overflow-x-auto">
        <table class="w-full">
            <thead class="bg-gray-50 border-b border-gray-100">
                <tr>
                    <th class="text-left px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wide">Ticket</th>
                    <th class="text-left px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wide">Client</th>
                    <th class="text-left px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wide">Sujet</th>
                    <th class="text-left px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wide">Priorité</th>
                    <th class="text-left px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wide">Statut</th>
                    <th class="text-left px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wide">Agent</th>
                    <th class="text-left px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wide">Date</th>
                    <th class="text-right px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wide">Actions</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-100">
                <?php if (empty($tickets)): ?>
                <tr>
                    <td colspan="8" class="px-6 py-12 text-center text-gray-500">
                        <i class="fas fa-ticket-alt text-4xl text-gray-300 mb-3"></i>
                        <p>Aucun ticket trouvé</p>
                    </td>
                </tr>
                <?php else: ?>
                <?php foreach ($tickets as $ticket): ?>
                <?php 
                    $priorityColors = ['urgent' => 'red', 'high' => 'orange', 'medium' => 'yellow', 'low' => 'gray'];
                    $statusColors = ['open' => 'blue', 'assigned' => 'indigo', 'in_progress' => 'cyan', 'resolved' => 'green', 'closed' => 'gray'];
                    $pColor = $priorityColors[$ticket['priorite']] ?? 'gray';
                    $sColor = $statusColors[$ticket['statut']] ?? 'gray';
                ?>
                <tr class="hover:bg-gray-50 transition">
                    <td class="px-6 py-4">
                        <span class="font-mono font-semibold text-orange-600"><?= htmlspecialchars($ticket['ticket_number']) ?></span>
                    </td>
                    <td class="px-6 py-4">
                        <div class="flex items-center gap-3">
                            <div class="w-8 h-8 bg-gray-100 rounded-full flex items-center justify-center">
                                <i class="fas fa-user text-gray-500 text-sm"></i>
                            </div>
                            <div>
                                <p class="font-medium text-gray-800"><?= htmlspecialchars($ticket['client_nom'] ?? 'Client') ?></p>
                                <?php if ($ticket['email']): ?>
                                <p class="text-xs text-gray-500"><?= htmlspecialchars($ticket['email']) ?></p>
                                <?php endif; ?>
                            </div>
                        </div>
                    </td>
                    <td class="px-6 py-4">
                        <p class="text-gray-800 truncate max-w-xs"><?= htmlspecialchars($ticket['sujet']) ?></p>
                    </td>
                    <td class="px-6 py-4">
                        <span class="px-2 py-1 text-xs font-medium rounded-full bg-<?= $pColor ?>-100 text-<?= $pColor ?>-700">
                            <?= ucfirst($ticket['priorite']) ?>
                        </span>
                    </td>
                    <td class="px-6 py-4">
                        <span class="px-2 py-1 text-xs font-medium rounded-full bg-<?= $sColor ?>-100 text-<?= $sColor ?>-700">
                            <?= ucfirst(str_replace('_', ' ', $ticket['statut'])) ?>
                        </span>
                    </td>
                    <td class="px-6 py-4">
                        <?php if ($ticket['agent_nom']): ?>
                        <span class="text-gray-700"><?= htmlspecialchars($ticket['agent_prenom'] . ' ' . $ticket['agent_nom']) ?></span>
                        <?php else: ?>
                        <span class="text-gray-400 italic">Non assigné</span>
                        <?php endif; ?>
                    </td>
                    <td class="px-6 py-4 text-sm text-gray-500">
                        <?= date('d/m/Y H:i', strtotime($ticket['created_at'])) ?>
                    </td>
                    <td class="px-6 py-4 text-right">
                        <div class="flex items-center justify-end gap-2">
                            <?php if (empty($ticket['assigned_agent_id'])): ?>
                            <form action="<?= BASE_URL ?>/agent/tickets/<?= $ticket['id'] ?>/take" method="POST" class="inline">
                                <button type="submit" class="px-3 py-1.5 bg-orange-500 text-white rounded-lg hover:bg-orange-600 transition text-sm">
                                    <i class="fas fa-hand-paper mr-1"></i> Prendre
                                </button>
                            </form>
                            <?php endif; ?>
                            <a href="<?= BASE_URL ?>/agent/tickets/<?= $ticket['id'] ?>" class="px-3 py-1.5 bg-indigo-100 text-indigo-700 rounded-lg hover:bg-indigo-200 transition text-sm">
                                <i class="fas fa-eye mr-1"></i> Voir
                            </a>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php 
$content = ob_get_clean();
$title = 'Tickets - ' . APP_NAME;
$pageTitle = 'Gestion des Tickets';
include VIEWS_PATH . '/layouts/main.php';
?>
