<style>
    /* ========================================================
       1. CSS CORE RESPONSIVE (ANTI HALAMAN JEBOL)
       ======================================================== */
    body, html {
        overflow-x: hidden !important;
        width: 100%;
        max-width: 100vw;
    }

    /* Mencegah SB Admin memberikan margin kosong di HP */
    @media (max-width: 991.98px) {
        #layoutSidenav_content {
            padding-left: 0 !important;
            margin-left: 0 !important;
            width: 100% !important;
            max-width: 100vw !important;
        }
        .container-fluid {
            padding-left: 10px !important;
            padding-right: 10px !important;
        }
    }

    #layoutSidenav_content {
        width: 100%;
        max-width: 100vw;
        overflow-x: hidden !important;
    }

    /* Memaksa Tabel dan Card agar menyesuaikan layar */
    table, table.dataTable {
        min-width: 0 !important;
        width: 100% !important;
        border-collapse: collapse !important;
    }

    .card {
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
        overflow-x: hidden; 
    }

    /* SCROLL HORIZONTAL CSS (STABIL DI ANDROID) */
    .table-responsive {
        width: 100% !important;
        max-width: 100% !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        border: none;
    }

    /* ========================================================
       2. STYLING TAB / PILLS
       ======================================================== */
    .nav-pills .nav-link {
        border-radius: 50px;
        padding: 8px 16px;
        font-weight: 500;
        margin-right: 10px;
        margin-bottom: 10px;
        border: 1px solid #e3e6f0;
        color: #5a5c69;
        background-color: #fff;
        white-space: nowrap; /* Mencegah teks tab terlipat ke bawah */
        cursor: pointer;
    }
    
    .nav-pills .nav-link.active, .nav-pills .nav-link:hover {
        background-color: #0d6efd;
        color: #fff;
        border-color: #0d6efd;
    }

    /* Agar deretan Tab bisa di-scroll menyamping di HP */
    .nav-pills {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 5px;
    }
    .nav-pills::-webkit-scrollbar {
        height: 4px;
    }
    .nav-pills::-webkit-scrollbar-track {
        background: #f1f1f1; 
    }
    .nav-pills::-webkit-scrollbar-thumb {
        background: #c1c1c1; 
        border-radius: 4px;
    }

    /* ========================================================
       3. STYLING FORM FILTER & SELECT2
       ======================================================== */
    .filter-wrapper {
        background-color: #f8f9fa;
        border-radius: 8px;
        padding: 15px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    }

    .select2-container .select2-selection--single {
        height: 38px !important; 
        border: 1px solid #ced4da !important;
        border-radius: 0.375rem !important;
        display: flex;
        align-items: center;
    }
    .select2-container--default .select2-selection--single .select2-selection__arrow {
        height: 36px !important;
    }

    /* Responsive Grid Button */
    @media (max-width: 768px) {
        .filter-wrapper .form-group {
            margin-bottom: 12px;
        }
        .btn-group-custom {
            display: flex;
            flex-direction: column;
            gap: 10px;
            width: 100%;
            margin-top: 10px;
        }
        .btn-group-custom button { width: 100%; }
        /* Membatasi input datatable search agar tidak melar di HP */
        div.dataTables_wrapper div.dataTables_filter input { width: 140px !important; }
    }

    @media (min-width: 769px) {
        .btn-group-custom {
            display: flex;
            gap: 10px;
            align-items: flex-end;
            height: 100%;
            padding-bottom: 2px;
        }
    }
</style>

<div id="layoutSidenav_content">
    <main>
        <div class="container-fluid px-3 px-md-4 overflow-hidden">
            <h1 class="mt-4 fs-3" id="judulPasien">Data Radiologi</h1>
            <ol class="breadcrumb mb-4 shadow-sm bg-white p-2 rounded">
                <li class="breadcrumb-item"><a href="#" class="text-decoration-none text-muted">Dashboard</a></li>
                <li class="breadcrumb-item active">Radiologi</li>
            </ol>

            <ul class="nav nav-pills mb-4" id="pills-tab" role="tablist">
                <li class="nav-item">
                    <a class="nav-link active shadow-sm" id="pills-radiologi-tab" data-bs-toggle="pill" href="#pills-radiologi" role="tab">
                        <i class="fas fa-shopping-cart me-2"></i>Penjualan Radiologi
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link shadow-sm" id="pills-rekaprad-tab" data-bs-toggle="pill" href="#pills-rekaprad" role="tab">
                        <i class="fas fa-solid fa-file-lines me-2"></i>Rekap Data Radiologi
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link shadow-sm" id="pills-periksarad-tab" data-bs-toggle="pill" href="#pills-periksarad" role="tab">
                        <i class="fas fa-regular fa-file-zipper me-2"></i>Rekap Permeriksaan Radiologi 
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link shadow-sm" id="pills-detailrad-tab" data-bs-toggle="pill" href="#pills-detailrad" role="tab">
                        <i class="fas fa-solid fa-file-pen me-2"></i>Rekap Detail Radiologi
                    </a>
                </li>
            </ul>

            <div class="tab-content" id="pills-tabContent">
                
                <div class="tab-pane fade show active" id="pills-radiologi" role="tabpanel">
                    <h5 class="mb-3 border-bottom pb-2 text-muted fw-bold">Data Penjualan Radiologi</h5>
                    
                    <div class="card mb-4 border-0 shadow-sm">
                        <div class="card-body filter-wrapper">
                            <form id="filterFormRadiologi">
                                <div class="row g-2 align-items-end">
                                    <div class="col-12 col-md-3">
                                        <label class="form-label text-muted small fw-bold mb-1">Tanggal Awal</label>
                                        <input type="date" id="tanggal_awal" name="tanggal_awal" class="form-control" value="<?= $tgl_awal ?>" required />
                                    </div>
                                    <div class="col-12 col-md-3">
                                        <label class="form-label text-muted small fw-bold mb-1">Tanggal Akhir</label>
                                        <input type="date" id="tanggal_akhir" name="tanggal_akhir" class="form-control" value="<?= $tgl_akhir ?>" required />
                                    </div>
                                    <div class="col-12 col-md-3">
                                        <label class="form-label text-muted small fw-bold mb-1">Jenis Pembayaran</label>
                                        <select id="kd_group_bayar" name="kd_group_bayar" class="form-select" required>
                                            <option value="" disabled selected>Pilih Pembayaran</option>
                                            <option value="1">BPJS</option>
                                            <option value="2">ASURANSI</option>
                                            <option value="0">UMUM</option>
                                        </select>
                                    </div>
                                    <div class="col-12 col-md-3">
                                        <div class="btn-group-custom mt-3 mt-md-0">
                                            <button type="submit" class="btn btn-primary px-3 shadow-sm"><i class="fas fa-search me-1"></i> Submit</button>
                                            <button type="button" id="downloadButton" class="btn btn-success px-3 shadow-sm" style="display: none;"><i class="fas fa-file-excel me-1"></i> Excel</button>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>

                    <div class="card border-0 shadow-sm mb-5">
                        <div class="card-body p-2 p-md-3">
                            <div id="loadingIcon" style="display: none;" class="py-5 text-center">
                                <img src="<?= base_url('assets_/assets/img/loading-new.gif') ?>" alt="Loading..." style="width: 50px;" />
                            </div>
                            <div id="tableContainer" style="display: none;"></div>
                        </div>
                    </div>
                </div>

                <div class="tab-pane fade" id="pills-rekaprad" role="tabpanel">
                    <h5 class="mb-3 border-bottom pb-2 text-muted fw-bold">Data Rekap Radiologi</h5>
                    
                    <div class="card mb-4 border-0 shadow-sm">
                        <div class="card-body filter-wrapper">
                            <form id="filterFormRekapRad">
                                <div class="row g-2 align-items-end">
                                    <div class="col-12 col-md-3">
                                        <label class="form-label text-muted small fw-bold mb-1">Tanggal Awal</label>
                                        <input type="date" id="tanggal_awal_rekaprad" name="tanggal_awal" class="form-control" value="<?= $tgl_awal ?>" required />
                                    </div>
                                    <div class="col-12 col-md-3">
                                        <label class="form-label text-muted small fw-bold mb-1">Tanggal Akhir</label>
                                        <input type="date" id="tanggal_akhir_rekaprad" name="tanggal_akhir" class="form-control" value="<?= $tgl_akhir ?>" required />
                                    </div>
                                    <div class="col-12 col-md-6">
                                        <div class="btn-group-custom mt-3 mt-md-0">
                                            <button type="submit" class="btn btn-primary px-3 shadow-sm"><i class="fas fa-search me-1"></i> Submit</button>
                                            <button type="button" id="downloadButton_rekaprad" class="btn btn-success px-3 shadow-sm" style="display: none;"><i class="fas fa-file-excel me-1"></i> Excel</button>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>

                    <div class="card border-0 shadow-sm mb-5">
                        <div class="card-body p-2 p-md-3">
                            <div id="loadingIcon_rekaprad" style="display: none;" class="py-5 text-center">
                                <img src="<?= base_url('assets_/assets/img/loading-new.gif') ?>" alt="Loading..." style="width: 50px;" />
                            </div>
                            <div id="tableContainer_rekaprad" style="display: none;"></div>
                        </div>
                    </div>
                </div>

                <div class="tab-pane fade" id="pills-periksarad" role="tabpanel">
                    <h5 class="mb-3 border-bottom pb-2 text-muted fw-bold">Data Rekap Pemeriksaan Radiologi</h5>
                    
                    <div class="card mb-4 border-0 shadow-sm">
                        <div class="card-body filter-wrapper">
                            <form id="filterFormPeriksaRad">
                                <div class="row g-2 align-items-end">
                                    <div class="col-12 col-md-3">
                                        <label class="form-label text-muted small fw-bold mb-1">Tanggal Awal</label>
                                        <input type="date" id="tanggal_awal_periksarad" name="tanggal_awal" class="form-control" value="<?= $tgl_awal ?>" required />
                                    </div>
                                    <div class="col-12 col-md-3">
                                        <label class="form-label text-muted small fw-bold mb-1">Tanggal Akhir</label>
                                        <input type="date" id="tanggal_akhir_periksarad" name="tanggal_akhir" class="form-control" value="<?= $tgl_akhir ?>" required />
                                    </div>
                                    <div class="col-12 col-md-6">
                                        <div class="btn-group-custom mt-3 mt-md-0">
                                            <button type="submit" class="btn btn-primary px-3 shadow-sm"><i class="fas fa-search me-1"></i> Submit</button>
                                            <button type="button" id="downloadButton_periksarad" class="btn btn-success px-3 shadow-sm" style="display: none;"><i class="fas fa-file-excel me-1"></i> Excel</button>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>

                    <div class="card border-0 shadow-sm mb-5">
                        <div class="card-body p-2 p-md-3">
                            <div id="loadingIcon_periksarad" style="display: none;" class="py-5 text-center">
                                <img src="<?= base_url('assets_/assets/img/loading-new.gif') ?>" alt="Loading..." style="width: 50px;" />
                            </div>
                            <div id="tableContainer_periksarad" style="display: none;"></div>
                        </div>
                    </div>
                </div>

                <div class="tab-pane fade" id="pills-detailrad" role="tabpanel">
                    <h5 class="mb-3 border-bottom pb-2 text-muted fw-bold">Data Rekap Detail Radiologi</h5>
                    
                    <div class="card mb-4 border-0 shadow-sm">
                        <div class="card-body filter-wrapper">
                            <form id="filterFormRekapdtlrad">
                                <div class="row g-2 align-items-end">
                                    <div class="col-12 col-md-3">
                                        <label class="form-label text-muted small fw-bold mb-1">Tanggal Awal</label>
                                        <input type="date" id="tanggal_awal_rekapdtlrad" name="tanggal_awal" class="form-control" value="<?= $tgl_awal ?>" required />
                                    </div>
                                    <div class="col-12 col-md-3">
                                        <label class="form-label text-muted small fw-bold mb-1">Tanggal Akhir</label>
                                        <input type="date" id="tanggal_akhir_rekapdtlrad" name="tanggal_akhir" class="form-control" value="<?= $tgl_akhir ?>" required />
                                    </div>
                                    <div class="col-12 col-md-6">
                                        <div class="btn-group-custom mt-3 mt-md-0">
                                            <button type="submit" class="btn btn-primary px-3 shadow-sm"><i class="fas fa-search me-1"></i> Submit</button>
                                            <button type="button" id="downloadButton_rekapdtlrad" class="btn btn-success px-3 shadow-sm" style="display: none;"><i class="fas fa-file-excel me-1"></i> Excel</button>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>

                    <div class="card border-0 shadow-sm mb-5">
                        <div class="card-body p-2 p-md-3">
                            <div id="loadingIcon_rekapdtlrad" style="display: none;" class="py-5 text-center">
                                <img src="<?= base_url('assets_/assets/img/loading-new.gif') ?>" alt="Loading..." style="width: 50px;" />
                            </div>
                            <div id="tableContainer_rekapdtlrad" style="display: none;"></div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </main>
    
    <footer class="py-4 bg-light mt-auto border-top">
        <div class="container-fluid px-4">
            <div class="text-muted small">Copyright &copy; IT RSU Ananda Purwokerto 2024</div>
        </div>
    </footer>
</div>

<script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

<script>
    $(document).ready(function() {
        
        // ==========================================
        // LOGIKA TAB 1: PENJUALAN RADIOLOGI
        // ==========================================
        $('#filterFormRadiologi').on('submit', function(event) {
            event.preventDefault(); 
            $('#loadingIcon').show();
            $('#tableContainer').hide();

            var tglAwal = $('#tanggal_awal').val();
            var tglAkhir = $('#tanggal_akhir').val();
            var kdBayar = $('#kd_group_bayar').val();

            $.ajax({
                url: '<?= base_url("radiologi/penjualanradiologi") ?>',
                type: 'POST',
                data: { TANGGAL_AWAL: tglAwal, TANGGAL_AKHIR: tglAkhir, KD_GROUP_BAYAR: kdBayar },
                success: function(response) {
                    $('#loadingIcon').hide();
                    try {
                        const data = JSON.parse(response); 
                        if (data && Array.isArray(data.response) && data.response.length > 0) {
                            
                            if ($.fn.DataTable.isDataTable('#datatablesSimple')) {
                                $('#datatablesSimple').DataTable().destroy();
                            }

                            let html = `
                            <div class="table-responsive w-100">
                                <table id="datatablesSimple" class="table table-striped table-hover table-bordered text-nowrap w-100">
                                    <thead class="table-light">
                                        <tr>
                                            <th>Tanggal Jual</th>
                                            <th>Unit</th>
                                            <th>Nama Poli</th>
                                            <th>Nama Pembayaran</th>
                                            <th>Nama Dokter</th>
                                            <th>Nama Pasien</th>
                                            <th>Jenis Pasien</th>
                                            <th>Grup Pembayaran</th>
                                            <th class="text-end">Total</th>
                                        </tr>
                                    </thead>
                                <tbody>`;

                            data.response.forEach(item => {
                                let totalFormat = parseFloat(item.total || 0).toLocaleString('id-ID');
                                html += `<tr>
                                    <td>${item.tgl_jual}</td>
                                    <td>${item.radiologi}</td>
                                    <td>${item.nm_poliklinik}</td>
                                    <td>${item.nm_pembayaran}</td>
                                    <td>${item.nm_dokter}</td>
                                    <td>${item.nm_pasien}</td>
                                    <td>${item.nm_jenis_pasien}</td>
                                    <td>${item.nm_group_bayar}</td>
                                    <td class="text-end fw-bold text-success">${totalFormat}</td>
                                </tr>`;
                            });
                            html += '</tbody></table></div>';
                            
                            $('#tableContainer').html(html).show(); 

                            $('#datatablesSimple').DataTable({ 
                                "order": [[0, "asc"]],
                                responsive: false,
                                scrollX: false,
                                autoWidth: false
                            });
                            $('#downloadButton').show();
                        } else {
                            $('#tableContainer').html('<div class="alert alert-info small m-3">Tidak ada data penjualan.</div>').show();
                            $('#downloadButton').hide();
                        }
                    } catch (e) {
                        $('#tableContainer').html('<div class="alert alert-danger m-3">Error Parsing JSON.</div>').show();
                    }
                },
                error: function() {
                    $('#loadingIcon').hide();
                    alert("Gagal koneksi server.");
                }
            });
        });

        $('#downloadButton').on('click', function() {
            const ta = $('#tanggal_awal').val();
            const tak = $('#tanggal_akhir').val();
            const kb = $('#kd_group_bayar').val();
            window.location.href = `<?= base_url("radiologi/unduhpenjualanradiologi") ?>?TANGGAL_AWAL=${ta}&TANGGAL_AKHIR=${tak}&KD_GROUP_BAYAR=${kb}`;
        });

        // ==========================================
        // LOGIKA TAB 2: REKAP RAD 
        // ==========================================
        $('#filterFormRekapRad').on('submit', function(event) {
            event.preventDefault(); 
            $('#loadingIcon_rekaprad').show();
            $('#tableContainer_rekaprad').hide();

            var tglAwal = $('#tanggal_awal_rekaprad').val();
            var tglAkhir = $('#tanggal_akhir_rekaprad').val();

            $.ajax({
                url: '<?= base_url("radiologirekap/rekapdata") ?>', 
                type: 'POST',
                data: { TANGGAL_AWAL: tglAwal, TANGGAL_AKHIR: tglAkhir },
                success: function(response) {
                    $('#loadingIcon_rekaprad').hide();
                    try {
                        const data = JSON.parse(response); 
                        if (data && Array.isArray(data.response) && data.response.length > 0) {
                            
                            if ($.fn.DataTable.isDataTable('#datatablesRekaprad')) {
                                $('#datatablesRekaprad').DataTable().destroy();
                            }

                            let html = `
                            <div class="table-responsive w-100">
                                <table id="datatablesRekaprad" class="table table-striped table-hover table-bordered text-nowrap w-100">
                                    <thead class="table-light">
                                        <tr>
                                            <th>Nama Pembayaran</th>
                                            <th class="text-center">Jumlah Pasien</th>
                                            <th class="text-center">Total Jml Pemeriksaan</th>
                                            <th class="text-end">Total Nilai</th>
                                        </tr>
                                    </thead>
                                <tbody>`;
                            
                            data.response.forEach(item => {
                                html += `<tr>
                                            <td class="fw-bold">${item.nm_pembayaran}</td>
                                            <td class="text-center">${item.jumlah_pasien}</td>
                                            <td class="text-center">${item.total_qty_pemeriksaan}</td>
                                            <td class="text-end fw-bold text-success">${parseFloat(item.total_nilai).toLocaleString('id-ID')}</td>
                                        </tr>`;
                            });
                            html += '</tbody></table></div>';

                            $('#tableContainer_rekaprad').html(html).show(); 
                            $('#datatablesRekaprad').DataTable({ responsive: false, scrollX: false, autoWidth: false });
                            $('#downloadButton_rekaprad').show();
                        } else {
                            $('#tableContainer_rekaprad').html('<div class="alert alert-info m-3 small">Tidak ada data rekap radiologi.</div>').show();
                            $('#downloadButton_rekaprad').hide();
                        }
                    } catch (e) {
                        $('#tableContainer_rekaprad').html('<div class="alert alert-danger m-3">Error Parsing JSON.</div>').show();
                    }
                },
                error: function() {
                    $('#loadingIcon_rekaprad').hide();
                    alert("Gagal koneksi server.");
                }
            });
        });

        $('#downloadButton_rekaprad').on('click', function() {
            const a1 = $('#tanggal_awal_rekaprad').val();
            const a2 = $('#tanggal_akhir_rekaprad').val();
            window.location.href = `<?= base_url("radiologirekap/unduhdatarekap") ?>?TANGGAL_AWAL=${a1}&TANGGAL_AKHIR=${a2}`;
        });

        // ==========================================
        // LOGIKA TAB 3: REKAP RAD PEMERIKSAAN
        // ==========================================
        $('#filterFormPeriksaRad').on('submit', function(event) {
            event.preventDefault(); 
            $('#loadingIcon_periksarad').show();
            $('#tableContainer_periksarad').hide(); 

            var tglAwal = $('#tanggal_awal_periksarad').val();
            var tglAkhir = $('#tanggal_akhir_periksarad').val();

            $.ajax({
                url: '<?= base_url("radiologipemeriksaan/rekapdata") ?>', 
                type: 'POST',
                data: { TANGGAL_AWAL: tglAwal, TANGGAL_AKHIR: tglAkhir },
                success: function(response) {
                    $('#loadingIcon_periksarad').hide();
                    try {
                        const data = JSON.parse(response); 
                        if (data && Array.isArray(data.response) && data.response.length > 0) {
                            
                            if ($.fn.DataTable.isDataTable('#datatablesPemeriksaanRad')) {
                                $('#datatablesPemeriksaanRad').DataTable().destroy();
                            }

                            let html = `
                            <div class="table-responsive w-100">
                                <table id="datatablesPemeriksaanRad" class="table table-striped table-hover table-bordered text-nowrap w-100">
                                    <thead class="table-light">
                                        <tr>
                                            <th>Nama Pembayaran</th>
                                            <th>Nama Pemeriksaan</th>
                                            <th class="text-center">Jumlah Pasien</th>
                                            <th class="text-center">Total Qty Pemeriksaan</th>
                                            <th class="text-end">Total Nilai</th>
                                        </tr>
                                    </thead>
                                <tbody>`;

                            data.response.forEach(item => {
                                html += `<tr>
                                            <td class="fw-bold">${item.nm_pembayaran}</td>
                                            <td>${item.nm_periksa}</td>
                                            <td class="text-center">${item.jumlah_pasien}</td>
                                            <td class="text-center">${item.total_qty_pemeriksaan}</td>
                                            <td class="text-end fw-bold text-success">${parseFloat(item.total_nilai).toLocaleString('id-ID')}</td>
                                        </tr>`;
                            });
                            html += '</tbody></table></div>';

                            $('#tableContainer_periksarad').html(html).show(); 
                            $('#datatablesPemeriksaanRad').DataTable({ responsive: false, scrollX: false, autoWidth: false });
                            $('#downloadButton_periksarad').show();
                        } else {
                            $('#tableContainer_periksarad').html('<div class="alert alert-info m-3 small">Tidak ada data pemeriksaan.</div>').show();
                            $('#downloadButton_periksarad').hide();
                        }
                    } catch (e) {
                        $('#tableContainer_periksarad').html('<div class="alert alert-danger m-3">Error Parsing JSON.</div>').show();
                    }
                },
                error: function() {
                    $('#loadingIcon_periksarad').hide();
                    alert("Gagal koneksi server.");
                }
            });
        });

        $('#downloadButton_periksarad').on('click', function() {
            const pk1 = $('#tanggal_awal_periksarad').val();
            const pk2 = $('#tanggal_akhir_periksarad').val();
            window.location.href = `<?= base_url("radiologipemeriksaan/unduhdatarekap") ?>?TANGGAL_AWAL=${pk1}&TANGGAL_AKHIR=${pk2}`;
        });

        // ==========================================
        // LOGIKA TAB 4: DATA DETAIL RAD
        // ==========================================
        $('#filterFormRekapdtlrad').on('submit', function(event) {
            event.preventDefault(); 
            $('#loadingIcon_rekapdtlrad').show();
            $('#tableContainer_rekapdtlrad').hide();

            var tglAwal = $('#tanggal_awal_rekapdtlrad').val();
            var tglAkhir = $('#tanggal_akhir_rekapdtlrad').val();

            $.ajax({
                url: '<?= base_url("radrekapdetail/tampildata") ?>', 
                type: 'POST',
                data: { TANGGAL_AWAL: tglAwal, TANGGAL_AKHIR: tglAkhir },
                success: function(response) {
                    $('#loadingIcon_rekapdtlrad').hide();
                    try {
                        const data = JSON.parse(response); 
                        if (data && Array.isArray(data.response) && data.response.length > 0) {
                            
                            if ($.fn.DataTable.isDataTable('#datatablesRekapDtlrad')) {
                                $('#datatablesRekapDtlrad').DataTable().destroy();
                            }

                            let html = `
                            <div class="table-responsive w-100">
                                <table id="datatablesRekapDtlrad" class="table table-striped table-hover table-bordered text-nowrap w-100">
                                    <thead class="table-dark">
                                        <tr>
                                            <th>Nama Pemeriksaan</th>
                                            <th class="text-center">Pasien BPJS</th>   
                                            <th class="text-center">Jumlah BPJS</th>
                                            <th class="text-end">Total Nilai BPJS</th>
                                            <th class="text-center">Pasien UMUM</th>
                                            <th class="text-center">Jumlah UMUM</th>
                                            <th class="text-end">Total Nilai UMUM</th>
                                            <th class="text-center">Pasien ASURANSI</th>
                                            <th class="text-center">Jumlah ASURANSI</th>
                                            <th class="text-end">Total Nilai ASURANSI</th>
                                            <th class="text-center">Total Pasien</th>
                                            <th class="text-center">Total Jumlah</th>
                                            <th class="text-end text-warning">Total Nilai Akhir</th>
                                        </tr>
                                    </thead>
                                <tbody>`;

                            data.response.forEach(item => {
                                html += `<tr>
                                            <td class="fw-bold">${item.nm_periksa}</td>
                                            <td class="text-center">${item.bpjs_pasien}</td>
                                            <td class="text-center">${item.bpjs_qty}</td>
                                            <td class="text-end">${parseFloat(item.bpjs_nilai||0).toLocaleString('id-ID')}</td>
                                            <td class="text-center">${item.umum_pasien}</td>
                                            <td class="text-center">${item.umum_qty}</td>
                                            <td class="text-end">${parseFloat(item.umum_nilai||0).toLocaleString('id-ID')}</td>
                                            <td class="text-center">${item.asuransi_pasien}</td>
                                            <td class="text-center">${item.asuransi_qty}</td>
                                            <td class="text-end">${parseFloat(item.asuransi_nilai||0).toLocaleString('id-ID')}</td>
                                            <td class="text-center fw-bold">${item.total_pasien}</td>
                                            <td class="text-center fw-bold">${item.total_qty}</td>
                                            <td class="text-end fw-bold text-success">${parseFloat(item.total_nilai||0).toLocaleString('id-ID')}</td>
                                        </tr>`;
                            });
                            html += '</tbody></table></div>';
                            
                            $('#tableContainer_rekapdtlrad').html(html).show(); 
                            $('#datatablesRekapDtlrad').DataTable({ responsive: false, scrollX: false, autoWidth: false });
                            $('#downloadButton_rekapdtlrad').show();
                        } else {
                            $('#tableContainer_rekapdtlrad').html('<div class="alert alert-info m-3 small">Tidak ada data.</div>').show();
                            $('#downloadButton_rekapdtlrad').hide();
                        }
                    } catch (e) {
                        $('#tableContainer_rekapdtlrad').html('<div class="alert alert-danger m-3">Error Parsing JSON</div>').show();
                    }
                },
                error: function() {
                    $('#loadingIcon_rekapdtlrad').hide();
                    alert("Gagal koneksi server.");
                }
            });
        });
        
        $('#downloadButton_rekapdtlrad').on('click', function() {
            const dtl1 = $('#tanggal_awal_rekapdtlrad').val();
            const dtl2 = $('#tanggal_akhir_rekapdtlrad').val();
            window.location.href = `<?= base_url("radrekapdetail/unduhdata") ?>?TANGGAL_AWAL=${dtl1}&TANGGAL_AKHIR=${dtl2}`;
        });

    });
</script>