@extends('layouts.admin')

@section('title', 'Commandes')
@section('page-title', 'Gestion des Commandes')

@section('content')

{{-- ── Filtres statut ── --}}
<div class="flex flex-wrap gap-2 mb-6">
    @foreach([null => 'Toutes', 'pending' => 'En attente', 'confirmed' => 'Confirmées', 'processing' => 'En traitement', 'shipped' => 'Expédiées', 'delivered' => 'Livrées', 'cancelled' => 'Annulées'] as $status => $label)
    <a href="{{ route('admin.orders.index', $status ? ['status' => $status] : []) }}"
       class="px-4 py-2 rounded-xl text-xs font-medium transition-all {{ $statusFilter === $status || ($status === null && !$statusFilter) ? 'bg-primary text-white shadow-sm' : 'bg-white text-gray-600 hover:bg-gray-50 border border-gray-200' }}">
        {{ $label }}
    </a>
    @endforeach
</div>

<div class="bg-white rounded-2xl shadow-sm border border-gray-100 overflow-hidden">
    <div class="overflow-x-auto">
        <table class="w-full text-sm">
            <thead>
                <tr class="bg-gray-50 border-b border-gray-100">
                    <th class="text-left px-5 py-3.5 font-semibold text-gray-600">N° Commande</th>
                    <th class="text-left px-5 py-3.5 font-semibold text-gray-600 hidden sm:table-cell">Client</th>
                    <th class="text-left px-5 py-3.5 font-semibold text-gray-600 hidden md:table-cell">Montant</th>
                    <th class="text-left px-5 py-3.5 font-semibold text-gray-600">Statut</th>
                    <th class="text-left px-5 py-3.5 font-semibold text-gray-600 hidden lg:table-cell">Date</th>
                    <th class="text-right px-5 py-3.5 font-semibold text-gray-600">Actions</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-50">
                @forelse($orders as $order)
                <tr class="hover:bg-gray-50 transition-colors">
                    <td class="px-5 py-3.5">
                        <span class="font-mono text-xs font-semibold text-gray-700">{{ $order->order_number }}</span>
                        <p class="text-xs text-gray-400">{{ $order->items->count() }} article(s)</p>
                    </td>
                    <td class="px-5 py-3.5 hidden sm:table-cell">
                        <div class="font-medium text-gray-800">{{ $order->customer_name }}</div>
                        <div class="text-xs text-gray-400">{{ $order->customer_phone }}</div>
                    </td>
                    <td class="px-5 py-3.5 hidden md:table-cell font-bold text-gray-800">
                        {{ number_format($order->total_amount, 0, ',', ' ') }} FCFA
                    </td>
                    <td class="px-5 py-3.5">
                        <span class="inline-block px-2.5 py-0.5 rounded-full text-xs font-medium
                            @if($order->status === 'pending') bg-yellow-100 text-yellow-700
                            @elseif($order->status === 'confirmed') bg-blue-100 text-blue-700
                            @elseif($order->status === 'processing') bg-indigo-100 text-indigo-700
                            @elseif($order->status === 'shipped') bg-purple-100 text-purple-700
                            @elseif($order->status === 'delivered') bg-green-100 text-green-700
                            @elseif($order->status === 'cancelled') bg-red-100 text-red-700
                            @else bg-gray-100 text-gray-700 @endif">
                            {{ $order->status_label }}
                        </span>
                    </td>
                    <td class="px-5 py-3.5 hidden lg:table-cell text-gray-400 text-xs">
                        {{ $order->created_at->format('d/m/Y H:i') }}
                    </td>
                    <td class="px-5 py-3.5 text-right">
                        <div class="flex items-center justify-end gap-2">
                            <a href="{{ route('admin.orders.show', $order) }}"
                               class="p-2 text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" title="Voir">
                                <i class="fas fa-eye"></i>
                            </a>
                            <form action="{{ route('admin.orders.destroy', $order) }}" method="POST"
                                  onsubmit="return confirm('Supprimer cette commande ?')">
                                @csrf @method('DELETE')
                                <button type="submit" class="p-2 text-red-500 hover:bg-red-50 rounded-lg transition-colors">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </form>
                        </div>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="6" class="px-5 py-12 text-center text-gray-400">
                        <i class="fas fa-inbox text-3xl mb-2 block"></i>
                        Aucune commande trouvée.
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    @if($orders->hasPages())
    <div class="px-5 py-4 border-t border-gray-100">
        {{ $orders->links() }}
    </div>
    @endif
</div>

@endsection
