<?php
$subtotal = 0;
$totalJumlah = 0;
?>
<main class="receipt">
    <section class="header">
        <div class="store-name"><?= esc($appName) ?></div>
    </section>

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

    <section>
        <div class="row">
            <span class="label">Nota</span>
            <span class="value"><?= esc($transaksi['no_transaksi'] ?? '-') ?></span>
        </div>
        <div class="row">
            <span class="label">Tanggal</span>
            <span class="value"><?= esc($tanggal) ?> <?= esc($jam) ?></span>
        </div>
        <div class="row">
            <span class="label">Kasir</span>
            <span class="value"><?= esc($transaksi['nama_user'] ?? ($userName ?? '-')) ?></span>
        </div>
        <div class="row">
            <span class="label">Pelanggan</span>
            <span class="value"><?= esc($transaksi['nama_pelanggan'] ?? '-') ?></span>
        </div>
    </section>

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

    <section>
        <?php foreach ($details as $detail) : ?>
            <?php
            $jumlah = (int) ($detail['jumlah'] ?? 0);
            $hargaJual = (float) ($detail['harga_jual'] ?? 0);
            $itemSubtotal = (float) ($detail['subtotal'] ?? ($jumlah * $hargaJual));
            $subtotal += $itemSubtotal;
            $totalJumlah += $jumlah;
            ?>
            <div class="item">
                <div class="item-name"><?= esc($detail['nama_barang'] ?? '-') ?></div>
                <div class="item-calc">
                    <span><?= esc((string) $jumlah) ?> <?= esc($detail['satuan'] ?? '') ?> x <?= esc(number_format($hargaJual, 0, ',', '.')) ?></span>
                    <span><?= esc(number_format($itemSubtotal, 0, ',', '.')) ?></span>
                </div>
            </div>
        <?php endforeach; ?>

        <?php if (empty($details)) : ?>
            <div class="muted">Tidak ada item.</div>
        <?php endif; ?>
    </section>

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

    <section class="totals">
        <div class="row">
            <span>Jumlah Item</span>
            <span><?= esc((string) $totalJumlah) ?></span>
        </div>
        <div class="row">
            <span>Subtotal</span>
            <span><?= esc(number_format((float) ($transaksi['subtotal'] ?? $subtotal), 0, ',', '.')) ?></span>
        </div>
        <div class="row">
            <span>Pajak <?= esc((string) ((int) ($transaksi['pajak_persen'] ?? 0))) ?>%</span>
            <span><?= esc(number_format((float) ($transaksi['pajak_nominal'] ?? 0), 0, ',', '.')) ?></span>
        </div>
        <div class="row total-main">
            <span>Total</span>
            <span><?= esc(number_format($totalPenjualan, 0, ',', '.')) ?></span>
        </div>
        <div class="row">
            <span>Dibayar</span>
            <span><?= esc(number_format($totalDibayar, 0, ',', '.')) ?></span>
        </div>
        <div class="row">
            <span>Sisa</span>
            <span><?= esc(number_format($sisa, 0, ',', '.')) ?></span>
        </div>
    </section>

    <?php if (! empty($transaksi['catatan'])) : ?>
        <div class="line"></div>
        <section>
            <div class="muted">Catatan:</div>
            <div><?= nl2br(esc($transaksi['catatan'])) ?></div>
        </section>
    <?php endif; ?>

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

    <section class="footer">
        <div>Terima kasih</div>
        <div class="muted">Barang yang sudah dibeli harap dicek kembali.</div>
    </section>
</main>
