@extends('layouts.courier')
@section('title', 'Delivery Barang')

@section('content')
<div class="py-2">

    <div class="mb-5">
        <h1 class="text-xl font-bold text-gray-800">Delivery Barang</h1>
        <p class="text-sm text-gray-500 mt-0.5">{{ $stats['service_queue'] + $stats['sale_queue'] }} antrean pengantaran ke customer</p>
    </div>

    <form method="GET" class="mb-3 space-y-3">
        <input type="text" name="search" value="{{ $search ?? '' }}" placeholder="Cari kode/pelanggan/perangkat"
               class="w-full rounded-xl border border-gray-300 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent">

        <div class="grid grid-cols-3 gap-2 text-xs font-semibold">
            <a href="{{ route('courier.delivery', ['search' => $search, 'queue_type' => 'all']) }}"
               class="text-center rounded-lg px-2 py-2 border transition {{ $queueType === 'all' ? 'bg-brand-600 text-white border-brand-600' : 'bg-white text-gray-600 border-gray-200 hover:bg-gray-50' }}">Semua</a>
            <a href="{{ route('courier.delivery', ['search' => $search, 'queue_type' => 'service']) }}"
               class="text-center rounded-lg px-2 py-2 border transition {{ $queueType === 'service' ? 'bg-green-600 text-white border-green-600' : 'bg-white text-gray-600 border-gray-200 hover:bg-gray-50' }}">Service</a>
            <a href="{{ route('courier.delivery', ['search' => $search, 'queue_type' => 'sale']) }}"
               class="text-center rounded-lg px-2 py-2 border transition {{ $queueType === 'sale' ? 'bg-blue-600 text-white border-blue-600' : 'bg-white text-gray-600 border-gray-200 hover:bg-gray-50' }}">Sale</a>
        </div>
    </form>

    <div class="mb-4 rounded-2xl border border-gray-200 bg-white px-4 py-3 shadow-sm">
        <p class="text-xs font-semibold uppercase tracking-wide text-gray-500">Ringkasan Antrean</p>
        <div class="mt-2 grid grid-cols-2 gap-3 text-sm">
            <div class="rounded-xl bg-green-50 border border-green-100 px-3 py-2">
                <p class="text-[11px] text-green-700">Queue Service</p>
                <p class="text-lg font-bold text-green-800">{{ $stats['service_queue'] }}</p>
            </div>
            <div class="rounded-xl bg-blue-50 border border-blue-100 px-3 py-2">
                <p class="text-[11px] text-blue-700">Queue Sale</p>
                <p class="text-lg font-bold text-blue-800">{{ $stats['sale_queue'] }}</p>
            </div>
            <div class="rounded-xl bg-emerald-50 border border-emerald-100 px-3 py-2">
                <p class="text-[11px] text-emerald-700">Service Delivered Hari Ini</p>
                <p class="text-lg font-bold text-emerald-800">{{ $stats['service_delivered_today'] }}</p>
            </div>
            <div class="rounded-xl bg-sky-50 border border-sky-100 px-3 py-2">
                <p class="text-[11px] text-sky-700">Sale Delivered Hari Ini</p>
                <p class="text-lg font-bold text-sky-800">{{ $stats['sale_delivered_today'] }}</p>
            </div>
        </div>
        <p class="text-[11px] text-gray-500 mt-2">Minggu ini: Service {{ $stats['service_delivered_week'] }} • Sale {{ $stats['sale_delivered_week'] }}</p>
    </div>

    @if($queueType !== 'sale')
    <div class="mb-3">
        <h2 class="text-sm font-semibold text-gray-700">Delivery Service</h2>
        <p class="text-xs text-gray-500 mt-0.5">Tiket service yang perlu diantar ke customer.</p>
    </div>

    @if($tickets->isEmpty())
    <div class="rounded-2xl bg-white border border-gray-200 shadow-sm p-8 text-center mb-5">
        <p class="text-gray-500 text-sm font-medium">Tidak ada tiket service untuk delivery saat ini.</p>
    </div>
    @else
    <div class="space-y-3">
        @foreach($tickets as $ticket)
        <div class="rounded-2xl bg-white border border-green-100 shadow-sm overflow-hidden">
            <div class="h-1 bg-green-500"></div>
            <div class="p-4 flex items-center justify-between gap-4 flex-wrap">
                <div>
                    <p class="font-mono font-bold text-gray-800 text-base">{{ $ticket->ticket_code }}</p>
                    <p class="text-sm font-semibold text-gray-700 mt-0.5">{{ $ticket->customer->name }}</p>
                    <p class="text-xs text-gray-400">{{ $ticket->brand }} {{ $ticket->model }} &middot; {{ $ticket->device_type }}</p>
                    @if($ticket->status === 'Cancelled')
                        <span class="inline-flex items-center mt-2 px-2 py-0.5 rounded-full text-[11px] font-semibold bg-red-50 text-red-700 border border-red-200">Batal - Delivery Pembatalan</span>
                    @else
                        <span class="inline-flex items-center mt-2 px-2 py-0.5 rounded-full text-[11px] font-semibold bg-green-50 text-green-700 border border-green-200">Selesai - Delivery</span>
                    @endif
                </div>
                <a href="{{ route('handover.show', $ticket->id) }}"
                   class="flex-shrink-0 inline-flex items-center gap-1.5 bg-green-600 text-white text-sm font-semibold rounded-xl px-4 py-2.5 hover:bg-green-700 transition">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                    </svg>
                    Proses Delivery
                </a>
            </div>
        </div>
        @endforeach
    </div>
    @endif

    <div class="mt-4 mb-7">
        {{ $tickets->appends(['sale_page' => request('sale_page'), 'queue_type' => $queueType, 'search' => $search])->links() }}
    </div>
    @endif

    @if($queueType !== 'service')
    <div class="mb-3">
        <h2 class="text-sm font-semibold text-gray-700">Delivery Barang Pembelian</h2>
        <p class="text-xs text-gray-500 mt-0.5">Invoice penjualan barang yang sudah issued dan siap dikirim.</p>
    </div>

    @if($saleInvoices->isEmpty())
    <div class="rounded-2xl bg-white border border-gray-200 shadow-sm p-8 text-center">
        <p class="text-gray-500 text-sm font-medium">Tidak ada invoice sale untuk delivery saat ini.</p>
    </div>
    @else
    <div class="space-y-3">
        @foreach($saleInvoices as $saleInvoice)
        <div class="rounded-2xl bg-white border border-blue-100 shadow-sm overflow-hidden">
            <div class="h-1 bg-blue-500"></div>
            <div class="p-4 flex items-center justify-between gap-4 flex-wrap">
                <div>
                    <p class="font-mono font-bold text-gray-800 text-base">{{ $saleInvoice->invoice_number ?: ('INV-DRAFT-' . $saleInvoice->id) }}</p>
                    <p class="text-sm font-semibold text-gray-700 mt-0.5">{{ $saleInvoice->customer->name }}</p>
                    <p class="text-xs text-gray-400">{{ $saleInvoice->items->take(2)->pluck('description')->implode(', ') ?: 'Item barang pembelian' }}</p>
                    <span class="inline-flex items-center mt-2 px-2 py-0.5 rounded-full text-[11px] font-semibold bg-blue-50 text-blue-700 border border-blue-200">Sale - Siap Delivery</span>
                </div>
                <a href="{{ route('courier.delivery.sales.show', $saleInvoice) }}"
                   class="flex-shrink-0 inline-flex items-center gap-1.5 bg-blue-600 text-white text-sm font-semibold rounded-xl px-4 py-2.5 hover:bg-blue-700 transition">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v8m4-4H8m13 0a9 9 0 11-18 0 9 9 0 0118 0z"/>
                    </svg>
                    Konfirmasi Delivery
                </a>
            </div>
        </div>
        @endforeach
    </div>

    <div class="mt-4 mb-7">
        {{ $saleInvoices->appends(['service_page' => request('service_page'), 'queue_type' => $queueType, 'search' => $search])->links() }}
    </div>
    @endif
    @endif

    <div class="mt-2 rounded-2xl bg-white border border-gray-200 shadow-sm p-4">
        <h2 class="text-sm font-semibold text-gray-700 mb-3">Riwayat Delivery Terbaru</h2>
        <div class="space-y-2">
            @forelse($recentServiceDeliveries as $history)
            @php
                $deliveryLog = $history->logs->first(function ($log) {
                    $note = strtolower($log->note);

                    return str_contains($note, 'delivery ke customer selesai') || str_contains($note, 'delivery pembatalan ke customer selesai');
                });
            @endphp
            <div class="rounded-xl bg-gray-50 border border-gray-100 px-3 py-2">
                <p class="text-xs font-semibold text-gray-700">Service • {{ $history->ticket_code }}</p>
                <p class="text-xs text-gray-500">{{ $history->customer->name }} • {{ optional($history->picked_up_at)->format('d M Y H:i') }}</p>
                <p class="text-[11px] text-gray-500 mt-0.5">Kurir: {{ $deliveryLog?->user?->name ?? '-' }}</p>
            </div>
            @empty
            <p class="text-xs text-gray-400">Belum ada riwayat delivery service.</p>
            @endforelse

            @forelse($recentSaleDeliveries as $history)
            <div class="rounded-xl bg-gray-50 border border-gray-100 px-3 py-2">
                <p class="text-xs font-semibold text-gray-700">Sale • {{ $history->invoice_number ?: ('INV-' . $history->id) }}</p>
                <p class="text-xs text-gray-500">{{ $history->customer->name }} • {{ optional($history->delivered_at)->format('d M Y H:i') }}</p>
                <p class="text-[11px] text-gray-500 mt-0.5">Kurir: {{ $history->deliverer?->name ?? '-' }}</p>
            </div>
            @empty
            <p class="text-xs text-gray-400">Belum ada riwayat delivery sale.</p>
            @endforelse
        </div>
    </div>

</div>
@endsection
