<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Billingharian extends CI_Controller
{



    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     *	- or -
     * 		http://example.com/index.php/welcome/index
     *	- or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/userguide3/general/urls.html
     */

    public function tampilbillingharian()
    {
        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AWAL']) && isset($_POST['KD_JENIS_PASIEN'])) {
            // Mengambil input tanggal dari form
            $tanggalawal = $_POST['TANGGAL_AWAL'];
            $tanggalakhir = $_POST['TANGGAL_AKHIR'];
            $kdjenispasien = $_POST['KD_JENIS_PASIEN'];

            // Mengonversi format tanggal ke dd/mm/yyyy
            $tanggal_awal = date('d/m/Y', strtotime($tanggalawal));
            $tanggal_akhir = date('d/m/Y', strtotime($tanggalakhir));

            // Mengambil data dari API
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/service_rs/billing_harian',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'TANGGAL_AWAL' => $tanggal_awal,
                    'TANGGAL_AKHIR' => $tanggal_akhir,
                    'KD_JENIS_PASIEN' => $kdjenispasien
                ),
            ));

            $response = curl_exec($curl);
            curl_close($curl);

            // Mengembalikan respons API dalam format JSON
            if ($response) {
                // Jika response bukan JSON, kirimkan error
                json_decode($response);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    echo json_encode(['error' => 'Data tidak valid.']);
                } else {
                    echo $response; // Kirimkan kembali data yang benar
                }
            } else {
                echo json_encode(['error' => 'Tidak ada data yang ditemukan atau terjadi kesalahan.']);
            }
        }
    }

    public function downloadbillingharian()
    {
        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');
        $tanggal_awal = $this->input->get('TANGGAL_AWAL');
        $tanggal_akhir = $this->input->get('TANGGAL_AKHIR');
        $kdjenispasien = $this->input->get('KD_JENIS_PASIEN');

        // Ubah format tanggal jika diperlukan
        $tanggal_awali = date('d/m/Y', strtotime($tanggal_awal));
        $tanggal_akhiri = date('d/m/Y', strtotime($tanggal_akhir));

        // Memanggil API menggunakan cURL
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/billing_harian',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awali,
                'TANGGAL_AKHIR' => $tanggal_akhiri,
                'KD_JENIS_PASIEN' => $kdjenispasien

            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);

        //log harian download
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download Data Billing Harian."

        ];
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => '103.255.241.146/api/service_rs/sinaran_download',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $datai,
        ]);

        $responsee = curl_exec($curl);
        curl_close($curl);
        if ($response) {
            $data = json_decode($response, true)['response'];

            // Load library PhpSpreadsheet
            //require_once APPPATH . 'third_party/PhpSpreadsheet/src/Bootstrap.php';

            // Load library PhpSpreadsheet
            require_once APPPATH . '../vendor/autoload.php';

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // Set header kolom
            $sheet->setCellValue('A1', 'Jenis');
            $sheet->setCellValue('B1', 'Nama Tindakan');
            $sheet->setCellValue('C1', 'Jumlah');
            $sheet->setCellValue('D1', 'Total');


            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['jenis']);
                $sheet->setCellValue('B' . $row, $item['nm_tindakan']);
                $sheet->setCellValue('C' . $row, $item['qty']);
                $sheet->setCellValue('D' . $row, $item['total']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_billing_harian.xlsx"');
            header('Cache-Control: max-age=0');
            header('Expires: 0');
            header('Pragma: public');

            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $writer->save('php://output');
            exit();
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }


    public function tampilbillingharianpenjamin()
    {
        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AWAL']) && isset($_POST['KD_JENIS_PASIEN'])) {

            // Mengambil input tanggal dari form
            $tanggalawal = $_POST['TANGGAL_AWAL'];
            $tanggalakhir = $_POST['TANGGAL_AKHIR'];
            $kdjenispasien = $_POST['KD_JENIS_PASIEN'];

            // Mengonversi format tanggal ke dd/mm/yyyy
            $tanggal_awal = date('d/m/Y', strtotime($tanggalawal));
            $tanggal_akhir = date('d/m/Y', strtotime($tanggalakhir));

            // Mengambil data dari API
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/service_rs/billing_harian_penjamin',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'TANGGAL_AWAL' => $tanggal_awal,
                    'TANGGAL_AKHIR' => $tanggal_akhir,
                    'KD_JENIS_PASIEN' => $kdjenispasien
                ),
            ));

            $response = curl_exec($curl);
            curl_close($curl);

            // Mengembalikan respons API dalam format JSON
            if ($response) {
                // Jika response bukan JSON, kirimkan error
                json_decode($response);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    echo json_encode(['error' => 'Data tidak valid.']);
                } else {
                    echo $response; // Kirimkan kembali data yang benar
                }
            } else {
                echo json_encode(['error' => 'Tidak ada data yang ditemukan atau terjadi kesalahan.']);
            }
        }
    }

    public function downloadbillingharianpenjamin()
    {
        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');
        $tanggal_awal = $this->input->get('TANGGAL_AWAL');
        $tanggal_akhir = $this->input->get('TANGGAL_AKHIR');
        $kdjenispasien = $this->input->get('KD_JENIS_PASIEN');

        // Ubah format tanggal jika diperlukan
        $tanggal_awali = date('d/m/Y', strtotime($tanggal_awal));
        $tanggal_akhiri = date('d/m/Y', strtotime($tanggal_akhir));

        // Memanggil API menggunakan cURL
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/billing_harian_penjamin',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awali,
                'TANGGAL_AKHIR' => $tanggal_akhiri,
                'KD_JENIS_PASIEN' => $kdjenispasien

            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);

        //log harian download
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download Data Billing Harian Penjamin."

        ];
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => '103.255.241.146/api/service_rs/sinaran_download',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $datai,
        ]);

        $responsee = curl_exec($curl);
        curl_close($curl);

        if ($response) {
            $data = json_decode($response, true)['response'];

            // Load library PhpSpreadsheet
            //require_once APPPATH . 'third_party/PhpSpreadsheet/src/Bootstrap.php';

            // Load library PhpSpreadsheet
            require_once APPPATH . '../vendor/autoload.php';

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // Set header kolom
            $sheet->setCellValue('A1', 'Jenis');
            $sheet->setCellValue('B1', 'Nama Tindakan');
            $sheet->setCellValue('C1', 'Jumlah');
            $sheet->setCellValue('D1', 'Pembayaran');
            $sheet->setCellValue('E1', 'Total');


            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['jenis']);
                $sheet->setCellValue('B' . $row, $item['nm_tindakan']);
                $sheet->setCellValue('C' . $row, $item['qty']);
                $sheet->setCellValue('D' . $row, $item['nm_pembayaran']);
                $sheet->setCellValue('E' . $row, $item['total']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_billing_harian_penjamin.xlsx"');
            header('Cache-Control: max-age=0');
            header('Expires: 0');
            header('Pragma: public');

            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $writer->save('php://output');
            exit();
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }



    public function tampilbillingkeuanganall()
    {
        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AWAL']) && isset($_POST['KD_JENIS_PASIEN'])) {
            // Mengambil input tanggal dari form
            $tanggalawal = $_POST['TANGGAL_AWAL'];
            $tanggalakhir = $_POST['TANGGAL_AKHIR'];
            $kdjenispasien = $_POST['KD_JENIS_PASIEN'];

            // Mengonversi format tanggal ke dd/mm/yyyy
            $tanggal_awal = date('d/m/Y', strtotime($tanggalawal));
            $tanggal_akhir = date('d/m/Y', strtotime($tanggalakhir));

            // Mengambil data dari API
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/service_rs/tampil',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'TANGGAL_AWAL' => $tanggal_awal,
                    'TANGGAL_AKHIR' => $tanggal_akhir,
                    'KD_JENIS_PASIEN' => $kdjenispasien
                ),
            ));

            $tampil = curl_exec($curl);
            curl_close($curl);

            // Mengembalikan respons API dalam format JSON
            if ($tampil) {
                // Jika response bukan JSON, kirimkan error
                json_decode($tampil);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    echo json_encode(['error' => 'Data tidak valid.']);
                } else {
                    echo $tampil; // Kirimkan kembali data yang benar
                }
            } else {
                echo json_encode(['error' => 'Tidak ada data yang ditemukan atau terjadi kesalahan.']);
            }
        }
    }


    public function downloadbillingkeuanganall()
    {
        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');
        $tanggal_awal = $this->input->get('TANGGAL_AWAL');
        $tanggal_akhir = $this->input->get('TANGGAL_AKHIR');
        $kdjenispasien = $this->input->get('KD_JENIS_PASIEN');

        // Ubah format tanggal jika diperlukan
        $tanggal_awali = date('d/m/Y', strtotime($tanggal_awal));
        $tanggal_akhiri = date('d/m/Y', strtotime($tanggal_akhir));

        // Memanggil API menggunakan cURL
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awali,
                'TANGGAL_AKHIR' => $tanggal_akhiri,
                'KD_JENIS_PASIEN' => $kdjenispasien

            ),
        ));

        $tampil = curl_exec($curl);
        curl_close($curl);

        //log harian download
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download Data Billing Keuangan Sirus & BPJS."

        ];
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => '103.255.241.146/api/service_rs/sinaran_download',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $datai,
        ]);

        $responsee = curl_exec($curl);
        curl_close($curl);

        if ($tampil) {
            $data = json_decode($tampil, true)['tampil'];

            // Load library PhpSpreadsheet
            //require_once APPPATH . 'third_party/PhpSpreadsheet/src/Bootstrap.php';

            // Load library PhpSpreadsheet
            require_once APPPATH . '../vendor/autoload.php';

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // Set header kolom
            $sheet->setCellValue('A1', 'Kode Regist');
            $sheet->setCellValue('B1', 'Nama Pasien');
            $sheet->setCellValue('C1', 'Nama Pembayaran');
            $sheet->setCellValue('D1', 'Nama Poliklinik');
            $sheet->setCellValue('E1', 'Kelas');
            $sheet->setCellValue('F1', 'Tanggal Registrasi');
            $sheet->setCellValue('G1', 'Status Periksa');
            $sheet->setCellValue('H1', 'Keterangan');
            $sheet->setCellValue('I1', 'Tarif Inacbg');
            $sheet->setCellValue('J1', 'DPJP');
            $sheet->setCellValue('K1', 'Prosedur Non Bedah');
            $sheet->setCellValue('L1', 'Tenaga Ahli');
            $sheet->setCellValue('M1', 'Radiologi');
            $sheet->setCellValue('N1', 'Rehabilitasi');
            $sheet->setCellValue('O1', 'Obat');
            $sheet->setCellValue('P1', 'Sewa Alat');
            $sheet->setCellValue('Q1', 'Prosedur Bedah');
            $sheet->setCellValue('R1', 'Keperawatan');
            $sheet->setCellValue('S1', 'Laboratorium');
            $sheet->setCellValue('T1', 'Kamar dan Akomodasi');
            $sheet->setCellValue('U1', 'Alkes');
            $sheet->setCellValue('V1', 'Konsultasi');
            $sheet->setCellValue('W1', 'Penunjang');
            $sheet->setCellValue('X1', 'Pelayanan Darah');
            $sheet->setCellValue('Y1', 'Rawat Intensif');
            $sheet->setCellValue('Z1', 'BMHP');
            $sheet->setCellValue('AA1', 'Total Tagihan');

            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['kd_registrasi']);
                $sheet->setCellValue('B' . $row, $item['nm_pasien']);
                $sheet->setCellValue('C' . $row, $item['nm_pembayaran']);
                $sheet->setCellValue('D' . $row, $item['nm_poliklinik']);
                $sheet->setCellValue('E' . $row, $item['kelas']);
                $sheet->setCellValue('F' . $row, $item['tgl_registrasi']);
                $sheet->setCellValue('G' . $row, $item['nm_status_periksa']);
                $sheet->setCellValue('H' . $row, $item['keterangan']);
                $sheet->setCellValue('I' . $row, $item['tarif_inacbg']);
                $sheet->setCellValue('J' . $row, $item['dpjp']);
                $sheet->setCellValue('K' . $row, $item['prosedur_non_bedah']);
                $sheet->setCellValue('L' . $row, $item['tenaga_ahli']);
                $sheet->setCellValue('M' . $row, $item['radiologi']);
                $sheet->setCellValue('N' . $row, $item['rehabilitasi']);
                $sheet->setCellValue('O' . $row, $item['obat']);
                $sheet->setCellValue('P' . $row, $item['sewa_alat']);
                $sheet->setCellValue('Q' . $row, $item['prosedur_bedah']);
                $sheet->setCellValue('R' . $row, $item['keperawatan']);
                $sheet->setCellValue('S' . $row, $item['laboratorium']);
                $sheet->setCellValue('T' . $row, $item['kamar_dan_akomodasi']);
                $sheet->setCellValue('U' . $row, $item['alkes']);
                $sheet->setCellValue('V' . $row, $item['konsultasi']);
                $sheet->setCellValue('W' . $row, $item['penunjang']);
                $sheet->setCellValue('X' . $row, $item['pelayanan_darah']);
                $sheet->setCellValue('Y' . $row, $item['rawat_intensif']);
                $sheet->setCellValue('Z' . $row, $item['bmhp']);
                $sheet->setCellValue('AA' . $row, $item['total_tagihan']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_billing_keuangan_sirus&bpjs.xlsx"');
            header('Cache-Control: max-age=0');
            header('Expires: 0');
            header('Pragma: public');

            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $writer->save('php://output');
            exit();
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }


    public function tampilbillingnonbpjs()
    {
        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AWAL']) && isset($_POST['KD_JENIS_PASIEN'])) {
            // Mengambil input tanggal dari form
            $tanggalawal = $_POST['TANGGAL_AWAL'];
            $tanggalakhir = $_POST['TANGGAL_AKHIR'];
            $kdjenispasien = $_POST['KD_JENIS_PASIEN'];

            // Mengonversi format tanggal ke dd/mm/yyyy
            $tanggal_awal = date('d/m/Y', strtotime($tanggalawal));
            $tanggal_akhir = date('d/m/Y', strtotime($tanggalakhir));

            // Mengambil data dari API
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_nonbpjs',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'TANGGAL_AWAL' => $tanggal_awal,
                    'TANGGAL_AKHIR' => $tanggal_akhir,
                    'KD_JENIS_PASIEN' => $kdjenispasien
                ),
            ));

            $tampil_nonbpjs = curl_exec($curl);
            curl_close($curl);

            // Mengembalikan respons API dalam format JSON
            if ($tampil_nonbpjs) {
                // Jika response bukan JSON, kirimkan error
                json_decode($tampil_nonbpjs);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    echo json_encode(['error' => 'Data tidak valid.']);
                } else {
                    echo $tampil_nonbpjs; // Kirimkan kembali data yang benar
                }
            } else {
                echo json_encode(['error' => 'Tidak ada data yang ditemukan atau terjadi kesalahan.']);
            }
        }
    }


    public function downloadbillingnonbpjs()
    {
        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');
        $tanggal_awal = $this->input->get('TANGGAL_AWAL');
        $tanggal_akhir = $this->input->get('TANGGAL_AKHIR');
        $kdjenispasien = $this->input->get('KD_JENIS_PASIEN');

        // Ubah format tanggal jika diperlukan
        $tanggal_awali = date('d/m/Y', strtotime($tanggal_awal));
        $tanggal_akhiri = date('d/m/Y', strtotime($tanggal_akhir));

        // Memanggil API menggunakan cURL
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_nonbpjs',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awali,
                'TANGGAL_AKHIR' => $tanggal_akhiri,
                'KD_JENIS_PASIEN' => $kdjenispasien

            ),
        ));

        $tampil_nonbpjs = curl_exec($curl);
        curl_close($curl);

        //log harian download
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download Data Billing Keuangan Sirus NON BPJS."

        ];
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => '103.255.241.146/api/service_rs/sinaran_download',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $datai,
        ]);

        $responsee = curl_exec($curl);
        curl_close($curl);

        if ($tampil_nonbpjs) {
            $data = json_decode($tampil_nonbpjs, true)['tampil_nonbpjs'];

            // Load library PhpSpreadsheet
            //require_once APPPATH . 'third_party/PhpSpreadsheet/src/Bootstrap.php';

            // Load library PhpSpreadsheet
            require_once APPPATH . '../vendor/autoload.php';

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // Set header kolom
            $sheet->setCellValue('A1', 'Kode Regist');
            $sheet->setCellValue('B1', 'Nama Pasien');
            if ($kdjenispasien = 2) { // **Tambahkan kelas hanya jika Rawat Inap**
                $sheet->setCellValue('C1', 'Kelas');
                $sheet->setCellValue('D1', 'Tanggal Registrasi');
                $sheet->setCellValue('E1', 'Status Periksa');
                $sheet->setCellValue('F1', 'Total Tagihan');
            } else {
                $sheet->setCellValue('C1', 'Tanggal Registrasi');
                $sheet->setCellValue('D1', 'Status Periksa');
                $sheet->setCellValue('E1', 'Total Tagihan');
            }
            $sheet->setCellValue('G1', 'DPJP');
            $sheet->setCellValue('H1', 'Prosedur Non Bedah');
            $sheet->setCellValue('I1', 'Tenaga Ahli');
            $sheet->setCellValue('J1', 'Radiologi');
            $sheet->setCellValue('K1', 'Rehabilitasi');
            $sheet->setCellValue('L1', 'Obat');
            $sheet->setCellValue('M1', 'Sewa Alat');
            $sheet->setCellValue('N1', 'Prosedur Bedah');
            $sheet->setCellValue('O1', 'Keperawatan');
            $sheet->setCellValue('P1', 'Laboratorium');
            $sheet->setCellValue('Q1', 'Kamar dan Akomodasi');
            $sheet->setCellValue('R1', 'Alkes');
            $sheet->setCellValue('S1', 'Konsultasi');
            $sheet->setCellValue('T1', 'Penunjang');
            $sheet->setCellValue('U1', 'Pelayanan Darah');
            $sheet->setCellValue('V1', 'Rawat Intensif');
            $sheet->setCellValue('W1', 'BMHP');
            $sheet->setCellValue('X1', 'Nama Penjamin');
            $sheet->setCellValue('Y1', 'Nama Poliklinik');

            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['kd_registrasi']);
                $sheet->setCellValue('B' . $row, $item['nm_pasien']);
                if ($kdjenispasien = 2) { // **Tambahkan kelas hanya jika bukan Rawat Jalan**
                    $sheet->setCellValue('C' . $row, $item['kelas']);
                    $sheet->setCellValue('D' . $row, $item['tgl_registrasi']);
                    $sheet->setCellValue('E' . $row, $item['nm_status_periksa']);
                    $sheet->setCellValue('F' . $row, $item['total_tagihan']);
                } else {
                    $sheet->setCellValue('C' . $row, $item['tgl_registrasi']);
                    $sheet->setCellValue('D' . $row, $item['nm_status_periksa']);
                    $sheet->setCellValue('E' . $row, $item['total_tagihan']);
                }
                $sheet->setCellValue('G' . $row, $item['dpjp']);
                $sheet->setCellValue('H' . $row, $item['prosedur_non_bedah']);
                $sheet->setCellValue('I' . $row, $item['tenaga_ahli']);
                $sheet->setCellValue('J' . $row, $item['radiologi']);
                $sheet->setCellValue('K' . $row, $item['rehabilitasi']);
                $sheet->setCellValue('L' . $row, $item['obat']);
                $sheet->setCellValue('M' . $row, $item['sewa_alat']);
                $sheet->setCellValue('N' . $row, $item['prosedur_bedah']);
                $sheet->setCellValue('O' . $row, $item['keperawatan']);
                $sheet->setCellValue('P' . $row, $item['laboratorium']);
                $sheet->setCellValue('Q' . $row, $item['kamar_dan_akomodasi']);
                $sheet->setCellValue('R' . $row, $item['alkes']);
                $sheet->setCellValue('S' . $row, $item['konsultasi']);
                $sheet->setCellValue('T' . $row, $item['penunjang']);
                $sheet->setCellValue('U' . $row, $item['pelayanan_darah']);
                $sheet->setCellValue('V' . $row, $item['rawat_intensif']);
                $sheet->setCellValue('W' . $row, $item['bmhp']);
                $sheet->setCellValue('X' . $row, $item['nm_pembayaran']);
                $sheet->setCellValue('Y' . $row, $item['nm_poliklinik']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_billing_keuangan_sirus_nonbpjs.xlsx"');
            header('Cache-Control: max-age=0');
            header('Expires: 0');
            header('Pragma: public');

            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $writer->save('php://output');
            exit();
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }



    public function tampilpasienikbs()
    {
        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AWAL'])) {
            // Mengambil input tanggal dari form
            $tanggalawal = $_POST['TANGGAL_AWAL'];
            $tanggalakhir = $_POST['TANGGAL_AKHIR'];

            // Mengonversi format tanggal ke dd/mm/yyyy
            $tanggal_awal = date('d/m/Y', strtotime($tanggalawal));
            $tanggal_akhir = date('d/m/Y', strtotime($tanggalakhir));

            // Mengambil data dari API
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/service_rs/ikbs_pasien',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'TANGGAL_AWAL' => $tanggal_awal,
                    'TANGGAL_AKHIR' => $tanggal_akhir
                ),
            ));

            $response = curl_exec($curl);
            curl_close($curl);

            // Mengembalikan respons API dalam format JSON
            if ($response) {
                // Jika response bukan JSON, kirimkan error
                json_decode($response);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    echo json_encode(['error' => 'Data tidak valid.']);
                } else {
                    echo $response; // Kirimkan kembali data yang benar
                }
            } else {
                echo json_encode(['error' => 'Tidak ada data yang ditemukan atau terjadi kesalahan.']);
            }
        }
    }

    public function downloadpasienikbs()
    {
        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');
        $tanggal_awal = $this->input->get('TANGGAL_AWAL');
        $tanggal_akhir = $this->input->get('TANGGAL_AKHIR');

        // Ubah format tanggal jika diperlukan
        $tanggal_awali = date('d/m/Y', strtotime($tanggal_awal));
        $tanggal_akhiri = date('d/m/Y', strtotime($tanggal_akhir));

        // Memanggil API menggunakan cURL
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/ikbs_pasien',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awali,
                'TANGGAL_AKHIR' => $tanggal_akhiri
            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);

        //log harian download
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download data pasien IKBS."

        ];
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => '103.255.241.146/api/service_rs/sinaran_download',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $datai,
        ]);

        $responsee = curl_exec($curl);
        curl_close($curl);


        if ($response) {
            $data = json_decode($response, true)['response'];

            // Load library PhpSpreadsheet
            //require_once APPPATH . 'third_party/PhpSpreadsheet/src/Bootstrap.php';

            // Load library PhpSpreadsheet
            require_once APPPATH . '../vendor/autoload.php';

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // Set header kolom
            $sheet->setCellValue('A1', 'Kode Operasi');
            $sheet->setCellValue('B1', 'Kode Regist');
            $sheet->setCellValue('C1', 'Tanggal Regist');
            $sheet->setCellValue('D1', 'Nama Pasien');
            $sheet->setCellValue('E1', 'Nama Pembayaran');
            $sheet->setCellValue('F1', 'Nama Poli');
            $sheet->setCellValue('G1', 'Nama Jenis Pasien');
            $sheet->setCellValue('H1', 'Nama Tindakan OP');
            $sheet->setCellValue('I1', 'Realisasi Tindakan');
            $sheet->setCellValue('J1', 'Nama Dokter');
            $sheet->setCellValue('K1', 'Asal PPK');

            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['operasi']);
                $sheet->setCellValue('B' . $row, $item['kd_registrasi']); 
                $sheet->setCellValue('C' . $row, $item['tgl_registrasi']);
                $sheet->setCellValue('D' . $row, $item['nm_pasien']);
                $sheet->setCellValue('E' . $row, $item['nm_pembayaran']);
                $sheet->setCellValue('F' . $row, $item['nm_poliklinik']);
                $sheet->setCellValue('G' . $row, $item['nm_jenis_pasien']);
                $sheet->setCellValue('H' . $row, $item['tindakan_op']);
                $sheet->setCellValue('I' . $row, $item['realisasi_tindakan']);
                $sheet->setCellValue('J' . $row, $item['nm_dokter']);
                $sheet->setCellValue('K' . $row, $item['asal_ppk']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_pasien_IKBS.xlsx"');
            header('Cache-Control: max-age=0');
            header('Expires: 0');
            header('Pragma: public');

            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $writer->save('php://output');
            exit();
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }
    public function tampilpenjamin()
    {
        if (isset($_POST['TANGGAL_AWAL'])) {
            // Mengambil input tanggal dari form
            $tanggal_periksa = $_POST['TANGGAL_AWAL'];

            // Mengonversi format tanggal ke dd/mm/yyyy
            $tanggal_periksa = date('d/m/Y', strtotime($tanggal_periksa));
            // $tanggal_akhir = date('d/m/Y', strtotime($tanggalakhir));

            // Mengambil data dari API
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/service_rs/penjamin_poliklinik',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'TANGGAL_PERIKSA' => $tanggal_periksa,

                ),
            ));

            $response = curl_exec($curl);
            curl_close($curl);

            // Mengembalikan respons API dalam format JSON
            if ($response) {
                // Jika response bukan JSON, kirimkan error
                json_decode($response);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    echo json_encode(['error' => 'Data tidak valid.']);
                } else {
                    echo $response; // Kirimkan kembali data yang benar
                }
            } else {
                echo json_encode(['error' => 'Tidak ada data yang ditemukan atau terjadi kesalahan.']);
            }
        }
    }

    public function downloadpenjamin()
    {
        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');
        $tanggal_periksa = $this->input->get('TANGGAL_AWAL');
        // $tanggal_akhir = $this->input->get('TANGGAL_AKHIR');

        // Ubah format tanggal jika diperlukan
        $tanggal_periksa = date('d/m/Y', strtotime($tanggal_periksa));
        // $tanggal_akhiri = date('d/m/Y', strtotime($tanggal_akhir));

        // Memanggil API menggunakan cURL
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/penjamin_poliklinik',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_PERIKSA' => $tanggal_periksa,
                // 'TANGGAL_AKHIR' => $tanggal_akhiri
            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);

        //log harian download
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download data Penjamin Poliklinik."

        ];
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => '103.255.241.146/api/service_rs/sinaran_download',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $datai,
        ]);

        $responsee = curl_exec($curl);
        curl_close($curl);

        if ($response) {
            $data = json_decode($response, true)['response'];

            // Load library PhpSpreadsheet
            //require_once APPPATH . 'third_party/PhpSpreadsheet/src/Bootstrap.php';

            // Load library PhpSpreadsheet
            require_once APPPATH . '../vendor/autoload.php';

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // Set header kolom
            $sheet->setCellValue('A1', 'Nama Pembayaran');
            $sheet->setCellValue('B1', 'Jumlah');


            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['nm_pembayaran']);
                $sheet->setCellValue('B' . $row, $item['qty']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_penjamin_poliklinik.xlsx"');
            header('Cache-Control: max-age=0');

            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $writer->save('php://output');
            exit();
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }

    public function rekapbpjsnonbpjs()
    {
        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AWAL'])) {
            // Mengambil input tanggal dari form
            $tanggalawal = $_POST['TANGGAL_AWAL'];
            $tanggalakhir = $_POST['TANGGAL_AKHIR'];

            // Mengonversi format tanggal ke dd/mm/yyyy
            $tanggal_awal = date('d/m/Y', strtotime($tanggalawal));
            $tanggal_akhir = date('d/m/Y', strtotime($tanggalakhir));

            // Mengambil data dari API
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/service_rs/rekap_sirus_nonbpjs_bpjs',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'TANGGAL_AWAL' => $tanggal_awal,
                    'TANGGAL_AKHIR' => $tanggal_akhir
                ),
            ));

            $response = curl_exec($curl);
            curl_close($curl);

            // Mengembalikan respons API dalam format JSON
            if ($response) {
                // Jika response bukan JSON, kirimkan error
                json_decode($response);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    echo json_encode(['error' => 'Data tidak valid.']);
                } else {
                    echo $response; // Kirimkan kembali data yang benar
                }
            } else {
                echo json_encode(['error' => 'Tidak ada data yang ditemukan atau terjadi kesalahan.']);
            }
        }
    }

    public function downloadrekapsirusbpjsnonbpjs()
    {
        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');
        $tanggal_awal = $this->input->get('TANGGAL_AWAL');
        $tanggal_akhir = $this->input->get('TANGGAL_AKHIR');

        // Ubah format tanggal jika diperlukan
        $tanggal_awali = date('d/m/Y', strtotime($tanggal_awal));
        $tanggal_akhiri = date('d/m/Y', strtotime($tanggal_akhir));

        // Memanggil API menggunakan cURL
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/rekap_sirus_nonbpjs_bpjs',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awali,
                'TANGGAL_AKHIR' => $tanggal_akhiri
            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);

        //log harian download
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download data Rekap SIRUS NON BPJS-BPJS."

        ];
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => '103.255.241.146/api/service_rs/sinaran_download',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $datai,
        ]);

        $responsee = curl_exec($curl);
        curl_close($curl);


        if ($response) {
            $data = json_decode($response, true)['response'];

            // Load library PhpSpreadsheet
            //require_once APPPATH . 'third_party/PhpSpreadsheet/src/Bootstrap.php';

            // Load library PhpSpreadsheet
            require_once APPPATH . '../vendor/autoload.php';

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // Set header kolom
            $sheet->setCellValue('A1', 'Kode Operasi');
            $sheet->setCellValue('B1', 'Kode Regist');
            $sheet->setCellValue('C1', 'Tanggal Regist');

            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['nm_jenis_pasien']);
                $sheet->setCellValue('B' . $row, $item['nm_group_bayar']);
                $sheet->setCellValue('C' . $row, $item['total']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_rekap_bpjs_nonbpjs.xlsx"');
            header('Cache-Control: max-age=0');
            header('Expires: 0');
            header('Pragma: public');

            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $writer->save('php://output');
            exit();
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }

    public function datarekapkeubpjs()
    {
        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AKHIR']) && isset($_POST['KD_JENIS_PASIEN'])) {
            $tanggalawal   = $_POST['TANGGAL_AWAL'];
            $tanggalakhir  = $_POST['TANGGAL_AKHIR'];
            $kdjenispasien = $_POST['KD_JENIS_PASIEN'];

            // Satu kali panggilan API dengan rentang penuh (jauh lebih cepat)
            $api_awal  = date('d/m/Y', strtotime($tanggalawal));
            $api_akhir = date('d/m/Y', strtotime($tanggalakhir));

            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_rekap_bpjs',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 60,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'TANGGAL_AWAL'    => $api_awal,
                    'TANGGAL_AKHIR'   => $api_akhir,
                    'KD_JENIS_PASIEN' => $kdjenispasien
                ),
            ));
            $response = curl_exec($curl);
            curl_close($curl);

            $all_data = [];
            if ($response) {
                $parsed = json_decode($response, true);
                if (json_last_error() === JSON_ERROR_NONE && isset($parsed['tampil']) && is_array($parsed['tampil'])) {
                    $all_data = $parsed['tampil'];
                }
            }

            // Group by DPJP dan jumlahkan kolom numerik
            if (!empty($all_data)) {
                $numeric_cols = ['jlm_ri', 'tarif_bpjs', 'tarif_rs', 'prosedur_non_bedah', 'prosedur_bedah', 'konsultasi', 'keperawatan', 'tenaga_ahli', 'laboratorium', 'radiologi', 'kamar_akomodasi', 'obat', 'bmhp'];
                $grouped = [];
                foreach ($all_data as $row) {
                    $raw_dpjp = isset($row['dpjp']) ? $row['dpjp'] : 'Tidak Diketahui';
                    $key = strtoupper(trim(preg_replace('/\s+/u', ' ', $raw_dpjp)));
                    if (!isset($grouped[$key])) {
                        $grouped[$key] = ['dpjp' => trim(preg_replace('/\s+/u', ' ', $raw_dpjp))];
                        foreach ($numeric_cols as $col) {
                            $grouped[$key][$col] = 0;
                        }
                    }
                    foreach ($numeric_cols as $col) {
                        $val = isset($row[$col]) ? $row[$col] : 0;
                        $grouped[$key][$col] += is_numeric($val) ? (float)$val : 0;
                    }
                }
                echo json_encode(['tampil' => array_values($grouped)]);
            } else {
                echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
            }
        }
    }


    public function downloadrekapkeubpjs()
    {
        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');
        $tanggal_awal  = $this->input->get('TANGGAL_AWAL');
        $tanggal_akhir = $this->input->get('TANGGAL_AKHIR');
        $kdjenispasien = $this->input->get('KD_JENIS_PASIEN');

        // Satu kali panggilan API dengan rentang penuh
        $api_awal  = date('d/m/Y', strtotime($tanggal_awal));
        $api_akhir = date('d/m/Y', strtotime($tanggal_akhir));

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_rekap_bpjs',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL'    => $api_awal,
                'TANGGAL_AKHIR'   => $api_akhir,
                'KD_JENIS_PASIEN' => $kdjenispasien
            ),
        ));
        $response_api = curl_exec($curl);
        curl_close($curl);

        $all_data = [];
        if ($response_api) {
            $parsed = json_decode($response_api, true);
            if (json_last_error() === JSON_ERROR_NONE && isset($parsed['tampil']) && is_array($parsed['tampil'])) {
                $all_data = $parsed['tampil'];
            }
        }

        // Group by DPJP dan jumlahkan kolom numerik
        $numeric_cols = ['jlm_ri', 'tarif_bpjs', 'tarif_rs', 'prosedur_non_bedah', 'prosedur_bedah', 'konsultasi', 'keperawatan', 'tenaga_ahli', 'laboratorium', 'radiologi', 'kamar_akomodasi', 'obat', 'bmhp', 'penunjang', 'pelayanan_darah', 'rehabilitasi', 'rawat_intensif', 'alkes', 'sewa_alat'];
        $grouped = [];
        foreach ($all_data as $row) {
            $raw_dpjp = isset($row['dpjp']) ? $row['dpjp'] : 'Tidak Diketahui';
            $key = strtoupper(trim(preg_replace('/\s+/u', ' ', $raw_dpjp)));
            if (!isset($grouped[$key])) {
                $grouped[$key] = ['dpjp' => trim(preg_replace('/\s+/u', ' ', $raw_dpjp))];
                foreach ($numeric_cols as $col) {
                    $grouped[$key][$col] = 0;
                }
            }
            foreach ($numeric_cols as $col) {
                $val = isset($row[$col]) ? $row[$col] : 0;
                $grouped[$key][$col] += is_numeric($val) ? (float)$val : 0;
            }
        }
        $data_grouped = array_values($grouped);
        $tampil = !empty($data_grouped);

        //log harian download (timeout singkat agar tidak memblokir proses)
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download Data Rekap Keuangan SIRUS BPJS."
        ];
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => '103.255.241.146/api/service_rs/sinaran_download',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 3,
            CURLOPT_TIMEOUT => 5,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $datai,
        ]);

        $responsee = curl_exec($curl);
        curl_close($curl);


        if ($tampil) {
            $data = $data_grouped;

            require_once APPPATH . '../vendor/autoload.php';

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // Set header kolom
            $sheet->setCellValue('A1', 'DPJP');
            $sheet->setCellValue('B1', 'Jumlah ');
            $sheet->setCellValue('C1', 'Tarif BPJS');
            $sheet->setCellValue('D1', 'Tarif RS');
            $sheet->setCellValue('E1', 'Prosedur Non Bedah');
            $sheet->setCellValue('F1', 'Prosedur Bedah');
            $sheet->setCellValue('G1', 'Konsultasi');
            $sheet->setCellValue('H1', 'Tenaga Ahli');
            $sheet->setCellValue('I1', 'Keperawatan');
            $sheet->setCellValue('J1', 'Penunjang');
            $sheet->setCellValue('K1', 'Radiologi');
            $sheet->setCellValue('L1', 'Laboratorium');
            $sheet->setCellValue('M1', 'Pelayanan Darah');
            $sheet->setCellValue('N1', 'Rehabilitasi');
            $sheet->setCellValue('O1', 'Kamar Akomodasi');
            $sheet->setCellValue('P1', 'Rawat Intensif');
            $sheet->setCellValue('Q1', 'Obat');
            $sheet->setCellValue('R1', 'Alkes');
            $sheet->setCellValue('S1', 'BMHP');
            $sheet->setCellValue('T1', 'Sewa Alat');

            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['dpjp']);
                $sheet->setCellValue('B' . $row, $item['jlm_ri']);
                $sheet->setCellValue('C' . $row, $item['tarif_bpjs']);
                $sheet->setCellValue('D' . $row, $item['tarif_rs']);
                $sheet->setCellValue('E' . $row, $item['prosedur_non_bedah']);
                $sheet->setCellValue('F' . $row, $item['prosedur_bedah']);
                $sheet->setCellValue('G' . $row, $item['konsultasi']);
                $sheet->setCellValue('H' . $row, $item['tenaga_ahli']);
                $sheet->setCellValue('I' . $row, $item['keperawatan']);
                $sheet->setCellValue('J' . $row, $item['penunjang']);
                $sheet->setCellValue('K' . $row, $item['radiologi']);
                $sheet->setCellValue('L' . $row, $item['laboratorium']);
                $sheet->setCellValue('M' . $row, $item['pelayanan_darah']);
                $sheet->setCellValue('N' . $row, $item['rehabilitasi']);
                $sheet->setCellValue('O' . $row, $item['kamar_akomodasi']);
                $sheet->setCellValue('P' . $row, $item['rawat_intensif']);
                $sheet->setCellValue('Q' . $row, $item['obat']);
                $sheet->setCellValue('R' . $row, $item['alkes']);
                $sheet->setCellValue('S' . $row, $item['bmhp']);
                $sheet->setCellValue('T' . $row, $item['sewa_alat']);
                $row++;
            }

            // Simpan ke file sementera lalu kirim — ini cara paling andal di CodeIgniter
            // agar tidak ada byte dari buffer CI yang ikut masuk dan merusak xlsx
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $tmpFile = tempnam(sys_get_temp_dir(), 'bpjs_xlsx_');
            $writer->save($tmpFile);

            // Bersihkan semua level output buffer milik CI
            while (ob_get_level()) {
                ob_end_clean();
            }

            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_rekap_keuangan_bpjs.xlsx"');
            header('Cache-Control: max-age=0');
            header('Expires: 0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($tmpFile));

            readfile($tmpFile);
            @unlink($tmpFile);
            exit();
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }

    public function datarekapkeunonbpjs()
    {
        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AWAL']) && isset($_POST['KD_JENIS_PASIEN'])) {
            // Mengambil input tanggal dari form
            $tanggalawal = $_POST['TANGGAL_AWAL'];
            $tanggalakhir = $_POST['TANGGAL_AKHIR'];
            $kdjenispasien = $_POST['KD_JENIS_PASIEN'];

            // Mengonversi format tanggal ke dd/mm/yyyy
            $tanggal_awal = date('d/m/Y', strtotime($tanggalawal));
            $tanggal_akhir = date('d/m/Y', strtotime($tanggalakhir));

            // Mengambil data dari API
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_rekap_nonbpjs',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'TANGGAL_AWAL' => $tanggal_awal,
                    'TANGGAL_AKHIR' => $tanggal_akhir,
                    'KD_JENIS_PASIEN' => $kdjenispasien
                ),
            ));

            $tampil = curl_exec($curl);
            curl_close($curl);

            // Mengembalikan respons API dalam format JSON
            if ($tampil) {
                // Jika response bukan JSON, kirimkan error
                json_decode($tampil);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    echo json_encode(['error' => 'Data tidak valid.']);
                } else {
                    echo $tampil; // Kirimkan kembali data yang benar
                }
            } else {
                echo json_encode(['error' => 'Tidak ada data yang ditemukan atau terjadi kesalahan.']);
            }
        }
    }


    public function downloadrekapkeunonbpjs()
    {
        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');
        $tanggal_awal = $this->input->get('TANGGAL_AWAL');
        $tanggal_akhir = $this->input->get('TANGGAL_AKHIR');
        $kdjenispasien = $this->input->get('KD_JENIS_PASIEN');

        // Ubah format tanggal jika diperlukan
        $tanggal_awali = date('d/m/Y', strtotime($tanggal_awal));
        $tanggal_akhiri = date('d/m/Y', strtotime($tanggal_akhir));

        // Memanggil API menggunakan cURL
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_rekap_nonbpjs',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awali,
                'TANGGAL_AKHIR' => $tanggal_akhiri,
                'KD_JENIS_PASIEN' => $kdjenispasien
            ),
        ));

        $tampil = curl_exec($curl);
        curl_close($curl);

        //log harian download
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download Data Rekap KEUANGAN NON BPJS ."

        ];
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => '103.255.241.146/api/service_rs/sinaran_download',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $datai,
        ]);

        $responsee = curl_exec($curl);
        curl_close($curl);


        if ($tampil) {
            $data = json_decode($tampil, true)['tampil'];

            // Load library PhpSpreadsheet
            //require_once APPPATH . 'third_party/PhpSpreadsheet/src/Bootstrap.php';

            // Load library PhpSpreadsheet
            require_once APPPATH . '../vendor/autoload.php';

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // Set header kolom
            $sheet->setCellValue('A1', 'DPJP');
            $sheet->setCellValue('B1', 'Jumlah ');
            $sheet->setCellValue('C1', 'Tarif RS');
            $sheet->setCellValue('D1', 'Prosedur Non Bedah');
            $sheet->setCellValue('E1', 'Prosedur Bedah');
            $sheet->setCellValue('F1', 'Konsultasi');
            $sheet->setCellValue('G1', 'Tenaga Ahli');
            $sheet->setCellValue('H1', 'Keperawatan');
            $sheet->setCellValue('I1', 'Penunjang');
            $sheet->setCellValue('J1', 'Radiologi');
            $sheet->setCellValue('K1', 'Laboratorium');
            $sheet->setCellValue('L1', 'Pelayanan Darah');
            $sheet->setCellValue('M1', 'Rehabilitasi');
            $sheet->setCellValue('N1', 'Kamar Akomodasi');
            $sheet->setCellValue('O1', 'Rawat Intensif');
            $sheet->setCellValue('P1', 'Obat');
            $sheet->setCellValue('Q1', 'Alkes');
            $sheet->setCellValue('R1', 'BMHP');
            $sheet->setCellValue('S1', 'Sewa Alat');

            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['dpjp']);
                $sheet->setCellValue('B' . $row, $item['jlm_ri']);
                $sheet->setCellValue('C' . $row, $item['tarif_rs']);
                $sheet->setCellValue('D' . $row, $item['prosedur_non_bedah']);
                $sheet->setCellValue('E' . $row, $item['prosedur_bedah']);
                $sheet->setCellValue('F' . $row, $item['konsultasi']);
                $sheet->setCellValue('G' . $row, $item['tenaga_ahli']);
                $sheet->setCellValue('H' . $row, $item['keperawatan']);
                $sheet->setCellValue('I' . $row, $item['penunjang']);
                $sheet->setCellValue('J' . $row, $item['radiologi']);
                $sheet->setCellValue('K' . $row, $item['laboratorium']);
                $sheet->setCellValue('L' . $row, $item['pelayanan_darah']);
                $sheet->setCellValue('M' . $row, $item['rehabilitasi']);
                $sheet->setCellValue('N' . $row, $item['kamar_akomodasi']);
                $sheet->setCellValue('O' . $row, $item['rawat_intensif']);
                $sheet->setCellValue('P' . $row, $item['obat']);
                $sheet->setCellValue('Q' . $row, $item['alkes']);
                $sheet->setCellValue('R' . $row, $item['bmhp']);
                $sheet->setCellValue('S' . $row, $item['sewa_alat']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_rekap_keuangan_nonbpjs.xlsx"');
            header('Cache-Control: max-age=0');
            header('Expires: 0');
            header('Pragma: public');

            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $writer->save('php://output');
            exit();
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }

    public function tampilbillingharianicu()
    {
        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AKHIR'])) {
            // Mengambil input tanggal dari form
            $tanggalawal = $_POST['TANGGAL_AWAL'];
            $tanggalakhir = $_POST['TANGGAL_AKHIR'];


            // Mengonversi format tanggal ke dd/mm/yyyy
            $tanggal_awal = date('d/m/Y', strtotime($tanggalawal));
            $tanggal_akhir = date('d/m/Y', strtotime($tanggalakhir));

            // Mengambil data dari API
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/service_rs/billing_harian_icu',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'TANGGAL_AWAL' => $tanggal_awal,
                    'TANGGAL_AKHIR' => $tanggal_akhir
                ),
            ));

            $response = curl_exec($curl);
            curl_close($curl);

            // Mengembalikan respons API dalam format JSON
            if ($response) {
                // Jika response bukan JSON, kirimkan error
                json_decode($response);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    echo json_encode(['error' => 'Data tidak valid.']);
                } else {
                    echo $response; // Kirimkan kembali data yang benar
                }
            } else {
                echo json_encode(['error' => 'Tidak ada data yang ditemukan atau terjadi kesalahan.']);
            }
        }
    }

    public function downloadbillingharianicu()
    {
        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');
        $tanggal_awal = $this->input->get('TANGGAL_AWAL');
        $tanggal_akhir = $this->input->get('TANGGAL_AKHIR');


        // Ubah format tanggal jika diperlukan
        $tanggal_awali = date('d/m/Y', strtotime($tanggal_awal));
        $tanggal_akhiri = date('d/m/Y', strtotime($tanggal_akhir));

        // Memanggil API menggunakan cURL
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/billing_harian_icu',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awali,
                'TANGGAL_AKHIR' => $tanggal_akhiri,

            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);

        //log harian download
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download Data Billing Harian ICU/HCU."

        ];
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => '103.255.241.146/api/service_rs/sinaran_download',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $datai,
        ]);

        $responsee = curl_exec($curl);
        curl_close($curl);
        if ($response) {
            $data = json_decode($response, true)['response'];

            // Load library PhpSpreadsheet
            //require_once APPPATH . 'third_party/PhpSpreadsheet/src/Bootstrap.php';

            // Load library PhpSpreadsheet
            require_once APPPATH . '../vendor/autoload.php';

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // Set header kolom
            $sheet->setCellValue('A1', 'KD Registrasi');
            $sheet->setCellValue('B1', 'Nama Pasien');
            $sheet->setCellValue('C1', 'Nama Ruangan');
            $sheet->setCellValue('D1', 'No Bed');
            $sheet->setCellValue('E1', 'Tgl Tindakan');
            $sheet->setCellValue('F1', 'Nama Tindakan');
            $sheet->setCellValue('G1', 'Jumlah');
            $sheet->setCellValue('H1', 'Total');


            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['kd_registrasi']);
                $sheet->setCellValue('B' . $row, $item['nm_pasien']);
                $sheet->setCellValue('C' . $row, $item['nm_ruang']);
                $sheet->setCellValue('D' . $row, $item['nm_bed']);
                $sheet->setCellValue('E' . $row, $item['tgl_tindakan']);
                $sheet->setCellValue('F' . $row, $item['nm_tindakan']);
                $sheet->setCellValue('G' . $row, $item['qty']);
                $sheet->setCellValue('H' . $row, $item['total']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_billing_harian_ICU/HCU.xlsx"');
            header('Cache-Control: max-age=0');
            header('Expires: 0');
            header('Pragma: public');

            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $writer->save('php://output');
            exit();
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }
}
