@extends('layouts.app')

@section('content')
<div class="space-y-6">
    <div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
        <div>
            <h1 class="text-2xl font-bold text-gray-900 dark:text-white">
                Surat Jalan
            </h1>

            <p class="text-sm text-gray-500 dark:text-gray-400">
                Riwayat dan pembuatan surat jalan.
            </p>
        </div>

        <a href="{{ route('delivery-notes.create') }}"
   style="display:inline-flex !important;align-items:center !important;justify-content:center !important;background-color:#2563eb !important;color:#ffffff !important;border:2px solid #1d4ed8 !important;border-radius:12px !important;padding:12px 18px !important;font-size:14px !important;font-weight:800 !important;text-decoration:none !important;line-height:1.2 !important;box-shadow:0 6px 14px rgba(37,99,235,.30) !important;">
    + Buat Surat Jalan
</a>
    </div>

    @if(session('success'))
        <div class="rounded-lg bg-green-50 p-4 text-sm text-green-700 dark:bg-green-500/10 dark:text-green-300">
            {{ session('success') }}
        </div>
    @endif

    @if(session('error'))
        <div class="rounded-lg bg-red-50 p-4 text-sm text-red-700 dark:bg-red-500/10 dark:text-red-300">
            {{ session('error') }}
        </div>
    @endif

    <form method="GET" class="rounded-xl border border-gray-200 bg-white p-4 dark:border-gray-800 dark:bg-gray-900">
        <div class="grid gap-3 md:grid-cols-3">
            <input
                name="search"
                value="{{ request('search') }}"
                placeholder="Cari SJ / invoice / customer..."
                class="rounded-lg border border-gray-300 px-3 py-2 text-sm dark:border-gray-700 dark:bg-gray-800 dark:text-white"
            >

            <select
                name="status"
                class="rounded-lg border border-gray-300 px-3 py-2 text-sm dark:border-gray-700 dark:bg-gray-800 dark:text-white"
            >
                <option value="">Semua status</option>
                <option value="draft" @selected(request('status') === 'draft')>Draft</option>
                <option value="printed" @selected(request('status') === 'printed')>Sudah Dicetak</option>
                <option value="delivered" @selected(request('status') === 'delivered')>Dikirim</option>
                <option value="received" @selected(request('status') === 'received')>Diterima</option>
                <option value="cancelled" @selected(request('status') === 'cancelled')>Dibatalkan</option>
            </select>

            <button
                type="submit"
                style="display:inline-flex !important;align-items:center !important;justify-content:center !important;background-color:#111827 !important;color:#ffffff !important;border:1px solid #111827 !important;border-radius:10px !important;padding:10px 16px !important;font-size:14px !important;font-weight:700 !important;cursor:pointer !important;">
                Filter
            </button>
        </div>
    </form>

    <div class="overflow-hidden rounded-xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-gray-900">
        <div class="overflow-x-auto">
            <table class="min-w-full text-sm">
                <thead>
                    <tr class="bg-gray-50 dark:bg-gray-800">
                        <th class="px-4 py-3 text-left">No SJ</th>
                        <th class="px-4 py-3 text-left">Order</th>
                        <th class="px-4 py-3 text-left">Kendaraan</th>
                        <th class="px-4 py-3 text-left">Tanggal</th>
                        <th class="px-4 py-3 text-left">Status</th>
                        <th class="px-4 py-3 text-right">Aksi</th>
                    </tr>
                </thead>

                <tbody>
                    @forelse($deliveryNotes as $deliveryNote)
                        <tr class="border-t border-gray-200 dark:border-gray-800">
                            <td class="px-4 py-3 font-medium text-gray-900 dark:text-white">
                                {{ $deliveryNote->number }}
                            </td>

                            <td class="px-4 py-3">
                                <a href="{{ route('orders.show', $deliveryNote->order) }}"
                                   class="font-medium text-indigo-600 hover:underline">
                                    {{ $deliveryNote->order->invoice_number ?? '-' }}
                                </a>

                                <div class="text-xs text-gray-500">
                                    {{ $deliveryNote->order->customer_name ?? $deliveryNote->recipient_name }}
                                </div>
                            </td>

                            <td class="px-4 py-3 text-gray-700 dark:text-gray-300">
                                {{ $deliveryNote->vehicle_name ?: optional($deliveryNote->vehicle)->name ?: '-' }}

                                <div class="text-xs text-gray-500">
                                    {{ $deliveryNote->plate_number ?: optional($deliveryNote->vehicle)->plate_number }}
                                </div>
                            </td>

                            <td class="px-4 py-3 text-gray-700 dark:text-gray-300">
                                {{ optional($deliveryNote->delivery_date)->format('d/m/Y') ?: '-' }}
                            </td>

                            <td class="px-4 py-3">
                                @php
                                    $statusClass = match($deliveryNote->status) {
                                        'draft' => 'background:#f3f4f6;color:#374151;',
                                        'printed' => 'background:#dbeafe;color:#1d4ed8;',
                                        'delivered' => 'background:#fef3c7;color:#92400e;',
                                        'received' => 'background:#dcfce7;color:#166534;',
                                        'cancelled' => 'background:#fee2e2;color:#991b1b;',
                                        default => 'background:#f3f4f6;color:#374151;',
                                    };
                                @endphp

                                <span style="display:inline-flex;align-items:center;border-radius:999px;padding:4px 10px;font-size:12px;font-weight:700;{{ $statusClass }}">
                                    {{ $deliveryNote->status_label }}
                                </span>
                            </td>

                            <td class="px-4 py-3 text-right">
                                <div class="inline-flex items-center justify-end gap-2">
                                    <a href="{{ route('delivery-notes.show', $deliveryNote) }}"
                                       style="display:inline-flex !important;align-items:center !important;justify-content:center !important;background:#ffffff !important;color:#374151 !important;border:1px solid #d1d5db !important;border-radius:8px !important;padding:6px 10px !important;font-size:12px !important;font-weight:700 !important;text-decoration:none !important;">
                                        Detail
                                    </a>

                                    <a target="_blank"
                                       href="{{ route('delivery-notes.print', $deliveryNote) }}"
                                       style="display:inline-flex !important;align-items:center !important;justify-content:center !important;background:#2563eb !important;color:#ffffff !important;border:1px solid #1d4ed8 !important;border-radius:8px !important;padding:6px 10px !important;font-size:12px !important;font-weight:700 !important;text-decoration:none !important;">
                                        Print
                                    </a>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="px-4 py-8 text-center text-gray-500">
                                Belum ada surat jalan.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <div class="border-t border-gray-200 p-4 dark:border-gray-800">
            {{ $deliveryNotes->links() }}
        </div>
    </div>
</div>
@endsection