@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 {{ $deliveryNote->number }}
            </h1>

            <p class="text-sm text-gray-500 dark:text-gray-400">
                Order {{ $deliveryNote->order->invoice_number ?? '-' }}
            </p>
        </div>

        <div class="flex flex-wrap gap-2">
            <a target="_blank"
               href="{{ route('delivery-notes.print', ['deliveryNote' => $deliveryNote, 'paper' => 'a4']) }}"
               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:10px !important;padding:10px 14px !important;font-size:13px !important;font-weight:700 !important;text-decoration:none !important;box-shadow:0 4px 10px rgba(37,99,235,.25) !important;">
                Cetak A4
            </a>

            <a target="_blank"
               href="{{ route('delivery-notes.print', ['deliveryNote' => $deliveryNote, 'paper' => 'a5']) }}"
               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:10px !important;padding:10px 14px !important;font-size:13px !important;font-weight:700 !important;text-decoration:none !important;box-shadow:0 4px 10px rgba(37,99,235,.25) !important;">
                Cetak A5
            </a>

            <a target="_blank"
               href="{{ route('delivery-notes.print', ['deliveryNote' => $deliveryNote, 'paper' => '215x165']) }}"
               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:10px !important;padding:10px 14px !important;font-size:13px !important;font-weight:700 !important;text-decoration:none !important;box-shadow:0 4px 10px rgba(37,99,235,.25) !important;">
                Cetak 21,5 × 16,5
            </a>

            <a href="{{ route('delivery-notes.edit', $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:10px !important;padding:10px 14px !important;font-size:13px !important;font-weight:700 !important;text-decoration:none !important;">
                Edit
            </a>

            <a href="{{ route('delivery-notes.index') }}"
               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:10px !important;padding:10px 14px !important;font-size:13px !important;font-weight:700 !important;text-decoration:none !important;">
                Kembali
            </a>
        </div>
    </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

    <div class="rounded-xl border border-gray-200 bg-white p-6 shadow-sm dark:border-gray-800 dark:bg-gray-900">
        <div class="mb-5 flex items-center justify-between gap-4">
            <h2 class="text-lg font-semibold text-gray-900 dark:text-white">
                Informasi Surat Jalan
            </h2>

            @php
                $statusStyle = 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:5px 12px;font-size:12px;font-weight:800;{{ $statusStyle }}">
                {{ $deliveryNote->status_label }}
            </span>
        </div>

        <dl class="grid gap-5 md:grid-cols-2">
            <div>
                <dt class="text-xs font-semibold uppercase tracking-wide text-gray-500">No SJ</dt>
                <dd class="mt-1 font-medium text-gray-900 dark:text-white">{{ $deliveryNote->number }}</dd>
            </div>

            <div>
                <dt class="text-xs font-semibold uppercase tracking-wide text-gray-500">Tanggal</dt>
                <dd class="mt-1 text-gray-900 dark:text-white">{{ optional($deliveryNote->delivery_date)->format('d/m/Y') ?: '-' }}</dd>
            </div>

            <div>
                <dt class="text-xs font-semibold uppercase tracking-wide text-gray-500">Kendaraan</dt>
                <dd class="mt-1 text-gray-900 dark:text-white">
                    {{ $deliveryNote->vehicle_name ?: '-' }}
                    {{ $deliveryNote->plate_number ? '- ' . $deliveryNote->plate_number : '' }}
                </dd>
            </div>

            <div>
                <dt class="text-xs font-semibold uppercase tracking-wide text-gray-500">Supir</dt>
                <dd class="mt-1 text-gray-900 dark:text-white">
                    {{ $deliveryNote->driver_name ?: '-' }}
                    @if($deliveryNote->driver_phone)
                        <span class="text-gray-500">({{ $deliveryNote->driver_phone }})</span>
                    @endif
                </dd>
            </div>

            <div>
                <dt class="text-xs font-semibold uppercase tracking-wide text-gray-500">Penerima</dt>
                <dd class="mt-1 text-gray-900 dark:text-white">
                    {{ $deliveryNote->recipient_name ?: '-' }}
                    @if($deliveryNote->recipient_phone)
                        <span class="text-gray-500">({{ $deliveryNote->recipient_phone }})</span>
                    @endif
                </dd>
            </div>

            <div>
                <dt class="text-xs font-semibold uppercase tracking-wide text-gray-500">Order</dt>
                <dd class="mt-1">
                    <a href="{{ route('orders.show', $deliveryNote->order) }}" class="font-medium text-indigo-600 hover:underline">
                        {{ $deliveryNote->order->invoice_number ?? '-' }}
                    </a>
                </dd>
            </div>

            <div class="md:col-span-2">
                <dt class="text-xs font-semibold uppercase tracking-wide text-gray-500">Alamat Tujuan</dt>
                <dd class="mt-1 whitespace-pre-line text-gray-900 dark:text-white">{{ $deliveryNote->recipient_address ?: '-' }}</dd>
            </div>

            <div class="md:col-span-2">
                <dt class="text-xs font-semibold uppercase tracking-wide text-gray-500">Catatan</dt>
                <dd class="mt-1 whitespace-pre-line text-gray-900 dark:text-white">{{ $deliveryNote->notes ?: '-' }}</dd>
            </div>
        </dl>
    </div>

    <div class="rounded-xl border border-gray-200 bg-white p-6 shadow-sm dark:border-gray-800 dark:bg-gray-900">
        <h2 class="mb-4 text-lg font-semibold text-gray-900 dark:text-white">
            Aksi Status
        </h2>

        <div class="flex flex-col gap-3 sm:flex-row sm:flex-wrap">
            <form method="POST" action="{{ route('delivery-notes.mark-delivered', $deliveryNote) }}">
                @csrf
                <button type="submit"
                        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:10px !important;padding:10px 16px !important;font-size:14px !important;font-weight:700 !important;box-shadow:0 4px 10px rgba(37,99,235,.25) !important;cursor:pointer !important;">
                    Tandai Dikirim
                </button>
            </form>

            <form method="POST" action="{{ route('delivery-notes.mark-received', $deliveryNote) }}">
                @csrf
                <button type="submit"
                        style="display:inline-flex !important;align-items:center !important;justify-content:center !important;background:#059669 !important;color:#ffffff !important;border:1px solid #047857 !important;border-radius:10px !important;padding:10px 16px !important;font-size:14px !important;font-weight:700 !important;box-shadow:0 4px 10px rgba(5,150,105,.25) !important;cursor:pointer !important;">
                    Tandai Diterima
                </button>
            </form>

            <form method="POST"
                  action="{{ route('delivery-notes.destroy', $deliveryNote) }}"
                  onsubmit="return confirm('Batalkan surat jalan ini?')">
                @csrf
                @method('DELETE')
                <button type="submit"
                        style="display:inline-flex !important;align-items:center !important;justify-content:center !important;background:#ffffff !important;color:#b91c1c !important;border:1px solid #fca5a5 !important;border-radius:10px !important;padding:10px 16px !important;font-size:14px !important;font-weight:700 !important;cursor:pointer !important;">
                    Batalkan
                </button>
            </form>
        </div>
    </div>
</div>
@endsection
