<?php ob_start(); ?>

<div class="mb-6">
    <div class="flex flex-wrap gap-2">
        <a href="<?= BASE_URL ?>/admin/conversations" 
           class="px-4 py-2 rounded-lg <?= !$currentStatut ? 'bg-indigo-600 text-white' : 'bg-gray-100 text-gray-700 hover:bg-gray-200' ?>">
            Toutes
        </a>
        <a href="<?= BASE_URL ?>/admin/conversations?statut=open" 
           class="px-4 py-2 rounded-lg <?= $currentStatut === 'open' ? 'bg-yellow-500 text-white' : 'bg-gray-100 text-gray-700 hover:bg-gray-200' ?>">
            <i class="fas fa-inbox mr-1"></i>Ouvertes
        </a>
        <a href="<?= BASE_URL ?>/admin/conversations?statut=in_progress" 
           class="px-4 py-2 rounded-lg <?= $currentStatut === 'in_progress' ? 'bg-blue-500 text-white' : 'bg-gray-100 text-gray-700 hover:bg-gray-200' ?>">
            <i class="fas fa-spinner mr-1"></i>En cours
        </a>
        <a href="<?= BASE_URL ?>/admin/conversations?statut=resolved" 
           class="px-4 py-2 rounded-lg <?= $currentStatut === 'resolved' ? 'bg-green-500 text-white' : 'bg-gray-100 text-gray-700 hover:bg-gray-200' ?>">
            <i class="fas fa-check mr-1"></i>Résolues
        </a>
        <a href="<?= BASE_URL ?>/admin/conversations?statut=closed" 
           class="px-4 py-2 rounded-lg <?= $currentStatut === 'closed' ? 'bg-gray-500 text-white' : 'bg-gray-100 text-gray-700 hover:bg-gray-200' ?>">
            <i class="fas fa-archive mr-1"></i>Fermées
        </a>
    </div>
</div>

<div class="bg-white rounded-xl shadow-sm border border-gray-100">
    <div class="p-6">
        <?php if (empty($conversations)): ?>
            <div class="text-center py-12">
                <div class="w-16 h-16 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-4">
                    <i class="fas fa-comments text-2xl text-gray-400"></i>
                </div>
                <p class="text-gray-500">Aucune conversation</p>
            </div>
        <?php else: ?>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead>
                        <tr class="text-left text-sm text-gray-500 border-b">
                            <th class="pb-3 font-medium">Client</th>
                            <th class="pb-3 font-medium">Sujet</th>
                            <th class="pb-3 font-medium">Canal</th>
                            <th class="pb-3 font-medium">Agent</th>
                            <th class="pb-3 font-medium">Statut</th>
                            <th class="pb-3 font-medium">Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($conversations as $conv): ?>
                        <tr class="border-b border-gray-100 hover:bg-gray-50 cursor-pointer" onclick="window.location='<?= BASE_URL ?>/admin/conversations/<?= $conv['id'] ?>'">
                            <td class="py-3">
                                <div class="flex items-center">
                                    <div class="w-8 h-8 bg-gray-100 rounded-full flex items-center justify-center mr-2">
                                        <i class="fas fa-user text-gray-500 text-xs"></i>
                                    </div>
                                    <div>
                                        <p class="font-medium text-gray-800"><?= htmlspecialchars($conv['client_nom'] ?? 'Anonyme') ?></p>
                                        <p class="text-xs text-gray-500"><?= htmlspecialchars($conv['client_telephone'] ?? '-') ?></p>
                                    </div>
                                </div>
                            </td>
                            <td class="py-3 text-gray-700"><?= htmlspecialchars($conv['sujet'] ?? '-') ?></td>
                            <td class="py-3">
                                <span class="px-2 py-1 text-xs rounded-full 
                                    <?= $conv['canal'] === 'whatsapp' ? 'bg-green-100 text-green-700' : 
                                       ($conv['canal'] === 'voice' ? 'bg-purple-100 text-purple-700' : 'bg-blue-100 text-blue-700') ?>">
                                    <i class="<?= $conv['canal'] === 'whatsapp' ? 'fab fa-whatsapp' : 
                                                 ($conv['canal'] === 'voice' ? 'fas fa-phone' : 'fas fa-globe') ?> mr-1"></i>
                                    <?= ucfirst($conv['canal']) ?>
                                </span>
                            </td>
                            <td class="py-3 text-gray-600">
                                <?= $conv['agent_nom'] ? htmlspecialchars($conv['agent_prenom'] . ' ' . $conv['agent_nom']) : '<span class="text-yellow-600">Non assigné</span>' ?>
                            </td>
                            <td class="py-3">
                                <span class="px-2 py-1 text-xs rounded-full 
                                    <?= $conv['statut'] === 'resolved' ? 'bg-green-100 text-green-700' : 
                                       ($conv['statut'] === 'in_progress' ? 'bg-blue-100 text-blue-700' : 
                                       ($conv['statut'] === 'open' ? 'bg-yellow-100 text-yellow-700' : 'bg-gray-100 text-gray-700')) ?>">
                                    <?= ucfirst(str_replace('_', ' ', $conv['statut'])) ?>
                                </span>
                                <?php if ($conv['is_escalated']): ?>
                                    <span class="ml-1 px-2 py-1 text-xs bg-red-100 text-red-700 rounded-full">Escaladée</span>
                                <?php endif; ?>
                            </td>
                            <td class="py-3 text-sm text-gray-500">
                                <?= date('d/m/Y H:i', strtotime($conv['created_at'])) ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php 
$content = ob_get_clean();
$title = 'Conversations - ' . APP_NAME;
$pageTitle = 'Toutes les Conversations';
include VIEWS_PATH . '/layouts/main.php';
?>
