<?php
$totalPartQty = 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">Pelanggan</span>
            <span class="value"><?= esc($transaksi['nama_pelanggan'] ?? '-') ?></span>
        </div>
        <div class="row">
            <span class="label">Teknisi</span>
            <span class="value"><?= esc($transaksi['nama_teknisi'] ?? '-') ?></span>
        </div>
        <div class="row">
            <span class="label">Objek</span>
            <span class="value"><?= ! empty($transaksi['merk_tipe']) ? esc($transaksi['merk_tipe']) : '-' ?></span>
        </div>
        <?php if (! empty($transaksi['nomor_objek'])) : ?>
            <div class="row">
                <span class="label">Nomor</span>
                <span class="value"><?= esc($transaksi['nomor_objek']) ?></span>
            </div>
        <?php endif; ?>
    </section>

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

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

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

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

    <section>
        <div class="section-title">JASA</div>
        <?php foreach ($jasaItems as $item) : ?>
            <?php $biaya = (float) ($item['biaya'] ?? 0); ?>
            <div class="item">
                <div class="item-calc no-indent">
                    <span><?= esc($item['nama_jasa'] ?? '-') ?></span>
                    <span><?= esc(number_format($biaya, 0, ',', '.')) ?></span>
                </div>
            </div>
        <?php endforeach; ?>

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

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

    <section class="totals">
        <div class="row">
            <span>Jumlah Part</span>
            <span><?= esc((string) $totalPartQty) ?></span>
        </div>
        <div class="row">
            <span>Subtotal Part</span>
            <span><?= esc(number_format((float) ($transaksi['subtotal_barang'] ?? 0), 0, ',', '.')) ?></span>
        </div>
        <div class="row">
            <span>Subtotal Jasa</span>
            <span><?= esc(number_format((float) ($transaksi['subtotal_jasa'] ?? 0), 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($totalService, 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['keterangan'])) : ?>
        <div class="line"></div>
        <section>
            <div class="muted">Keluhan:</div>
            <div><?= nl2br(esc($transaksi['keterangan'])) ?></div>
        </section>
    <?php endif; ?>

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

    <section class="footer">
        <div>Terima kasih</div>
        <div class="muted">Simpan nota ini sebagai bukti transaksi service.</div>
    </section>
</main>
