<!doctype html>
<html lang="id">
<?php
$setting = function_exists('app_setting') ? app_setting() : [];
$companyName = trim((string) ($setting['nama_perusahaan'] ?? ''));

if ($companyName === '') {
    $companyName = trim((string) ($setting['nama_aplikasi'] ?? ''));
}
?>
<head>
    <meta charset="utf-8">
    <title>Pembelian <?= esc($transaksi['no_transaksi']) ?></title>
    <style>
        @page {
            margin: 14mm;
        }

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

        .header {
            padding-bottom: 10px;
            border-bottom: 2px solid #111827;
            text-align: center;
        }

        .title {
            font-size: 18px;
            font-weight: 700;
            letter-spacing: .5px;
        }

        .subtitle {
            margin-top: 2px;
            color: #4b5563;
            font-size: 11px;
        }

        .meta {
            width: 100%;
            margin-top: 16px;
            border-collapse: collapse;
        }

        .meta-cell {
            width: 50%;
            padding: 0 8px 0 0;
            vertical-align: top;
        }

        .box {
            min-height: 82px;
            padding: 10px;
            border: 1px solid #d1d5db;
        }

        .box-title {
            margin-bottom: 6px;
            color: #374151;
            font-size: 10px;
            font-weight: 700;
            text-transform: uppercase;
        }

        .info-table {
            width: 100%;
            border-collapse: collapse;
        }

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

        .label {
            width: 92px;
            color: #6b7280;
        }

        .items {
            width: 100%;
            margin-top: 16px;
            border-collapse: collapse;
        }

        .items th,
        .items td {
            padding: 7px 8px;
            border: 1px solid #d1d5db;
        }

        .items th {
            background: #f3f4f6;
            font-weight: 700;
            text-align: left;
        }

        .text-right {
            text-align: right !important;
        }

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

        .item-code {
            display: block;
            font-weight: 700;
        }

        .item-name {
            display: block;
            margin-top: 2px;
        }

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

        .signatures {
            width: 100%;
            margin-top: 30px;
            border-collapse: collapse;
        }

        .signatures td {
            width: 50%;
            padding: 0 24px;
            text-align: center;
            vertical-align: top;
        }

        .signature-space {
            height: 58px;
        }

        .signature-name {
            display: inline-block;
            min-width: 170px;
            padding-top: 4px;
            border-top: 1px solid #111827;
            font-weight: 700;
        }

        .small {
            color: #6b7280;
            font-size: 10px;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="title">BUKTI PEMBELIAN</div>
        <div class="subtitle">Dokumen pembelian barang dari supplier</div>
    </div>

    <table class="meta">
        <tr>
            <td class="meta-cell">
                <div class="box">
                    <div class="box-title">Informasi Transaksi</div>
                    <table class="info-table">
                        <tr>
                            <td class="label">No. Transaksi</td>
                            <td>: <?= esc($transaksi['no_transaksi']) ?></td>
                        </tr>
                        <tr>
                            <td class="label">Tanggal</td>
                            <td>: <?= esc(date('d/m/Y', strtotime($transaksi['tanggal_pembelian']))) ?></td>
                        </tr>
                        <tr>
                            <td class="label">Dokumen/Nota</td>
                            <td>: <?= ! empty($transaksi['nomor_dokumen']) ? esc($transaksi['nomor_dokumen']) : '-' ?></td>
                        </tr>
                        <tr>
                            <td class="label">User Input</td>
                            <td>: <?= esc($transaksi['nama_user'] ?? '-') ?></td>
                        </tr>
                    </table>
                </div>
            </td>
            <td class="meta-cell">
                <div class="box">
                    <div class="box-title">Data Supplier</div>
                    <table class="info-table">
                        <tr>
                            <td class="label">Nama</td>
                            <td>: <?= esc($transaksi['nama_suplier'] ?? '-') ?></td>
                        </tr>
                        <tr>
                            <td class="label">Perusahaan</td>
                            <td>: <?= esc($transaksi['perusahaan_suplier'] ?? '-') ?></td>
                        </tr>
                        <tr>
                            <td class="label">No. HP</td>
                            <td>: <?= esc($transaksi['no_hp_suplier'] ?? '-') ?></td>
                        </tr>
                        <tr>
                            <td class="label">Email</td>
                            <td>: <?= esc($transaksi['email_suplier'] ?? '-') ?></td>
                        </tr>
                    </table>
                </div>
            </td>
        </tr>
    </table>

    <table class="items">
        <thead>
            <tr>
                <th style="width: 34px;" class="text-center">No</th>
                <th>Barang</th>
                <th style="width: 92px;text-align:center" class="text-center">Jumlah</th>
                <th style="width: 90px;" class="text-right">Harga Beli</th>
                <th style="width: 96px;" class="text-right">Subtotal</th>
            </tr>
        </thead>
        <tbody>
            <?php $totalJumlah = 0; $totalHarga = 0; ?>
            <?php foreach ($details as $index => $detail) : ?>
                <?php
                $jumlah = (int) $detail['jumlah'];
                $hargaBeli = (float) ($detail['harga_beli'] ?? 0);
                $subtotal = (float) ($detail['subtotal'] ?? ($jumlah * $hargaBeli));
                $totalJumlah += $jumlah;
                $totalHarga += $subtotal;
                ?>
                <tr>
                    <td class="text-center"><?= esc((string) ($index + 1)) ?></td>
                    <td>
                        <span class="item-code"><?= esc($detail['kode_barang']) ?></span>
                        <span class="item-name"><?= esc($detail['nama_barang']) ?></span>
                    </td>
                    <td class="text-center"><?= esc((string) $jumlah) ?> <?= esc($detail['satuan']) ?></td>
                    <td class="text-right">Rp <?= esc(number_format($hargaBeli, 0, ',', '.')) ?></td>
                    <td class="text-right">Rp <?= esc(number_format($subtotal, 0, ',', '.')) ?></td>
                </tr>
            <?php endforeach; ?>
            <?php
            $pajakPersen = (int) ($transaksi['pajak_persen'] ?? 0);
            $pajakNominal = (float) ($transaksi['pajak_nominal'] ?? round($totalHarga * $pajakPersen / 100));
            $totalPembelian = (float) ($transaksi['total_pembelian'] ?? ($totalHarga + $pajakNominal));
            ?>
            <tr>
                <th colspan="2" class="text-right">Subtotal</th>
                <th class="text-center"><?= esc((string) $totalJumlah) ?></th>
                <th></th>
                <th class="text-right">Rp <?= esc(number_format($totalHarga, 0, ',', '.')) ?></th>
            </tr>
            <tr>
                <th colspan="4" class="text-right">Pajak (<?= esc((string) $pajakPersen) ?>%)</th>
                <th class="text-right">Rp <?= esc(number_format($pajakNominal, 0, ',', '.')) ?></th>
            </tr>
            <tr>
                <th colspan="4" class="text-right">Total Pembelian</th>
                <th class="text-right">Rp <?= esc(number_format($totalPembelian, 0, ',', '.')) ?></th>
            </tr>
        </tbody>
    </table>

    <div class="note">
        <div class="box-title">Catatan</div>
        <?= ! empty($transaksi['catatan']) ? nl2br(esc($transaksi['catatan'])) : '-' ?>
    </div>

    <table class="signatures">
        <tr>
            <td>
                <div>Supplier</div>
                <div class="signature-space"></div>
                <div class="signature-name"><?= esc($transaksi['nama_suplier'] ?? 'Supplier') ?></div>
                <div class="small"><?= esc($transaksi['perusahaan_suplier'] ?? '') ?></div>
            </td>
            <td>
                <div>Penerima</div>
                <div class="signature-space"></div>
                <div class="signature-name"><?= esc($transaksi['nama_user'] ?? 'User') ?></div>
                <div class="small"><?= esc($companyName !== '' ? $companyName : 'Penerima') ?></div>
            </td>
        </tr>
    </table>
</body>
</html>
