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



$topbarAction .= '</div>';
?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'penjualan',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Detail Penjualan',
    '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 Penjualan</div>
                    <div class="fw-semibold"><?= esc(date('d/m/Y', strtotime($transaksi['tanggal_penjualan']))) ?></div>
                </div>

                <div class="mb-3">
                    <div class="small text-secondary">Pelanggan</div>
                    <div class="fw-semibold"><?= esc($transaksi['nama_pelanggan'] ?? '-') ?></div>
                    <?php if (! empty($transaksi['no_hp_pelanggan'])) : ?>
                        <div class="small text-secondary"><?= esc($transaksi['no_hp_pelanggan']) ?></div>
                    <?php endif; ?>
                </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'] ?? 'piutang') === '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' : 'Piutang' ?></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 Piutang</div>
                    <div class="fw-semibold">Rp <?= esc(number_format(max(0, (float) ($transaksi['total_penjualan'] ?? 0) - (float) ($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 Penjualan</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 Jual</th>
                                <?php if ($isAdministrator) : ?>
                                    <th class="text-end">Harga Modal</th>
                                <?php endif; ?>
                                <th class="text-end">Subtotal</th>
                                <?php if ($isAdministrator) : ?>
                                    <th class="text-end">Total Modal</th>
                                    <th class="text-end">Laba</th>
                                <?php endif; ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $totalJumlah = 0; $subtotal = 0; $totalModal = 0; $totalLaba = 0; ?>
                            <?php foreach ($details as $detail) : ?>
                                <?php
                                $jumlah = (int) $detail['jumlah'];
                                $hargaJual = (float) ($detail['harga_jual'] ?? 0);
                                $hargaBeli = (float) ($detail['harga_beli'] ?? 0);
                                $itemSubtotal = (float) ($detail['subtotal'] ?? ($jumlah * $hargaJual));
                                $itemModal = (float) ($detail['total_modal'] ?? ($jumlah * $hargaBeli));
                                $itemLaba = $itemSubtotal - $itemModal;
                                $totalJumlah += $jumlah;
                                $subtotal += $itemSubtotal;
                                $totalModal += $itemModal;
                                $totalLaba += $itemLaba;
                                ?>
                                <tr>
                                    <td><span class="fw-semibold d-block small"><?= 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($hargaJual, 0, ',', '.')) ?></td>
                                    <?php if ($isAdministrator) : ?>
                                        <td class="text-end">Rp <?= esc(number_format($hargaBeli, 0, ',', '.')) ?></td>
                                    <?php endif; ?>
                                    <td class="text-end">Rp <?= esc(number_format($itemSubtotal, 0, ',', '.')) ?></td>
                                    <?php if ($isAdministrator) : ?>
                                        <td class="text-end">Rp <?= esc(number_format($itemModal, 0, ',', '.')) ?></td>
                                        <td class="text-end fw-semibold <?= $itemLaba < 0 ? 'text-danger' : 'text-success' ?>">Rp <?= esc(number_format($itemLaba, 0, ',', '.')) ?></td>
                                    <?php endif; ?>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (empty($details)) : ?>
                                <tr>
                                    <td colspan="<?= $isAdministrator ? '7' : '4' ?>" class="text-center text-secondary py-4">Belum ada detail barang.</td>
                                </tr>
                            <?php else : ?>
                                <tr>
                                    <th class="text-end">Subtotal</th>
                                    <th class="text-end"><?= esc((string) $totalJumlah) ?></th>
                                    <th></th>
                                    <?php if ($isAdministrator) : ?>
                                        <th></th>
                                    <?php endif; ?>
                                    <th class="text-end">Rp <?= esc(number_format((float) ($transaksi['subtotal'] ?? $subtotal), 0, ',', '.')) ?></th>
                                    <?php if ($isAdministrator) : ?>
                                        <th class="text-end">Rp <?= esc(number_format($totalModal, 0, ',', '.')) ?></th>
                                        <th class="text-end <?= $totalLaba < 0 ? 'text-danger' : 'text-success' ?>">Rp <?= esc(number_format($totalLaba, 0, ',', '.')) ?></th>
                                    <?php endif; ?>
                                </tr>
                                <tr>
                                    <th colspan="<?= $isAdministrator ? '4' : '3' ?>" class="text-end">Pajak <?= esc((string) ((int) ($transaksi['pajak_persen'] ?? 0))) ?>%</th>
                                    <th class="text-end">Rp <?= esc(number_format((float) ($transaksi['pajak_nominal'] ?? 0), 0, ',', '.')) ?></th>
                                    <?php if ($isAdministrator) : ?>
                                        <th></th>
                                        <th></th>
                                    <?php endif; ?>
                                </tr>
                                <tr>
                                    <th colspan="<?= $isAdministrator ? '4' : '3' ?>" class="text-end">Total Penjualan</th>
                                    <th class="text-end">Rp <?= esc(number_format((float) ($transaksi['total_penjualan'] ?? 0), 0, ',', '.')) ?></th>
                                    <?php if ($isAdministrator) : ?>
                                        <th></th>
                                        <th></th>
                                    <?php endif; ?>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>

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