<?php ob_start(); ?>

<?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';
?>

<div class="mb-6">
    <a href="<?= BASE_URL ?>/agent/tickets" class="text-indigo-600 hover:text-indigo-800 transition">
        <i class="fas fa-arrow-left mr-2"></i> Retour aux tickets
    </a>
</div>

<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
    <!-- Détails du ticket -->
    <div class="lg:col-span-2 space-y-6">
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
            <div class="p-6 border-b border-gray-100 bg-gradient-to-r from-orange-50 to-red-50">
                <div class="flex items-start justify-between">
                    <div>
                        <div class="flex items-center gap-3 mb-2">
                            <span class="font-mono text-2xl font-bold text-orange-600"><?= htmlspecialchars($ticket['ticket_number']) ?></span>
                            <span class="px-3 py-1 text-sm font-medium rounded-full bg-<?= $pColor ?>-100 text-<?= $pColor ?>-700">
                                <?= ucfirst($ticket['priorite']) ?>
                            </span>
                            <span class="px-3 py-1 text-sm font-medium rounded-full bg-<?= $sColor ?>-100 text-<?= $sColor ?>-700">
                                <?= ucfirst(str_replace('_', ' ', $ticket['statut'])) ?>
                            </span>
                        </div>
                        <h2 class="text-xl font-semibold text-gray-800"><?= htmlspecialchars($ticket['sujet']) ?></h2>
                    </div>
                </div>
            </div>
            
            <div class="p-6">
                <h3 class="font-semibold text-gray-700 mb-3">Description</h3>
                <div class="bg-gray-50 rounded-lg p-4 text-gray-700">
                    <?= nl2br(htmlspecialchars($ticket['description'] ?? 'Aucune description')) ?>
                </div>
            </div>
        </div>

        <!-- Historique de la conversation -->
        <?php if (!empty($messages)): ?>
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
            <div class="p-4 border-b border-gray-100">
                <h3 class="font-semibold text-gray-800">
                    <i class="fas fa-comments mr-2 text-indigo-600"></i>Historique de la conversation
                </h3>
            </div>
            <div class="p-4 max-h-96 overflow-y-auto space-y-4">
                <?php foreach ($messages as $msg): ?>
                <div class="flex <?= $msg['sender_type'] === 'client' ? 'justify-end' : 'justify-start' ?>">
                    <div class="max-w-[80%] rounded-xl p-3 <?= $msg['sender_type'] === 'client' ? 'bg-indigo-500 text-white' : 'bg-gray-100 text-gray-800' ?>">
                        <p class="text-sm"><?= nl2br(htmlspecialchars($msg['contenu'])) ?></p>
                        <p class="text-xs mt-1 <?= $msg['sender_type'] === 'client' ? 'text-indigo-200' : 'text-gray-400' ?>">
                            <?= date('d/m H:i', strtotime($msg['created_at'])) ?>
                        </p>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Mise à jour du statut -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
            <div class="p-4 border-b border-gray-100">
                <h3 class="font-semibold text-gray-800">
                    <i class="fas fa-edit mr-2 text-green-600"></i>Mettre à jour le ticket
                </h3>
            </div>
            <form action="<?= BASE_URL ?>/agent/tickets/<?= $ticket['id'] ?>/status" method="POST" class="p-6">
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Nouveau statut</label>
                        <select name="status" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                            <option value="assigned" <?= $ticket['statut'] === 'assigned' ? 'selected' : '' ?>>Assigné</option>
                            <option value="in_progress" <?= $ticket['statut'] === 'in_progress' ? 'selected' : '' ?>>En cours</option>
                            <option value="resolved" <?= $ticket['statut'] === 'resolved' ? 'selected' : '' ?>>Résolu</option>
                            <option value="closed" <?= $ticket['statut'] === 'closed' ? 'selected' : '' ?>>Fermé</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Notes de résolution</label>
                        <textarea name="notes" rows="3" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500" placeholder="Ajouter des notes..."><?= htmlspecialchars($ticket['resolution_notes'] ?? '') ?></textarea>
                    </div>
                </div>
                <div class="mt-4 flex justify-end">
                    <button type="submit" class="px-6 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition">
                        <i class="fas fa-save mr-2"></i>Enregistrer
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Sidebar -->
    <div class="space-y-6">
        <!-- Infos client -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
            <div class="p-4 border-b border-gray-100 bg-gray-50">
                <h3 class="font-semibold text-gray-800">
                    <i class="fas fa-user mr-2 text-gray-600"></i>Client
                </h3>
            </div>
            <div class="p-4 space-y-3">
                <div class="flex items-center gap-3">
                    <div class="w-12 h-12 bg-indigo-100 rounded-full flex items-center justify-center">
                        <i class="fas fa-user text-indigo-600"></i>
                    </div>
                    <div>
                        <p class="font-semibold text-gray-800"><?= htmlspecialchars($ticket['client_nom'] ?? 'Client') ?></p>
                    </div>
                </div>
                <?php if ($ticket['email']): ?>
                <div class="flex items-center gap-2 text-sm text-gray-600">
                    <i class="fas fa-envelope w-5"></i>
                    <a href="mailto:<?= htmlspecialchars($ticket['email']) ?>" class="hover:text-indigo-600"><?= htmlspecialchars($ticket['email']) ?></a>
                </div>
                <?php endif; ?>
                <?php if ($ticket['telephone']): ?>
                <div class="flex items-center gap-2 text-sm text-gray-600">
                    <i class="fas fa-phone w-5"></i>
                    <a href="tel:<?= htmlspecialchars($ticket['telephone']) ?>" class="hover:text-indigo-600"><?= htmlspecialchars($ticket['telephone']) ?></a>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Infos ticket -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
            <div class="p-4 border-b border-gray-100 bg-gray-50">
                <h3 class="font-semibold text-gray-800">
                    <i class="fas fa-info-circle mr-2 text-blue-600"></i>Informations
                </h3>
            </div>
            <div class="p-4 space-y-4">
                <div>
                    <p class="text-xs text-gray-500 uppercase tracking-wide">Créé le</p>
                    <p class="font-medium text-gray-800"><?= date('d/m/Y à H:i', strtotime($ticket['created_at'])) ?></p>
                </div>
                <?php if ($ticket['agent_nom']): ?>
                <div>
                    <p class="text-xs text-gray-500 uppercase tracking-wide">Assigné à</p>
                    <p class="font-medium text-gray-800"><?= htmlspecialchars($ticket['agent_prenom'] . ' ' . $ticket['agent_nom']) ?></p>
                </div>
                <?php endif; ?>
                <?php if ($ticket['resolved_at']): ?>
                <div>
                    <p class="text-xs text-gray-500 uppercase tracking-wide">Résolu le</p>
                    <p class="font-medium text-green-600"><?= date('d/m/Y à H:i', strtotime($ticket['resolved_at'])) ?></p>
                </div>
                <?php endif; ?>
                <?php if ($ticket['conversation_id']): ?>
                <div class="pt-2 border-t border-gray-100">
                    <a href="<?= BASE_URL ?>/agent/conversations/<?= $ticket['conversation_id'] ?>" class="flex items-center gap-2 text-indigo-600 hover:text-indigo-800">
                        <i class="fas fa-comments"></i>
                        Voir la conversation
                    </a>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Actions rapides -->
        <?php if (empty($ticket['assigned_agent_id'])): ?>
        <div class="bg-gradient-to-br from-orange-500 to-red-500 rounded-xl shadow-sm p-6 text-white">
            <h3 class="font-semibold mb-3">
                <i class="fas fa-hand-paper mr-2"></i>Ticket non assigné
            </h3>
            <p class="text-sm opacity-90 mb-4">Ce ticket n'est assigné à aucun agent.</p>
            <form action="<?= BASE_URL ?>/agent/tickets/<?= $ticket['id'] ?>/take" method="POST">
                <button type="submit" class="w-full py-2 bg-white text-orange-600 rounded-lg hover:bg-orange-50 transition font-medium">
                    Prendre en charge
                </button>
            </form>
        </div>
        <?php endif; ?>
    </div>
</div>

<?php 
$content = ob_get_clean();
$title = 'Ticket ' . htmlspecialchars($ticket['ticket_number']) . ' - ' . APP_NAME;
$pageTitle = 'Ticket ' . htmlspecialchars($ticket['ticket_number']);
include VIEWS_PATH . '/layouts/main.php';
?>
