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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Data Billing Kasir Perbank</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <style>
        .summary-table-container {
            border: 1px solid #dee2e6;
            border-radius: .25rem;
            box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, .075);
            max-height: 140px;
            overflow-y: auto;
        }

        .summary-table-container table {
            margin-bottom: 0;
        }

        .summary-table-container thead th {
            position: sticky;
            top: 0;
            background-color: #f8f9fa;
            z-index: 1;
        }

        .summary-table-container .table-title th {
            font-size: 1rem;
            border-bottom: 1px solid #ccc !important;
        }

        .summary-table-container .table td,
        .summary-table-container .table th {
            padding: 0.5rem 1.25rem;
            font-size: 0.875rem;
        }

        #downloadButton {
            margin-left: 10px;
        }

        @media (max-width: 768px) {
            .form-group {
                margin-bottom: 10px;
            }
        }
    </style>
</head>

<body>
    <div id="layoutSidenav_content">
        <main>
            <div class="container-fluid px-4">
                <h1 class="mt-4" id="judulPasien">Data Billing Kasir Perbank</h1>
                <ol class="breadcrumb mb-4">
                    <li class="breadcrumb-item"><a href="#">Dashboard</a></li>
                    <li class="breadcrumb-item active">Tampilkan Data Billing Kasir Total</li>
                </ol>

                <div class="row mb-4 align-items-start">
                    <div class="col-lg-7">
                        <div class="card h-100">
                            <div class="card-body">
                                <form id="filterForm">
                                    <!-- **PERUBAHAN POSISI TOMBOL DIMULAI DI SINI** -->
                                    <div class="row">
                                        <div class="col-md-5 form-group">
                                            <label for="tanggal_awal" class="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-md-6 form-group">
                                            <label for="tanggal_akhir" class="mb-1">Tanggal Akhir & Bank</label>
                                            <div class="input-group">
                                                <input type="date" id="tanggal_akhir" name="tanggal_akhir" class="form-control" value="<?= $tgl_akhir ?? '' ?>" required />
                                                <select id="kd_jenisbank" name="kd_jenisbank" class="custom-select ml-2" value="<?= $kd_jenisbank ?>" style="max-width: 180px;">
                                                    <option value="" disabled selected>Pilih Jenis BANK</option>
                                                    <option value="100">POTONG GAJI</option>
                                                    <option value="1001">KAS TUNAI PENERIMAAN</option>
                                                    <option value="1002">QRIS MANDIRI</option>
                                                    <option value="1003">EDC MANDIRI</option>
                                                    <option value="1004">QRIS BCA</option>
                                                    <option value="1005">KAS KASIR</option>
                                                    <option value="1006">EDC BCA</option>
                                                    <option value="1007">QRIS BNI</option>
                                                    <option value="1008">EDC BNI</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row mt-1">
                                        <div class="col-12 text-left">
                                            <button type="submit" class="btn btn-primary">Filter</button>
                                            <button id="downloadButton" type="button" class="btn btn-success" style="display: none;">Download Excel</button>
                                        </div>
                                    </div>
                                    <!-- **PERUBAHAN POSISI TOMBOL SELESAI** -->
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-5">
                        <div id="summaryContainer" style="display: none;">
                            <!-- Tabel ringkasan -->
                        </div>
                    </div>
                </div>

                <div id="result">
                    <div id="loadingIcon" style="display: none; text-align: center;">
                        <img src="<?= base_url('assets_/assets/img/loading-new.gif') ?>" alt="Loading..." />
                    </div>
                    <div class="table-responsive" id="tableContainer" style="display: none;">
                        <!-- Tabel utama -->
                    </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 Support RSU Ananda Purwokerto 2024</div>
                    <div>
                        <a href="#">Privacy Policy</a> &middot; <a href="#">Terms &amp; Conditions</a>
                    </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 src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <script>
        $(document).ready(function() {

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

                $.ajax({
                    url: '<?= base_url("kasirperbank/tampilkasirperbank") ?>',
                    type: 'POST',
                    data: {
                        TANGGAL_AWAL: $('#tanggal_awal').val(),
                        TANGGAL_AKHIR: $('#tanggal_akhir').val(),
                        KD_BANK: $('#kd_jenisbank').val()
                    },
                    dataType: 'json',
                    success: function(data) {
                        $('#loadingIcon').hide();
                        if (data.status === 'error') {
                            Swal.fire('Error', data.message, 'error');
                            return;
                        }
                        if (data.summary && data.summary.length > 0) {
                            let summaryHtml = '<div class="summary-table-container">' +
                                '<table class="table table-sm table-hover">' +
                                '<thead>' +
                                '<tr class="table-title">' +
                                '</tr>' +
                                '<tr>' +
                                '<th>Nama Bank</th>' +
                                '<th class="text-right">Jumlah</th>' +
                                '</tr>' +
                                '</thead>' +
                                '<tbody>';
                            data.summary.forEach(item => {
                                // **PERBAIKAN:** Mengubah item.sum menjadi item.jumlah
                                const jumlah = (parseFloat(item.jumlah) || 0).toLocaleString('id-ID', {
                                    style: 'currency',
                                    currency: 'IDR',
                                    minimumFractionDigits: 0
                                });
                                summaryHtml += `<tr><td><strong>${item.nm_bank}</strong></td><td class="text-right">${jumlah}</td></tr>`;
                            });
                            summaryHtml += '</tbody></table></div>';
                            $('#summaryContainer').html(summaryHtml).show();
                        }
                        if (data.response && data.response.length > 0) {
                            const list = data.response;
                            if ($.fn.DataTable.isDataTable('#datatablesSimple')) {
                                $('#datatablesSimple').DataTable().destroy();
                            }
                            let html = '<table id="datatablesSimple" class="table table-striped table-bordered" style="width:100%"><thead><tr><th>KD Bayar</th><th>No Bayar</th><th>Kd Regist</th><th>Tgl Bayar</th><th>Pencatat</th><th>Jumlah</th><th>Selisih</th><th>Biaya Transaksi</th><th>Keterangan</th></tr></thead><tbody>';
                            list.forEach(item => {
                                const jumlahFormatted = parseFloat(item.jumlah).toLocaleString('id-ID');
                                html += `<tr><td>${item.kd_bayar}
                                </td><td>${item.no_bayar}</td>
                                <td>${item.kd_registrasi}</td>
                                <td>${item.tgl_bayar}</td>
                                <td>${item.pencatat}</td>
                                <td class="text-right">${jumlahFormatted}</td>
                                <td>${item.selisih}</td>
                                <td>${item.biaya_transaksi}</td>
                                <td>${item.keterangan}</td></tr>`;
                            });
                            html += '</tbody></table>';
                            $('#tableContainer').html(html).show();
                            $('#datatablesSimple').DataTable({
                                responsive: true
                            });
                            $('#downloadButton').show();
                        } else {
                            $('#tableContainer').html('<div class="alert alert-warning text-center">Tidak ada data billing yang ditemukan.</div>').show();
                            $('#downloadButton').hide();
                        }
                    },
                    error: function() {
                        $('#loadingIcon').hide();
                        Swal.fire('Oops...', 'Terjadi kesalahan saat menghubungi server. Silakan coba lagi.', 'error');
                    }
                });
            });

            $('#downloadButton').on('click', function() {
                const tanggalAwal = $('#tanggal_awal').val();
                const tanggalAkhir = $('#tanggal_akhir').val();
                const kdJenisBank = $('#kd_jenisbank').val();
                window.location.href = `<?= base_url("kasirperbankan/unduhdatakasirall") ?>?TANGGAL_AWAL=${tanggalAwal}&TANGGAL_AKHIR=${tanggalAkhir}&KD_JENISBANK=${kdJenisBank}`;
            });
        });
    </script>

</body>

</html>