@extends('layouts.app')

@section('title', 'Mouvements de stock')
@section('page-title', 'Mouvements de stock')

@section('content')
<div class="space-y-6">
    {{-- Filtres --}}
    <div class="p-6 rounded-xl bg-slate-50 border border-slate-200 overflow-x-auto">
        <form method="GET" action="{{ route('mouvements.index') }}" class="flex flex-nowrap items-end gap-6 min-w-max">
            <div class="min-w-[400px]">
                <label for="magasin_id" class="block text-xs font-medium text-slate-500 uppercase tracking-wider mb-2">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>
            <div class="min-w-[360px]">
                <label for="type" class="block text-xs font-medium text-slate-500 uppercase tracking-wider mb-2">Type</label>
                <select id="type" name="type" class="form-input w-full text-sm">
                    <option value="">Tous les types</option>
                    <option value="entree" {{ request('type') === 'entree' ? 'selected' : '' }}>Entrée</option>
                    <option value="sortie" {{ request('type') === 'sortie' ? 'selected' : '' }}>Sortie</option>
                </select>
            </div>
            <div class="flex items-center gap-2 shrink-0">
                <button type="submit" class="inline-flex items-center justify-center gap-2 px-5 py-2.5 bg-blue-600 hover:bg-blue-700 text-white text-sm font-medium rounded-lg transition-colors">
                    <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('mouvements.index') }}" class="inline-flex items-center justify-center gap-2 px-5 py-2.5 border border-slate-300 hover:bg-slate-50 text-slate-700 text-sm font-medium rounded-lg transition-colors">
                    Réinitialiser
                </a>
                <a href="{{ route('mouvements.export', request()->only(['magasin_id', 'type'])) }}" class="inline-flex items-center justify-center gap-2 px-5 py-2.5 bg-emerald-600 hover:bg-emerald-700 text-white text-sm font-medium rounded-lg transition-colors">
                    <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>
        </form>
    </div>

    {{-- Tableau --}}
    <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">Historique des mouvements</h2>
            <p class="text-sm text-slate-500 mt-1">Entrées, sorties et transferts de stock</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">Référence</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Date</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Heure</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Type</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">Produit</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Quantité</th>

                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @forelse($mouvements as $mouvement)
                    <tr class="hover:bg-slate-50/50">
                        <td class="px-4 py-3 font-mono text-sm text-slate-500">{{ $mouvement->reference }}</td>
                        <td class="px-4 py-3 text-slate-600">{{ $mouvement->created_at?->format('d/m/Y') ?? '-' }}</td>
                        <td class="px-4 py-3 text-slate-600">{{ $mouvement->created_at?->format('H:i') ?? '-' }}</td>
                        <td>
                            @php
                            $typeClass = match($mouvement->type ?? '') {
                            'entree' => 'bg-emerald-100 text-emerald-700',
                            'sortie' => 'bg-red-100 text-red-700',
                            default => 'bg-slate-100 text-slate-600',
                            };
                            @endphp
                            <span class="inline-flex px-2 py-1 text-xs font-medium rounded-full {{ $typeClass }}">{{ $mouvement->type }}</span>
                        </td>
                        <td class="px-4 py-3 text-slate-800">{{ $mouvement->magasin?->nom ?? '-' }}</td>
                        <td class="px-4 py-3 text-slate-600">{{ $mouvement->produit?->designation ?? '-' }}</td>
                        <td class="px-4 py-3 font-semibold {{ ($mouvement->type ?? '') === 'entree' ? 'text-emerald-600' : (($mouvement->type ?? '') === 'sortie' ? 'text-red-600' : 'text-slate-600') }}">{{ $mouvement->quantite }}</td>

                    </tr>
                    @empty
                    <tr>
                        <td colspan="6" class="px-4 py-8 text-center text-slate-500">Aucun mouvement trouvé.</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($mouvements->total() > 0)
        <div class="px-4 sm:px-6 py-4 border-t border-slate-200">
            {{ $mouvements->links() }}
        </div>
        @endif
    </div>
</div>
@endsection
