@extends('layouts.app')

@section('title', 'Stock')
@section('page-title', 'Stock')

@section('content')
<div x-data="{
    produitSearch: '',
    produitDropdownOpen: false,
    selectedProduitId: @js(request('produit_id')),
    produits: @js($produits->map(fn($p) => ['id' => $p->id, 'designation' => $p->designation, 'reference' => $p->reference ?? ''])->values()->all()),
    get filteredProduits() {
        if (!this.produitSearch.trim()) return this.produits;
        const q = this.produitSearch.toLowerCase();
        return this.produits.filter(p =>
            (p.designation || '').toLowerCase().includes(q) ||
            (p.reference || '').toLowerCase().includes(q)
        );
    },
    selectProduit(p) {
        this.selectedProduitId = p.id;
        this.produitSearch = '';
        this.produitDropdownOpen = false;
        document.getElementById('produit_id').value = p.id;
    },
    clearProduit() {
        this.selectedProduitId = null;
        document.getElementById('produit_id').value = '';
        this.produitDropdownOpen = false;
    }
}" @click.outside="produitDropdownOpen = false">
    <div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden">
        {{-- Header --}}
        <div class="px-4 sm:px-6 py-5 border-b border-slate-200 bg-gradient-to-b from-slate-50/80 to-white">
            <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
                <div>
                    <h2 class="font-semibold text-slate-800 text-lg">État des stocks par magasin</h2>
                    <p class="text-sm text-slate-500 mt-0.5">Vue d'ensemble des quantités disponibles</p>
                </div>
            </div>

            {{-- Filtres --}}
            <div class="mt-5 p-4 rounded-xl bg-slate-50 border border-slate-200">
                <form id="stock-filter-form" method="GET" action="{{ route('stock.index') }}">
                    <div class="flex flex-col sm:flex-row sm:items-end gap-4">
                        <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 flex-1">
                            {{-- Filtre Magasin --}}
                            <div>
                                <label for="magasin_id" class="block text-xs font-medium text-slate-500 uppercase tracking-wider mb-1.5">Magasin</label>
                                <select id="magasin_id" name="magasin_id" class="form-input w-full text-sm">
                                    <option value="">Tous les magasins</option>
                                    @foreach($magasins as $m)
                                    <option value="{{ $m->id }}" {{ request('magasin_id') == $m->id ? 'selected' : '' }}>{{ $m->nom }}</option>
                                    @endforeach
                                </select>
                            </div>

                            {{-- Filtre Produit avec recherche --}}
                            <div class="relative">
                                <label for="produit_search" class="block text-xs font-medium text-slate-500 uppercase tracking-wider mb-1.5">Produit</label>
                                <input type="hidden" name="produit_id" id="produit_id" value="{{ request('produit_id') }}">
                                <div class="relative">
                                    <input type="text" id="produit_search" x-model="produitSearch" @focus="produitDropdownOpen = true" placeholder="Rechercher un produit..." class="form-input w-full text-sm pr-10" autocomplete="off">
                                    <span class="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400">
                                        <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="produitDropdownOpen" 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-56">
                                    <div class="p-2 border-b border-slate-100 bg-slate-50/50 sticky top-0">
                                        <p class="text-xs text-slate-500 font-medium">Sélectionnez un produit</p>
                                    </div>
                                    <div class="max-h-44 overflow-y-auto py-1">
                                        <button type="button" @click="clearProduit()" class="w-full px-4 py-2.5 text-left text-sm hover:bg-slate-50 text-slate-600 flex items-center gap-2">
                                            <span>Aucun filtre produit</span>
                                            <span x-show="selectedProduitId" class="text-blue-600 text-xs">(effacer)</span>
                                        </button>
                                        <template x-for="p in filteredProduits" :key="p.id">
                                            <button type="button" @click="selectProduit(p)" class="w-full px-4 py-2.5 text-left text-sm hover:bg-blue-50 transition-colors" :class="{ 'bg-blue-50 text-blue-700 font-medium': selectedProduitId == p.id }">
                                                <span x-text="p.designation"></span>
                                                <span x-show="p.reference" class="text-slate-400 text-xs ml-1" x-text="'(' + p.reference + ')'"></span>
                                            </button>
                                        </template>
                                        <p x-show="filteredProduits.length === 0" class="px-4 py-3 text-sm text-slate-500">Aucun produit trouvé</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="flex flex-col sm:flex-row gap-2 sm:shrink-0">
                            <button type="submit" form="stock-filter-form" 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('stock.export', request()->only(['magasin_id', 'produit_id'])) }}" 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>
                            @if(request()->hasAny(['magasin_id', 'produit_id']))
                            <a href="{{ route('stock.index') }}" class="px-4 py-2.5 bg-slate-200 hover:bg-slate-300 text-slate-700 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="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>
                                Réinitialiser
                            </a>
                            @endif
                        </div>
                    </div>
                </form>
            </div>
        </div>

        {{-- Tableau --}}
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-slate-200">
                <thead class="bg-slate-50/80">
                    <tr>
                        <th class="px-4 sm:px-6 py-3.5 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">Magasin</th>
                        <th class="px-4 sm:px-6 py-3.5 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">Produit</th>
                        <th class="px-4 sm:px-6 py-3.5 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">Quantité</th>
                        <th class="px-4 sm:px-6 py-3.5 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">Seuil alerte</th>
                        <th class="px-4 sm:px-6 py-3.5 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">Statut</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 bg-white">
                    @forelse($stocks as $stock)
                    <tr class="hover:bg-slate-50/60 transition-colors">
                        <td class="px-4 sm:px-6 py-3.5">
                            <div class="flex items-center gap-2">
                                <span class="w-8 h-8 rounded-lg bg-slate-100 flex items-center justify-center text-slate-600">
                                    <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="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4" /></svg>
                                </span>
                                <span class="font-medium text-slate-800">{{ $stock->magasin?->nom ?? '-' }}</span>
                            </div>
                        </td>
                        <td class="px-4 sm:px-6 py-3.5">
                            <span class="text-slate-700">{{ $stock->produit?->designation ?? '-' }}</span>
                            @if($stock->produit?->reference)
                            <span class="text-slate-400 text-xs ml-1">({{ $stock->produit->reference }})</span>
                            @endif
                        </td>
                        <td class="px-4 sm:px-6 py-3.5">
                            <span class="font-semibold text-slate-800 tabular-nums">{{ number_format($stock->quantite ?? 0, 0, ',', ' ') }}</span>
                        </td>
                        <td class="px-4 sm:px-6 py-3.5 text-slate-600 tabular-nums">{{ $stock->seuil_alerte ?? 0 }}</td>
                        <td class="px-4 sm:px-6 py-3.5">
                            @if($stock->quantite <= $stock->seuil_alerte)
                                <span class="inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs font-medium bg-red-50 text-red-700">
                                    <span class="w-1.5 h-1.5 rounded-full bg-red-500"></span>
                                    Critique
                                </span>
                                @else
                                <span class="inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs font-medium bg-emerald-50 text-emerald-700">
                                    <span class="w-1.5 h-1.5 rounded-full bg-emerald-500"></span>
                                    OK
                                </span>
                                @endif
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" class="px-4 sm:px-6 py-12 text-center">
                            <div class="flex flex-col items-center gap-2 text-slate-500">
                                <svg class="w-12 h-12 text-slate-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M20 7l-8 4-8-4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4" /></svg>
                                <p class="font-medium">Aucun stock trouvé</p>
                                <p class="text-sm">Modifiez les filtres ou ajoutez des produits en stock.</p>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{-- Pagination --}}
        @if($stocks->total() > 0)
        <div class="px-4 sm:px-6 py-4 flex flex-col sm:flex-row items-center justify-between gap-3 border-t border-slate-200 bg-slate-50/30">
            <p class="text-sm text-slate-500">
                Affichage de <span class="font-medium text-slate-700">{{ $stocks->firstItem() }}</span> à <span class="font-medium text-slate-700">{{ $stocks->lastItem() }}</span> sur <span class="font-medium text-slate-700">{{ $stocks->total() }}</span> stocks
            </p>
            @if($stocks->hasPages())
            <div class="flex items-center gap-2">
                @if($stocks->onFirstPage())
                <span class="px-4 py-2 text-slate-400 text-sm cursor-not-allowed rounded-lg">Précédent</span>
                @else
                <a href="{{ $stocks->previousPageUrl() }}" class="px-4 py-2 text-blue-600 hover:text-blue-700 hover:bg-blue-50 text-sm font-medium rounded-lg transition-colors">Précédent</a>
                @endif
                <span class="px-3 py-2 text-slate-600 text-sm font-medium">Page {{ $stocks->currentPage() }} / {{ $stocks->lastPage() }}</span>
                @if($stocks->hasMorePages())
                <a href="{{ $stocks->nextPageUrl() }}" class="px-4 py-2 text-blue-600 hover:text-blue-700 hover:bg-blue-50 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 rounded-lg">Suivant</span>
                @endif
            </div>
            @endif
        </div>
        @endif
    </div>
</div>
@endsection
