<?php
$setting = function_exists('app_setting') ? app_setting() : [];
$appName = trim((string) ($setting['nama_perusahaan'] ?? $setting['nama_aplikasi'] ?? 'Nusa POS')) ?: 'Nusa POS';
$tagline = trim((string) ($setting['tagline'] ?? ''));
$tanggal = ! empty($transaksi['tanggal_penjualan']) ? date('d-m-Y', strtotime($transaksi['tanggal_penjualan'])) : date('d-m-Y');
$jam = ! empty($transaksi['created_at']) ? date('H:i:s', strtotime($transaksi['created_at'])) : date('H:i:s');
$subtotal = 0;
$totalJumlah = 0;
$totalDibayar = (float) ($totalDibayar ?? 0);
$totalPenjualan = (float) ($transaksi['total_penjualan'] ?? 0);
$sisa = max(0, $totalPenjualan - $totalDibayar);
?>
<!doctype html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <title>Nota <?= esc($transaksi['no_transaksi'] ?? '') ?></title>
    <style>
        @page {
            size: 210mm 140mm;
            margin: 5mm;
        }

        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            color: #111;
            font-family: "Courier New", Courier, monospace;
            font-size: 9px;
            line-height: 1.25;
        }

        .header {
            text-align: center;
        }

        .store-name {
            font-size: 10px;
            font-weight: 700;
            text-transform: uppercase;
        }

        .tagline {
            margin-top: 2px;
            font-size: 9px;
        }

        .line {
            border-top: 1px solid #111;
            margin: 4px 0 8px;
        }

        .info-table,
        .items-table,
        .total-table,
        .signature-table {
            width: 100%;
            border-collapse: collapse;
        }

        .info-table {
            margin-bottom: 6px;
        }

        .info-table td {
            padding: 1px 0;
            vertical-align: top;
        }

        .info-label {
            width: 30mm;
        }

        .info-value {
            width: 70mm;
        }

        .info-label-right {
            width: 25mm;
        }

        .items-table th,
        .items-table td {
            border: 1px solid #111;
            padding: 3px 4px;
            vertical-align: top;
        }

        .items-table th {
            font-weight: 700;
            text-align: left;
        }

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

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

        .total-table {
            margin-top: 6px;
        }

        .total-table td {
            padding: 1px 0;
        }

        .total-spacer {
            width: 120mm;
        }

        .total-label {
            width: 30mm;
        }

        .total-separator {
            width: 5mm;
            text-align: center;
        }

        .total-amount {
            width: 40mm;
            text-align: right;
        }

        .fw-bold {
            font-weight: 700;
        }

        .signature-table {
            margin-top: 12px;
        }

        .signature-table td {
            vertical-align: top;
            text-align: center;
        }

        .signature-left,
        .signature-right {
            width: 60mm;
        }

        .signature-gap {
            width: 78mm;
        }

        .signature-space {
            height: 16mm;
        }

        .footer {
            margin-top: 6px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="store-name"><?= esc($appName) ?></div>
        <?php if ($tagline !== '') : ?>
            <div class="tagline"><?= esc($tagline) ?></div>
        <?php endif; ?>
    </div>

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

    <table class="info-table">
        <tr>
            <td class="info-label">Invoice</td>
            <td class="info-value">: <?= esc($transaksi['no_transaksi'] ?? '-') ?></td>
            <td class="info-label-right">Tanggal</td>
            <td>: <?= esc($tanggal) ?> <?= esc($jam) ?></td>
        </tr>
        <tr>
            <td class="info-label">Kasir</td>
            <td class="info-value">: <?= esc($transaksi['nama_user'] ?? ($userName ?? '-')) ?></td>
            <td class="info-label-right">Pelanggan</td>
            <td>: <?= esc($transaksi['nama_pelanggan'] ?? '-') ?></td>
        </tr>
    </table>

    <table class="items-table">
        <thead>
            <tr>
                <th style="width: 5mm;" class="text-center">No</th>
                <th style="width: 103mm;">Nama Barang</th>
                <th style="width: 30mm;" class="text-center">Harga</th>
                <th style="width: 13mm;" class="text-center">Qty</th>
                <th style="width: 35mm;" class="text-center">Total</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($details as $index => $detail) : ?>
                <?php
                $jumlah = (int) ($detail['jumlah'] ?? 0);
                $hargaJual = (float) ($detail['harga_jual'] ?? 0);
                $itemSubtotal = (float) ($detail['subtotal'] ?? ($jumlah * $hargaJual));
                $subtotal += $itemSubtotal;
                $totalJumlah += $jumlah;
                ?>
                <tr>
                    <td class="text-center"><?= esc((string) ($index + 1)) ?></td>
                    <td><?= esc($detail['nama_barang'] ?? '-') ?></td>
                    <td class="text-end"><?= esc(number_format($hargaJual, 0, ',', '.')) ?></td>
                    <td class="text-center"><?= esc((string) $jumlah) ?></td>
                    <td class="text-end"><?= esc(number_format($itemSubtotal, 0, ',', '.')) ?></td>
                </tr>
            <?php endforeach; ?>

            <?php if (empty($details)) : ?>
                <tr>
                    <td colspan="5" class="text-center">Tidak ada item.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>

    <table class="total-table">
        <tr>
            <td class="total-spacer"></td>
            <td class="total-label">Sub Total</td>
            <td class="total-separator">:</td>
            <td class="total-amount"><?= esc(number_format((float) ($transaksi['subtotal'] ?? $subtotal), 0, ',', '.')) ?></td>
        </tr>
        <tr>
            <td class="total-spacer"></td>
            <td class="total-label">Pajak <?= esc((string) ((int) ($transaksi['pajak_persen'] ?? 0))) ?>%</td>
            <td class="total-separator">:</td>
            <td class="total-amount"><?= esc(number_format((float) ($transaksi['pajak_nominal'] ?? 0), 0, ',', '.')) ?></td>
        </tr>
        <tr class="fw-bold">
            <td class="total-spacer"></td>
            <td class="total-label">Total</td>
            <td class="total-separator">:</td>
            <td class="total-amount"><?= esc(number_format($totalPenjualan, 0, ',', '.')) ?></td>
        </tr>
        <tr>
            <td class="total-spacer"></td>
            <td class="total-label">Bayar</td>
            <td class="total-separator">:</td>
            <td class="total-amount"><?= esc(number_format($totalDibayar, 0, ',', '.')) ?></td>
        </tr>
        <tr>
            <td class="total-spacer"></td>
            <td class="total-label">Sisa</td>
            <td class="total-separator">:</td>
            <td class="total-amount"><?= esc(number_format($sisa, 0, ',', '.')) ?></td>
        </tr>
    </table>

    <table class="signature-table">
        <tr>
            <td class="signature-left">Pelanggan</td>
            <td class="signature-gap"></td>
            <td class="signature-right">Pemilik Usaha</td>
        </tr>
        <tr>
            <td class="signature-space"></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td>( <?= esc($transaksi['nama_pelanggan'] ?? '') ?> )</td>
            <td></td>
            <td>( <?= esc($appName) ?> )</td>
        </tr>
    </table>

    <div class="footer">
        <?= ! empty($transaksi['catatan']) ? nl2br(esc($transaksi['catatan'])) : 'Barang yang sudah dibeli harap dicek kembali.' ?>
    </div>
</body>
</html>
