<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Rekap Keuangan SIRUS</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <style>
        #downloadButton {
            margin-left: 10px;
        }

        @media (max-width: 768px) {
            .container {
                width: 100%;
            }

            .form-group {
                margin-bottom: 10px;
            }

            .btn-container {
                margin-top: 10px;
            }
        }

        @media (min-width: 769px) and (max-width: 1024px) {
            .container {
                width: 80%;
            }

            #downloadButton {
                margin-top: 10px;
                /* Memberikan jarak pada tombol download di bawah form */
            }
        }

        @media (min-width: 1025px) {
            .container {
                width: 60%;
            }
        }
    </style>
</head>


<body>
    <div id="layoutSidenav_content">
        <main>
            <div class="container-fluid px-4">
                <h1 class="mt-4" id="judulPasien">Data Rekap Keuangan SIRUS NON BPJS</h1>
                <ol class="breadcrumb mb-4">
                    <li class="breadcrumb-item"><a href="index.html">Dashboard</a></li>
                    <li class="breadcrumb-item active">Tampilan Data Rekap Keuangan SIRUS NON BPJS</li>
                </ol>
                <!-- Filter Tanggal -->

                <div class="row">
                    <div class="col-md-12">
                        <div class="card mb-4" style="width:100%; height:auto">
                            <div class="card-body">
                                <!-- <div class="input-group-append btn-container">
                                    <button type="submit" class="btn btn-primary ml-2 ">Filter</button>
                                    <button id="downloadButton" class="btn btn-success ml-2" style="display: none;">Download Excel</button>
                                </div> -->
                                <form id="filterForm" class="row g-1">
                                    <div class="row">
                                        <div class="col-md-3">
                                            <div class="form-group">
                                                <label for="tgl_awal">Tanggal Awal</label>
                                                <input type="date" id="tanggal_awal" name="tanggal_awal" class="form-control" value="<?= $tgl_awal ?>" required style="max-width: 100;" />
                                            </div>
                                        </div>
                                        <div class="col-md-7">
                                            <div class="form-group">
                                                <label for="tgl_akhir">Tanggal Akhir</label>
                                                <div class="input-group">
                                                    <input type="date" id="tanggal_akhir" name="tanggal_akhir" class="form-control" value="<?= $tgl_akhir ?>" required style="max-width: 230px;" />
                                                    <select id="kd_jenis_pasien" name="kd_jenis_pasien" class="custom-select" value="<?= $kd_jenis_pasien ?>" required style="max-width: 160px; margin-left:15px">
                                                        <option value="" disabled selected>Pilih Jenis Pasien</option>
                                                        <option value="1">Rawat Jalan</option>
                                                        <option value="2">Rawat Inap</option>
                                                        <option value="3">IGD</option>
                                                        <!-- <option value="4">Pemeriksaan Penunjang</option> -->
                                                    </select>
                                                    <button type="submit" class="btn btn-primary ml-2 ">Submit</button>
                                                    <button id="downloadButton" class="btn btn-success ml-2" style="display: none;">Download Excel</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="input-group-append btn-container">
                    <!-- <button type="submit" class="btn btn-primary ml-2 ">Filter</button> -->
                    <!-- <button id="downloadButton" class="btn btn-success ml-2" style="display: none;">Download Excel</button> -->
                </div>
                <div id="result" class="table-responsive">
                    <div id="loadingIcon" style="display: none;">
                        <center><img src="<?= base_url('assets_/assets/img/loading-new.gif') ?>" alt="Loading..." /></center>
                    </div>
                    <div class="table-responsive" id="tableContainer" style="display: none;">
                        <!-- Tabel akan dimasukkan di sini setelah pemrosesan -->
                    </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="#">Privacy Policy</a>
                        &middot;
                        <a href="#">Terms &amp; Conditions</a>
                    </div>
                </div>
            </div>
        </footer>
    </div>


    <!-- Jquery -->
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <!-- DataTables JS -->
    <script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
    <script src="<?php echo base_url('assets_/js/scripts.js'); ?>"></script>
    <script src="https://cdn.jsdelivr.net/npm/simple-datatables@7.1.2/dist/umd/simple-datatables.min.js" crossorigin="anonymous"></script>
    <script src="<?php echo base_url('assets_/js/datatables-simple-demo.js'); ?>"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script>
        $(document).ready(function() {
            $('#filterForm').on('submit', function(event) {
                event.preventDefault(); // Mencegah refresh halaman
                $('#loadingIcon').show(); // Tampilkan ikon loading
                $('#tableContainer').hide(); // Sembunyikan tabel sebelum memulai pengambilan data

                $.ajax({
                    url: '<?= base_url("billing/tampildatarekapkeunonbpjs") ?>',
                    type: 'POST',
                    data: {
                        TANGGAL_AWAL: $('#tanggal_awal').val(),
                        TANGGAL_AKHIR: $('#tanggal_akhir').val(),
                        KD_JENIS_PASIEN: $('#kd_jenis_pasien').val()
                    },
                    success: function(tampil) {
                        $('#loadingIcon').hide(); // Sembunyikan ikon loading setelah permintaan selesai

                        try {
                            const data = JSON.parse(tampil); // Parsing JSON dari response

                            // Periksa status dan data
                            if (data && Array.isArray(data.tampil)) {
                                const list = data.tampil;

                                // Hancurkan DataTable sebelumnya jika sudah ada
                                if ($.fn.DataTable.isDataTable('#datatablesSimple')) {
                                    $('#datatablesSimple').DataTable().destroy();
                                }

                                if (list.length > 0) {
                                    let html = '<table id="datatablesSimple" class="table table-striped table-bordered"><thead><tr><th>DPJP</th><th>Jumlah</th><th>Tarif RS</th><th>Prosedur Non Bedah</th><th>Prosedur Bedah</th><th>Konsultasi</th><th>Keperawatan</th><th>Tenaga Ahli</th><th>Laboratorium</th><th>Radiologi</th><th>Kamar & Akomodasi</th><th>Konsultasi</th><th>Obat</th><th>BMHP</th></tr></thead><tbody>';

                                    // Looping untuk menampilkan data
                                    list.forEach(item => {

                                        html += `<tr>
                                        <td>${item.dpjp}</td>
                                        <td>${item.jlm_ri}</td>
                                        <td>${item.tarif_rs}</td>
                                        <td>${item.prosedur_non_bedah}</td>
                                        <td>${item.prosedur_bedah}</td>
                                        <td>${item.konsultasi}</td>
                                        <td>${item.keperawatan}</td>
                                        <td>${item.tenaga_ahli}</td>
                                        <td>${item.laboratorium}</td>
                                        <td>${item.radiologi}</td>
                                        <td>${item.kamar_akomodasi}</td>
                                        <td>${item.konsultasi}</td>
                                        <td>${item.obat}</td>
                                        <td>${item.bmhp}</td>
                                       
                                    </tr>`;
                                    });
                                    html += '</tbody></table>';
                                    $('#tableContainer').html(html).show(); // Tampilkan tabel hasil

                                    // Inisialisasi DataTable setelah tabel ditambahkan ke DOM
                                    $('#datatablesSimple').DataTable({
                                        "columnDefs": [{
                                            "targets": 3, // Misalkan kolom tanggal berada di index 2
                                            "type": "date"
                                        }],
                                        "order": [
                                            [2, "asc"]
                                        ] // Urutkan tanggal ascending
                                    });

                                    // Tampilkan tombol download
                                    $('#downloadButton').show();
                                } else {
                                    $('#tableContainer').html('<p>Tidak ada data yang ditemukan.</p>').show();
                                    $('#downloadButton').hide(); // Sembunyikan tombol download jika tidak ada data
                                }
                            } else {
                                $('#tableContainer').html('<p>Tidak ada data yang ditemukan.</p>').show();
                                $('#downloadButton').hide(); // Sembunyikan tombol download jika tidak ada data
                            }

                            // Ubah URL tanpa menyegarkan halaman
                            history.replaceState(null, '', '<?= base_url("billing/tampildatarekapkeunonbpjs") ?>');
                        } catch (e) {
                            console.error("Error parsing JSON: ", e);
                            $('#tableContainer').html('<p>Terjadi kesalahan saat memproses data.</p>').show();
                            $('#downloadButton').hide(); // Sembunyikan tombol download jika terjadi kesalahan
                        }
                    },
                    error: function(xhr, status, error) {
                        $('#loadingIcon').hide(); // Sembunyikan ikon loading jika terjadi error
                        console.error("AJAX Error: ", error);
                    }
                });
            });

            // Event untuk tombol 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/unduhrekapkeusirusnonbpjs") ?>?TANGGAL_AWAL=${tanggalAwal}&TANGGAL_AKHIR=${tanggalAkhir}&KD_JENIS_PASIEN=${kdjenispasien}`;
            });
        });
    </script>
</body>

</html>