@extends('layouts.app')

@section('title', 'Détails du client')
@section('page-title', $client->nom)

@section('content')
@php
    $formatPrice = fn($n) => number_format($n, 0, ',', ' ') . ' FCFA';
@endphp

<div class="space-y-6">
    <a href="{{ route('clients.index') }}" class="inline-flex items-center gap-2 text-slate-600 hover:text-slate-800 text-sm font-medium">
        <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="M10 19l-7-7m0 0l7-7m-7 7h18"/></svg>
        Retour à la liste
    </a>

    <div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden">
        <div class="p-6 border-b border-slate-100 bg-gradient-to-b from-slate-50 to-white">
            <h2 class="text-lg font-semibold text-slate-800">Informations client</h2>
        </div>
        <div class="p-6">
            <div class="p-4 rounded-xl bg-violet-50 border border-violet-100">
                <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-x-6 gap-y-4 text-sm">
                    <div>
                        <span class="text-slate-500 block">Référence</span>
                        <p class="font-medium text-slate-800 font-mono">{{ $client->reference }}</p>
                    </div>
                    <div>
                        <span class="text-slate-500 block">Nom complet</span>
                        <p class="font-medium text-slate-800">{{ $client->nom }}</p>
                    </div>
                    <div>
                        <span class="text-slate-500 block">Téléphone</span>
                        <p class="font-medium text-slate-800">{{ $client->telephone }}</p>
                    </div>
                    <div>
                        <span class="text-slate-500 block">Adresse</span>
                        <p class="font-medium text-slate-800">{{ $client->adresse ?? '—' }}</p>
                    </div>
                    <div>
                        <span class="text-slate-500 block">Caution</span>
                        <p class="font-semibold text-emerald-600">{{ $formatPrice($client->caution) }}</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden">
        <div class="px-6 py-4 border-b border-slate-200">
            <h3 class="font-semibold text-slate-800">Historique des achats (factures)</h3>
        </div>
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-slate-200">
                <thead class="bg-slate-50">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Réf.</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Produit</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Magasin</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Montant</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Date</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @forelse($client->ventes as $vente)
                    <tr class="hover:bg-slate-50/50">
                        <td class="px-4 py-3 font-mono text-slate-600">{{ $vente->reference }}</td>
                        <td class="px-4 py-3 text-slate-600">{{ $vente->produit?->designation ?? '—' }}</td>
                        <td class="px-4 py-3 text-slate-600">{{ $vente->magasin?->nom ?? '—' }}</td>
                        <td class="px-4 py-3 font-semibold text-emerald-600">{{ $formatPrice($vente->montant) }}</td>
                        <td class="px-4 py-3 text-slate-600">{{ $vente->date_vente?->format('d/m/Y') ?? '—' }}</td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" class="px-4 py-8 text-center text-slate-500">Aucun achat enregistré</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($client->ventes->isNotEmpty())
        <div class="px-6 py-4 border-t border-slate-100 bg-slate-50/50">
            <p class="text-sm text-slate-600">
                Total : <span class="font-semibold text-emerald-600">{{ $formatPrice($client->ventes->sum('montant')) }}</span>
            </p>
        </div>
        @endif
    </div>
</div>
@endsection
