@extends('layouts.app')

@section('title', 'Cautions clients')
@section('page-title', 'Cautions clients')

@section('content')
<div class="space-y-6">
    <div class="grid sm:grid-cols-2 lg:grid-cols-3 gap-4">
        <div class="rounded-xl shadow-sm border border-emerald-200 bg-gradient-to-br from-emerald-50 to-white p-5">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm font-medium text-emerald-700/80">Total cautions actives</p>
                    <p class="text-xl font-bold text-emerald-800 mt-1">{{ number_format($totalCautionsActives, 0, ',', ' ') }} FCFA</p>
                </div>
                <div class="w-11 h-11 rounded-lg bg-emerald-100 flex items-center justify-center">
                    <svg class="w-5 h-5 text-emerald-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>
                </div>
            </div>
        </div>
        <div class="rounded-xl shadow-sm border border-violet-200 bg-gradient-to-br from-violet-50 to-white p-5">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm font-medium text-violet-700/80">Clients avec caution</p>
                    <p class="text-xl font-bold text-violet-800 mt-1">{{ $clientsAvecCaution }}</p>
                </div>
                <div class="w-11 h-11 rounded-lg bg-violet-100 flex items-center justify-center">
                    <svg class="w-5 h-5 text-violet-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z" /></svg>
                </div>
            </div>
        </div>
        <div class="rounded-xl shadow-sm border border-blue-200 bg-gradient-to-br from-blue-50 to-white p-5">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm font-medium text-blue-700/80">Nombre de caution épuisée</p>
                    <p class="text-xl font-bold text-blue-800 mt-1">{{ $cautionTerminer }}</p>
                </div>
                <div class="w-11 h-11 rounded-lg bg-blue-100 flex items-center justify-center">
                    <svg class="w-5 h-5 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" /></svg>
                </div>
            </div>
        </div>
    </div>

    {{-- Filtres --}}
    <div x-data="{
        clientSearch: '',
        clientDropdownOpen: false,
        selectedClientId: @js(request('client_id')),
        clientsFiltre: @js($clients->map(fn($c) => ['id' => $c->id, 'nom' => $c->nom])->values()->all()),
        get filteredClients() {
            if (!this.clientSearch.trim()) return this.clientsFiltre;
            const q = this.clientSearch.toLowerCase();
            return this.clientsFiltre.filter(c => (c.nom || '').toLowerCase().includes(q));
        },
        get selectedClientLabel() {
            if (!this.selectedClientId) return null;
            const c = this.clientsFiltre.find(c => c.id == this.selectedClientId);
            return c ? c.nom : null;
        },
        selectClient(c) {
            this.selectedClientId = c.id;
            this.clientSearch = '';
            this.clientDropdownOpen = false;
            document.getElementById('client_filter_id').value = c.id;
        },
        clearClient() {
            this.selectedClientId = null;
            document.getElementById('client_filter_id').value = '';
            this.clientDropdownOpen = false;
        }
    }" @click.outside="clientDropdownOpen = false" class="p-6 rounded-xl bg-slate-50 border border-slate-200">
        <form method="GET" action="{{ route('cautions.index') }}">
            <div class="space-y-5">
                {{-- Ligne 1 : Champs de filtre --}}
                <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-5 items-end">
                    <div>
                        <label for="date_from" class="block text-xs font-medium text-slate-500 uppercase tracking-wider mb-1.5">Date dépôt du</label>
                        <input type="date" id="date_from" name="date_from" value="{{ request('date_from') }}" class="form-input w-full text-sm">
                    </div>
                    <div>
                        <label for="date_to" class="block text-xs font-medium text-slate-500 uppercase tracking-wider mb-1.5">Date dépôt au</label>
                        <input type="date" id="date_to" name="date_to" value="{{ request('date_to') }}" class="form-input w-full text-sm">
                    </div>
                    <div class="relative">
                        <label for="client_filter_search" class="block text-xs font-medium text-slate-500 uppercase tracking-wider mb-1.5">Client</label>
                        <input type="hidden" name="client_id" id="client_filter_id" value="{{ request('client_id') }}">
                        <div class="relative">
                            <input type="text" id="client_filter_search" :value="clientDropdownOpen ? clientSearch : (selectedClientLabel || clientSearch)" @input="clientSearch = $event.target.value" @focus="clientDropdownOpen = true" placeholder="Rechercher un client..." class="form-input w-full text-sm pr-9" autocomplete="off">
                            <span class="absolute right-2.5 top-1/2 -translate-y-1/2 text-slate-400 pointer-events-none">
                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" /></svg>
                            </span>
                        </div>
                        <div x-show="clientDropdownOpen" x-cloak x-transition:enter="transition ease-out duration-100" x-transition:enter-start="opacity-0" class="absolute z-20 w-full mt-1 bg-white border border-slate-200 rounded-lg shadow-lg overflow-hidden max-h-52 min-w-[12rem]">
                            <div class="p-2 border-b border-slate-100 bg-slate-50/50 sticky top-0">
                                <input type="text" x-model="clientSearch" @click.stop placeholder="Rechercher..." class="w-full px-3 py-2 text-sm border border-slate-200 rounded-lg focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 focus:outline-none">
                            </div>
                            <div class="max-h-40 overflow-y-auto py-1">
                                <button type="button" @click="clearClient()" class="w-full px-3 py-2 text-left text-sm hover:bg-slate-50 text-slate-600 flex items-center gap-2">
                                    <span>Tous les clients</span>
                                    <span x-show="selectedClientId" class="text-blue-600 text-xs">(effacer)</span>
                                </button>
                                <template x-for="c in filteredClients" :key="c.id">
                                    <button type="button" @click="selectClient(c)" class="w-full px-3 py-2 text-left text-sm hover:bg-blue-50 transition-colors" :class="{ 'bg-blue-50 text-blue-700 font-medium': selectedClientId == c.id }" x-text="c.nom"></button>
                                </template>
                                <p x-show="filteredClients.length === 0" class="px-3 py-2 text-sm text-slate-500">Aucun client trouvé</p>
                            </div>
                        </div>
                    </div>
                    <div>
                        <label for="statut" class="block text-xs font-medium text-slate-500 uppercase tracking-wider mb-1.5">Statut</label>
                        <select id="statut" name="statut" class="form-input w-full text-sm">
                            <option value="">Tous</option>
                            <option value="active" {{ request('statut') === 'active' ? 'selected' : '' }}>Active</option>
                            {{-- <option value="remboursée" {{ request('statut') === 'remboursée' ? 'selected' : '' }}>Remboursée</option>
                            <option value="endeté" {{ request('statut') === 'endeté' ? 'selected' : '' }}>Endetté</option>
                            <option value="partiellement utilisée" {{ request('statut') === 'partiellement utilisée' ? 'selected' : '' }}>Partiellement utilisée</option> --}}
                            <option value="solde epuisé" {{ request('statut') === 'solde epuisé' ? 'selected' : '' }}>Solde épuisé</option>
                        </select>
                    </div>
                </div>
                {{-- Ligne 2 : Actions --}}
                <div class="flex flex-wrap items-center gap-3 pt-4 border-t border-slate-200/80">
                    <button type="submit" class="px-4 py-2.5 bg-blue-600 hover:bg-blue-700 text-white text-sm font-medium rounded-lg transition-colors inline-flex items-center justify-center gap-2">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 4a1 1 0 011-1h16a1 1 0 011 1v2.586a1 1 0 01-.293.707l-6.414 6.414a1 1 0 00-.293.707V17l-4 4v-6.586a1 1 0 00-.293-.707L3.293 7.293A1 1 0 013 6.586V4z" /></svg>
                        Filtrer
                    </button>
                    <a href="{{ route('cautions.index') }}" class="px-4 py-2.5 border border-slate-300 hover:bg-slate-50 text-slate-700 text-sm font-medium rounded-lg transition-colors inline-flex items-center justify-center gap-2">
                        Réinitialiser
                    </a>
                    <a href="{{ route('cautions.export', request()->only(['date_from', 'date_to', 'client_id', 'statut'])) }}" class="px-4 py-2.5 bg-emerald-600 hover:bg-emerald-700 text-white text-sm font-medium rounded-lg transition-colors inline-flex items-center justify-center gap-2">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" /></svg>
                        Exporter
                    </a>

                </div>
            </div>
        </form>
    </div>

    <div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden">
        <div class="px-4 sm:px-6 py-4 border-b border-slate-200">
            <h2 class="font-semibold text-slate-800 text-lg">Détail des cautions par client</h2>
            <p class="text-sm text-slate-500 mt-1">Historique des dépôts et restitutions</p>
        </div>
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-slate-200">
                <thead>
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Client</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Montant déposé</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Date dépôt</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Statut</th>
                        {{-- <th class="px-4 py-3 text-right text-xs font-semibold text-slate-500 uppercase tracking-wider w-20">Actions</th>  --}}
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @forelse($cautions as $caution)
                    <tr class="hover:bg-slate-50/50">
                        <td class="px-4 py-3 font-medium text-slate-800">{{ $caution->client?->nom ?? '-' }}</td>
                        <td class="px-4 py-3 font-semibold text-emerald-600">{{ number_format($caution->montant, 0, ',', ' ') }} FCFA</td>
                        <td class="px-4 py-3 text-slate-600">{{ $caution->date_depot?->format('d/m/Y') ?? '-' }}</td>
                        <td class="px-4 py-3">
                            @if($caution->montant <= 0) <span class="inline-flex px-2 py-1 text-xs font-medium rounded-full bg-red-100 text-red-700">
                                solde épuisé
                                </span>
                                @else
                                <span class="inline-flex px-2 py-1 text-xs font-medium rounded-full bg-emerald-100 text-emerald-700">
                                    active
                                </span>
                                @endif

                        </td>
                        {{-- <td class="px-3 py-3 text-right w-20">
                            @if($caution->statut === 'active')
                            <button class="p-2 rounded-lg text-slate-500 hover:text-emerald-600 hover:bg-emerald-50 transition-colors inline-flex" title="Restituer la caution">
                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" /></svg>
                            </button>
                            @endif
                        </td>  --}}
                    </tr>
                    @empty
                    <tr>
                        <td colspan="4" class="px-4 py-8 text-center text-slate-500">Aucune caution.</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($cautions->total() > 0)
        <div class="px-4 sm:px-6 py-4 border-t border-slate-200 flex flex-col sm:flex-row items-center justify-between gap-3">
            <p class="text-sm text-slate-500">
                Affichage de <span class="font-medium">{{ $cautions->firstItem() }}</span> à <span class="font-medium">{{ $cautions->lastItem() }}</span> sur <span class="font-medium">{{ $cautions->total() }}</span> cautions
            </p>
            @if($cautions->hasPages())
            <div class="flex gap-3">
                @if($cautions->onFirstPage())
                <span class="px-4 py-2 text-slate-400 text-sm cursor-not-allowed">Précédent</span>
                @else
                <a href="{{ $cautions->previousPageUrl() }}" class="px-4 py-2 text-blue-600 hover:text-blue-700 text-sm font-medium rounded-lg hover:bg-blue-50 transition-colors">Précédent</a>
                @endif
                <span class="px-2 py-2 text-slate-600 text-sm">Page {{ $cautions->currentPage() }} / {{ $cautions->lastPage() }}</span>
                @if($cautions->hasMorePages())
                <a href="{{ $cautions->nextPageUrl() }}" class="px-4 py-2 text-blue-600 hover:text-blue-700 text-sm font-medium rounded-lg transition-colors">Suivant</a>
                @else
                <span class="px-4 py-2 text-slate-400 text-sm cursor-not-allowed">Suivant</span>
                @endif
            </div>
            @endif
        </div>
        @endif
    </div>
</div>
@endsection
