<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Paiements - AS Talange</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://unpkg.com/htmx.org@1.9.11"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style> body { font-family: 'Inter', sans-serif; } </style>
</head>
<body class="bg-gray-50 text-gray-900 flex h-screen overflow-hidden">
    <!-- Sidebar -->
    <div th:replace="~{fragments/sidebar :: sidebar}"></div>

    <main class="flex-1 flex flex-col h-screen overflow-hidden">
        <header class="h-16 bg-white border-b border-gray-200 flex items-center px-6">
            <h2 class="text-lg font-semibold text-gray-800">Historique des Paiements</h2>
        </header>

        <div class="flex-1 overflow-auto p-6">
            <div class="flex justify-between items-center mb-6">
                <h3 class="text-xl font-bold text-gray-900">Transactions Récentes</h3>
            </div>

            <div id="paiements-table-container" class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
                <div class="p-4 border-b border-gray-200 flex justify-between items-center">
                    <form hx-get="/paiements" hx-target="#paiements-table-container" hx-select="#paiements-table-container" class="flex items-center space-x-2">
                        <input type="text" name="query" th:value="${searchQuery}" placeholder="Nom, prénom, catégorie..." 
                               class="border border-gray-300 rounded-lg px-4 py-2 text-sm w-64 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                        <button type="submit" class="bg-gray-100 text-gray-700 px-4 py-2 rounded-lg text-sm font-medium hover:bg-gray-200 border border-gray-300 transition-colors">Chercher</button>
                        <a th:if="${searchQuery != null and !searchQuery.isEmpty()}" th:href="@{/paiements}" class="text-sm text-gray-500 hover:text-gray-700 underline underline-offset-2 ml-2">Effacer</a>
                    </form>
                </div>

                <form id="filter-form" hx-get="/paiements" hx-target="#paiements-table-container" hx-select="#paiements-table-container" hx-trigger="change, keyup changed delay:300ms" hx-sync="this:replace" hx-on::config-request="if (event.detail.trigger.tagName !== 'BUTTON') { event.detail.parameters['page'] = 0; }" class="m-0">
                    <input type="hidden" name="query" th:value="${searchQuery}">
                    <div class="overflow-x-auto">
                        <table class="w-full text-left border-collapse min-w-[1000px]">
                        <thead>
                            <tr class="bg-gray-50 text-gray-500 text-sm uppercase tracking-wider border-b border-gray-200">
                                <th class="py-3 px-6 font-medium text-left">Date</th>
                                <th class="py-3 px-6 font-medium text-left">Adhérent</th>
                                <th class="py-3 px-6 font-medium text-left">Catégorie</th>
                                <th class="py-3 px-6 font-medium text-center">Saison</th>
                                <th class="py-3 px-6 font-medium text-left">Mode de paiement</th>
                                <th class="py-3 px-6 font-medium text-right">Montant</th>
                                <th class="py-3 px-6 font-medium text-right pr-6">Actions</th>
                            </tr>
                            <!-- Filter Row -->
                            <tr class="bg-gray-100 border-b border-gray-200 text-xs">
                                <td class="py-2 px-3"></td>
                                <td class="py-2 px-3">
                                    <input type="text" id="filterAdherent" name="adherent" th:value="${adherentFilter}" placeholder="Filtrer Adhérent..." class="w-full border border-gray-300 rounded px-2 py-1 text-xs focus:ring-1 focus:ring-blue-500 focus:outline-none">
                                </td>
                                <td class="py-2 px-3">
                                    <select id="filterCategorie" name="categorie" class="w-full border border-gray-300 rounded px-2 py-1 text-xs focus:ring-1 focus:ring-blue-500 focus:outline-none">
                                        <option value="">Toutes</option>
                                        <option th:each="cat : ${categories}" th:value="${cat.nom}" th:text="${cat.nom}" th:selected="${categorieFilter == cat.nom}"></option>
                                    </select>
                                </td>
                                <td class="py-2 px-3">
                                    <input type="text" id="filterSaison" name="saison" th:value="${saisonFilter}" placeholder="Filtrer Saison..." class="w-full border border-gray-300 rounded px-2 py-1 text-xs focus:ring-1 focus:ring-blue-500 focus:outline-none">
                                </td>
                                <td class="py-2 px-3">
                                    <select id="filterModePaiement" name="modePaiement" class="w-full border border-gray-300 rounded px-2 py-1 text-xs focus:ring-1 focus:ring-blue-500 focus:outline-none">
                                        <option value="">Tous</option>
                                        <option th:each="mode : ${modesPaiement}" th:value="${mode.nom}" th:text="${mode.nom}" th:selected="${modePaiementFilter == mode.nom}"></option>
                                    </select>
                                </td>
                                <td class="py-2 px-3">
                                    <input type="text" id="filterMontant" name="montant" th:value="${montantFilter}" placeholder="Filtrer Montant..." class="w-full border border-gray-300 rounded px-2 py-1 text-xs focus:ring-1 focus:ring-blue-500 focus:outline-none text-right">
                                </td>
                                <td class="py-2 px-3"></td>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200 text-sm">
                            <tr th:if="${#lists.isEmpty(paiements)}">
                                <td colspan="7" class="py-8 text-center text-gray-500">Aucun paiement trouvé.</td>
                            </tr>
                            <tr th:each="p : ${paiements}" class="hover:bg-gray-50 transition-colors">
                                <td class="py-4 px-6 text-gray-600" th:text="${#temporals.format(p.datePaiement, 'dd/MM/yyyy')}">15/05/2026</td>
                                <td class="py-4 px-6 font-medium text-gray-900">
                                    <a th:href="@{/adherents/{id}/edit(id=${p.licence.adherent.id})}" 
                                       class="text-blue-600 hover:text-blue-800 hover:underline"
                                       th:text="${p.licence.adherent.prenom + ' ' + p.licence.adherent.nom}">Jean Dupont</a>
                                </td>
                                <td class="py-4 px-6 text-gray-600" th:text="${p.licence.categorie.nom}">U13</td>
                                <td class="py-4 px-6 text-center text-gray-600" th:text="${p.licence.saison.nom}">2024-2025</td>
                                <td class="py-4 px-6 text-gray-600">
                                    <span class="px-2 py-0.5 rounded border border-gray-200 bg-gray-50 text-gray-700 text-xs" 
                                          th:text="${p.modePaiement.nom}">Chèque</span>
                                </td>
                                <td class="py-4 px-6 text-right font-semibold text-green-600" th:text="${p.montant + ' €'}">50.00 €</td>
                                <td class="py-4 px-6 text-right pr-6">
                                    <div class="flex justify-end items-center space-x-2">
                                        <button type="button"
                                                th:data-paiement-id="${p.id}"
                                                th:data-montant="${p.montant}"
                                                th:data-date="${p.datePaiement}"
                                                th:data-mode-id="${p.modePaiement.id}"
                                                th:data-licence-id="${p.licence.id}"
                                                th:data-max-amount="${p.licence.getResteAPayer().add(p.montant)}"
                                                onclick="openEditPaiementModal(this.getAttribute('data-paiement-id'), this.getAttribute('data-montant'), this.getAttribute('data-date'), this.getAttribute('data-mode-id'), this.getAttribute('data-licence-id'), this.getAttribute('data-max-amount'))"
                                                class="text-blue-600 hover:text-blue-900 bg-blue-50 hover:bg-blue-100 p-1.5 rounded transition-colors inline-flex items-center"
                                                title="Modifier">
                                            <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"/>
                                            </svg>
                                        </button>
                                        <form th:action="@{/paiements/{id}/delete(id=${p.id})}" method="post" class="inline m-0" onsubmit="return confirm('Êtes-vous sûr de vouloir supprimer ce paiement ?');">
                                            <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
                                            <input type="hidden" name="redirect" value="/paiements" />
                                            <button type="submit" class="text-red-600 hover:text-red-900 bg-red-50 hover:bg-red-100 p-1.5 rounded transition-colors inline-flex items-center" title="Supprimer">
                                                <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                                                </svg>
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                        </table>
                    </div>
                </form>

                <!-- Pagination Footer -->
                <div class="px-6 py-4 border-t border-gray-200 flex items-center justify-between bg-gray-50 text-sm text-gray-700">
                    <div th:if="${totalElements > 0}">
                        Affichage de <span class="font-semibold" th:text="${currentPage * pageSize + 1}">1</span> à 
                        <span class="font-semibold" th:text="${T(java.lang.Math).min((currentPage + 1) * pageSize, totalElements)}">20</span> sur 
                        <span class="font-semibold" th:text="${totalElements}">100</span> paiements
                    </div>
                    <div th:if="${totalElements == 0}">
                        Aucun paiement à afficher
                    </div>
                    <div class="flex items-center space-x-2" th:if="${totalPages > 1}">
                        <!-- Page Précédente -->
                        <button type="button"
                                th:if="${currentPage > 0}"
                                hx-get="/paiements"
                                hx-target="#paiements-table-container"
                                hx-select="#paiements-table-container"
                                hx-include="#filter-form"
                                th:attr="hx-vals=|{'page': ${currentPage - 1}}|"
                                class="px-3 py-1 border border-gray-300 rounded hover:bg-gray-100 transition-colors">
                            Précédent
                        </button>
                        <span th:if="${currentPage == 0}" class="px-3 py-1 border border-gray-200 rounded text-gray-400 bg-gray-50 cursor-not-allowed">
                            Précédent
                        </span>

                        <!-- Numéros de pages -->
                        <th:block th:each="pageNum : ${#numbers.sequence(0, totalPages - 1)}" th:if="${totalPages > 0}">
                            <button type="button"
                                    hx-get="/paiements"
                                    hx-target="#paiements-table-container"
                                    hx-select="#paiements-table-container"
                                    hx-include="#filter-form"
                                    th:attr="hx-vals=|{'page': ${pageNum}}|"
                                    th:text="${pageNum + 1}"
                                    class="px-3 py-1 rounded transition-colors"
                                    th:classappend="${currentPage == pageNum ? 'bg-blue-600 text-white' : 'border border-gray-300 hover:bg-gray-100'}">
                                1
                            </button>
                        </th:block>

                        <!-- Page Suivante -->
                        <button type="button"
                                th:if="${currentPage < totalPages - 1}"
                                hx-get="/paiements"
                                hx-target="#paiements-table-container"
                                hx-select="#paiements-table-container"
                                hx-include="#filter-form"
                                th:attr="hx-vals=|{'page': ${currentPage + 1}}|"
                                class="px-3 py-1 border border-gray-300 rounded hover:bg-gray-100 transition-colors">
                            Suivant
                        </button>
                        <span th:if="${currentPage == totalPages - 1}" class="px-3 py-1 border border-gray-200 rounded text-gray-400 bg-gray-50 cursor-not-allowed">
                            Suivant
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <!-- Edit Paiement Modal -->
    <div id="editPaiementModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full hidden z-50">
        <div class="relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-xl bg-white text-left">
            <div class="mt-3 text-center">
                <h3 class="text-lg leading-6 font-semibold text-gray-900 mb-4">Modifier le Paiement</h3>
                <form id="editPaiementForm" method="post" class="space-y-4 text-left">
                    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
                    <input type="hidden" name="redirect" value="/paiements">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Montant (€)</label>
                        <input type="number" step="0.01" min="0.01" id="editPaiementMontant" name="montant" required class="w-full border border-gray-300 rounded-lg px-4 py-2 text-sm focus:ring-2 focus:ring-blue-500 outline-none">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Date</label>
                        <input type="date" id="editPaiementDate" name="datePaiement" required class="w-full border border-gray-300 rounded-lg px-4 py-2 text-sm focus:ring-2 focus:ring-blue-500 outline-none">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Mode de paiement</label>
                        <select id="editPaiementMode" name="modePaiementId" required class="w-full border border-gray-300 rounded-lg px-4 py-2 text-sm focus:ring-2 focus:ring-blue-500 outline-none">
                            <option value="">Sélectionnez un mode...</option>
                            <option th:each="mode : ${modesPaiement}" th:value="${mode.id}" th:text="${mode.nom}"></option>
                        </select>
                    </div>
                    <div class="items-center px-4 py-3 flex justify-end space-x-2">
                        <button type="button" onclick="closeEditPaiementModal()" class="px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-lg hover:bg-gray-50">Annuler</button>
                        <button type="submit" class="px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700">Enregistrer</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        function openEditPaiementModal(paiementId, montant, date, modePaiementId, licenceId, maxAmount) {
            document.getElementById('editPaiementModal').classList.remove('hidden');
            document.getElementById('editPaiementForm').action = '/paiements/' + paiementId + '/update';
            document.getElementById('editPaiementMontant').value = montant;
            document.getElementById('editPaiementMontant').max = maxAmount;
            document.getElementById('editPaiementDate').value = date;
            document.getElementById('editPaiementMode').value = modePaiementId;
        }
        function closeEditPaiementModal() {
            document.getElementById('editPaiementModal').classList.add('hidden');
        }
    </script>
</body>
</html>
