<?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_service']) ? date('d-m-Y', strtotime($transaksi['tanggal_service'])) : date('d-m-Y');
$jam = ! empty($transaksi['created_at']) ? date('H:i:s', strtotime($transaksi['created_at'])) : date('H:i:s');
$totalDibayar = (float) ($totalDibayar ?? 0);
$totalService = (float) ($transaksi['total_service'] ?? 0);
$sisa = max(0, $totalService - $totalDibayar);
$rows = [];
foreach ($barangItems as $item) {
    $rows[] = [
        'nama' => (string) ($item['nama_barang'] ?? '-'),
        'harga' => (float) ($item['harga_jual'] ?? 0),
        'qty' => (int) ($item['jumlah'] ?? 0),
        'total' => (float) ($item['subtotal'] ?? 0),
    ];
}
foreach ($jasaItems as $item) {
    $rows[] = [
        'nama' => (string) ($item['nama_jasa'] ?? '-'),
        'harga' => (float) ($item['biaya'] ?? 0),
        'qty' => 1,
        'total' => (float) ($item['biaya'] ?? 0),
    ];
}
?>
<!doctype html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <title>Nota Service <?= 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">Service</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">Teknisi</td>
            <td class="info-value">: <?= esc($transaksi['nama_teknisi'] ?? '-') ?></td>
            <td class="info-label-right">Pelanggan</td>
            <td>: <?= esc($transaksi['nama_pelanggan'] ?? '-') ?></td>
        </tr>
        <tr>
            <td class="info-label">Objek</td>
            <td class="info-value">: <?= ! empty($transaksi['merk_tipe']) ? esc($transaksi['merk_tipe']) : '-' ?></td>
            <td class="info-label-right">Nomor</td>
            <td>: <?= ! empty($transaksi['nomor_objek']) ? esc($transaksi['nomor_objek']) : '-' ?></td>
        </tr>
    </table>

    <table class="items-table">
        <thead>
            <tr>
                <th style="width: 5mm;" class="text-center">No</th>
                <th style="width: 103mm;">Part / Jasa</th>
                <th style="width: 30mm;" class="text-center">Harga</th>
                <th style="width: 10mm;" class="text-center">Qty</th>
                <th style="width: 30mm;" class="text-center">Total</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($rows as $index => $row) : ?>
                <tr>
                    <td class="text-center"><?= esc((string) ($index + 1)) ?></td>
                    <td><?= esc($row['nama']) ?></td>
                    <td class="text-end"><?= esc(number_format($row['harga'], 0, ',', '.')) ?></td>
                    <td class="text-center"><?= esc((string) $row['qty']) ?></td>
                    <td class="text-end"><?= esc(number_format($row['total'], 0, ',', '.')) ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($rows)) : ?><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 Part</td><td class="total-separator">:</td><td class="total-amount"><?= esc(number_format((float) ($transaksi['subtotal_barang'] ?? 0), 0, ',', '.')) ?></td></tr>
        <tr><td class="total-spacer"></td><td class="total-label">Sub Jasa</td><td class="total-separator">:</td><td class="total-amount"><?= esc(number_format((float) ($transaksi['subtotal_jasa'] ?? 0), 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($totalService, 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['keterangan']) ? nl2br(esc($transaksi['keterangan'])) : 'Simpan nota ini sebagai bukti transaksi service.' ?>
    </div>
</body>
</html>
