<?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', strtotime($transaksi['created_at'])) : date('H:i');
$totalDibayar = (float) ($totalDibayar ?? 0);
$totalService = (float) ($transaksi['total_service'] ?? 0);
$sisa = max(0, $totalService - $totalDibayar);
?>
<!doctype html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <title>Nota Service <?= 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; }
        .header-table, .info-table, .items-table, .summary-table { width: 100%; border-collapse: collapse; }
        .header-table td, .info-table td, .items-table td { vertical-align: top; }
        .store-name { font-size: 20px; font-weight: 700; text-transform: uppercase; }
        .tagline, .muted { color: #6b7280; }
        .title { text-align: right; font-size: 18px; font-weight: 700; text-transform: uppercase; }
        .info-table { margin-bottom: 14px; }
        .info-table td { padding: 2px 0; }
        .info-table .label { width: 105px; color: #4b5563; }
        .section-title { margin: 14px 0 6px; font-weight: 700; }
        .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; }
        .text-center { text-align: center; }
        .text-end { text-align: right; }
        .summary { width: 280px; 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 Service</td>
            </tr>
        </table>
    </div>

    <table class="info-table">
        <tr>
            <td class="label">No. Service</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">Teknisi</td>
            <td>: <?= esc($transaksi['nama_teknisi'] ?? '-') ?></td>
        </tr>
        <tr>
            <td class="label">Objek</td>
            <td>: <?= ! empty($transaksi['merk_tipe']) ? esc($transaksi['merk_tipe']) : '-' ?></td>
            <td class="label">Nomor</td>
            <td>: <?= ! empty($transaksi['nomor_objek']) ? esc($transaksi['nomor_objek']) : '-' ?></td>
        </tr>
    </table>

    <div class="section-title">Part/Barang Diganti</div>
    <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 ($barangItems as $index => $item) : ?>
                <tr>
                    <td><?= esc((string) ($index + 1)) ?></td>
                    <td><strong><?= esc($item['nama_barang'] ?? '-') ?></strong><br><span class="muted"><?= esc($item['kode_barang'] ?? '-') ?></span></td>
                    <td class="text-center"><?= esc((string) ($item['jumlah'] ?? 0)) ?> <?= esc($item['satuan'] ?? '') ?></td>
                    <td class="text-end"><?= esc(number_format((float) ($item['harga_jual'] ?? 0), 0, ',', '.')) ?></td>
                    <td class="text-end"><?= esc(number_format((float) ($item['subtotal'] ?? 0), 0, ',', '.')) ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($barangItems)) : ?><tr><td colspan="5" class="text-center muted">Tidak ada part.</td></tr><?php endif; ?>
        </tbody>
    </table>

    <div class="section-title">Jasa Service</div>
    <table class="items-table">
        <thead>
            <tr>
                <th style="width: 32px;">No</th>
                <th>Jasa</th>
                <th style="width: 115px;" class="text-end">Biaya</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($jasaItems as $index => $item) : ?>
                <tr>
                    <td><?= esc((string) ($index + 1)) ?></td>
                    <td><?= esc($item['nama_jasa'] ?? '-') ?></td>
                    <td class="text-end"><?= esc(number_format((float) ($item['biaya'] ?? 0), 0, ',', '.')) ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($jasaItems)) : ?><tr><td colspan="3" class="text-center muted">Tidak ada jasa.</td></tr><?php endif; ?>
        </tbody>
    </table>

    <div class="summary">
        <table class="summary-table">
            <tr><td>Subtotal Part</td><td class="amount"><?= esc(number_format((float) ($transaksi['subtotal_barang'] ?? 0), 0, ',', '.')) ?></td></tr>
            <tr><td>Subtotal Jasa</td><td class="amount"><?= esc(number_format((float) ($transaksi['subtotal_jasa'] ?? 0), 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($totalService, 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['keterangan'])) : ?>
        <div class="note"><strong>Keluhan</strong><br><?= nl2br(esc($transaksi['keterangan'])) ?></div>
    <?php endif; ?>

    <div class="footer">Terima kasih. Simpan nota ini sebagai bukti transaksi service.</div>
</body>
</html>
