<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Surat Jalan - {{ $order->invoice_number ?? '-' }}</title>
    <style>
        @page {
            margin: 18px 22px;
        }

        * {
            box-sizing: border-box;
        }

        body {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #222;
            margin: 0;
            background: #fff;
        }

        .page {
            width: 100%;
        }

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

        .print-btn {
            display: inline-block;
            padding: 8px 14px;
            background: #111827;
            color: #fff;
            text-decoration: none;
            border-radius: 6px;
            border: none;
            cursor: pointer;
            font-size: 13px;
        }

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

        .header td {
            vertical-align: top;
        }

        .header-left {
            width: 58%;
        }

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

        .logo-wrap {
            display: flex;
            align-items: flex-start;
            gap: 10px;
        }

        .logo-wrap img {
            max-width: 150px;
            max-height: 70px;
            object-fit: contain;
        }

        .shop-name-text {
            font-size: 12px;
            line-height: 1.25;
            color: #444;
        }

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

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

        .recipient-table .label,
        .meta-table .label {
            white-space: nowrap;
            width: 68px;
        }

        .recipient-table .sep,
        .meta-table .sep {
            width: 10px;
            text-align: center;
        }

        .red {
            color: #e11d48;
        }

        .sj-line {
            margin-top: 8px;
            margin-bottom: 10px;
            font-size: 15px;
            line-height: 1.4;
        }

        .sj-line strong {
            font-size: 15px;
        }

        .shipping-info {
            margin-bottom: 6px;
            font-size: 15px;
            line-height: 1.35;
        }

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

        .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: 14px;
            font-weight: bold;
            text-align: center;
            background: #fff;
        }

        .items-table td {
            height: 28px;
            padding: 4px 8px;
            font-size: 13px;
        }

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

        .desc-col {
            width: auto;
        }

        .text-center {
            text-align: center;
        }

        .signature-wrap {
            width: 100%;
            margin-top: 14px;
        }

        .signature-table {
            width: 100%;
            border-collapse: collapse;
        }

        .signature-table td {
            width: 50%;
            text-align: center;
            vertical-align: top;
            font-size: 16px;
        }

        .signature-space {
            height: 70px;
        }

        .sign-name {
            display: inline-block;
            min-width: 180px;
            border-top: 1px solid transparent;
            padding-top: 2px;
        }

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

        @media print {
            .no-print {
                display: none;
            }

            body {
                margin: 0;
            }
        }
    </style>
</head>
<body>
@php
    $items = collect($order->items ?? []);
    $printedDate = $printedAt ?? now();

    $customerName = trim((string) ($order->customer_name ?? optional($order->customer)->name ?? 'INSTANSI'));
    $invoiceNumber = trim((string) ($order->invoice_number ?? '-'));

    $suratJalanNumber = 'SJ/' . $printedDate->format('d') . '/' . preg_replace('/[^A-Za-z0-9]/', '', strtoupper(substr($customerName, 0, 12))) . '/' . $printedDate->format('m/Y');

    $prNo = $order->pr_number ?? '-';
    $poNo = $order->po_number ?? '-';
    $vehicleName = $order->vehicle_name ?? '';
    $plateNo = $order->plate_number ?? '';

    $preparedBy = optional($order->designer)->name ?: 'Admin';

    $rows = [];

    foreach ($items as $item) {
        $qty = (float) ($item->qty ?? 0);
        $qtyText = $qty > 0 ? rtrim(rtrim(number_format($qty, 2, ',', '.'), '0'), ',') : '-';

        $productName = trim((string) ($item->product_name ?? ''));
        $fileName = trim((string) ($item->file_name ?? ''));
        $sizeDesc = trim((string) ($item->size_description ?? ''));
        $finishing = trim((string) ($item->finishing_note ?? ''));

        $descParts = array_filter([
            $productName !== '' ? $productName : null,
            $fileName !== '' && $fileName !== $productName ? 'File: ' . $fileName : null,
            $sizeDesc !== '' ? 'Ukuran: ' . $sizeDesc : null,
            $finishing !== '' ? 'Finishing: ' . $finishing : null,
        ]);

        $rows[] = [
            'qty' => $qtyText,
            'desc' => !empty($descParts) ? implode(' | ', $descParts) : '-',
        ];
    }

    $minRows = 10;
    $blankRows = max($minRows - count($rows), 0);
@endphp

<div class="page">
    <div class="no-print">
        <button class="print-btn" onclick="window.print()">Print Surat Jalan</button>
    </div>

    <table class="header">
        <tr>
            <td class="header-left">
                <div class="logo-wrap">
                    @if(!empty($shopLogo))
                        <img src="{{ asset('storage/' . ltrim($shopLogo, '/')) }}" alt="Logo">
                    @else
                        <div class="shop-name-text">
                            <strong style="font-size: 20px;">{{ $shopName ?? 'Nama Perusahaan' }}</strong><br>
                            {{ $shopAddress ?? '' }}<br>
                            {{ $shopPhone ?? '' }}
                        </div>
                    @endif
                </div>
            </td>

            <td class="header-right">
                <table class="recipient-table">
                    <tr>
                        <td class="label">Kepada</td>
                        <td class="sep">:</td>
                        <td class="red">{{ $customerName !== '' ? $customerName : 'INSTANSI' }}</td>
                    </tr>
                </table>

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

                <table class="meta-table">
                    <tr>
                        <td class="label"><strong>PR No.</strong></td>
                        <td class="sep">:</td>
                        <td>{{ $prNo }}</td>
                    </tr>
                    <tr>
                        <td class="label"><strong>PO No.</strong></td>
                        <td class="sep">:</td>
                        <td>{{ $poNo }}</td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>

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

    <div class="shipping-info">
        Kami kirimkan barang - barang tersebut dibawah ini dengan kendaraan
        <span class="underline-text red">{{ $vehicleName !== '' ? $vehicleName : 'Kendaraan' }}</span>
        No.
        <span class="underline-text red">{{ $plateNo !== '' ? $plateNo : 'Plat No' }}</span>
    </div>

    <table class="items-table">
        <thead>
            <tr>
                <th class="qty-col">QUANTITY</th>
                <th class="desc-col">RINCIAN</th>
            </tr>
        </thead>
        <tbody>
            @forelse($rows as $row)
                <tr>
                    <td class="qty-col text-center">{{ $row['qty'] }}</td>
                    <td class="desc-col">{{ $row['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>

    <div class="signature-wrap">
        <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>
                    <span class="sign-name">&nbsp;</span>
                </td>
                <td>
                    <span class="sign-name">{{ $preparedBy }}</span>
                </td>
            </tr>
        </table>
    </div>

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