@extends('layouts.app')
@section('title', 'Demande #'.$serviceRequest->id)
@section('content')
<a href="{{ route('courier.requests.index') }}" class="mb-3 d-inline-block">&laquo; Retour</a>

<div class="card p-4" style="max-width:700px;">
    <h4>{{ \App\Models\ServiceRequest::types()[$serviceRequest->type] }} — {{ $serviceRequest->partner->name }}</h4>
    <p class="text-muted mb-2">{{ $serviceRequest->partner->address }} — {{ $serviceRequest->partner->phone }}</p>
    <p><strong>Statut actuel :</strong> <span class="badge bg-secondary">{{ \App\Models\ServiceRequest::statuses()[$serviceRequest->status] }}</span></p>
    <p><strong>Quantite demandee :</strong> {{ $serviceRequest->requested_quantity ?? '-' }}</p>
    @if($serviceRequest->requested_slot)
        <p><strong>Creneau souhaite :</strong> {{ $serviceRequest->requested_slot }}</p>
    @endif

    <hr>
    <h6>Details</h6>
    <ul class="small">
        @foreach($serviceRequest->details ?? [] as $key => $value)
            <li><strong>{{ ucfirst(str_replace('_', ' ', $key)) }} :</strong> {{ $value }}</li>
        @endforeach
    </ul>

    @if(in_array($serviceRequest->status, ['assigne']))
    <hr>
    <h5>Confirmer la collecte</h5>
    <p class="small text-muted">Seule cette confirmation, faite par vous sur le terrain, fait foi. Toute difference avec la quantite demandee sera automatiquement signalee a l'administration — pas besoin de discuter, ecris simplement ce que tu as reellement recupere.</p>
    <form method="POST" action="{{ route('courier.requests.collect', $serviceRequest) }}" enctype="multipart/form-data" class="mb-4">
        @csrf
        <div class="mb-3">
            <label class="form-label">Quantite reellement collectee</label>
            <input type="number" name="collected_quantity" class="form-control" min="0" required>
        </div>
        <div class="mb-3">
            <label class="form-label">Photo (optionnel)</label>
            <input type="file" name="photo" accept="image/*" capture="environment" class="form-control">
        </div>
        <div class="mb-3">
            <label class="form-label">Commentaire (optionnel)</label>
            <input type="text" name="comment" class="form-control">
        </div>
        <button class="btn btn-success">Confirmer la collecte</button>
    </form>

    <h6>Ou signaler une indisponibilite</h6>
    <form method="POST" action="{{ route('courier.requests.unavailable', $serviceRequest) }}" class="d-flex gap-2">
        @csrf
        <select name="unavailable_reason" class="form-select" required>
            <option value="">Motif...</option>
            <option value="Pas encore pret">Pas encore pret</option>
            <option value="Clinique fermee">Clinique fermee</option>
            <option value="Refuse par le client">Refuse par le client</option>
            <option value="Autre">Autre</option>
        </select>
        <button class="btn btn-outline-warning">Signaler</button>
    </form>
    @endif

    @if(in_array($serviceRequest->status, ['collecte', 'ecart']))
    <hr>
    <form method="POST" action="{{ route('courier.requests.deliver', $serviceRequest) }}">
        @csrf
        <button class="btn btn-primary">Confirmer la livraison au laboratoire</button>
    </form>
    @endif
</div>
@endsection
