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

class Dashboardlayanan 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 filter_data()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }

        $akses = $this->session->userdata("kd_akses");
        $app = $this->session->userdata("kd_app");

        // Ambil tanggal awal dari input POST
        $tgl_awal = $_POST['tanggal_awal'];
        $tgl_akhir = $_POST['tanggal_akhir'];

        // Atur tanggal akhir secara otomatis (misalnya, 1 hari setelah tanggal awal)
        // $tgl_akhir = date('Y-m-d', strtotime($tgl_awal));

        // Format tanggal sesuai kebutuhan API
        $tanggal_awal = date('d/m/Y', strtotime($tgl_awal));
        $tanggal_akhir = date('d/m/Y', strtotime($tgl_akhir));

        // Siapkan data untuk dikirim ke API
        $datai["TANGGAL_AWAL"] = $tanggal_awal;
        $data["TANGGAL_PERIKSA"] = $tanggal_awal;
        $datae["TANGGAL_AKHIR"] = $tanggal_akhir;


        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/d_reg_online/tampil_pasien_smf',
            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 // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $json =  str_replace('\r\n     ', "---0", $response);
        $json =  str_replace('\r\n', "---", $json);
        $json = json_decode($json);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/d_reg_online/tampil_pasien_smf_sum',
            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 // Tambahkan tanggal akhir ke body request
            ),

        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsoni =  str_replace('\r\n     ', "---0", $response);
        $jsoni =  str_replace('\r\n', "---", $jsoni);
        $jsoni = json_decode($jsoni);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/d_reg_online/tampil_pasien_inap_sum',
            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 // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsono =  str_replace('\r\n     ', "---0", $response);
        $jsono =  str_replace('\r\n', "---", $jsono);
        $jsono = json_decode($jsono);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_igd_sum',
            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 // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsonu =  str_replace('\r\n     ', "---0", $response);
        $jsonu =  str_replace('\r\n', "---", $jsonu);
        $jsonu = json_decode($jsonu);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_igd',
            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 // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsonoa =  str_replace('\r\n     ', "---0", $response);
        $jsonoa =  str_replace('\r\n', "---", $jsonoa);
        $jsonoa = json_decode($jsonoa);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/d_reg_online/tampil_pasien_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' => $tanggal_awal,
                'TANGGAL_AKHIR' => $tanggal_akhir // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsone =  str_replace('\r\n     ', "---0", $response);
        $jsone =  str_replace('\r\n', "---", $jsone);
        $jsone = json_decode($jsone);

        $curl = curl_init();
        //API DATA TAMPIL IGD LAMA-BARU
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_igd_lama_baru',
            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 // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsonf =  str_replace('\r\n     ', "---0", $response);
        $jsonf =  str_replace('\r\n', "---", $jsonf);
        $jsonf = json_decode($jsonf);

        $curl = curl_init();

        //API DATA TAMPIL POLI LAMA-BARU
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_poli_lama_baru',
            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 // Tambahkan tanggal akhir ke body request

            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsong =  str_replace('\r\n     ', "---0", $response);
        $jsong =  str_replace('\r\n', "---", $jsong);
        $jsong = json_decode($jsong);

        $curl = curl_init();


        //API DATA TAMPIL KETERSEDIAN TEMPAT TIDUR
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_ketersediaan_tempat_tidur',
            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 // Tambahkan tanggal akhir ke body request

            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsonh =  str_replace('\r\n     ', "---0", $response);
        $jsonh =  str_replace('\r\n', "---", $jsonh);
        $jsonh = json_decode($jsonh);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/daftar_online',
            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 // Tambahkan tanggal akhir ke body request

            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsond =  str_replace('\r\n     ', "---0", $response);
        $jsond =  str_replace('\r\n', "---", $jsond);
        $jsond = json_decode($jsond);

    



        $data["pasienrajal"]                 = $json;
        $data["pasienrajaltotal"]            = $jsoni;
        $data["pasienranap"]                 = $jsone;
        $data["pasienranaptotal"]            = $jsono;
        $data["pasienigdtotal"]              = $jsonu;
        $data["pasienigd"]                   = $jsonoa;
        $data["pasienigdlamabaru"]           = $jsonf;
        $data["pasienpolilamabaru"]          = $jsong;
        $data["ketersediantt"]               = $jsonh;
        $data["daftaronlinerekap"]           = $jsond;
        $data["akses"]               = $akses;
        $data["app"]               = $app;
        $data["tglawal"]        = $tgl_awal;
        $data["tglakhir"]        = $tgl_akhir;
        $this->load->view('dashboard/header', $data);
        $this->load->view('dashboard/dashboardlayananan', $data);
    }



    public function tampildashboardkeuangan()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }

        $akses = $this->session->userdata("kd_akses");
        $app = $this->session->userdata("kd_app");

       // ==========================================
        // PERBAIKAN: CEK POST AGAR TIDAK ERROR "UNDEFINED KEY"
        // ==========================================
        if(isset($_POST['tanggal_awal']) && isset($_POST['tanggal_akhir'])){
            // Jika form di-submit (tombol Filter ditekan)
            $tgl_awal = $_POST['tanggal_awal'];
            $tgl_akhir = $_POST['tanggal_akhir'];
        } else {
            // Jika halaman pertama kali dibuka (belum ada submit)
            $tgl_awal = date('Y-m-d'); // Default hari ini
            $tgl_akhir = date('Y-m-d'); // Default hari ini
        }

        // Format tanggal sesuai kebutuhan API (d/m/Y)
        $tanggal_awal = date('d/m/Y', strtotime($tgl_awal));
        $tanggal_akhir = date('d/m/Y', strtotime($tgl_akhir));

        // Siapkan data untuk dikirim ke API
        // Perbaikan: Satukan variabel agar rapi
        $datai["TANGGAL_AWAL"] = $tanggal_awal;
        $datai["TANGGAL_AKHIR"] = $tanggal_akhir;


        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/d_reg_online/tampil_pasien_smf',
            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 // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $json =  str_replace('\r\n     ', "---0", $response);
        $json =  str_replace('\r\n', "---", $json);
        $json = json_decode($json);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/d_reg_online/tampil_pasien_smf_sum',
            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 // Tambahkan tanggal akhir ke body request
            ),

        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsoni =  str_replace('\r\n     ', "---0", $response);
        $jsoni =  str_replace('\r\n', "---", $jsoni);
        $jsoni = json_decode($jsoni);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/d_reg_online/tampil_pasien_inap_sum',
            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 // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsono =  str_replace('\r\n     ', "---0", $response);
        $jsono =  str_replace('\r\n', "---", $jsono);
        $jsono = json_decode($jsono);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_igd_sum',
            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 // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsonu =  str_replace('\r\n     ', "---0", $response);
        $jsonu =  str_replace('\r\n', "---", $jsonu);
        $jsonu = json_decode($jsonu);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_igd',
            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 // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsonoa =  str_replace('\r\n     ', "---0", $response);
        $jsonoa =  str_replace('\r\n', "---", $jsonoa);
        $jsonoa = json_decode($jsonoa);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/d_reg_online/tampil_pasien_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' => $tanggal_awal,
                'TANGGAL_AKHIR' => $tanggal_akhir // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsone =  str_replace('\r\n     ', "---0", $response);
        $jsone =  str_replace('\r\n', "---", $jsone);
        $jsone = json_decode($jsone);

        $curl = curl_init();
        //API DATA TAMPIL IGD LAMA-BARU
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_igd_lama_baru',
            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 // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsonf =  str_replace('\r\n     ', "---0", $response);
        $jsonf =  str_replace('\r\n', "---", $jsonf);
        $jsonf = json_decode($jsonf);

        $curl = curl_init();

        //API DATA TAMPIL POLI LAMA-BARU
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_poli_lama_baru',
            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 // Tambahkan tanggal akhir ke body request

            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsong =  str_replace('\r\n     ', "---0", $response);
        $jsong =  str_replace('\r\n', "---", $jsong);
        $jsong = json_decode($jsong);

        $curl = curl_init();


        //API DATA TAMPIL KETERSEDIAN TEMPAT TIDUR
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_ketersediaan_tempat_tidur',
            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 // Tambahkan tanggal akhir ke body request

            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsonh =  str_replace('\r\n     ', "---0", $response);
        $jsonh =  str_replace('\r\n', "---", $jsonh);
        $jsonh = json_decode($jsonh);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/daftar_online',
            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 // Tambahkan tanggal akhir ke body request

            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsond =  str_replace('\r\n     ', "---0", $response);
        $jsond =  str_replace('\r\n', "---", $jsond);
        $jsond = json_decode($jsond);

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/inap_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',
            // Saya pasang array kosong sesuai info Anda body-nya kosong
            // Tapi jika nanti datanya tidak terfilter tanggal, coba masukkan parameter tanggal seperti diatas
            CURLOPT_POSTFIELDS => array(), 
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        $json_penjamin =  str_replace('\r\n     ', "---0", $response);
        $json_penjamin =  str_replace('\r\n', "---", $json_penjamin);
        $json_penjamin = json_decode($json_penjamin);

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/dashboard_poli',
            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 // Tambahkan tanggal akhir ke body request
            ),
        ));

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

        $response = str_replace('\r\n     ', "---0", $response);
        $response = str_replace('\r\n', "---", $response);
        $json_dashboardpoli = json_decode($response);
        

        $data["pasienrajal"]                    = $json;
        $data["dashboardpoli"]                  = $json_dashboardpoli;
        $data["pasienrajaltotal"]               = $jsoni;
        $data["pasienranap"]                    = $jsone;
        $data["pasienranaptotal"]               = $jsono;
        $data["inappenjamin"]                   = $json_penjamin;
        $data["pasienigdtotal"]               = $jsonu;
        $data["pasienigd"]                    = $jsonoa;
        $data["pasienigdlamabaru"]              = $jsonf;
        $data["pasienpolilamabaru"]               = $jsong;
        $data["ketersediantt"]               = $jsonh;
        $data["daftaronlinerekap"]           = $jsond;
        $data["akses"]               = $akses;
        $data["app"]               = $app;
        $data["tglawal"]        = $tgl_awal;
        $data["tglakhir"]        = $tgl_akhir;
        $this->load->view('dashboard/header', $data);
        $this->load->view('dashboard/dashboardlayananan', $data);
    }

    public function dashboardpoliperbulan()
    {
        // Cek Sesi Login
        if (!$this->session->userdata('berhasil_loginu')) {
            header('Content-Type: application/json');
            echo json_encode(['error' => 'Sesi habis']);
            return;
        }

        // ========================================================
        // KUNCI PERBAIKAN: Gunakan $this->input->post, JANGAN $_POST
        // ========================================================
        $post_awal = $this->input->post('tanggal_awal');
        $post_akhir = $this->input->post('tanggal_akhir');

        // Jika kosong, beri nilai default
        $tgl_awal = !empty($post_awal) ? $post_awal : date('Y-m-01');
        $tgl_akhir = !empty($post_akhir) ? $post_akhir : date('Y-m-d');

        // Format tanggal untuk API (d/m/Y)
        $tanggal_awal = date('d/m/Y', strtotime($tgl_awal));
        $tanggal_akhir = date('d/m/Y', strtotime($tgl_akhir));

        $datai["TANGGAL_AWAL"] = $tanggal_awal;
        $datai["TANGGAL_AKHIR"] = $tanggal_akhir;

        // 1. POLI
        $curl_poli = curl_init();
        curl_setopt_array($curl_poli, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/dashboard_poli',
            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
        ));
        $response_poli = curl_exec($curl_poli);
        curl_close($curl_poli);
        $response_poli = str_replace('\r\n     ', "---0", $response_poli);
        $response_poli = str_replace('\r\n', "---", $response_poli);
        $json_poli = json_decode($response_poli);

        // 2. IGD
        $curl_igd = curl_init();
        curl_setopt_array($curl_igd, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/dashboard_igd',
            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
        ));
        $response_igd = curl_exec($curl_igd);
        curl_close($curl_igd);
        $response_igd = str_replace('\r\n     ', "---0", $response_igd);
        $response_igd = str_replace('\r\n', "---", $response_igd);
        $json_igd = json_decode($response_igd);

        // 3. INAP
        $curl_inap = curl_init();
        curl_setopt_array($curl_inap, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/dashboard_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 => $datai
        ));
        $response_inap = curl_exec($curl_inap);
        curl_close($curl_inap);
        $response_inap = str_replace('\r\n     ', "---0", $response_inap);
        $response_inap = str_replace('\r\n', "---", $response_inap);
        $json_inap = json_decode($response_inap);

        // 4. OPERASI
        $curl_operasi = curl_init();
        curl_setopt_array($curl_operasi, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/dashboard_operasi',
            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
        ));
        $response_operasi = curl_exec($curl_operasi);
        curl_close($curl_operasi); // <--- Perbaikan typo curl_close
        $response_operasi = str_replace('\r\n     ', "---0", $response_operasi);
        $response_operasi = str_replace('\r\n', "---", $response_operasi);
        $json_operasi = json_decode($response_operasi);

        // 5. POLI TAHUN
        $curl_politahun = curl_init();
        curl_setopt_array($curl_politahun, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/dashboard_poli_tahun',
            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
        ));
        $response_politahun = curl_exec($curl_politahun);
        curl_close($curl_politahun);
        $response_politahun = str_replace('\r\n     ', "---0", $response_politahun);
        $response_politahun = str_replace('\r\n', "---", $response_politahun);
        $json_politahun = json_decode($response_politahun);

        // 6. POLI PULANG
        $curl_polipulang = curl_init();
        curl_setopt_array($curl_polipulang, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/dashboard_poli_pulang',
            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
        ));
        $response_polipulang = curl_exec($curl_polipulang);
        curl_close($curl_polipulang);
        $response_polipulang = str_replace('\r\n     ', "---0", $response_polipulang);
        $response_polipulang = str_replace('\r\n', "---", $response_polipulang);
        $json_polipulang = json_decode($response_polipulang);

        // 7. HOME CARE
        $curl_homecare = curl_init();
        curl_setopt_array($curl_homecare, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/dashboard_home_care',
            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
        ));
        $response_homecare = curl_exec($curl_homecare);
        curl_close($curl_homecare);
        $response_homecare = str_replace('\r\n     ', "---0", $response_homecare);
        $response_homecare = str_replace('\r\n', "---", $response_homecare);
        $json_homecare = json_decode($response_homecare);

        // 7. RAJAL TO INAP 
        $curl_rajaltoinap = curl_init();
        curl_setopt_array($curl_rajaltoinap, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/dashboard_jalan_to_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 => $datai
        ));
        $response_rajaltoinap = curl_exec($curl_rajaltoinap);
        curl_close($curl_rajaltoinap);
        $response_rajaltoinap = str_replace('\r\n     ', "---0", $response_rajaltoinap);
        $response_rajaltoinap = str_replace('\r\n', "---", $response_rajaltoinap);
        $json_rajaltoinap = json_decode($response_rajaltoinap);

        // 6. GABUNGKAN & KELUARKAN JSON (PASTIKAN TIDAK ADA ERROR DI ATASNYA)
        // Bersihkan output buffer jika ada error HTML nyasar sebelumnya
        if (ob_get_length()) ob_clean(); 
        
        header('Content-Type: application/json');
        echo json_encode([
            'poli'      => $json_poli,
            'igd'       => $json_igd,
            'inap'      => $json_inap,
            'operasi'   => $json_operasi,
            'politahun' => $json_politahun,
            'polipulang'=> $json_polipulang,
            'homecare'  => $json_homecare,
            'rajaltoinap'=> $json_rajaltoinap
        ]);
        exit;
    }

    public function batal_sep()
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $kd_registrasi = isset($_POST['kd_registrasi']) ? trim($_POST['kd_registrasi']) : '';
            $no_sep = isset($_POST['no_sep']) ? trim($_POST['no_sep']) : '';

            // Debugging: Periksa data input sebelum dikirim ke API
            if (empty($kd_registrasi) || empty($no_sep)) {
                echo json_encode(array('status' => false, 'message' => 'KD_REGISTRASI atau NO_SEP tidak boleh kosong.'));
                return;
            }

            // Log data sebelum dikirim ke API
            error_log("Mengirim KD_REGISTRASI: $kd_registrasi, NO_SEP: $no_sep");

            // Inisialisasi cURL
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => 'http://103.255.241.146/api/d_reg_online/batal_sep',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => http_build_query(array(
                    'KD_REGISTRASI' => $kd_registrasi,
                    'NO_SEP' => $no_sep
                )),
                CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'),
            ));

            $response = curl_exec($curl);
            $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

            if (curl_errno($curl)) {
                echo json_encode(array('status' => false, 'message' => 'cURL Error: ' . curl_error($curl)));
            } else {
                error_log("Response API: $response");

                $response_data = json_decode($response, true);
                if ($http_code == 200 && isset($response_data['status']) && $response_data['status'] === true) {
                    echo json_encode(array('status' => true, 'message' => 'Berhasil membatalkan SEP.'));
                } else {
                    echo json_encode(array('status' => false, 'message' => 'Gagal membatalkan SEP: ' . ($response_data['message'] ?? 'Unknown error')));
                }
            }

            curl_close($curl);
        } else {
            echo json_encode(array('status' => false, 'message' => 'Invalid request method.'));
        }
    }

    public function download() {}
}
