@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">Restituées ce mois</p>
                    <p class="text-xl font-bold text-blue-800 mt-1">{{ $restitueesCeMois }}</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>

    <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">
                            @php
                            $statutClass = match($caution->statut ?? '') {
                            'active' => 'bg-emerald-100 text-emerald-700',
                            'remboursée' => 'bg-blue-100 text-blue-700',
                            default => 'bg-slate-100 text-slate-600',
                            };
                            @endphp
                            <span class="inline-flex px-2 py-1 text-xs font-medium rounded-full {{ $statutClass }}">
                                {{ $caution->statut }}
                            </span>
                        </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="5" 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
