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

<div class="row g-3">
    <div class="col-md-7">
        <div class="card p-4">
            <h4>Demande #{{ $serviceRequest->id }} — {{ \App\Models\ServiceRequest::types()[$serviceRequest->type] }}</h4>
            <p class="mb-1"><strong>Partenaire :</strong> {{ $serviceRequest->partner->name }} ({{ $serviceRequest->partner->type }})</p>
            <p class="mb-1"><strong>Statut :</strong>
                <span class="badge {{ $serviceRequest->status === 'ecart' ? 'badge-ecart' : 'bg-secondary' }}">
                    {{ \App\Models\ServiceRequest::statuses()[$serviceRequest->status] }}
                </span>
            </p>
            <p class="mb-1"><strong>Urgence :</strong> {{ $serviceRequest->urgency }}</p>
            <p class="mb-1"><strong>Qte demandee :</strong> {{ $serviceRequest->requested_quantity ?? '-' }}</p>
            <p class="mb-1"><strong>Qte collectee :</strong> {{ $serviceRequest->collected_quantity ?? '-' }}</p>

            @if($serviceRequest->hasDiscrepancy())
                <div class="alert alert-danger mt-2 mb-2">
                    Ecart detecte entre la quantite demandee et la quantite reellement collectee par le coursier.
                </div>
            @endif

            @if($serviceRequest->unavailable_reason)
                <p class="mb-1"><strong>Motif non-disponible :</strong> {{ $serviceRequest->unavailable_reason }}</p>
            @endif

            @if($serviceRequest->photo_path)
                <div class="mt-3">
                    <strong>Photo de la collecte :</strong><br>
                    <img src="{{ asset('storage/'.$serviceRequest->photo_path) }}" class="img-fluid rounded mt-2" style="max-height:300px;">
                </div>
            @endif

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

            @if(! $serviceRequest->courier)
            <hr>
            <form action="{{ route('admin.requests.assign', $serviceRequest) }}" method="POST" class="d-flex gap-2">
                @csrf
                <select name="assigned_courier_id" class="form-select" required>
                    <option value="">Assigner a un coursier...</option>
                    @foreach($couriers as $c)
                        <option value="{{ $c->id }}">{{ $c->name }}</option>
                    @endforeach
                </select>
                <button class="btn btn-primary">Assigner</button>
            </form>
            @else
                <p class="mt-2"><strong>Coursier assigne :</strong> {{ $serviceRequest->courier->name }}</p>
            @endif
        </div>
    </div>

    <div class="col-md-5">
        <div class="card p-4">
            <h5>Historique</h5>
            <ul class="list-group list-group-flush">
                @foreach($serviceRequest->statusHistories as $h)
                    <li class="list-group-item">
                        <div class="fw-bold">{{ \App\Models\ServiceRequest::statuses()[$h->status] ?? $h->status }}</div>
                        <div class="small text-muted">{{ $h->created_at->format('d/m/Y H:i') }} — {{ $h->author->name ?? 'Systeme' }}</div>
                        @if($h->comment)<div class="small">{{ $h->comment }}</div>@endif
                    </li>
                @endforeach
            </ul>
        </div>
    </div>
</div>
@endsection
