@extends('layouts.app')
@section('title', 'Demandes')
@section('content')
<h3 class="mb-4">Toutes les demandes</h3>

<form method="GET" class="row g-2 mb-3">
    <div class="col-md-3">
        <select name="type" class="form-select" onchange="this.form.submit()">
            <option value="">Tous types</option>
            @foreach($types as $key => $label)
                <option value="{{ $key }}" @selected(request('type') === $key)>{{ $label }}</option>
            @endforeach
        </select>
    </div>
    <div class="col-md-3">
        <select name="status" class="form-select" onchange="this.form.submit()">
            <option value="">Tous statuts</option>
            @foreach($statuses as $key => $label)
                <option value="{{ $key }}" @selected(request('status') === $key)>{{ $label }}</option>
            @endforeach
        </select>
    </div>
    <div class="col-md-3">
        <a href="{{ route('admin.requests.index') }}" class="btn btn-outline-secondary">Reinitialiser</a>
    </div>
</form>

<div class="card p-3">
    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Partenaire</th>
                    <th>Type</th>
                    <th>Urgence</th>
                    <th>Qte demandee</th>
                    <th>Qte collectee</th>
                    <th>Statut</th>
                    <th>Coursier</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @forelse($requests as $r)
                <tr class="{{ $r->status === 'ecart' ? 'table-danger' : '' }}">
                    <td>{{ $r->id }}</td>
                    <td>{{ $r->partner->name }}</td>
                    <td>{{ $types[$r->type] }}</td>
                    <td>
                        @if($r->urgency === 'urgent')
                            <span class="badge badge-urgent">Urgent</span>
                        @else
                            <span class="text-muted small">Normal</span>
                        @endif
                    </td>
                    <td>{{ $r->requested_quantity ?? '-' }}</td>
                    <td>{{ $r->collected_quantity ?? '-' }}</td>
                    <td><span class="badge {{ $r->status === 'ecart' ? 'badge-ecart' : 'bg-secondary' }}">{{ $statuses[$r->status] }}</span></td>
                    <td>
                        @if($r->courier)
                            {{ $r->courier->name }}
                        @else
                            <form action="{{ route('admin.requests.assign', $r) }}" method="POST" class="d-flex gap-1">
                                @csrf
                                <select name="assigned_courier_id" class="form-select form-select-sm" required>
                                    <option value="">Assigner...</option>
                                    @foreach($couriers as $c)
                                        <option value="{{ $c->id }}">{{ $c->name }}</option>
                                    @endforeach
                                </select>
                                <button class="btn btn-sm btn-primary">OK</button>
                            </form>
                        @endif
                    </td>
                    <td><a href="{{ route('admin.requests.show', $r) }}" class="btn btn-sm btn-outline-primary">Detail</a></td>
                </tr>
                @empty
                <tr><td colspan="9" class="text-center text-muted">Aucune demande trouvee.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
    {{ $requests->links() }}
</div>
@endsection
