<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Detail PO: <?= $kd_po_title ?></title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <link href="https://cdn.datatables.net/1.13.4/css/dataTables.bootstrap4.min.css" rel="stylesheet">
    <style>th, td { white-space: nowrap; font-size: 13px; }</style>
</head>
<body class="bg-light p-4">

    <div class="card">
        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
            <h5 class="mb-0">Detail PO Obat: <?= $kd_po_title ?></h5>
            <a href="<?= base_url('POfarmasidetail/unduhdatadtlPO?KD_PO='.$kd_po_title) ?>" class="btn btn-success btn-sm">
                <i class="fas fa-file-excel"></i> Download Excel
            </a>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table id="tblDetail" class="table table-bordered table-striped w-100">
                    <thead class="bg-info text-white">
                        <tr>
                            <th>Kode PO</th>
                            <th>Kode Obat</th>
                            <th>Nama Barang</th>
                            <th>Expired Date</th>
                            <th>Nama Suplier</th>
                            <th>Qty PO</th>
                            <th>Harga PO</th>
                            <th>Subtotal PO</th>
                            <th>Diskon PO</th>
                            <th>Total PO</th>
                            <th>Tgl PO</th>
                            <th>Qty Beli</th>
                            <th>Harga Beli</th>
                            <th>Subtotal Beli</th>
                            <th>Diskon Beli</th>
                            <th>Total Beli</th>
                            <th>Tgl Beli</th>
                            <th>No Nota</th>
                            <th>Selisih Qty</th>
                            <th>% Pemenuhan</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(!empty($detail_po)): ?>
                            <?php foreach($detail_po as $item): 
                                // Handling null values di PHP View
                                $qtyBeli    = $item['qty_pembelian'] ?? 0;
                                $hargaBeli  = $item['harga_pembelian'] ?? 0;
                                $subBeli    = $item['subtotal_pembelian'] ?? 0;
                                $discBeli   = $item['diskon_pembelian'] ?? 0;
                                $totalBeli  = $item['total_pembelian'] ?? 0;
                                $tglBeli    = $item['tgl_beli'] ?? '-';
                                $noNota     = $item['no_nota'] ?? '-';
                            ?>
                            <tr>
                                <td><?= $item['kd_po'] ?></td>
                                <td><?= $item['kd_obat'] ?></td>
                                <td><?= $item['nm_barang'] ?></td>
                                <td><?= $item['expired_date'] ?? '-' ?></td>
                                <td><?= $item['nm_suplier'] ?></td>
                                <td class="text-center font-weight-bold"><?= $item['qty_po'] ?></td>
                                <td class="text-right"><?= number_format($item['harga_po'], 0, ',', '.') ?></td>
                                <td class="text-right"><?= number_format($item['subtotal_po'], 0, ',', '.') ?></td>
                                <td class="text-right"><?= number_format($item['diskon_po'], 0, ',', '.') ?></td>
                                <td class="text-right"><?= number_format($item['total_po'], 0, ',', '.') ?></td>
                                <td><?= $item['tgl_po'] ?></td>
                                
                                <td class="text-center font-weight-bold text-primary"><?= $qtyBeli ?></td>
                                <td class="text-right"><?= number_format($hargaBeli, 0, ',', '.') ?></td>
                                <td class="text-right"><?= number_format($subBeli, 0, ',', '.') ?></td>
                                <td class="text-right"><?= number_format($discBeli, 0, ',', '.') ?></td>
                                <td class="text-right"><?= number_format($totalBeli, 0, ',', '.') ?></td>
                                <td><?= $tglBeli ?></td>
                                <td><?= $noNota ?></td>
                                
                                <td class="text-center text-danger font-weight-bold"><?= $item['selisih_qty'] ?></td>
                                <td class="text-center"><?= $item['persentase_pemenuhan'] ?></td>
                            </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr><td colspan="20" class="text-center">Tidak ada data detail ditemukan.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.4/js/dataTables.bootstrap4.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#tblDetail').DataTable({
                "pageLength": 10,
                "scrollX": true
            });
        });
    </script>
</body>
</html>