<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;
        }
    }

    /* 3. MEMASTIKAN TABEL BISA DI-SCROLL KE SAMPING DI HP */
    .table-responsive {
        width: 100%;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        border: none;
    }

    /* Paksa tabel agar lurus ke samping (tidak turun ke bawah) */
    table.dataTable {
        width: 100% !important;
        min-width: 600px !important; /* Membuat tabel cukup lebar agar bisa di-scroll */
        border-collapse: collapse;
        margin-top: 10px !important;
    }

    /* 4. FORM FILTER AGAR RAPI DI HP */
    .filter-wrapper {
        background-color: #f8f9fa;
        border-radius: 8px;
        padding: 15px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    }

    @media (max-width: 768px) {
        .filter-wrapper .row > div {
            margin-bottom: 12px; /* Jarak antar input di HP */
        }
        .btn-group-custom {
            display: flex;
            flex-direction: column;
            gap: 10px;
            width: 100%;
            margin-top: 5px;
        }
        .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-3 px-md-4">
            <h1 class="mt-4 fs-3" id="judulPasien">Data Billing Harian</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</li>
            </ol>

            <div class="card mb-4 border-0 shadow-sm">
                <div class="card-body filter-wrapper">
                    <form id="filterForm">
                        <div class="row g-2 align-items-end">
                            <div class="col-12 col-md-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-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-3">
                                <label for="kd_jenis_pasien" class="form-label text-muted small mb-1">Jenis Pasien</label>
                                <select id="kd_jenis_pasien" name="kd_jenis_pasien" class="form-select" required>
                                    <option value="" disabled <?= empty($kd_jenis_pasien) ? 'selected' : '' ?>>Pilih Jenis Pasien</option>
                                    <option value="1" <?= ($kd_jenis_pasien == '1') ? 'selected' : '' ?>>Rawat Jalan</option>
                                    <option value="2" <?= ($kd_jenis_pasien == '2') ? 'selected' : '' ?>>Rawat Inap</option>
                                    <option value="3" <?= ($kd_jenis_pasien == '3') ? 'selected' : '' ?>>IGD</option>
                                </select>
                            </div>

                            <div class="col-12 col-md-3">
                                <div class="btn-group-custom">
                                    <button type="submit" class="btn btn-primary px-3"><i class="fas fa-search me-1"></i> Submit</button>
                                    <button type="button" id="downloadButton" class="btn btn-success px-3" 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-4" style="min-width: 0;">
                <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: 40px;" />
                        <p class="text-muted mt-2 small">Memuat data...</p>
                    </div>
                    
                    <div id="tableContainer" style="display: none;">
                        </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 text-muted">
                <div>Copyright &copy; IT RSU Ananda Purwokerto 2024</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/billingharian") ?>',
                type: 'POST',
                data: {
                    TANGGAL_AWAL: $('#tanggal_awal').val(),
                    TANGGAL_AKHIR: $('#tanggal_akhir').val(),
                    KD_JENIS_PASIEN: $('#kd_jenis_pasien').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) {
                                // PERHATIKAN: Dibungkus dengan <div class="table-responsive"> agar scroll HP jalan sempurna
                                let html = `
                                    <div class="table-responsive">
                                        <table id="datatablesSimple" class="table table-striped table-hover table-bordered text-nowrap">
                                            <thead class="table-light">
                                                <tr>
                                                    <th>Jenis</th>
                                                    <th>Nama Tindakan</th>
                                                    <th>Jumlah</th>
                                                    <th>Total</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                `;

                                list.forEach(item => {
                                    html += `
                                        <tr>
                                            <td>${item.jenis}</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></div>';
                                
                                $('#tableContainer').html(html).show(); 

                                // 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').show();
                            } else {
                                $('#tableContainer').html('<div class="alert alert-info text-center m-3 small">Tidak ada data transaksi yang ditemukan.</div>').show();
                            }
                        } else {
                            $('#tableContainer').html('<div class="alert alert-warning text-center m-3 small">Data tidak valid.</div>').show();
                        }

                        history.replaceState(null, '', '<?= base_url("dashboard/billing") ?>');
                    } catch (e) {
                        console.error("Error: ", e);
                        $('#tableContainer').html('<div class="alert alert-danger text-center m-3 small">Terjadi kesalahan sistem.</div>').show();
                    }
                },
                error: function(xhr, status, error) {
                    $('#loadingIcon').hide(); 
                    $('#tableContainer').html('<div class="alert alert-danger text-center m-3 small">Koneksi gagal.</div>').show();
                }
            });
        });

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