@extends('layouts.ekspedisi')

@section('title', 'Dalam Pengiriman')
@section('page-title', 'Dalam Pengiriman')

@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">Dalam Pengiriman</h2>
        <p class="text-sm text-gray-500">Pesanan yang sedang Anda antar</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-truck text-3xl mb-2 block"></i>
            Tidak ada pesanan yang sedang dikirim
        </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">Diambil</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>

                    <!-- WAKTU -->
                    <td class="px-6 py-4">
                        <span class="text-xs bg-blue-100 text-blue-600 px-2 py-1 rounded-full">
                            {{ $p->updated_at->diffForHumans() }}
                        </span>
                    </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 flex gap-2 flex-wrap">

                        <!-- BUTTON SELESAI -->
                        <button onclick="openModal({{ $p->id }})"
                            class="px-3 py-1.5 text-xs bg-green-500 text-white rounded-md hover:bg-green-600">
                            <i class="bi bi-check-lg"></i> Selesai
                        </button>

                        <!-- FORM MASALAH -->
                        <form method="POST" action="{{ route('ekspedisi.masalah', $p->id) }}">
                            @csrf
                            <button 
                                onclick="return confirm('Tandai sebagai bermasalah?')"
                                class="px-3 py-1.5 text-xs bg-red-500 text-white rounded-md hover:bg-red-600">
                                <i class="bi bi-exclamation-lg"></i> Masalah
                            </button>
                        </form>

                    </td>

                </tr>

                <!-- MODAL -->
                <div id="modal-{{ $p->id }}" class="fixed inset-0 bg-black/50 hidden items-center justify-center z-50">
                    <div class="bg-white rounded-xl w-full max-w-md p-6">

                        <h3 class="text-lg font-semibold mb-4">
                            Selesaikan Pesanan #{{ $p->id }}
                        </h3>

                        <form method="POST" action="{{ route('ekspedisi.selesai', $p->id) }}" enctype="multipart/form-data">
                            @csrf

                            <label class="block text-sm mb-2">Upload Bukti</label>
                            <input type="file" name="foto_bukti_pengiriman" required
                                class="w-full border rounded-lg p-2 mb-4 text-sm">

                            <div class="flex justify-end gap-2">
                                <button type="button" onclick="closeModal({{ $p->id }})"
                                    class="px-4 py-2 text-sm border rounded-lg">
                                    Batal
                                </button>

                                <button class="px-4 py-2 text-sm bg-green-500 text-white rounded-lg hover:bg-green-600">
                                    Kirim
                                </button>
                            </div>

                        </form>

                    </div>
                </div>

                @endforeach

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

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

    @endif

</div>

@endsection

@push('scripts')
<script>
    function openModal(id) {
        document.getElementById('modal-' + id).classList.remove('hidden');
        document.getElementById('modal-' + id).classList.add('flex');
    }

    function closeModal(id) {
        document.getElementById('modal-' + id).classList.add('hidden');
        document.getElementById('modal-' + id).classList.remove('flex');
    }
</script>
@endpush