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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Detail Tagihan BPJS</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">

    <style>
        /* Tipografi & Background Utama */
        body {
            background-color: #f0f2f5;
            font-family: 'Nunito', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            color: #3d464d;
        }

        /* Desain Card Modern */
        .card-custom {
            border: none;
            border-radius: 12px;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.04);
            overflow: hidden;
            margin-bottom: 25px;
        }

        .card-header-gradient {
            background: linear-gradient(135deg, #2193b0, #6dd5ed);
            color: white;
            padding: 20px 25px;
            border-bottom: none;
        }

        .filter-label {
            font-weight: 700;
            font-size: 0.8rem;
            color: #5a6268;
            text-transform: uppercase;
            letter-spacing: 0.8px;
            margin-bottom: 8px;
        }

        /* Styling Input Form */
        .form-control,
        .custom-select {
            border-radius: 8px;
            border: 1px solid #ced4da;
            padding: 10px 15px;
            height: auto;
            font-size: 0.95rem;
            transition: all 0.3s ease;
        }

        .form-control:focus,
        .custom-select:focus {
            box-shadow: 0 0 0 0.2rem rgba(33, 147, 176, 0.25);
            border-color: #2193b0;
        }

        /* Styling Tombol */
        .btn-custom {
            border-radius: 8px;
            font-weight: 600;
            padding: 10px 20px;
            letter-spacing: 0.5px;
            transition: all 0.3s;
        }

        .btn-primary-custom {
            background-color: #2193b0;
            border-color: #2193b0;
            color: white;
        }

        .btn-primary-custom:hover {
            background-color: #177189;
            border-color: #177189;
            transform: translateY(-1px);
            box-shadow: 0 4px 8px rgba(33, 147, 176, 0.3);
        }

        .btn-success-custom {
            background-color: #10b981;
            border-color: #10b981;
            color: white;
        }

        .btn-success-custom:hover {
            background-color: #059669;
            border-color: #059669;
            transform: translateY(-1px);
            box-shadow: 0 4px 8px rgba(16, 185, 129, 0.3);
        }

        /* Styling Tabel */
        .table-custom thead th {
            background-color: #f8f9fa;
            color: #495057;
            font-weight: 700;
            font-size: 0.85rem;
            text-transform: uppercase;
            vertical-align: middle;
            border-top: none;
            border-bottom: 2px solid #e2e8f0;
            padding: 15px;
        }

        .table-custom tbody td {
            vertical-align: middle;
            padding: 12px 15px;
            font-size: 0.95rem;
            border-bottom: 1px solid #f1f5f9;
        }

        .table-custom tbody tr:hover {
            background-color: #f8fafc;
        }

        .btn-group-wrapper {
            display: flex;
            gap: 10px;
        }

        @media (max-width: 768px) {
            .btn-group-wrapper {
                flex-direction: column;
                width: 100%;
                margin-top: 15px;
            }

            .btn-group-wrapper button {
                width: 100%;
                margin-left: 0 !important;
            }
        }
    </style>
</head>

<body>
    <div id="layoutSidenav_content">
        <main>
            <div class="container-fluid px-4 py-4">

                <div class="mb-4">
                    <h2 class="font-weight-bold mb-1"><i class="fas fa-file-invoice-dollar text-info mr-2"></i>Detail Tagihan BPJS</h2>
                    <ol class="breadcrumb bg-transparent p-0 mb-0">
                        <li class="breadcrumb-item"><a href="#" class="text-muted text-decoration-none">Dashboard</a></li>
                        <li class="breadcrumb-item active font-weight-bold text-info">Tampilan Detail Tagihan</li>
                    </ol>
                </div>

                <div class="row">
                    <div class="col-md-12">
                        <div class="card card-custom">
                            <div class="card-header-gradient">
                                <h5 class="mb-0 font-weight-bold"><i class="fas fa-filter mr-2"></i>Filter Data Tagihan</h5>
                            </div>
                            <div class="card-body p-4">
                                <form id="filterForm">
                                    <input type="hidden" id="kd_pembayaran" name="kd_pembayaran" value="A12">

                                    <div class="row align-items-end">
                                        <div class="col-12 col-md-3 mb-3 mb-md-0">
                                            <div class="form-group mb-0">
                                                <label class="filter-label" for="tanggal_awal">Tanggal Awal</label>
                                                <input type="date" id="tanggal_awal" name="tanggal_awal" class="form-control" value="<?= $tgl_awal ?>" required />
                                            </div>
                                        </div>

                                        <div class="col-12 col-md-3 mb-3 mb-md-0">
                                            <div class="form-group mb-0">
                                                <label class="filter-label" for="tanggal_akhir">Tanggal Akhir</label>
                                                <input type="date" id="tanggal_akhir" name="tanggal_akhir" class="form-control" value="<?= $tgl_akhir ?>" required />
                                            </div>
                                        </div>

                                        <div class="col-12 col-md-3 mb-3 mb-md-0">
                                            <div class="form-group mb-0">
                                                <label class="filter-label" for="kd_jenis_pasien">Jenis Pasien</label>
                                                <select id="kd_jenis_pasien" name="kd_jenis_pasien" class="custom-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>

                                        <div class="col-12 col-md-3">
                                            <div class="btn-group-wrapper">
                                                <button type="submit" class="btn btn-custom btn-primary-custom flex-grow-1"><i class="fas fa-search mr-1"></i> Submit</button>
                                                <button type="button" id="downloadButton" class="btn btn-custom btn-success-custom" style="display: none;"><i class="fas fa-file-excel mr-1"></i> Excel</button>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

                <div id="result">
                    <div id="loadingIcon" style="display: none;" class="py-5">
                        <center>
                            <img src="<?= base_url('assets_/assets/img/loading-new.gif') ?>" alt="Loading..." width="60" />
                            <p class="text-info mt-3 font-weight-bold">Mengambil data tagihan...</p>
                        </center>
                    </div>

                    <div class="card card-custom" id="tableCard" style="display: none;">
                        <div class="card-body p-0">
                            <div class="table-responsive p-3" id="tableContainer">
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </main>

        <footer class="py-4 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://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.bundle.min.js"></script>

    <script>
        $(document).ready(function() {
            $('#filterForm').on('submit', function(event) {
                event.preventDefault();
                $('#loadingIcon').show();
                $('#tableCard').hide();

                $.ajax({
                    url: '<?= base_url("detialtagihan/tampildata") ?>',
                    type: 'POST',
                    data: {
                        TANGGAL_AWAL: $('#tanggal_awal').val(),
                        TANGGAL_AKHIR: $('#tanggal_akhir').val(),
                        KD_JENIS_PASIEN: $('#kd_jenis_pasien').val(),
                        KD_PEMBAYARAN: $('#kd_pembayaran').val()
                    },
                    success: function(tampil) {
                        $('#loadingIcon').hide();

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

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

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

                                if (list.length > 0) {
                                    let html = `<table id="datatablesSimple" class="table table-custom text-nowrap w-100">
                                                    <thead>
                                                        <tr>
                                                            <th>DPJP</th>
                                                            <th class="text-center">Jumlah</th>
                                                            <th class="text-right">Tarif RS</th>
                                                            <th class="text-right">Prosedur Non Bedah</th>
                                                            <th class="text-right">Prosedur Bedah</th>
                                                            <th class="text-right">Konsultasi</th>
                                                            <th class="text-right">Keperawatan</th>
                                                            <th class="text-right">Tenaga Ahli</th>
                                                            <th class="text-right">Laboratorium</th>
                                                            <th class="text-right">Radiologi</th>
                                                            <th class="text-right">Kamar & Akomodasi</th>
                                                            <th class="text-right">Konsultasi</th>
                                                            <th class="text-right">Obat</th>
                                                            <th class="text-right">BMHP</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>`;

                                    list.forEach(item => {
                                        html += `<tr>
                                        <td><strong>${item.dpjp}</strong></td>
                                        <td class="text-center font-weight-bold text-info">${item.jlm_ri}</td>
                                        <td class="text-right">${item.tarif_rs}</td>
                                        <td class="text-right">${item.prosedur_non_bedah}</td>
                                        <td class="text-right">${item.prosedur_bedah}</td>
                                        <td class="text-right">${item.konsultasi}</td>
                                        <td class="text-right">${item.keperawatan}</td>
                                        <td class="text-right">${item.tenaga_ahli}</td>
                                        <td class="text-right">${item.laboratorium}</td>
                                        <td class="text-right">${item.radiologi}</td>
                                        <td class="text-right">${item.kamar_akomodasi}</td>
                                        <td class="text-right">${item.konsultasi}</td>
                                        <td class="text-right">${item.obat}</td>
                                        <td class="text-right">${item.bmhp}</td>
                                    </tr>`;
                                    });
                                    html += '</tbody></table>';

                                    $('#tableContainer').html(html);
                                    $('#tableCard').fadeIn();

                                    $('#datatablesSimple').DataTable({
                                        "columnDefs": [{
                                            "targets": 3,
                                            "type": "date"
                                        }],
                                        "order": [
                                            [2, "asc"]
                                        ]
                                    });

                                    $('#downloadButton').show();
                                } else {
                                    $('#tableContainer').html('<div class="alert alert-info text-center m-4"><i class="fas fa-info-circle mr-2"></i>Tidak ada data yang ditemukan.</div>');
                                    $('#tableCard').fadeIn();
                                    $('#downloadButton').hide();
                                }
                            } else {
                                $('#tableContainer').html('<div class="alert alert-warning text-center m-4"><i class="fas fa-exclamation-triangle mr-2"></i>Tidak ada data yang ditemukan.</div>');
                                $('#tableCard').fadeIn();
                                $('#downloadButton').hide();
                            }

                            history.replaceState(null, '', '<?= base_url("detialtagihan/tampildata") ?>');
                        } catch (e) {
                            console.error("Error parsing JSON: ", e);
                            $('#tableContainer').html('<div class="alert alert-danger text-center m-4"><i class="fas fa-times-circle mr-2"></i>Terjadi kesalahan format data dari server.</div>');
                            $('#tableCard').fadeIn();
                            $('#downloadButton').hide();
                        }
                    },
                    error: function(xhr, status, error) {
                        $('#loadingIcon').hide();
                        console.error("AJAX Error: ", error);
                        $('#tableContainer').html('<div class="alert alert-danger text-center m-4">Gagal terhubung ke server.</div>');
                        $('#tableCard').fadeIn();
                    }
                });
            });

            // Event Download
            $('#downloadButton').on('click', function() {
                const tanggalAwal = $('#tanggal_awal').val();
                const tanggalAkhir = $('#tanggal_akhir').val();
                const kdjenispasien = $('#kd_jenis_pasien').val();
                const kdpembayaran = $('#kd_pembayaran').val();

                window.location.href = `<?= base_url("detialtagihan/unduhdata") ?>?TANGGAL_AWAL=${tanggalAwal}&TANGGAL_AKHIR=${tanggalAkhir}&KD_JENIS_PASIEN=${kdjenispasien}&KD_PEMBAYARAN=${kdpembayaran}`;
            });
        });
    </script>
</body>

</html>