<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Detail Pembelian Supplier: <?= $no_nota_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">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        th, td { white-space: nowrap; font-size: 13px; }
        .bg-custom-header { background-color: #4e73df; color: white; }
    </style>
</head>
<body class="bg-light p-4">

    <div class="card shadow mb-4">
        <div class="card-header bg-custom-header d-flex justify-content-between align-items-center">
            <div>
                <h5 class="mb-0 font-weight-bold">Detail Pembelian Supplier</h5>
                <small>No Nota: <?= $no_nota_title ?> | Periode: <?= $tgl_periode_title ?></small>
            </div>
            
            <?php 
                // Mengambil parameter POST sebelumnya untuk link download
                // Karena ini view baru, parameter harus diteruskan manual atau disimpan di session/hidden input
                // Disini saya asumsikan parameter dikirim via GET untuk download
            ?>
            <a href="<?= base_url('farmasipemblianspl/unduhdatadetail?TANGGAL_AWAL='.$filter_tgl_awal.'&TANGGAL_AKHIR='.$filter_tgl_akhir.'&KD_SUPLIER='.$filter_kd_suplier.'&NO_NOTA='.$no_nota_title) ?>" class="btn btn-success btn-sm shadow-sm">
                <i class="fas fa-file-excel mr-1"></i> Download Excel
            </a>
        </div>
        
        <div class="card-body">
            <div class="table-responsive">
                <table id="tblDetailSupplier" class="table table-bordered table-striped w-100">
                    <thead class="thead-dark">
                        <tr>
                            <th>Tgl Masuk</th>
                            <th>Nama Supplier</th>
                            <th>Kode Pabrik</th>
                            <th>Nama Pabrik</th>
                            <th>Kode Produksi</th>
                            <th>No Nota</th>
                            <th>Nama Obat</th>
                            <th>Qty</th>
                            <th>Sub Total</th>
                            <th>Disc (%)</th>
                            <th>Nilai Disc</th>
                            <th>Pajak</th>
                            <th>Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(!empty($detail_pembelian)): ?>
                            <?php foreach($detail_pembelian as $item): ?>
                            <tr>
                                <td><?= $item['tgl_masuk'] ?></td>
                                <td><?= $item['nm_suplier'] ?></td>
                                <td><?= $item['kd_pabrik'] ?></td>
                                <td><?= $item['nm_pabrik'] ?></td>
                                <td><?= $item['kode_produksi'] ?></td>
                                <td class="font-weight-bold text-primary"><?= $item['no_nota'] ?></td>
                                <td><?= $item['nama_obat'] ?></td>
                                
                                <td class="text-center font-weight-bold"><?= $item['qty'] ?></td>
                                
                                <td class="text-right"><?= number_format((float)$item['sub_total'], 0, ',', '.') ?></td>
                                <td class="text-center"><?= $item['prosen_disc'] ?>%</td>
                                <td class="text-right"><?= number_format((float)$item['nilai_disc'], 0, ',', '.') ?></td>
                                <td class="text-right"><?= number_format((float)$item['pajak'], 0, ',', '.') ?></td>
                                <td class="text-right font-weight-bold"><?= number_format((float)$item['total'], 0, ',', '.') ?></td>
                            </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr><td colspan="13" class="text-center text-muted font-italic">Tidak ada data detail ditemukan untuk filter ini.</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.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.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() {
            $('#tblDetailSupplier').DataTable({
                "pageLength": 10,
                "scrollX": true,
                "language": {
                    "url": "//cdn.datatables.net/plug-ins/1.10.24/i18n/Indonesian.json"
                },
                "order": [[ 0, "asc" ]] // Urutkan berdasarkan Tgl Masuk secara default
            });
        });
    </script>
</body>
</html>