<x-portal-layout
    title="Request Pelanggan"
    :preview-mode="$previewMode ?? false"
    :preview-back-url="$previewBackUrl ?? null"
    :preview-home-url="$previewHomeUrl ?? null"
    :portal-request-url="$portalRequestUrl ?? null"
    :portal-account-url="$portalAccountUrl ?? null"
>
    @if (($previewMode ?? false) === true)
        <section class="mb-4 rounded-lg border border-amber-200 bg-amber-50 p-4 text-sm text-amber-800">
            <div class="font-semibold">Mode preview admin</div>
            <div class="mt-1">Form request disembunyikan agar preview tidak membuat data baru.</div>
            <a href="{{ $previewBackUrl ?? route('portal-preview.index') }}" class="mt-3 inline-block rounded-md border border-amber-300 bg-white px-3 py-2 text-amber-900">Kembali</a>
        </section>
    @endif

    <section class="rounded-lg bg-white p-5 shadow-sm">
        <div class="text-sm text-slate-500">Request pelanggan</div>
        <h1 class="text-2xl font-semibold">{{ $customer->name }}</h1>
    </section>

    @unless (($previewMode ?? false) === true)
        <section class="mt-4 rounded-lg bg-white p-5 shadow-sm">
            <h2 class="font-semibold">Request Pickup</h2>
            <form method="post" action="{{ route('portal.requests.store') }}" class="mt-3 space-y-3">
                @csrf
                <input type="hidden" name="type" value="pickup">
                <textarea name="pickup_address" placeholder="Alamat penjemputan" class="w-full rounded-md border px-3 py-3 text-sm">{{ old('pickup_address', $customer->address) }}</textarea>
                <div class="grid grid-cols-2 gap-3">
                    <input name="pickup_date" type="date" min="{{ now()->toDateString() }}" value="{{ old('pickup_date') }}" class="rounded-md border px-3 py-3 text-sm">
                    <input name="pickup_time" type="time" value="{{ old('pickup_time') }}" class="rounded-md border px-3 py-3 text-sm">
                </div>
                <textarea name="note" placeholder="Catatan tambahan" class="w-full rounded-md border px-3 py-3 text-sm">{{ old('note') }}</textarea>
                <button class="w-full rounded-md bg-slate-900 px-4 py-3 font-medium text-white">Kirim Request Pickup</button>
            </form>
        </section>

        <section class="mt-4 rounded-lg bg-white p-5 shadow-sm">
            <h2 class="font-semibold">Repeat Order</h2>
            <form method="post" action="{{ route('portal.requests.store') }}" class="mt-3 space-y-3">
                @csrf
                <input type="hidden" name="type" value="repeat">
                <select name="source_order_id" class="w-full rounded-md border px-3 py-3 text-sm">
                    <option value="">Pilih nota sebelumnya</option>
                    @foreach ($orders as $order)
                        <option value="{{ $order->id }}" @selected((int) old('source_order_id') === (int) $order->id)>{{ $order->invoice_no }} - {{ $order->created_at->format('d/m/Y') }}</option>
                    @endforeach
                </select>
                <textarea name="note" placeholder="Catatan repeat order" class="w-full rounded-md border px-3 py-3 text-sm">{{ old('note') }}</textarea>
                <button class="w-full rounded-md border px-4 py-3 font-medium">Kirim Repeat Order</button>
            </form>
        </section>

        <section class="mt-4 rounded-lg bg-white p-5 shadow-sm">
            <h2 class="font-semibold">Request Lainnya</h2>
            <form method="post" action="{{ route('portal.requests.store') }}" class="mt-3 space-y-3">
                @csrf
                <input type="hidden" name="type" value="general">
                <textarea name="note" required placeholder="Tulis kebutuhan atau pertanyaan Anda" class="w-full rounded-md border px-3 py-3 text-sm">{{ old('note') }}</textarea>
                <button class="w-full rounded-md border px-4 py-3 font-medium">Kirim Request</button>
            </form>
        </section>
    @endunless

    <section class="mt-4 rounded-lg bg-white p-5 shadow-sm">
        <h2 class="font-semibold">Riwayat Request</h2>
        <div class="mt-3 divide-y text-sm">
            @forelse ($requests as $request)
                <div class="py-3">
                    <div class="flex justify-between gap-3">
                        <span class="font-medium">{{ ['pickup' => 'Pickup', 'repeat' => 'Repeat Order', 'general' => 'Request Lainnya'][$request->type] ?? $request->type }}</span>
                        <span class="rounded-full bg-slate-100 px-2 py-1 text-xs">{{ $request->status }}</span>
                    </div>
                    @if ($request->note)
                        <div class="mt-2 text-slate-500">{{ $request->note }}</div>
                    @endif
                </div>
            @empty
                <div class="py-3 text-slate-500">Belum ada request.</div>
            @endforelse
        </div>
    </section>
</x-portal-layout>
