<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Laporan Pemesanan</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Arial', 'Helvetica', sans-serif;
            font-size: 9px;
            margin: 20px 25px;
            color: #333;
        }

        .header {
            text-align: center;
            margin-bottom: 15px;
            padding-bottom: 8px;
            border-bottom: 2px solid #2c3e50;
        }

        .header h1 {
            margin: 0 0 5px 0;
            font-size: 18px;
            font-weight: bold;
            color: #2c3e50;
            letter-spacing: 0.5px;
        }

        .header .company-name {
            margin: 3px 0;
            font-size: 11px;
            color: #555;
            font-weight: 500;
        }

        .header .period {
            margin: 3px 0;
            font-size: 9px;
            color: #777;
        }

        /* Summary Box */
        .summary-box {
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 3px;
            padding: 8px;
            margin-bottom: 15px;
        }

        .summary-grid {
            display: table;
            width: 100%;
        }

        .summary-row {
            display: table-row;
        }

        .summary-row>div {
            display: table-cell;
            padding: 6px 10px;
            border-bottom: 1px solid #e9ecef;
        }

        .summary-row:last-child>div {
            border-bottom: none;
        }

        .summary-label {
            width: 65%;
            font-weight: 500;
            color: #495057;
        }

        .summary-value {
            width: 35%;
            text-align: right;
            font-weight: bold;
            color: #2c3e50;
        }

        .summary-total {
            background-color: #e9ecef;
            font-size: 12px;
        }

        .summary-total .summary-label,
        .summary-total .summary-value {
            padding: 10px;
            color: #000;
        }

        /* Table Styles */
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
            font-size: 10px;
        }

        thead {
            background-color: #f8f9fa;
        }

        th {
            padding: 10px 8px;
            text-align: left;
            border: 1px solid #dee2e6;
            font-weight: bold;
            color: #2c3e50;
            font-size: 10px;
        }

        td {
            padding: 8px;
            border: 1px solid #dee2e6;
            vertical-align: middle;
        }

        tbody tr:nth-child(even) {
            background-color: #f8f9fa;
        }

        tbody tr:hover {
            background-color: #e9ecef;
        }

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

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

        /* Status Badges */
        .status-lunas {
            display: inline-block;
            padding: 3px 8px;
            background-color: #d4edda;
            color: #155724;
            border-radius: 3px;
            font-size: 9px;
            font-weight: bold;
        }

        .status-dp {
            display: inline-block;
            padding: 3px 8px;
            background-color: #fff3cd;
            color: #856404;
            border-radius: 3px;
            font-size: 9px;
            font-weight: bold;
        }

        .status-belum {
            display: inline-block;
            padding: 3px 8px;
            background-color: #f8d7da;
            color: #721c24;
            border-radius: 3px;
            font-size: 9px;
            font-weight: bold;
        }

        /* Footer Row */
        tfoot {
            background-color: #e9ecef;
            font-weight: bold;
        }

        tfoot td {
            padding: 10px 8px;
            border: 2px solid #dee2e6;
            font-size: 11px;
            color: #000;
        }

        /* Footer Section */
        .footer {
            text-align: right;
            margin-top: 30px;
            padding-top: 15px;
            border-top: 1px solid #dee2e6;
            font-size: 9px;
            color: #6c757d;
        }

        .footer p {
            margin: 3px 0;
        }

        /* Page break */
        @media print {
            .page-break {
                page-break-after: always;
            }
        }

        /* Detail Items Styles */
        .order-detail {
            margin-bottom: 20px;
            page-break-inside: avoid;
        }

        .order-header {
            background-color: #2c3e50;
            color: white;
            padding: 5px 8px;
            margin-bottom: 5px;
            font-size: 10px;
            font-weight: bold;
        }

        .detail-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 15px;
            font-size: 8px;
        }

        .detail-table th {
            background-color: #3498db;
            color: white;
            padding: 4px 6px;
            text-align: left;
            font-weight: bold;
            border: 1px solid #2980b9;
        }

        .detail-table td {
            padding: 3px 6px;
            border: 1px solid #dee2e6;
            vertical-align: top;
        }

        .detail-table tr:nth-child(even) {
            background-color: #f8f9fa;
        }

        .custom-note {
            font-size: 7px;
            color: #555;
            font-style: italic;
            padding-top: 2px;
            border-top: 1px dotted #ccc;
            margin-top: 2px;
        }
    </style>
</head>

<body>
    <div class="header">
        <h1>LAPORAN PEMESANAN</h1>
        <p class="company-name">PT/Toko Annisa Syar'i</p>
        <p class="period">Tanggal: <?= date('d F Y', strtotime($tanggal)) ?></p>
    </div>

    <!-- Summary Section -->
    <div class="summary-box">
        <div class="summary-grid">
            <div class="summary-row">
                <div class="summary-label">Total Pesanan</div>
                <div class="summary-value"><?= $summary['total_orders'] ?> pesanan</div>
            </div>
            <div class="summary-row">
                <div class="summary-label">Total Nominal Pesanan</div>
                <div class="summary-value">Rp. <?= number_format($summary['total_revenue'], 0, ",", ".") ?></div>
            </div>
            <div class="summary-row">
                <div class="summary-label">Total DP Terkumpul</div>
                <div class="summary-value">Rp. <?= number_format($summary['total_dp_collected'], 0, ",", ".") ?></div>
            </div>
            <div class="summary-row">
                <div class="summary-label">Total Pembayaran Lunas</div>
                <div class="summary-value">Rp. <?= number_format($summary['total_full_payment'], 0, ",", ".") ?></div>
            </div>
            <div class="summary-row summary-total">
                <div class="summary-label">Sisa Pembayaran</div>
                <div class="summary-value">Rp. <?= number_format($summary['remaining_payment'], 0, ",", ".") ?></div>
            </div>
        </div>
    </div>

    <!-- Details Table -->
    <h3 style="margin-bottom: 10px; color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 5px; font-size: 12px;">Ringkasan Pesanan</h3>
    <table>
        <thead>
            <tr>
                <th style="width: 4%" class="text-center">No</th>
                <th style="width: 13%">No Order</th>
                <th style="width: 18%">Customer</th>
                <th style="width: 13%">Tgl Order</th>
                <th style="width: 14%" class="text-right">Nominal</th>
                <th style="width: 12%" class="text-right">DP</th>
                <th style="width: 12%" class="text-right">Sisa</th>
                <th style="width: 14%" class="text-center">Status</th>
            </tr>
        </thead>
        <tbody>
            <?php $i = 1 ?>
            <?php foreach ($items as $item): ?>
                <tr>
                    <td class="text-center"><?= $i++ ?></td>
                    <td><?= $item->no_order_custom ?></td>
                    <td><?= $item->nama ?></td>
                    <td><?= date('d/m/Y H:i', strtotime($item->tanggal_order)) ?></td>
                    <td class="text-right">Rp. <?= number_format($item->nominal_pelunasan, 0, ",", ".") ?></td>
                    <td class="text-right">Rp. <?= number_format($item->dp_pembayaran ?? 0, 0, ",", ".") ?></td>
                    <td class="text-right">Rp. <?= number_format($item->sisa_pembayaran ?? $item->nominal_pelunasan, 0, ",", ".") ?></td>
                    <td class="text-center">
                        <?php if ($item->sisa_pembayaran == 0): ?>
                            <span class="status-lunas">Lunas</span>
                        <?php elseif ($item->dp_pembayaran > 0): ?>
                            <span class="status-dp">DP</span>
                        <?php else: ?>
                            <span class="status-belum">Belum Bayar</span>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="4" class="text-right"><strong>TOTAL</strong></td>
                <td class="text-right"><strong>Rp. <?= number_format($summary['total_revenue'], 0, ",", ".") ?></strong></td>
                <td class="text-right"><strong>Rp. <?= number_format($summary['total_dp_collected'], 0, ",", ".") ?></strong></td>
                <td class="text-right"><strong>Rp. <?= number_format($summary['remaining_payment'], 0, ",", ".") ?></strong></td>
                <td></td>
            </tr>
        </tfoot>
    </table>

    <!-- Detailed Items for Each Order -->
    <div class="page-break"></div>
    <h3 style="margin: 20px 0 10px 0; color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 5px; font-size: 12px;">Detail Pesanan Lengkap</h3>

    <?php foreach ($items as $order): ?>
        <div class="order-detail">
            <div class="order-header">
                <?= $order->no_order_custom ?> - <?= $order->nama ?> | <?= date('d/m/Y', strtotime($order->tanggal_order)) ?> |
                Total: Rp <?= number_format($order->nominal_pelunasan, 0, ",", ".") ?> |
                DP: Rp <?= number_format($order->dp_pembayaran ?? 0, 0, ",", ".") ?> |
                <?php if ($order->sisa_pembayaran == 0): ?>
                    <span style="background: #27ae60; padding: 2px 5px; border-radius: 2px;">LUNAS</span>
                <?php elseif ($order->dp_pembayaran > 0): ?>
                    Sisa: Rp <?= number_format($order->sisa_pembayaran, 0, ",", ".") ?>
                <?php endif; ?>
            </div>

            <!-- Customer Identity Section -->
            <?php if (isset($order->customer_info)): ?>
                <div style="background: #e8f4f8; border: 1px solid #b3d9e6; padding: 6px 8px; margin-bottom: 8px; font-size: 9px;">
                    <strong style="color: #2c3e50;">Identitas Customer:</strong><br>
                    <table style="border: none; margin-top: 3px; font-size: 8px;">
                        <tr style="border: none; background: none;">
                            <td style="border: none; padding: 2px 5px; width: 80px;">Nama</td>
                            <td style="border: none; padding: 2px 5px; width: 10px;">:</td>
                            <td style="border: none; padding: 2px 5px;"><?= $order->customer_info->nama ?? '-' ?></td>
                        </tr>
                        <tr style="border: none; background: none;">
                            <td style="border: none; padding: 2px 5px;">No. Telepon</td>
                            <td style="border: none; padding: 2px 5px;">:</td>
                            <td style="border: none; padding: 2px 5px;"><?= $order->customer_info->telepon ?? '-' ?></td>
                        </tr>
                        <tr style="border: none; background: none;">
                            <td style="border: none; padding: 2px 5px; vertical-align: top;">Alamat</td>
                            <td style="border: none; padding: 2px 5px; vertical-align: top;">:</td>
                            <td style="border: none; padding: 2px 5px;"><?= $order->customer_info->alamat ?? '-' ?></td>
                        </tr>
                    </table>
                </div>
            <?php endif; ?>

            <!-- Detail Items Table -->
            <?php if (isset($order->detail_items) && !empty($order->detail_items)): ?>
                <table class="detail-table">
                    <thead>
                        <tr>
                            <th style="width: 3%;">No</th>
                            <th style="width: 15%;">Model</th>
                            <th style="width: 8%;">Ukuran</th>
                            <th style="width: 5%;">Qty</th>
                            <th style="width: 15%;">Kain</th>
                            <th style="width: 7%;">Meter</th>
                            <th style="width: 12%;">Ongkos</th>
                            <th style="width: 12%;">Hrg Kain</th>
                            <th style="width: 23%;">Ukuran Custom</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $itemNum = 1; ?>
                        <?php foreach ($order->detail_items as $detail): ?>
                            <tr>
                                <td style="text-align: center;"><?= $itemNum++ ?></td>
                                <td><?= $detail->nama_model ?></td>
                                <td><?= $detail->ukuran_gamis ?></td>
                                <td style="text-align: center;"><?= $detail->jumlah_pcs ?></td>
                                <td>
                                    <?php if ($detail->nama_jenis_kain): ?>
                                        <strong><?= $detail->nama_jenis_kain ?></strong><br>
                                        <small style="color: #666;"><?= $detail->warna ?></small>

                                        <?php
                                        // Display additional fabrics if exists
                                        if (!empty($detail->additional_fabrics_enriched) && is_array($detail->additional_fabrics_enriched) && count($detail->additional_fabrics_enriched) > 0):
                                            echo '<br><small style="color: #999; font-size: 7px;">+ ' . count($detail->additional_fabrics_enriched) . ' kain lainnya:</small><br>';
                                            foreach ($detail->additional_fabrics_enriched as $idx => $addFabric):
                                                echo '<small style="font-size: 6px;">• ' . htmlspecialchars($addFabric['nama_jenis_kain']) . ' (' . htmlspecialchars($addFabric['warna']) . ')</small><br>';
                                            endforeach;
                                        endif;
                                        ?>
                                    <?php else: ?>
                                        <em style="font-size: 7px;">Bawa sendiri</em>
                                    <?php endif; ?>
                                </td>
                                <td style="text-align: center;">
                                    <?php if (empty($detail->meter_pemakaian) || $detail->meter_pemakaian == 0): ?>
                                        <span style="color: red; font-size: 7px;">Belum diinput</span>
                                    <?php else: ?>
                                        <?= $detail->meter_pemakaian ?>
                                    <?php endif; ?>
                                    <?php
                                    // Show additional meters if exists
                                    if (!empty($detail->additional_fabrics_enriched) && is_array($detail->additional_fabrics_enriched)):
                                        foreach ($detail->additional_fabrics_enriched as $addFabric):
                                            $meter = $addFabric['meter_pemakaian'] ?? 0;
                                            if ($meter == 0):
                                                echo '<br><small style="font-size: 6px; color: red;">Belum diinput</small>';
                                            else:
                                                echo '<br><small style="font-size: 6px;">' . $meter . '</small>';
                                            endif;
                                        endforeach;
                                    endif;
                                    ?>
                                </td>
                                <td style="text-align: right;">Rp <?= number_format($detail->subtotal_detail_order, 0, ",", ".") ?></td>
                                <td style="text-align: right;">
                                    <?php if ($detail->nama_jenis_kain): ?>
                                        Rp <?= number_format($detail->subtotal_detail_kain, 0, ",", ".") ?>
                                    <?php else: ?>
                                        -
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <?php
                                    $customs = [];
                                    if (!empty($detail->lingkar_dada) && $detail->lingkar_dada != 0) $customs[] = "LD: {$detail->lingkar_dada}";
                                    if (!empty($detail->lingkar_pinggang) && $detail->lingkar_pinggang != 0) $customs[] = "LP: {$detail->lingkar_pinggang}";
                                    if (!empty($detail->lingkar_panggul) && $detail->lingkar_panggul != 0) $customs[] = "LPg: {$detail->lingkar_panggul}";
                                    if (!empty($detail->lebar_bahu) && $detail->lebar_bahu != 0) $customs[] = "LB: {$detail->lebar_bahu}";
                                    if (!empty($detail->panjang_lengan) && $detail->panjang_lengan != 0) $customs[] = "PL: {$detail->panjang_lengan}";
                                    if (!empty($detail->panjang_gamis) && $detail->panjang_gamis != 0) $customs[] = "PG: {$detail->panjang_gamis}";

                                    if (!empty($customs)) {
                                        echo implode(', ', $customs);
                                    } else {
                                        echo '<em style="font-size: 7px;">Standard</em>';
                                    }
                                    ?>
                                    <?php if (!empty($detail->catatan_detail)): ?>
                                        <div class="custom-note"><?= $detail->catatan_detail ?></div>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>
    <?php endforeach; ?>

    <div class="footer">
        <p>Generated on: <?= date('d/m/Y H:i:s') ?></p>
        <p>Dokumen ini bersifat rahasia dan hanya untuk penggunaan internal</p>
    </div>
</body>

</html>