<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Surat Jalan {{ $deliveryNote->number }}</title>

    @php
        $paperSize = $paperSize ?? request('paper', 'a4');

        if (!in_array($paperSize, ['a4', 'a5', '215x165'], true)) {
            $paperSize = 'a4';
        }

        $pageCss = match ($paperSize) {
            'a5' => 'size: A5 portrait; margin: 9mm;',
            '215x165' => 'size: 215mm 165mm; margin: 7mm;',
            default => 'size: A4 portrait; margin: 12mm 14mm;',
        };

        $bodyFontSize = match ($paperSize) {
            'a5' => '12px',
            '215x165' => '12px',
            default => '14px',
        };

        $logoMaxWidth = match ($paperSize) {
            'a5' => '125px',
            '215x165' => '130px',
            default => '165px',
        };

        $logoMaxHeight = match ($paperSize) {
            'a5' => '58px',
            '215x165' => '60px',
            default => '78px',
        };

        $tableFontSize = match ($paperSize) {
            'a5' => '11px',
            '215x165' => '11px',
            default => '13px',
        };

        $tableCellHeight = match ($paperSize) {
            'a5' => '24px',
            '215x165' => '23px',
            default => '28px',
        };

        $signatureHeight = match ($paperSize) {
            'a5' => '45px',
            '215x165' => '42px',
            default => '72px',
        };

        $minRows = match ($paperSize) {
            'a5' => 7,
            '215x165' => 6,
            default => 10,
        };
    @endphp

    <style>
        @page {
            {{ $pageCss }}
        }

        * {
            box-sizing: border-box;
        }

        body {
            font-family: Arial, Helvetica, sans-serif;
            color: #222;
            margin: 0;
            background: #fff;
            font-size: {{ $bodyFontSize }};
            -webkit-print-color-adjust: exact;
            print-color-adjust: exact;
        }

        .page {
            width: 100%;
        }

        .no-print {
            text-align: right;
            margin-bottom: 14px;
        }

        .print-btn {
            background: #2563eb;
            color: #fff;
            border: 1px solid #1d4ed8;
            border-radius: 6px;
            padding: 8px 14px;
            cursor: pointer;
            font-weight: 700;
        }

        .paper-info {
            display: inline-block;
            margin-right: 8px;
            color: #4b5563;
            font-size: 12px;
        }

        .header {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 8px;
        }

        .header td {
            vertical-align: top;
        }

        .header-left {
            width: 58%;
        }

        .header-right {
            width: 42%;
            padding-top: 4px;
        }

        .logo {
            max-width: {{ $logoMaxWidth }};
            max-height: {{ $logoMaxHeight }};
            object-fit: contain;
        }

        .shop-fallback {
            font-size: 12px;
            line-height: 1.35;
            color: #444;
        }

        .recipient-table,
        .meta-table {
            width: 100%;
            border-collapse: collapse;
        }

        .recipient-table td,
        .meta-table td {
            padding: 1px 0;
            font-size: {{ $bodyFontSize }};
            vertical-align: top;
        }

        .label {
            white-space: nowrap;
            width: 76px;
        }

        .sep {
            width: 10px;
            text-align: center;
        }

        .red {
            color: #e11d48;
        }

        .sj-line {
            margin-top: 8px;
            margin-bottom: 10px;
            font-size: calc({{ $bodyFontSize }} + 1px);
            line-height: 1.4;
        }

        .shipping-info {
            margin-bottom: 6px;
            font-size: calc({{ $bodyFontSize }} + 1px);
            line-height: 1.35;
        }

        .underline-text {
            display: inline-block;
            min-width: 80px;
            border-bottom: 1px dotted #222;
            text-align: center;
            padding: 0 4px 1px;
            line-height: 1.1;
        }

        .items-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 4px;
        }

        .items-table th,
        .items-table td {
            border: 1px solid #3a2a2a;
        }

        .items-table th {
            height: 24px;
            font-size: {{ $bodyFontSize }};
            font-weight: bold;
            text-align: center;
        }

        .items-table td {
            height: {{ $tableCellHeight }};
            padding: 4px 8px;
            font-size: {{ $tableFontSize }};
            vertical-align: top;
        }

        .qty-col {
            width: 135px;
            text-align: center;
        }

        .desc-col {
            width: auto;
        }

        .signature-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 14px;
        }

        .signature-table td {
            width: 50%;
            text-align: center;
            vertical-align: top;
            font-size: calc({{ $bodyFontSize }} + 1px);
        }

        .signature-space {
            height: {{ $signatureHeight }};
        }

        .footer {
            margin-top: 8px;
            font-size: 11px;
            color: #666;
        }

        @media print {
            .no-print {
                display: none;
            }
        }
    </style>
</head>
<body onload="window.print()">
@php
    $items = collect($order->items ?? []);
    $blankRows = max($minRows - $items->count(), 0);

    $vehicleName = $deliveryNote->vehicle_name ?: optional($deliveryNote->vehicle)->name ?: 'Kendaraan';
    $plateNumber = $deliveryNote->plate_number ?: optional($deliveryNote->vehicle)->plate_number ?: 'Plat No';
    $recipientName = $deliveryNote->recipient_name ?: ($order->customer_name ?? 'INSTANSI');

    $paperLabel = match ($paperSize) {
        'a5' => 'A5',
        '215x165' => '21,5 × 16,5 cm',
        default => 'A4',
    };

    $logoSrc = '';

    if (!empty($shopLogo)) {
        if (str_starts_with($shopLogo, 'http://') || str_starts_with($shopLogo, 'https://') || str_starts_with($shopLogo, '/') || str_starts_with($shopLogo, 'data:image')) {
            $logoSrc = $shopLogo;
        } else {
            $logoSrc = asset('storage/' . ltrim($shopLogo, '/'));
        }
    }
@endphp

<div class="page">
    <div class="no-print">
        <span class="paper-info">Ukuran: {{ $paperLabel }}</span>
        <button class="print-btn" onclick="window.print()">Print Surat Jalan</button>
    </div>

    <table class="header">
        <tr>
            <td class="header-left">
                @if($logoSrc !== '')
                    <img src="{{ $logoSrc }}" alt="Logo" class="logo">
                @else
                    <div class="shop-fallback">
                        <strong style="font-size:20px;">{{ $shopName ?? config('app.name') }}</strong><br>
                        {{ $shopAddress ?? '' }}<br>
                        {{ $shopPhone ?? '' }}
                    </div>
                @endif
            </td>

            <td class="header-right">
                <table class="recipient-table">
                    <tr>
                        <td class="label">Kepada</td>
                        <td class="sep">:</td>
                        <td class="red">{{ $recipientName }}</td>
                    </tr>
                </table>

                <div style="height:14px;"></div>

                <table class="meta-table">
                    <tr>
                        <td class="label"><strong>Invoice</strong></td>
                        <td class="sep">:</td>
                        <td>{{ $order->invoice_number ?? '-' }}</td>
                    </tr>
                    <tr>
                        <td class="label"><strong>Tanggal</strong></td>
                        <td class="sep">:</td>
                        <td>{{ optional($deliveryNote->delivery_date)->format('d/m/Y') ?: now()->format('d/m/Y') }}</td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>

    <div class="sj-line">
        <strong>SURAT JALAN No.</strong>
        <span class="red">{{ $deliveryNote->number }}</span>
    </div>

    <div class="shipping-info">
        Kami kirimkan barang - barang tersebut dibawah ini dengan kendaraan
        <span class="underline-text red">{{ $vehicleName }}</span>
        No.
        <span class="underline-text red">{{ $plateNumber }}</span>
    </div>

    <table class="items-table">
        <thead>
            <tr>
                <th class="qty-col">QUANTITY</th>
                <th class="desc-col">RINCIAN</th>
            </tr>
        </thead>
        <tbody>
            @forelse($items as $item)
                @php
                    $qty = (float) ($item->qty ?? 0);
                    $qtyText = $qty > 0 ? rtrim(rtrim(number_format($qty, 2, ',', '.'), '0'), ',') : '-';

                    $desc = collect([
                        $item->product_name ?? null,
                        !empty($item->file_name) ? 'File: ' . $item->file_name : null,
                        !empty($item->size_description) ? 'Ukuran: ' . $item->size_description : null,
                        !empty($item->material_name) ? 'Bahan: ' . $item->material_name : null,
                        !empty($item->finishing_note) ? 'Finishing: ' . $item->finishing_note : null,
                    ])->filter()->implode(' | ');
                @endphp

                <tr>
                    <td class="qty-col">{{ $qtyText }}</td>
                    <td class="desc-col">{{ $desc ?: '-' }}</td>
                </tr>
            @empty
                <tr>
                    <td class="qty-col">&nbsp;</td>
                    <td class="desc-col">&nbsp;</td>
                </tr>
            @endforelse

            @for($i = 0; $i < $blankRows; $i++)
                <tr>
                    <td class="qty-col">&nbsp;</td>
                    <td class="desc-col">&nbsp;</td>
                </tr>
            @endfor
        </tbody>
    </table>

    @if($deliveryNote->notes)
        <div style="margin-top:8px;font-size:12px;">
            <strong>Catatan:</strong> {{ $deliveryNote->notes }}
        </div>
    @endif

    <table class="signature-table">
        <tr>
            <td>Tanda Terima</td>
            <td>Hormat Kami,</td>
        </tr>
        <tr>
            <td class="signature-space"></td>
            <td class="signature-space"></td>
        </tr>
        <tr>
            <td>(................................)</td>
            <td>{{ $shopName ?? config('app.name') }}</td>
        </tr>
    </table>

    @if(!empty($shopAddress) || !empty($shopPhone))
        <div class="footer">
            {{ $shopAddress ?? '' }}
            {{ (!empty($shopAddress) && !empty($shopPhone)) ? ' | ' : '' }}
            {{ $shopPhone ?? '' }}
        </div>
    @endif
</div>
</body>
</html>
