<?= view('admin/layouts/header', ['title' => 'Detail Pembelian']) ?>
<?php
$isAdministrator = ($userRole ?? '') === 'administrator';
$topbarAction = '<div class="d-flex flex-wrap gap-2 justify-content-end">'
    . '<a href="' . site_url('admin/pembelian') . '" class="btn btn-outline-dark"><i class="bi bi-arrow-left me-1"></i>Kembali</a>';



$topbarAction .= '</div>';
?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'pembelian',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Detail Pembelian',
    'pageSubtitle' => $transaksi['no_transaksi'],
    'topbarAction' => $topbarAction,
    'userName'     => $userName,
    'userRole'     => $userRole,
]) ?>

<section class="content-area">
    <div class="row g-4">
        <div class="col-12 col-xl-4">
            <div class="panel p-4 h-100">
                <div class="fw-bold mb-3">Informasi Transaksi</div>

                <div class="mb-3">
                    <div class="small text-secondary">No. Transaksi</div>
                    <div class="fw-semibold"><?= esc($transaksi['no_transaksi']) ?></div>
                </div>

                <div class="mb-3">
                    <div class="small text-secondary">Tanggal Pembelian</div>
                    <div class="fw-semibold"><?= esc(date('d/m/Y', strtotime($transaksi['tanggal_pembelian']))) ?></div>
                </div>

                <div class="mb-3">
                    <div class="small text-secondary">Supplier</div>
                    <div class="fw-semibold"><?= esc($transaksi['nama_suplier'] ?? '-') ?></div>
                    <?php if (! empty($transaksi['perusahaan_suplier'])) : ?>
                        <div class="small text-secondary"><?= esc($transaksi['perusahaan_suplier']) ?></div>
                    <?php endif; ?>
                    <?php if (! empty($transaksi['no_hp_suplier']) || ! empty($transaksi['email_suplier'])) : ?>
                        <div class="small text-secondary">
                            <?= esc(trim(($transaksi['no_hp_suplier'] ?? '') . ' ' . ($transaksi['email_suplier'] ?? ''))) ?>
                        </div>
                    <?php endif; ?>
                </div>

                <div class="mb-3">
                    <div class="small text-secondary">Nomor Dokumen/Nota</div>
                    <div class="fw-semibold"><?= ! empty($transaksi['nomor_dokumen']) ? esc($transaksi['nomor_dokumen']) : '-' ?></div>
                </div>

                <div class="mb-3">
                    <div class="small text-secondary">User Input</div>
                    <div class="fw-semibold"><?= esc($transaksi['nama_user'] ?? '-') ?></div>
                </div>

                <div>
                    <div class="small text-secondary">Catatan</div>
                    <div><?= ! empty($transaksi['catatan']) ? nl2br(esc($transaksi['catatan'])) : '-' ?></div>
                </div>

                <hr>

                <?php $isLunas = ($transaksi['status_pembayaran'] ?? 'hutang') === 'lunas'; ?>
                <div class="mb-3">
                    <div class="small text-secondary">Status Pembayaran</div>
                    <span class="badge <?= $isLunas ? 'text-bg-success' : 'text-bg-warning' ?>"><?= $isLunas ? 'Lunas' : 'Hutang' ?></span>
                </div>

                <div>
                    <div class="small text-secondary">Total Dibayar</div>
                    <div class="fw-semibold">Rp <?= esc(number_format((float) ($totalDibayar ?? 0), 0, ',', '.')) ?></div>
                </div>

                <div>
                    <div class="small text-secondary">Total Hutang</div>
                    <div class="fw-semibold">Rp <?= esc(number_format((float) ($transaksi['total_pembelian'] - $totalDibayar ?? 0), 0, ',', '.')) ?></div>
                </div>
            </div>
        </div>

        <div class="col-12 col-xl-8">
            <div class="panel p-4 h-100">
                <div class="d-flex align-items-center justify-content-between gap-3 mb-3">
                    <div class="fw-bold">Daftar Item Pembelian</div>
                    <div class="badge text-bg-light border"><?= esc((string) count($details)) ?> item</div>
                </div>

                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead>
                            <tr>
                                <th>Barang</th>
                                <th class="text-end">Jumlah</th>
                                <th class="text-end">Harga Beli</th>
                                <th class="text-end">Subtotal</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $totalJumlah = 0; $totalHarga = 0; ?>
                            <?php foreach ($details as $detail) : ?>
                                <?php
                                $jumlah = (int) $detail['jumlah'];
                                $hargaBeli = (float) ($detail['harga_beli'] ?? 0);
                                $subtotal = (float) ($detail['subtotal'] ?? ($jumlah * $hargaBeli));
                                $totalJumlah += $jumlah;
                                $totalHarga += $subtotal;
                                ?>
                                <tr>                                    
                                    <td><span class="d-block fw-semibold"><?= esc($detail['kode_barang']) ?></span><?= esc($detail['nama_barang']) ?></td>                                    
                                    <td class="text-end"><?= esc((string) $jumlah) ?> <?= esc($detail['satuan']) ?></td>
                                    <td class="text-end">Rp <?= esc(number_format($hargaBeli, 0, ',', '.')) ?></td>
                                    <td class="text-end">Rp <?= esc(number_format($subtotal, 0, ',', '.')) ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (empty($details)) : ?>
                                <tr>
                                    <td colspan="6" class="text-center text-secondary py-4">Belum ada detail barang.</td>
                                </tr>
                            <?php else : ?>
                                <?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="1" class="text-end">Subtotal</th>
                                    <th class="text-end"><?= esc((string) $totalJumlah) ?></th>
                                    <th></th>
                                    <th class="text-end">Rp <?= esc(number_format($totalHarga, 0, ',', '.')) ?></th>
                                </tr>
                                <tr>
                                    <th colspan="3" class="text-end">Pajak (<?= esc((string) $pajakPersen) ?>%)</th>
                                    <th class="text-end">Rp <?= esc(number_format($pajakNominal, 0, ',', '.')) ?></th>
                                </tr>
                                <tr>
                                    <th colspan="3" class="text-end">Total Pembelian</th>
                                    <th class="text-end">Rp <?= esc(number_format($totalPembelian, 0, ',', '.')) ?></th>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>

<?= view('admin/layouts/footer') ?>
