@extends('layouts.ekspedisi')

@section('title', 'Pesanan Masuk')
@section('page-title', 'Pesanan Masuk')

@section('content')

{{-- Flash --}}
@if(session('success'))
    <div class="mb-4 px-4 py-3 rounded-lg bg-green-100 text-green-700 flex items-center gap-2">
        <i class="bi bi-check-circle-fill"></i> {{ session('success') }}
    </div>
@endif

@if(session('error'))
    <div class="mb-4 px-4 py-3 rounded-lg bg-red-100 text-red-700 flex items-center gap-2">
        <i class="bi bi-exclamation-triangle-fill"></i> {{ session('error') }}
    </div>
@endif

<!-- HEADER -->
<div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4 mb-6">

    <div>
        <h2 class="text-xl font-bold text-gray-800">Pesanan Masuk</h2>
        <p class="text-sm text-gray-500">
            Daftar pesanan yang siap dikirim dan belum diambil kurir
        </p>
    </div>

    <!-- SEARCH -->
    <form method="GET" class="flex gap-2">
        <input 
            type="text" 
            name="search" 
            value="{{ request('search') }}"
            placeholder="Cari ID, nama, kota..."
            class="px-4 py-2 border rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none"
        >
        <button class="px-4 py-2 bg-blue-500 text-white rounded-lg text-sm hover:bg-blue-600">
            <i class="bi bi-search"></i>
        </button>
    </form>

</div>

<!-- TABLE -->
<div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">

    @if($pesanans->isEmpty())

        <div class="p-10 text-center text-gray-500">
            <i class="bi bi-inbox text-3xl mb-2 block"></i>
            Tidak ada pesanan
            @if(request('search'))
                untuk "{{ request('search') }}"
            @endif
        </div>

    @else

    <div class="overflow-x-auto">
        <table class="w-full text-sm">

            <thead class="bg-gray-50 text-gray-500 uppercase text-xs">
                <tr>
                    <th class="px-6 py-3 text-left">ID</th>
                    <th class="px-6 py-3 text-left">Pelanggan</th>
                    <th class="px-6 py-3 text-left">Tujuan</th>
                    <th class="px-6 py-3 text-left">Tanggal</th>
                    <th class="px-6 py-3 text-left">Total</th>
                    <th class="px-6 py-3 text-left">Aksi</th>
                </tr>
            </thead>

            <tbody class="divide-y">

                @foreach($pesanans as $p)
                <tr class="hover:bg-gray-50">

                    <!-- ID -->
                    <td class="px-6 py-4 font-semibold text-blue-600">
                        #{{ $p->id }}
                    </td>

                    <!-- USER -->
                    <td class="px-6 py-4">
                        <div class="font-medium text-gray-800">
                            {{ $p->user->name ?? '-' }}
                        </div>
                        <div class="text-xs text-gray-500">
                            {{ $p->user->email ?? '' }}
                        </div>
                    </td>

                    <!-- TUJUAN -->
                    <td class="px-6 py-4">
                        <div class="font-medium text-gray-800">
                            {{ $p->alamat_tujuan ?? '-' }}
                        </div>
                        <div class="text-xs text-gray-500">
                            {{ $p->kota ?? '-' }}
                        </div>
                    </td>

                    <!-- TANGGAL -->
                    <td class="px-6 py-4">
                        <div>
                            {{ \Carbon\Carbon::parse($p->tanggal_pesanan)->format('d M Y') }}
                        </div>
                        <div class="text-xs text-gray-500">
                            {{ $p->created_at->diffForHumans() }}
                        </div>
                    </td>

                    <!-- TOTAL -->
                    <td class="px-6 py-4 font-medium">
                        Rp {{ number_format($p->total_harga, 0, ',', '.') }}
                    </td>

                    <!-- AKSI -->
                    <td class="px-6 py-4">
                        <form method="POST" action="{{ route('ekspedisi.ambil', $p->id) }}">
                            @csrf
                            <button class="px-3 py-1.5 text-xs bg-blue-500 text-white rounded-md hover:bg-blue-600">
                                <i class="bi bi-truck"></i> Ambil
                            </button>
                        </form>
                    </td>

                </tr>
                @endforeach

            </tbody>
        </table>
    </div>

    <!-- PAGINATION -->
    @if($pesanans->hasPages())
        <div class="p-4 border-t">
            {{ $pesanans->links() }}
        </div>
    @endif

    @endif

</div>

@endsection