<?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', strtotime($transaksi['created_at'])) : date('H:i');
$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: A4 portrait;
            margin: 14mm;
        }

        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            color: #111827;
            font-family: DejaVu Sans, Arial, sans-serif;
            font-size: 11px;
            line-height: 1.45;
        }

        .header {
            border-bottom: 2px solid #111827;
            padding-bottom: 12px;
            margin-bottom: 16px;
        }

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

        .tagline {
            color: #4b5563;
            margin-top: 2px;
        }

        .title {
            text-align: right;
            font-size: 18px;
            font-weight: 700;
            text-transform: uppercase;
        }

        .header-table,
        .info-table,
        .summary-table,
        .items-table {
            width: 100%;
            border-collapse: collapse;
        }

        .header-table td {
            vertical-align: top;
        }

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

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

        .info-table .label {
            width: 105px;
            color: #4b5563;
        }

        .items-table th {
            background: #111827;
            color: #fff;
            font-weight: 700;
            padding: 7px 8px;
            text-align: left;
        }

        .items-table td {
            border-bottom: 1px solid #e5e7eb;
            padding: 7px 8px;
            vertical-align: top;
        }

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

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

        .muted {
            color: #6b7280;
        }

        .summary {
            width: 270px;
            margin-left: auto;
            margin-top: 14px;
        }

        .summary-table td {
            padding: 4px 0;
        }

        .summary-table .amount {
            text-align: right;
            font-weight: 600;
        }

        .summary-table .total td {
            border-top: 2px solid #111827;
            padding-top: 7px;
            font-size: 13px;
            font-weight: 700;
        }

        .note {
            margin-top: 18px;
            padding-top: 10px;
            border-top: 1px solid #e5e7eb;
        }

        .footer {
            margin-top: 28px;
            text-align: center;
            color: #4b5563;
        }
    </style>
</head>
<body>
    <div class="header">
        <table class="header-table">
            <tr>
                <td>
                    <div class="store-name"><?= esc($appName) ?></div>
                    <?php if ($tagline !== '') : ?>
                        <div class="tagline"><?= esc($tagline) ?></div>
                    <?php endif; ?>
                </td>
                <td class="title">Nota Penjualan</td>
            </tr>
        </table>
    </div>

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

    <table class="items-table">
        <thead>
            <tr>
                <th style="width: 32px;">No</th>
                <th>Barang</th>
                <th style="width: 80px;" class="text-center">Jumlah</th>
                <th style="width: 105px;" class="text-end">Harga</th>
                <th style="width: 115px;" class="text-end">Subtotal</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><?= esc((string) ($index + 1)) ?></td>
                    <td>
                        <strong><?= esc($detail['nama_barang'] ?? '-') ?></strong><br>
                        <span class="muted"><?= esc($detail['kode_barang'] ?? '-') ?></span>
                    </td>
                    <td class="text-center"><?= esc((string) $jumlah) ?> <?= esc($detail['satuan'] ?? '') ?></td>
                    <td class="text-end"><?= esc(number_format($hargaJual, 0, ',', '.')) ?></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 muted">Tidak ada item.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>

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

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

    <div class="footer">
        Terima kasih. Barang yang sudah dibeli harap dicek kembali.
    </div>
</body>
</html>
