<style>
    /* 1. MENCEGAH HALAMAN MELAR KE KANAN */
    body {
        overflow-x: hidden !important;
        width: 100%;
    }

    /* 2. KUNCI PERBAIKAN: MENGATASI BUG TERGENCET SB ADMIN DI HP */
    @media (max-width: 991.98px) {
        #layoutSidenav_content {
            padding-left: 0 !important;
            margin-left: 0 !important;
            width: 100% !important;
        }
    }
    
    table.dataTable {
        width: 100% !important;
        margin: 0 auto;
        clear: both;
        border-collapse: collapse;
        table-layout: fixed;
        word-wrap: break-word;
    }

    .filter-wrapper {
        background-color: #f8f9fa;
        border-radius: 8px;
        padding: 15px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    }

    /* Penyesuaian tombol di layar HP */
    @media (max-width: 768px) {
        .btn-group-custom {
            display: flex;
            flex-direction: column;
            gap: 10px;
            width: 100%;
            margin-top: 15px;
        }
        .btn-group-custom button {
            width: 100%;
        }
    }
    @media (min-width: 769px) {
        .btn-group-custom {
            display: flex;
            gap: 10px;
            align-items: flex-end;
        }
    }
</style>

<div id="layoutSidenav_content">
    <main>
        <div class="container-fluid px-4">
            <h1 class="mt-4" id="judulPasien">Data Billing Harian ICU/HCU</h1>
            <ol class="breadcrumb mb-4">
                <li class="breadcrumb-item"><a href="#" class="text-decoration-none">Dashboard</a></li>
                <li class="breadcrumb-item active">Tampilkan Data Billing Harian ICU/HCU</li>
            </ol>

            <div class="card mb-4 border-0 shadow-sm">
                <div class="card-body filter-wrapper">
                    <form id="filterForm">
                        <div class="row g-3 align-items-end">
                            <div class="col-12 col-md-4 col-lg-3">
                                <label for="tanggal_awal" class="form-label text-muted small 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-4 col-lg-3">
                                <label for="tanggal_akhir" class="form-label text-muted small 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-4 col-lg-6">
                                <div class="btn-group-custom">
                                    <button type="submit" class="btn btn-primary px-4"><i class="fas fa-search me-1"></i> Filter</button>
                                    <button type="button" id="downloadButton" class="btn btn-success px-4" style="display: none;"><i class="fas fa-file-excel me-1"></i> Download Excel</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body p-0 p-md-3">
                    <div id="result">
                        <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;" />
                            <p class="text-muted mt-2">Memuat data...</p>
                        </div>
                        <div class="table-responsive" id="tableContainer" style="display: none;">
                            </div>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <footer class="py-4 bg-light mt-auto">
        <div class="container-fluid px-4">
            <div class="d-flex align-items-center justify-content-between small">
                <div class="text-muted">Copyright &copy; By IT RSU Ananda Purwokerto 2024</div>
                <div>
                    <a href="#" class="text-decoration-none text-muted">Privacy Policy</a>
                    &middot;
                    <a href="#" class="text-decoration-none text-muted">Terms &amp; Conditions</a>
                </div>
            </div>
        </div>
    </footer>
</div>

<script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<script>
    $(document).ready(function() {

        $('#filterForm').on('submit', function(event) {
            event.preventDefault(); 
            $('#loadingIcon').show(); 
            $('#tableContainer').hide(); 
            $('#downloadButton').hide(); 

            $.ajax({
                url: '<?= base_url("billing/billingharianicu") ?>',
                type: 'POST',
                data: {
                    TANGGAL_AWAL: $('#tanggal_awal').val(),
                    TANGGAL_AKHIR: $('#tanggal_akhir').val()
                },
                success: function(response) {
                    $('#loadingIcon').hide(); 

                    try {
                        const data = JSON.parse(response); 

                        if (data && Array.isArray(data.response)) {
                            const list = data.response;

                            if ($.fn.DataTable.isDataTable('#datatablesSimple')) {
                                $('#datatablesSimple').DataTable().destroy();
                            }

                            if (list.length > 0) {
                                // Menambahkan class w-100 dan text-nowrap agar tabel tidak rusak di mobile
                                let html = `
                                    <table id="datatablesSimple" class="table table-striped table-hover table-bordered w-100 text-nowrap">
                                        <thead class="table-light">
                                            <tr>
                                                <th>KD Registrasi</th>
                                                <th>Nama Pasien</th>
                                                <th>Nama Ruang</th>
                                                <th>No Bed</th>
                                                <th>Tgl Tindakan</th>
                                                <th>Nama Tindakan</th>
                                                <th>Jumlah</th>
                                                <th>Total</th>
                                            </tr>
                                        </thead>
                                    <tbody>
                                `;

                                list.forEach(item => {
                                    html += `<tr>
                                        <td>${item.kd_registrasi}</td>
                                        <td>${item.nm_pasien}</td>
                                        <td>${item.nm_ruang}</td>
                                        <td>${item.nm_bed}</td>
                                        <td>${item.tgl_tindakan}</td>
                                        <td class="text-wrap" style="min-width: 200px;">${item.nm_tindakan}</td>
                                        <td>${item.qty}</td>
                                        <td class="text-end fw-bold">${item.total}</td>
                                    </tr>`;
                                });
                                html += '</tbody></table>';
                                $('#tableContainer').html(html).fadeIn(); 

                                // INISIALISASI DATATABLES DENGAN FITUR SCROLL-X
                                // INISIALISASI DATATABLES
                                $('#datatablesSimple').DataTable({
                                    responsive: false, // Dimatikan agar tidak konflik dengan table-responsive
                                    autoWidth: false,
                                    language: {
                                        search: "Cari:",
                                        lengthMenu: "Tampil _MENU_",
                                        info: "_START_-_END_ dari _TOTAL_"
                                    }
                                });

                                $('#downloadButton').fadeIn();
                            } else {
                                $('#tableContainer').html('<div class="alert alert-info text-center m-3">Tidak ada data transaksi yang ditemukan.</div>').fadeIn();
                            }
                        } else {
                            $('#tableContainer').html('<div class="alert alert-warning text-center m-3">Respon data kosong atau tidak valid.</div>').fadeIn();
                        }

                        history.replaceState(null, '', '<?= base_url("billing/billingharianicu") ?>');
                    } catch (e) {
                        console.error("Error parsing JSON: ", e);
                        $('#tableContainer').html('<div class="alert alert-danger text-center m-3">Terjadi kesalahan saat memproses data.</div>').fadeIn();
                    }
                },
                error: function(xhr, status, error) {
                    $('#loadingIcon').hide(); 
                    console.error("AJAX Error: ", error);
                    $('#tableContainer').html('<div class="alert alert-danger text-center m-3">Koneksi ke server gagal.</div>').fadeIn();
                }
            });
        });

        // Event download excel
        $('#downloadButton').on('click', function() {
            const tanggalAwal = $('#tanggal_awal').val();
            const tanggalAkhir = $('#tanggal_akhir').val();
            window.location.href = `<?= base_url("billing/unduhbillingharianicu") ?>?TANGGAL_AWAL=${tanggalAwal}&TANGGAL_AKHIR=${tanggalAkhir}`;
        });
    });
</script>