@extends('layouts.app')
@section('title', 'Tableau de bord Admin')
@section('content')
<h3 class="mb-4">Tableau de bord</h3>

<div class="row g-3 mb-4">
    <div class="col-md-2 col-6">
        <div class="card p-3 text-center">
            <div class="fs-4 fw-bold">{{ $stats['total_partners'] }}</div>
            <div class="small text-muted">Partenaires</div>
        </div>
    </div>
    <div class="col-md-2 col-6">
        <div class="card p-3 text-center">
            <div class="fs-4 fw-bold">{{ $stats['total_couriers'] }}</div>
            <div class="small text-muted">Coursiers</div>
        </div>
    </div>
    <div class="col-md-2 col-6">
        <div class="card p-3 text-center">
            <div class="fs-4 fw-bold text-secondary">{{ $stats['en_attente'] }}</div>
            <div class="small text-muted">En attente</div>
        </div>
    </div>
    <div class="col-md-2 col-6">
        <div class="card p-3 text-center">
            <div class="fs-4 fw-bold text-success">{{ $stats['collecte'] }}</div>
            <div class="small text-muted">Collectees / livrees</div>
        </div>
    </div>
    <div class="col-md-2 col-6">
        <div class="card p-3 text-center">
            <div class="fs-4 fw-bold text-warning">{{ $stats['non_disponible'] }}</div>
            <div class="small text-muted">Non disponibles</div>
        </div>
    </div>
    <div class="col-md-2 col-6">
        <div class="card p-3 text-center">
            <div class="fs-4 fw-bold text-danger">{{ $stats['ecarts'] }}</div>
            <div class="small text-muted">Ecarts detectes</div>
        </div>
    </div>
</div>

@if($stats['ecarts'] > 0)
<div class="alert alert-danger">
    <strong>Attention :</strong> {{ $stats['ecarts'] }} demande(s) presentent un ecart entre la quantite demandee et la quantite reellement collectee.
    <a href="{{ route('admin.requests.index', ['status' => 'ecart']) }}" class="alert-link">Voir les ecarts</a>
</div>
@endif

<div class="card p-3">
    <h5 class="mb-3">Dernieres demandes</h5>
    <div class="table-responsive">
        <table class="table table-sm align-middle">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Partenaire</th>
                    <th>Type</th>
                    <th>Statut</th>
                    <th>Coursier</th>
                    <th>Cree le</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @forelse($recentRequests as $r)
                <tr>
                    <td>{{ $r->id }}</td>
                    <td>{{ $r->partner->name }}</td>
                    <td>{{ \App\Models\ServiceRequest::types()[$r->type] }}</td>
                    <td>
                        <span class="badge {{ $r->status === 'ecart' ? 'badge-ecart' : ($r->urgency === 'urgent' ? 'badge-urgent' : 'bg-secondary') }}">
                            {{ \App\Models\ServiceRequest::statuses()[$r->status] }}
                        </span>
                    </td>
                    <td>{{ $r->courier->name ?? '-' }}</td>
                    <td>{{ $r->created_at->format('d/m/Y H:i') }}</td>
                    <td><a href="{{ route('admin.requests.show', $r) }}" class="btn btn-sm btn-outline-primary">Voir</a></td>
                </tr>
                @empty
                <tr><td colspan="7" class="text-center text-muted">Aucune demande pour le moment.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>
@endsection
