@extends('layouts.app')

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

@section('content')
<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>
    {{-- Filtres --}}
    <div class="px-5 sm:px-6 py-5 bg-slate-50 border-b border-slate-200">
        <form method="GET" action="{{ route('mouvements.index') }}" class="flex flex-wrap items-end gap-5">
            <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 text-sm w-48 sm:w-56">
                    <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>
                <label for="type" class="block text-xs font-medium text-slate-500 uppercase tracking-wider mb-1.5">Type</label>
                <select id="type" name="type" class="form-input text-sm w-40 sm:w-48">
                    <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 gap-2 shrink-0">
                <button type="submit" class="form-btn-primary text-sm px-4 py-2.5">Filtrer</button>
                <a href="{{ route('mouvements.index') }}" class="form-btn-secondary text-sm px-4 py-2.5">Réinitialiser</a>
            </div>
        </form>
    </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">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 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>
@endsection
