<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title><?= esc($title ?? 'Struk Penjualan') ?></title>
    <style>
        @media print {
            @page {
                size: 58mm auto;
                margin: 0;
            }
        }

        body {
            font-family: 'Courier New', Courier, monospace;
            font-size: 12px;
            width: 220px;
            margin: 0 auto;
            padding: 5px 8px;
        }

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

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

        .bold {
            font-weight: bold;
        }

        .line {
            border-top: 1px dashed #000;
            margin: 6px 0;
        }

        .item-row {
            display: flex;
            justify-content: space-between;
            font-weight: bold;
            font-size: 12px;
        }

        .item-detail {
            display: flex;
            justify-content: space-between;
            font-size: 11px;
            margin-bottom: 2px;
        }

        .footer {
            margin-top: 10px;
            text-align: center;
            font-size: 11px;
        }

        .logo {
            max-width: 100px;
            max-height: 100px;
            margin: 0 auto 5px auto;
        }

        .summary-block {
            display: flex;
            justify-content: space-between;
            margin-bottom: 2px;
        }

        .summary-block.bold {
            font-weight: bold;
            font-size: 12px;
        }

        .summary {
            margin-top: 6px;
            margin-bottom: 6px;
        }
        .info-row {
            display: flex;
            justify-content: space-between;
            font-size: 11px;
        }

        .info-left {
            text-align: left;
            white-space: nowrap;
        }

        .info-right {
            text-align: right;
            white-space: nowrap;
        }
    </style>
</head>
<body onload="window.print();">

    <!-- LOGO -->
    <?php if (!empty($receipt['logo'])): ?>
        <div class="text-center">
            <img src="<?= base_url('uploads/' . $receipt['logo']) ?>" alt="Logo" class="logo">
        </div>
    <?php endif; ?>

    <!-- HEADER TOKO -->
    <div class="text-center">
        <div class="bold"><?= esc($receipt['header'] ?? 'TOKO KAMI') ?></div>
    </div>

    <div class="line"></div>

    <!-- INFO TRANSAKSI -->
    
         <?php
        $tanggalWaktu = strtotime($sale['created_at'] ?? $sale['tanggal']);
        $tgl = date('Y-m-d', $tanggalWaktu);
        $jam = date('H:i:s', $tanggalWaktu);
    ?>
    <div class="info-row">
        <div class="info-left">
            No.<?= esc($sale['kode'] ?? '-') ?><br>
            <?= $tgl ?><br>
            <?= $jam ?>
        </div>
        <div class="info-right">
              <br>
              <br>
            Kasir : <?= esc($sale['user_name'] ?? '-') ?><br>
            <?= esc($sale['customer_name'] ?? '-') ?>
        </div>
    </div>


        <?= esc($sale['jenis_pesan'] ?? '-') ?><br>
        <?php if (($sale['jenis_pesan'] ?? '') === 'dine_in'): ?>
            No Meja&nbsp;&nbsp;: <?= esc($sale['no_meja'] ?? '-') ?>
        <?php endif; ?>


    <div class="line"></div>

    <!-- LIST ITEM -->
    <?php foreach ($details as $item): ?>
        <div class="item-row">
            <div><?= esc($item['nama_item']) ?></div>
            <div><?= number_format($item['subtotal']) ?></div>
        </div>
        <div class="item-detail">
            <?= $item['qty'] ?> x <?= number_format($item['harga']) ?> = <?= number_format($item['subtotal']) ?>
        </div>
    <?php endforeach; ?>

    <div class="line"></div>

    <!-- TOTAL & RINCIAN -->
    <?php
        $subtotal   = (int) ($sale['total'] ?? 0);
        $taxPercent = (float) ($sale['pajak_persen'] ?? 0);
        $taxNominal = (int) ($sale['pajak_nominal'] ?? 0);
        $diskonPersen = (float) ($sale['diskon_persen'] ?? 0);
        $diskonNominal = (int) ($sale['diskon_nominal'] ?? 0);
        $grandTotal = $subtotal - $diskonNominal + $taxNominal;
        $paid       = (int) ($sale['dibayar'] ?? 0);
        $change     = (int) ($sale['kembalian'] ?? ($paid - $grandTotal));
    ?>

    <div class="summary">
        <div class="summary-block">
            <span>Subtotal</span>
            <span><?= number_format($subtotal) ?></span>
        </div>

        <?php if ($taxNominal > 0): ?>
            <div class="summary-block">
                <span>Pajak (<?= $taxPercent ?>%)</span>
                <span><?= number_format($taxNominal) ?></span>
            </div>
        <?php endif; ?>

        <?php if (!empty($sale['diskon_nominal']) && $sale['diskon_nominal'] > 0): ?>
            <div class="summary-block">
                <span>Diskon (<?= $sale['diskon_persen'] ?>%)</span>
                <span><?= number_format($sale['diskon_nominal']) ?></span>
            </div>
        <?php endif; ?>

        <div class="summary-block bold">
            <span>Total</span>
            <span><?= number_format($grandTotal) ?></span>
        </div>

        <div class="line"></div>

        <div class="summary-block">
            <span>Dibayar</span>
            <span><?= number_format($paid) ?></span>
        </div>

        <div class="summary-block">
            <span>Kembalian</span>
            <span><?= number_format($change) ?></span>
        </div>
    </div>

    <div class="line"></div>

    <!-- FOOTER -->
    <div class="footer">
        <?= nl2br(esc($receipt['footer'] ?? "Terima kasih telah berbelanja\nSemoga bermanfaat")) ?>
    </div>

</body>
</html>
