<?php ob_start(); ?>

<div class="mb-6">
    <div class="flex gap-2">
        <a href="<?= BASE_URL ?>/agent/conversations" 
           class="px-4 py-2 rounded-lg <?= $type === 'mes' ? 'bg-indigo-600 text-white' : 'bg-gray-100 text-gray-700 hover:bg-gray-200' ?>">
            <i class="fas fa-user mr-1"></i>Mes conversations
        </a>
        <a href="<?= BASE_URL ?>/agent/conversations?type=non-assignees" 
           class="px-4 py-2 rounded-lg <?= $type === 'non-assignees' ? 'bg-yellow-500 text-white' : 'bg-gray-100 text-gray-700 hover:bg-gray-200' ?>">
            <i class="fas fa-inbox mr-1"></i>Non assignées
        </a>
    </div>
</div>

<div class="bg-white rounded-xl shadow-sm border border-gray-100">
    <?php if (empty($conversations)): ?>
        <div class="p-12 text-center">
            <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="divide-y divide-gray-100">
            <?php foreach ($conversations as $conv): ?>
            <a href="<?= BASE_URL ?>/agent/conversations/<?= $conv['id'] ?><?= $type === 'non-assignees' ? '?assign=1' : '' ?>" 
               class="block p-4 hover:bg-gray-50 transition">
                <div class="flex items-center justify-between">
                    <div class="flex items-center flex-1">
                        <div class="w-12 h-12 <?= $type === 'non-assignees' ? 'bg-yellow-100' : 'bg-indigo-100' ?> rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-user <?= $type === 'non-assignees' ? 'text-yellow-600' : 'text-indigo-600' ?>"></i>
                        </div>
                        <div class="flex-1">
                            <div class="flex items-center">
                                <p class="font-medium text-gray-800"><?= htmlspecialchars($conv['client_nom'] ?? 'Client anonyme') ?></p>
                                <?php if ($conv['is_escalated']): ?>
                                    <span class="ml-2 px-2 py-0.5 text-xs bg-red-100 text-red-600 rounded-full">Escaladé</span>
                                <?php endif; ?>
                            </div>
                            <p class="text-sm text-gray-500"><?= htmlspecialchars($conv['sujet'] ?? 'Sans sujet') ?></p>
                            <p class="text-xs text-gray-400 mt-1">
                                <i class="<?= $conv['canal'] === 'whatsapp' ? 'fab fa-whatsapp' : 'fas fa-globe' ?> mr-1"></i>
                                <?= ucfirst($conv['canal']) ?> • <?= date('d/m/Y H:i', strtotime($conv['created_at'])) ?>
                            </p>
                        </div>
                    </div>
                    <div class="text-right ml-4">
                        <span class="px-3 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' : 'bg-yellow-100 text-yellow-700') ?>">
                            <?= $conv['statut'] === 'in_progress' ? 'En cours' : ($conv['statut'] === 'resolved' ? 'Résolu' : 'Ouvert') ?>
                        </span>
                        <p class="text-xs text-gray-400 mt-2">
                            <i class="fas fa-arrow-right"></i>
                        </p>
                    </div>
                </div>
            </a>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>

<?php 
$content = ob_get_clean();
$title = 'Conversations - ' . APP_NAME;
$pageTitle = $type === 'non-assignees' ? 'Conversations non assignées' : 'Mes conversations';
include VIEWS_PATH . '/layouts/main.php';
?>
