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

class Keuangan1 extends CI_Controller

{
    public function billingkeunonbpjs_umum()

    {

        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AKHIR']) && 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 downloadbillingkeunonbpjs_umum()

    {
        $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-UMUM."

        ];

        $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

            // Sembunyikan notice PHP 8.1 dari PhpSpreadsheet agar tidak mengganggu output header
            error_reporting(E_ERROR | E_PARSE);

            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-UMUM.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 billingkeunonbpjs_asuransi()

    {

        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AKHIR']) && 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_asuransi',
                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 downloadbillingkeunonbpjs_asuransi()

    {
        $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_asuransi',
            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-ASURANSI."

        ];

        $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

            // Sembunyikan notice PHP 8.1 dari PhpSpreadsheet agar tidak mengganggu output header
            error_reporting(E_ERROR | E_PARSE);

            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-ASURANSI.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 datarekapkeunonbpjs_umum()

    {

        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_umum()

    {
        $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 SIRUS BPJS-UMUM."

        ];

        $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

            // Sembunyikan notice PHP 8.1 dari PhpSpreadsheet agar tidak mengganggu output header
            error_reporting(E_ERROR | E_PARSE);

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


            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $tmpFile = tempnam(sys_get_temp_dir(), 'nonbpjs_xlsx_');
            $writer->save($tmpFile);

            while (ob_get_level()) {
                ob_end_clean();
            }

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

            readfile($tmpFile);
            @unlink($tmpFile);
            exit();
        } else {

            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }



    public function datarekapkeunonbpjs_asuransi()
    {

        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_asuransi',
                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_asuransi()

    {
        set_time_limit(300);
        ini_set('memory_limit', '256M');

        $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_asuransi',
            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 SIRUS BPJS-ASURANSI."
        ];

        $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) {

            $decoded = json_decode($tampil, true);

            if (json_last_error() !== JSON_ERROR_NONE || empty($decoded['tampil']) || !is_array($decoded['tampil'])) {
                http_response_code(422);
                echo json_encode(['error' => 'Data tidak valid atau kosong dari API.']);
                exit();
            }

            $data = $decoded['tampil'];

            // Sembunyikan notice PHP 8.1 dari PhpSpreadsheet agar tidak mengganggu output header
            error_reporting(E_ERROR | E_PARSE);

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


            // Simpan ke file sementara (tidak terkontaminasi output buffer)
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $tmpFile = tempnam(sys_get_temp_dir(), 'asuransi_xlsx_');
            $writer->save($tmpFile);

            while (ob_get_level()) {
                ob_end_clean();
            }

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

            readfile($tmpFile);
            @unlink($tmpFile);
            exit();
        } else {

            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }


    public function rekapkeusirusnonbpjsumumanak()
    {
        // Atur header sebagai JSON di awal untuk semua kemungkinan output
        header('Content-Type: application/json');

        // Pastikan ini adalah request POST
        if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
            // Mengirim error jika bukan method POST
            echo json_encode(['status' => 'error', 'message' => 'Metode request tidak valid.']);
            return;
        }

        if (!isset($_POST['TANGGAL_AWAL']) || !isset($_POST['TANGGAL_AKHIR']) || !isset($_POST['KD_JENIS_PASIEN'])) {
            echo json_encode(['status' => 'error', 'message' => 'Parameter tanggal tidak lengkap.']);
            return;
        }

        try {
            // Mengambil dan memformat tanggal
            $tanggal_awal_input = $_POST['TANGGAL_AWAL'];
            $tanggal_akhir_input = $_POST['TANGGAL_AKHIR'];
            $kdjenispasien = $_POST['KD_JENIS_PASIEN'];

            $tanggal_awal_formatted = date('d/m/Y', strtotime($tanggal_awal_input));
            $tanggal_akhir_formatted = date('d/m/Y', strtotime($tanggal_akhir_input));

            // --- 1. PANGGIL API UNTUK DATA UTAMA (TABEL T2 / Billing) ---
            $curl_main = curl_init();
            curl_setopt_array($curl_main, array(
                // KOREKSI: Tambahkan http:// dan perbaiki garis miring ganda
                CURLOPT_URL => 'http://103.255.241.146/api/service_rs/tampil_rekap_nonbpjs',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => ['TANGGAL_AWAL' => $tanggal_awal_formatted, 'TANGGAL_AKHIR' => $tanggal_akhir_formatted, 'KD_JENIS_PASIEN' => $kdjenispasien],
            ));
            $response_main_raw = curl_exec($curl_main);
            $curl_error_main = curl_error($curl_main);
            curl_close($curl_main);

            if ($curl_error_main) {
                throw new Exception('Gagal terhubung ke API data billing: ' . $curl_error_main);
            }

            // --- 2. PANGGIL API UNTUK DATA RINGKASAN (TABEL T1 / Dokter) ---
            $curl_summary = curl_init();
            curl_setopt_array($curl_summary, array(
                // KOREKSI: Tambahkan http:// dan perbaiki garis miring ganda
                CURLOPT_URL => 'http://103.255.241.146/api/service_rs/tampil_rekap_nonbpjs_anak',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => ['TANGGAL_AWAL' => $tanggal_awal_formatted, 'TANGGAL_AKHIR' => $tanggal_akhir_formatted, 'KD_JENIS_PASIEN' => $kdjenispasien],
            ));
            $response_summary_raw = curl_exec($curl_summary);
            $curl_error_summary = curl_error($curl_summary);
            curl_close($curl_summary);

            if ($curl_error_summary) {
                throw new Exception('Gagal terhubung ke API data dokter: ' . $curl_error_summary);
            }

            // --- 3. DECODE DAN VALIDASI SETIAP RESPON ---

            $data_main = json_decode($response_main_raw, true);
            // Pengecekan JSON yang lebih spesifik
            if (json_last_error() !== JSON_ERROR_NONE) {
                throw new Exception('Gagal mem-parsing data billing. Respons mentah: ' . $response_main_raw);
            }

            $data_summary = json_decode($response_summary_raw, true);
            // Pengecekan JSON yang lebih spesifik
            if (json_last_error() !== JSON_ERROR_NONE) {
                throw new Exception('Gagal mem-parsing data dokter. Respons mentah: ' . $response_summary_raw);
            }

            // --- 4. GABUNGKAN KEDUA HASIL MENJADI SATU ARRAY ---

            // KOREKSI UTAMA: Gunakan 'tampil' untuk $data_main sesuai informasi Anda.
            // Data 'summary' ($data_summary) sudah benar menggunakan 'response'.
            $final_response = [
                'status'   => 'success',
                'response' => $data_main['tampil'] ?? [], // <--- INI PERUBAHANNYA
                'summary'  => $data_summary['response'] ?? []
            ];

            echo json_encode($final_response);
        } catch (Exception $e) {
            // Menangkap semua kemungkinan error dan mengirimkannya sebagai JSON
            echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
        }
    }

    public function unduhrekapkeuinapdpjpanak()
    {
        $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 => 'http://103.255.241.146/api/service_rs/tampil_rekap_nonbpjs_anak',
            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 Rekap KEU NON BPJS-UMUM DPJP ANAK."

        ];
        $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

            // Sembunyikan notice PHP 8.1 dari PhpSpreadsheet agar tidak mengganggu output header
            error_reporting(E_ERROR | E_PARSE);

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

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

            // Set header kolom
            $sheet->setCellValue('A1', 'Nama Dokter');
            $sheet->setCellValue('B1', 'Kode dokter');
            $sheet->setCellValue('C1', 'Jumlah Pasien Inap');
            $sheet->setCellValue('D1', 'Jumlah Bayar Pasien Umum');
            $sheet->setCellValue('E1', 'Jumlah Px Umum tidak Tagih');


            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['nm_dokter']);
                $sheet->setCellValue('B' . $row, $item['kd_spesialis']);
                $sheet->setCellValue('C' . $row, $item['jlm_ri']);
                $sheet->setCellValue('D' . $row, $item['pasien_umum_bayar_umum']);
                $sheet->setCellValue('E' . $row, $item['pasien_umum_gabung_ibu_alias_tidak_di_tagih']);
                $row++;
            }

            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $tmpFile = tempnam(sys_get_temp_dir(), 'dpjpanak_xlsx_');
            $writer->save($tmpFile);

            while (ob_get_level()) {
                ob_end_clean();
            }

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

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



    /**
     * FUNGSI HELPER PRIBADI: Untuk memanggil API cURL
     * (Anda mungkin sudah punya fungsi serupa, jika ya, sesuaikan)
     * Tambahkan private atau protected (diawali underscore _ )
     */
    private function _panggil_api($url, $post_data)
    {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $post_data,
        ));
        $response = curl_exec($curl);
        $error = curl_error($curl);
        curl_close($curl);

        if ($error) {
            throw new Exception("cURL Error: " . $error);
        }
        return $response;
    }


    public function tampildatarekapkeurajal2()

    {

        if (isset($_POST['TANGGAL_AWAL'])) {

            // Mengambil input tanggal dari form
            $tahun_input = $this->input->post('TANGGAL_AWAL'); // Dapat angka misal: 2024

            // Mengambil data dari API

            $curl = curl_init();

            curl_setopt_array($curl, array(

                CURLOPT_URL => '103.255.241.146/api/service_rs/rekap_sirus_asuransi_rajal',
                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' => $tahun_input,

                ),

            ));

            $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 tampildatarekapkeuranap2()

    {

        if (isset($_POST['TANGGAL_AWAL'])) {

            // Mengambil input tanggal dari form
            $tahun_input = $this->input->post('TANGGAL_AWAL'); // Dapat angka misal: 2024

            // Mengambil data dari API

            $curl = curl_init();

            curl_setopt_array($curl, array(

                CURLOPT_URL => '103.255.241.146/api/service_rs/rekap_sirus_asuransi_inap',
                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' => $tahun_input,

                ),

            ));

            $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 unduhrekapkeu_tahunan_rajal()

    {
        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');

        // Ubah format tanggal jika diperlukan
        $tahun_input = $this->input->get('TANGGAL_AWAL'); // Dapat angka misal: 2024

        // Validasi jika tahun kosong (mencegah error API)
        if (empty($tahun_input)) {
            $tahun_input = date('Y');
        }

        // [PERBAIKAN 2] Tambahkan http:// pada URL
        $url_api = 'http://103.255.241.146/api/service_rs/rekap_sirus_asuransi_rajal';



        // Memanggil API menggunakan cURL

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url_api,
            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' => $tahun_input,

            ),

        ));


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

        //log harian download
        $datai = [

            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download Data Rekap KEU Tahunan Rajal."

        ];

        $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

            // Sembunyikan notice PHP 8.1 dari PhpSpreadsheet agar tidak mengganggu output header
            error_reporting(E_ERROR | E_PARSE);

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

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();
            // Set header kolom

            $sheet->setCellValue('A1', 'No Urut');
            $sheet->setCellValue('B1', 'Bulan');
            $sheet->setCellValue('C1', 'Nama Asuransi'); // **Tambahkan kelas hanya jika Rawat Inap**
            $sheet->setCellValue('D1', 'Jumlah');
            $row = 2;

            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['urut']);
                $sheet->setCellValue('B' . $row, $item['bulan']);
                $sheet->setCellValue('C' . $row, $item['nama_asuransi']);
                $sheet->setCellValue('D' . $row, $item['qty']);
                $row++;
            }

            if (ob_get_contents()) ob_end_clean();
            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="rekap_keu_tahunan_rajal.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 unduhrekapkeu_tahunan_ranap()

    {
        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');

        // Ubah format tanggal jika diperlukan
        $tahun_input = $this->input->get('TANGGAL_AWAL'); // Dapat angka misal: 2024


        // Validasi jika tahun kosong (mencegah error API)
        if (empty($tahun_input)) {
            $tahun_input = date('Y');
        }

        $url_api_inap = 'http://103.255.241.146/api/service_rs/rekap_sirus_asuransi_inap';



        // Memanggil API menggunakan cURL

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url_api_inap,
            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' => $tahun_input,

            ),

        ));


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

        //log harian download
        $datai = [

            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download Data Rekap KEU Tahunan Ranap."

        ];

        $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

            // Sembunyikan notice PHP 8.1 dari PhpSpreadsheet agar tidak mengganggu output header
            error_reporting(E_ERROR | E_PARSE);

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

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();
            // Set header kolom

            $sheet->setCellValue('A1', 'Bulan');
            $sheet->setCellValue('B1', 'No Urut'); // **Tambahkan kelas hanya jika Rawat Inap**
            $sheet->setCellValue('C1', 'Nama Asuransi');
            $sheet->setCellValue('D1', 'Nama Dokter');
            $sheet->setCellValue('E1', 'Jumlah');
            $sheet->setCellValue('F1', 'Tarif');
            $row = 2;

            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['bulan']);
                $sheet->setCellValue('B' . $row, $item['urut']);
                $sheet->setCellValue('C' . $row, $item['nama_asuransi']);
                $sheet->setCellValue('D' . $row, $item['nm_dokter']);
                $sheet->setCellValue('E' . $row, $item['qty']);
                $sheet->setCellValue('F' . $row, $item['tarif']);
                $row++;
            }

            if (ob_get_contents()) ob_end_clean();
            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="rekap_keu_tahunan_ranap.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 datarekapkeuhomecare()
    {
        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_homecare_bulan',
                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 unduhrekapkeuhomecare()
    {
        $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_homecare_bulan',
            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);

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

            // Sembunyikan notice PHP 8.1 dari PhpSpreadsheet agar tidak mengganggu output header
            error_reporting(E_ERROR | E_PARSE);

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

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

            // Set header kolom
            $sheet->setCellValue('A1', 'Bulan');
            $sheet->setCellValue('B1', 'Tahun');
            $sheet->setCellValue('C1', 'Jumlah Tagihan');
            $sheet->setCellValue('D1', 'Total Tagihan');

            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['bulan']);
                $sheet->setCellValue('B' . $row, $item['tahun']);
                $sheet->setCellValue('C' . $row, $item['jumlah_pasien']);
                $sheet->setCellValue('D' . $row, $item['total_tagihan']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_rekap_keuangan_HOMECARE.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.']);
        }
    }
}
