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

class Dashboard 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 index()
    {
        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");

        $data['akses'] = $akses;
        $data['app'] = $app;
        $akses_valid = ['9999', '101', '112', 'DIR', 'RMI', 'IGD', 'ICU'];

        if (in_array($akses, $akses_valid)) { //UBAH KODE APP nya disini aja kalo mau masuk halaman mana aja 
            $akses = $this->session->userdata("kd_akses");

            $app = $this->session->userdata("kd_app");
            $tgl_awal = date('d/m/Y');

            $datai["TANGGAL_AWAL"]    = $tgl_awal;
            $data["TANGGAL_PERIKSA"]    = $tgl_awal;
            $datai["TANGGAL_AKHIR"]    = $tgl_awal;
            $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 => $datai
            ));

            $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 => $datai
            ));

            $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 => $datai
            ));

            $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 => $datai
            ));

            $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 => $datai
            ));

            $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 => $datai
            ));

            $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 => $datai
            ));

            $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 => $datai
            ));

            $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 => $datai
            ));

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


            $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["akses"]                          = $akses;
            $data["app"]                            = $app;
            $data["tglawal"]                        = date('Y-m-d');
            $this->load->view('dashboard/header', $data);
            $this->load->view('dashboard/dashboardlayananan', $data);
        } else {
            $files = glob('./uploads/*.pdf'); // Ambil semua file PDF
            $file_data = [];

            foreach ($files as $file) {
                $file_name = basename($file); // Hanya nama file
                $file_data[] = [
                    'file_name' => $file_name,
                    'file_path' => base_url('uploads/' . $file_name),
                ];
            }
            $this->load->view('header', $data);
            $this->load->view('pdf', ['file_data' => $file_data]);
        }
    }


    public function menejemen()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('index', $data);
    }
    public function rujukan()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('rujuk', $data);
    }

    public function demografiusia()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        // $data['akses'] = $this->session->userdata("kd_akses");
        // $data['app'] = $this->session->userdata("kd_app");
        // $data["tgl_awal"]        = date('Y-m-d');
        // $data["tgl_akhir"]        = date('Y-m-d');
        // $this->load->view('header', $data);
        // $this->load->view('demografiusia', $data);
        $akses = $this->session->userdata("kd_akses");
        $app = $this->session->userdata("kd_app");

        $tgl_awal = date('d/m/Y');

        $datai["TANGGAL_AWAL"]    = $tgl_awal;
        $datai["TANGGAL_AKHIR"]    = $tgl_awal;

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/demografi_kurang_satu_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 = curl_exec($curl);

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

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/demografi_satu_sampai_tiga_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 = curl_exec($curl);

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

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/demografi_tiga_sampai_lima_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 = curl_exec($curl);

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

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/demografi_lima_sampai_delapanbelas_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 = 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/demografi_delapanbelas_sampai_limadepalan_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 = 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();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/demografi_lebihdari_limadepalan_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 = curl_exec($curl);

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

        $data["usiakurang1thn"]                    = $jsona;
        $data["usia1sampai3thn"]               = $jsonb;
        $data["usia3sampai5thn"]               = $jsonc;
        $data["usia5sampai18thn"]                 = $jsond;
        $data["usia18sampai58thn"]                 = $jsone;
        $data["usia58thnlebih"]                 = $jsonf;
        $data["akses"]               = $akses;
        $data["app"]               = $app;
        $data["tgl_awal"]        = date('Y-m-d');
        $this->load->view('dashboard/header', $data);
        $this->load->view('dashboard/demografiusia', $data);
    }

    public function demografialamat()
    {
        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");

        $tgl_awal = date('d/m/Y');

        $datai["TANGGAL_AWAL"]    = $tgl_awal;
        $datai["TANGGAL_AKHIR"]    = $tgl_awal;

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/demografi_kabupate_kota',
            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 = curl_exec($curl);

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

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/demografi_kecamatan',
            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 = curl_exec($curl);

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

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/demografi_desa',
            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 = curl_exec($curl);

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


        $data["demokabupatenkota"]                = $jsona;
        $data["demokecamatan"]               = $jsonb;
        $data["demodesa"]               = $jsonc;
        $data["akses"]               = $akses;
        $data["app"]               = $app;
        $data["tgl_awal"]        = date('Y-m-d');
        $this->load->view('dashboard/header', $data);
        $this->load->view('dashboard/demografialamat', $data);
    }

    public function dashboardpasienigd()
    {
        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");

        $tgl_awal = date('d/m/Y');

        $datai["TANGGAL_AWAL"]    = $tgl_awal;
        $datai["TANGGAL_AKHIR"]    = $tgl_awal;

        $curl = curl_init();

        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 =>  $datai
        ));

        $response = curl_exec($curl);

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

        $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 =>  $datai
        ));

        $response = curl_exec($curl);

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

        $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 =>  $datai
        ));

        $response = curl_exec($curl);

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


        $data["igdlamabaru"]                = $jsona;
        $data["igdhariinitotal"]               = $jsonb;
        $data["igdtampilhariini"]               = $jsonc;
        $data["akses"]               = $akses;
        $data["app"]               = $app;
        $data["tgl_awal"]        = date('Y-m-d');
        $this->load->view('dashboard/header', $data);
        $this->load->view('dashboard/dashboardpasienigd', $data);
    }

    public function pasiensmfpoli()
    {
        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");

        $tgl_awal = date('d/m/Y');

        $datai["TANGGAL_AWAL"]    = $tgl_awal;
        $datai["TANGGAL_AKHIR"]    = $tgl_awal;

        $curl = curl_init();

        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 =>  $datai
        ));

        $response = curl_exec($curl);

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

        $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 =>  $datai
        ));

        $response = curl_exec($curl);

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

        $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 =>  $datai
        ));

        $response = curl_exec($curl);

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


        $data["pasienpolilamabaru"]                = $jsona;
        $data["pasienrajaltotal"]               = $jsonb;
        $data["tampilpasienrajal"]               = $jsonc;
        $data["akses"]               = $akses;
        $data["app"]               = $app;
        $data["tgl_awal"]        = date('Y-m-d');
        $this->load->view('dashboard/header', $data);
        $this->load->view('dashboard/pasiensmfpoli', $data);
    }

    public function datarawatinap()
    {
        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");

        $tgl_awal = date('d/m/Y');

        $datai["TANGGAL_AWAL"]    = $tgl_awal;
        $datai["TANGGAL_AKHIR"]    = $tgl_awal;

        $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 =>  $datai
        ));

        $response = curl_exec($curl);

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

        $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 =>  $datai
        ));

        $response = curl_exec($curl);

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

        $curl = curl_init();

        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 =>  $datai
        ));

        $response = curl_exec($curl);

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


        $data["pasieninap"]                = $jsona;
        $data["totalpasieninap"]               = $jsonb;
        $data["ketersediantt"]               = $jsonc;
        $data["akses"]               = $akses;
        $data["app"]               = $app;
        $data["tgl_awal"]        = date('Y-m-d');
        $this->load->view('dashboard/header', $data);
        $this->load->view('dashboard/pasienrawatinap', $data);
    }

    public function pasienigdtotal()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('pasienigdtotal', $data);
    }

    public function pasienigdbatal()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('pasienigdbatal', $data);
    }

    public function pasienigddilayani()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('pasienigddilayani', $data);
    }

    public function rajalhomecare()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('rajalhomecare', $data);
    }


    public function pasienrajaltotal()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('pasienrajaltotal', $data);
    }


    public function politotalkeuangan()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('politotalkeuangan', $data);
    }

    public function polibatalkeuangan()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('polibatalkeuangan', $data);
    }

    public function polidilayanikeuangan()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('polidilayanikeuangan', $data);
    }


    public function polihomecarekeuangan()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('polihomecarekeuangan', $data);
    }

    public function pasienrajalbatal()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('pasienrajalbatal', $data);
    }

    public function pasienrajaldilayani()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('pasienrajaldilayani', $data);
    }

    public function pasientransfer()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('pasienrajaltransfer', $data);
    }

    public function antreanpolirajal()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('antreanpoli', $data);
    }


    public function demografipekerjaan()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('demografipekerjaan', $data);
    }

    public function demografijeniskelamin()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('demografijeniskelamin', $data);
    }

    public function demografiasalkeinap()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('demografiasalkeinap', $data);
    }

    public function dokumen()
    {
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");

        $files = glob('./uploads/*.pdf'); // Ambil semua file PDF
        $file_data = [];

        foreach ($files as $file) {
            $file_name = basename($file); // Hanya nama file
            $file_data[] = [
                'file_name' => $file_name,
                'file_path' => base_url('uploads/' . $file_name),
            ];
        }
        $this->load->view('header', $data);
        $this->load->view('pdf', ['file_data' => $file_data]);
    }

    public function penjaminpoli()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('penjaminpoli', $data);
    }

    public function penjaminigd()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('penjaminigd', $data);
    }
    public function utama()
    {
        $akses = $this->session->userdata("kd_akses");
        $app = $this->session->userdata("kd_app");

        $tgl_awal = date('d/m/Y');

        $datai["TANGGAL_AWAL"]    = $tgl_awal;
        $datai["TANGGAL_AKHIR"]    = $tgl_awal;
        $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 => $datai
        ));

        $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 => $datai
        ));

        $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 => $datai
        ));

        $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 => $datai
        ));

        $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 => $datai
        ));

        $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 => $datai
        ));

        $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 => $datai
        ));

        $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 => $datai
        ));

        $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 => $datai
        ));

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


        $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["akses"]                          = $akses;
        $data["app"]                            = $app;
        $data["tglawal"]                        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('dashboard/dashboardlayananan', $data);
    }


    public function penjamininap()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('penjamininap', $data);
    }

    public function pasieninapdpjp()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('pasieninapdpjp', $data);
    }

    public function pasieninaptotal()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('pasieninaptotal', $data);
    }

    public function batalresumeranap()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $this->load->view('header', $data);
        $this->load->view('batalresume', $data);
    }

    public function dashboardlayanan()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('dashboardlayananan', $data);
    }

    public function farmasipenjualan()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('farmasipenjualan', $data);
    }

    public function farmasiwaktuserah()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('waktuserahfarmasi', $data);
    }

    public function billingharian()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('billingharian', $data);
    }

    public function menuikbs()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('pasienikbs', $data);
    }

    // public function penjaminpolikeuangan()
    // {
    //     if (!$this->session->userdata('berhasil_loginu')) {
    //         redirect(''); // Jika tidak login, redirect ke halaman login
    //     }
    //     $data['akses'] = $this->session->userdata("kd_akses");
    //     $data['app'] = $this->session->userdata("kd_app");
    //     $data["tgl_awal"]        = date('Y-m-01');
    //     $data["tgl_akhir"]        = date('Y-m-d');
    //     $this->load->view('header', $data);
    //     $this->load->view('penjaminpolikeuangan', $data);
    // }

    public function penjaminpolikeuangan()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('penjaminpolikeuangan', $data);
    }


    public function radiologipenjualan()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('radiologipenjualan', $data);
    }


    public function laboratoriumpenjualan()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }
        $data['akses'] = $this->session->userdata("kd_akses");
        $data['app'] = $this->session->userdata("kd_app");
        $data["tgl_awal"]        = date('Y-m-01');
        $data["tgl_akhir"]        = date('Y-m-d');
        $this->load->view('header', $data);
        $this->load->view('laboratoriumpenjualan', $data);
    }

    public function reproses_data()
    {
        if (isset($_POST['KD_JENIS_PASIEN']) && isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AKHIR'])) {
            $kd_jenis_pasien = $_POST['KD_JENIS_PASIEN'];
            $tanggalawal = $_POST['TANGGAL_AWAL'];
            $tanggalakhir = $_POST['TANGGAL_AKHIR'];
            $tanggal_awal             = strtotime($tanggalawal);
            $tanggal_awal             = date('d/m/Y', $tanggal_awal);
            $tanggal_akhir             = strtotime($tanggalakhir);
            $tanggal_akhir             = date('d/m/Y', $tanggal_akhir);

            $curl = curl_init();

            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/d_reg_online/tampil_pasien',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'KD_JENIS_PASIEN' => $kd_jenis_pasien,
                    'TANGGAL_AWAL' => $tanggal_awal,
                    'TANGGAL_AKHIR' => $tanggal_akhir
                ),
            ));

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

            // Mengubah response menjadi array asosiatif
            $data = json_decode($response, true);

            // Mengecek jika data valid
            if (is_array($data)) {
                echo json_encode($data); // Mengembalikan data sebagai JSON
            } else {
                echo json_encode([]); // Jika tidak ada data, kembalikan array kosong
            }
        } else {
            // Jika parameter tidak valid
            echo json_encode(['error' => 'Parameter tidak lengkap.']);
        }
    }

    public function batal_serah()
    {
        // Pastikan method request adalah POST
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            // Ambil data KD_REGISTRASI dari permintaan POST
            $kd_registrasi = isset($_POST['kd_registrasi']) ? $_POST['kd_registrasi'] : '';

            // Periksa apakah KD_REGISTRASI telah diisi
            if (!empty($kd_registrasi)) {
                // Inisialisasi cURL
                $curl = curl_init();

                // Set parameter cURL
                curl_setopt_array($curl, array(
                    CURLOPT_URL => '103.255.241.146/api/d_reg_online/batal_serah',
                    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(
                        'KD_REGISTRASI' => $kd_registrasi, // Mengirim kode registrasi dari permintaan
                        'STATUS_SERAH' => '0' // Status untuk pembatalan serah
                    ),
                ));

                // Eksekusi permintaan cURL dan ambil respons dari API
                $response = curl_exec($curl);

                // Periksa apakah ada error dalam eksekusi cURL
                if (curl_errno($curl)) {
                    echo 'Error:' . curl_error($curl);
                } else {
                    // Tampilkan respons API
                    echo $response;
                }

                // Tutup cURL
                curl_close($curl);
            } else {
                // Jika KD_REGISTRASI kosong, kirimkan pesan kesalahan
                echo json_encode(array('status' => false, 'message' => 'KD_REGISTRASI tidak valid.'));
            }
        } else {
            // Jika request method bukan POST
            echo json_encode(array('status' => false, 'message' => 'Invalid request method.'));
        }
    }
    public function batal_resume()
    {
        // Pastikan method request adalah POST
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            // Ambil data KD_REGISTRASI dari permintaan POST
            $kd_registrasi = isset($_POST['kd_registrasi']) ? $_POST['kd_registrasi'] : '';
            $kd_jenis_pasien = isset($_POST['kd_jenis_pasien']) ? $_POST['kd_jenis_pasien'] : '';

            // Periksa apakah KD_REGISTRASI dan KD_JENIS_PASIEN saat kosong
            if (!empty($kd_registrasi) && !empty($kd_jenis_pasien)) {
                // Inisialisasi cURL
                $curl = curl_init();

                // Set parameter cURL
                curl_setopt_array($curl, array(
                    CURLOPT_URL => '103.255.241.146/api/d_reg_online/batal_resume',
                    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(
                        'KD_REGISTRASI' => $kd_registrasi, // Mengirim kode registrasi dari permintaan
                        'KD_JENIS_PASIEN' => $kd_jenis_pasien // Mengirim kode jenis pasien dari permintaan
                    ),
                ));

                // Eksekusi permintaan cURL dan ambil respons dari API
                $response = curl_exec($curl);

                // Periksa apakah ada error dalam eksekusi cURL
                if (curl_errno($curl)) {
                    echo 'Error:' . curl_error($curl);
                } else {
                    // Tampilkan respons API
                    echo $response;
                }

                // Tutup cURL
                curl_close($curl);
            } else {
                // Jika KD_REGISTRASI kosong, kirimkan pesan kesalahan
                echo json_encode(array('status' => false, 'message' => 'KD_REGISTRASI tidak valid.'));
            }
        } else {
            // Jika request method bukan POST
            echo json_encode(array('status' => false, 'message' => 'Invalid request method.'));
        }
    }

    public function batal_sep()
    {
        // Pastikan method request adalah POST
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            // Ambil data KD_REGISTRASI dari permintaan POST
            $kd_registrasi = isset($_POST['kd_registrasi']) ? $_POST['kd_registrasi'] : '';
            $keterangan = isset($_POST['keterangan']) ? $_POST['keterangan'] : '';

            // Periksa apakah KD_REGISTRASI dan KD_JENIS_PASIEN saat kosong
            if (!empty($kd_registrasi) && !empty($keterangan)) {
                // Inisialisasi cURL
                $curl = curl_init();

                // Set parameter cURL
                curl_setopt_array($curl, array(
                    CURLOPT_URL => '103.255.241.146/api/d_reg_online/batal_sep',
                    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(
                        'KD_REGISTRASI' => $kd_registrasi, // Mengirim kode registrasi dari permintaan
                        'NO_SEP' => '0142R033' . $keterangan // Mengirim NO SEP
                    ),
                ));

                // Eksekusi permintaan cURL dan ambil respons dari API
                $response = curl_exec($curl);

                // Periksa apakah ada error dalam eksekusi cURL
                if (curl_errno($curl)) {
                    echo 'Error:' . curl_error($curl);
                } else {
                    // Tampilkan respons API
                    echo $response;
                }

                // Tutup cURL
                curl_close($curl);
            } else {
                // Jika KD_REGISTRASI kosong, kirimkan pesan kesalahan
                echo json_encode(array('status' => false, 'message' => 'KD_REGISTRASI tidak valid.'));
            }
        } else {
            // Jika request method bukan POST
            echo json_encode(array('status' => false, 'message' => 'Invalid request method.'));
        }
    }

    public function reproses_kasir()
    {
        // Pastikan method request adalah POST
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            // Ambil data KD_REGISTRASI dari permintaan POST
            $kd_registrasi = isset($_POST['kd_registrasi']) ? $_POST['kd_registrasi'] : '';

            // Periksa apakah KD_REGISTRASI saat kosong
            if (!empty($kd_registrasi)) {
                // Inisialisasi cURL
                $curl = curl_init();

                // Set parameter cURL
                curl_setopt_array($curl, array(
                    CURLOPT_URL => '103.255.241.146/api/d_reg_online/batal_billing',
                    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(
                        'KD_REGISTRASI' => $kd_registrasi, // Mengirim kode registrasi dari permintaan
                    ),
                ));

                // Eksekusi permintaan cURL dan ambil respons dari API
                $response = curl_exec($curl);

                // Periksa apakah ada error dalam eksekusi cURL
                if (curl_errno($curl)) {
                    echo 'Error:' . curl_error($curl);
                } else {
                    // Tampilkan respons API
                    echo $response;
                }

                // Tutup cURL
                curl_close($curl);
            } else {
                // Jika KD_REGISTRASI kosong, kirimkan pesan kesalahan
                echo json_encode(array('status' => false, 'message' => 'KD_REGISTRASI tidak valid.'));
            }
        } else {
            // Jika request method bukan POST
            echo json_encode(array('status' => false, 'message' => 'Invalid request method.'));
        }
    }
    public function batal_kasir()
    {
        // Pastikan method request adalah POST
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            // Ambil data KD_REGISTRASI dari permintaan POST
            $kd_registrasi = isset($_POST['kd_registrasi']) ? $_POST['kd_registrasi'] : '';

            // Periksa apakah KD_REGISTRASI telah diisi
            if (!empty($kd_registrasi)) {
                // Inisialisasi cURL
                $curl = curl_init();

                // Set parameter cURL
                curl_setopt_array($curl, array(
                    CURLOPT_URL => '103.255.241.146/api/d_reg_online/batal_kasir',
                    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(
                        'KD_REGISTRASI' => $kd_registrasi, // Mengirim kode registrasi dari permintaan
                        'STATUS_BAYAR_PULANG' => '0' // Status untuk pembatalan kasir
                    ),
                ));

                // Eksekusi permintaan cURL dan ambil respons dari API
                $response = curl_exec($curl);

                // Periksa apakah ada error dalam eksekusi cURL
                if (curl_errno($curl)) {
                    echo 'Error:' . curl_error($curl);
                } else {
                    // Tampilkan respons API
                    echo $response;
                }

                // Tutup cURL
                curl_close($curl);
            } else {
                // Jika KD_REGISTRASI kosong, kirimkan pesan kesalahan
                echo json_encode(array('status' => false, 'message' => 'KD_REGISTRASI tidak valid.'));
            }
        } else {
            // Jika request method bukan POST
            echo json_encode(array('status' => false, 'message' => 'Invalid request method.'));
        }
    }

    public function dpjp()
    {
        // Ambil input pencarian dari parameter GET
        $search = $this->input->get('search');

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/d_reg_online/ref_dokter',
            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',
        ));

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

        // Decode response dari API
        $data = json_decode($response, true);

        // Jika API mengembalikan data dengan sukses, filter berdasarkan pencarian
        if ($data['status'] && !empty($data['response']['list'])) {
            if ($search) {
                // Filter daftar dokter berdasarkan pencarian
                $data['response']['list'] = array_filter($data['response']['list'], function ($dokter) use ($search) {
                    return stripos($dokter['nm_dokter'], $search) !== false; // Kasus tidak sensitif
                });
            }
            echo json_encode($data); // Mengembalikan data dokter yang difilter
        } else {
            echo json_encode(['status' => false, 'message' => 'Gagal mengambil data dari API.']);
        }
    }

    public function tambah_dpjp()
    {
        // Pastikan method request adalah POST
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            // Ambil data KD_REGISTRASI dari permintaan POST
            $kd_registrasi = isset($_POST['kd_registrasi']) ? $_POST['kd_registrasi'] : '';
            $nip_petugas = isset($_POST['nip_petugas']) ? $_POST['nip_petugas'] : '';
            $nip_dokter = isset($_POST['nip_dokter']) ? $_POST['nip_dokter'] : '';
            $tanggal       = date('Y-m-d');
            $tanggal_now             = strtotime($tanggal);
            $tanggalnow             = date('d/m/Y', $tanggal_now);


            // Periksa apakah KD_REGISTRASI telah diisi
            if (!empty($kd_registrasi)) {
                // Inisialisasi cURL
                $curl = curl_init();

                // Set parameter cURL
                curl_setopt_array($curl, array(
                    CURLOPT_URL => '103.255.241.146/api/service_rs/tambah_dpjp',
                    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(
                        'KD_REGISTRASI' => $kd_registrasi, // Mengirim kode registrasi dari permintaan
                        'URT' => '0', // Status untuk pembatalan kasir
                        'NIP_DOKTER' => $nip_dokter,
                        'KD_JENIS_DPJP' => '1',
                        'TANGGAL_PERUBAHAN' => $tanggalnow,
                        'NIP_PETUGAS' => $nip_petugas,
                        'STATUS' => 'A',
                        'KETERANGAN' => 'DPJP Rawat Inap'
                    ),
                ));

                // Eksekusi permintaan cURL dan ambil respons dari API
                $response = curl_exec($curl);

                // Periksa apakah ada error dalam eksekusi cURL
                if (curl_errno($curl)) {
                    echo 'Error:' . curl_error($curl);
                } else {
                    // Tampilkan respons API
                    echo $response;
                }

                // Tutup cURL
                curl_close($curl);
            } else {
                // Jika KD_REGISTRASI kosong, kirimkan pesan kesalahan
                echo json_encode(array('status' => false, 'message' => 'KD_REGISTRASI tidak valid.'));
            }
        } else {
            // Jika request method bukan POST
            echo json_encode(array('status' => false, 'message' => 'Invalid request method.'));
        }
    }

    public function tambah_sep()
    {
        // Pastikan method request adalah POST
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            // Ambil data KD_REGISTRASI dari permintaan POST
            $kd_registrasi = isset($_POST['kd_registrasi']) ? $_POST['kd_registrasi'] : '';
            $keterangan = isset($_POST['keterangan']) ? $_POST['keterangan'] : '';

            // Periksa apakah KD_REGISTRASI dan KD_JENIS_PASIEN saat kosong
            if (!empty($kd_registrasi) && !empty($keterangan)) {
                // Inisialisasi cURL
                $curl = curl_init();

                // Set parameter cURL
                curl_setopt_array($curl, array(
                    CURLOPT_URL => '103.255.241.146/api/service_rs/tambah_sep',
                    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(
                        'KD_REGISTRASI' => $kd_registrasi, // Mengirim kode registrasi dari permintaan
                        'NO_SEP' => '0142R033' . $keterangan // Mengirim NO SEP
                    ),
                ));

                // Eksekusi permintaan cURL dan ambil respons dari API
                $response = curl_exec($curl);

                // Periksa apakah ada error dalam eksekusi cURL
                if (curl_errno($curl)) {
                    echo 'Error:' . curl_error($curl);
                } else {
                    // Tampilkan respons API
                    echo $response;
                }

                // Tutup cURL
                curl_close($curl);
            } else {
                // Jika KD_REGISTRASI kosong, kirimkan pesan kesalahan
                echo json_encode(array('status' => false, 'message' => 'KD_REGISTRASI tidak valid.'));
            }
        } else {
            // Jika request method bukan POST
            echo json_encode(array('status' => false, 'message' => 'Invalid request method.'));
        }
    }
    public function hapus_radlab()
    {
        // Pastikan method request adalah POST
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            // Ambil data permintaan POST
            $keterangan = isset($_POST['keterangan']) ? $_POST['keterangan'] : '';

            // Periksa apakah KD_REGISTRASI dan KD_JENIS_PASIEN saat kosong
            if (!empty($keterangan)) {
                // Inisialisasi cURL
                $curl = curl_init();

                // Set parameter cURL
                curl_setopt_array($curl, array(
                    CURLOPT_URL => '103.255.241.146/api/service_rs/hapus_kode_jual_radlab',
                    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(
                        'KD_JUAL' => $keterangan // Mengirim NO SEP
                    ),
                ));

                // Eksekusi permintaan cURL dan ambil respons dari API
                $response = curl_exec($curl);

                // Periksa apakah ada error dalam eksekusi cURL
                if (curl_errno($curl)) {
                    echo 'Error:' . curl_error($curl);
                } else {
                    // Tampilkan respons API
                    echo $response;
                }

                // Tutup cURL
                curl_close($curl);
            } else {
                // Jika KD_REGISTRASI kosong, kirimkan pesan kesalahan
                echo json_encode(array('status' => false, 'message' => 'KD_REGISTRASI tidak valid.'));
            }
        } else {
            // Jika request method bukan POST
            echo json_encode(array('status' => false, 'message' => 'Invalid request method.'));
        }
    }
    public function pembayaran()
    {
        // Ambil input pencarian dari parameter GET
        $search = $this->input->get('search');

        // Inisialisasi cURL
        $curl = curl_init();

        if ($curl === false) {
            echo json_encode(['status' => false, 'message' => 'Gagal menginisialisasi cURL']);
            return;
        }

        // Set options untuk cURL
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'http://103.255.241.146/api/service_rs/ref_pembayaran',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30, // Tambahkan timeout yang lebih lama jika perlu
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
        ));

        // Eksekusi cURL request
        $response = curl_exec($curl);

        // Cek apakah ada error saat eksekusi cURL
        if (curl_errno($curl)) {
            // Jika ada error, tampilkan pesan errornya
            echo json_encode(['status' => false, 'message' => 'cURL Error: ' . curl_error($curl)]);
            curl_close($curl);
            return;
        }

        // Tutup handle cURL setelah selesai
        curl_close($curl);

        // Debugging: Tampilkan respons API sebelum diproses
        echo '<pre>';
        print_r($response); // Menampilkan respons asli dari API
        echo '</pre>';

        // Decode response dari API
        $data = json_decode($response, true);

        // Debugging respons yang didecode
        echo '<pre>';
        print_r($data); // Menampilkan seluruh isi respons yang didecode
        echo '</pre>';

        // Pastikan ada data dan 'status' benar
        if (isset($data['metadata']) && isset($data['metadata']['code']) && $data['metadata']['code'] === 200) {
            // Proses data yang berhasil
            if (isset($data['response']) && !empty($data['response'])) {
                // Filter data berdasarkan parameter pencarian jika ada
                if (!empty($search)) {
                    $filtered_data = array_filter($data['response'], function ($item) use ($search) {
                        return stripos($item['nm_pembayaran'], $search) !== false; // Filter berdasarkan nama pembayaran
                    });
                } else {
                    $filtered_data = $data['response'];
                }

                // Mengembalikan data yang sudah difilter atau seluruh data
                echo json_encode(['status' => true, 'data' => array_values($filtered_data)]);
            } else {
                echo json_encode(['status' => false, 'message' => 'Tidak ada data yang ditemukan.']);
            }
        } else {
            // Jika status tidak sesuai atau ada masalah dengan API
            echo json_encode(['status' => false, 'message' => isset($data['message']) ? $data['message'] : 'Gagal mengambil data dari API atau data tidak valid.']);
        }
    }


    public function ubahpembayaran()
    {
        // Pastikan method request adalah POST
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            // Ambil data KD_REGISTRASI dari permintaan POST
            $kd_registrasi = isset($_POST['kd_registrasi']) ? $_POST['kd_registrasi'] : '';
            $kd_pembayaran = isset($_POST['kd_pembayaran']) ? $_POST['kd_pembayaran'] : '';


            // Periksa apakah KD_REGISTRASI telah diisi
            if (!empty($kd_registrasi)) {
                // Inisialisasi cURL
                $curl = curl_init();

                // Set parameter cURL
                curl_setopt_array($curl, array(
                    CURLOPT_URL => '103.255.241.146/api/service_rs/tambah_dpjp',
                    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(
                        'KD_REGISTRASI' => $kd_registrasi, // Mengirim kode registrasi dari permintaan
                        'KD_PEMBAYARAN' => $kd_pembayaran
                    ),
                ));

                // Eksekusi permintaan cURL dan ambil respons dari API
                $response = curl_exec($curl);

                // Periksa apakah ada error dalam eksekusi cURL
                if (curl_errno($curl)) {
                    echo 'Error:' . curl_error($curl);
                } else {
                    // Tampilkan respons API
                    echo $response;
                }

                // Tutup cURL
                curl_close($curl);
            } else {
                // Jika KD_REGISTRASI kosong, kirimkan pesan kesalahan
                echo json_encode(array('status' => false, 'message' => 'KD_REGISTRASI tidak valid.'));
            }
        } else {
            // Jika request method bukan POST
            echo json_encode(array('status' => false, 'message' => 'Invalid request method.'));
        }
    }

    public function get_files()
    {
        $files = array_diff(scandir('./uploads/'), array('..', '.'));
        $file_list = [];

        foreach ($files as $file) {
            $file_list[] = [
                'file_name' => $file,
                'file_path' => base_url('uploads/' . $file),
            ];
        }

        echo json_encode($file_list);
    }
    public function upload_pdf()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'pdf';
        $config['max_size'] = 2048; // 2MB  

        // Check if the upload directory exists, if not create it  
        if (!is_dir($config['upload_path'])) {
            mkdir($config['upload_path'], 0755, true);
        }

        $this->load->library('upload', $config);

        // Debugging output  
        if (!$this->upload->do_upload('file')) {
            // Log the error  
            log_message('error', $this->upload->display_errors('', ''));

            $response = [
                'status' => 'error',
                'message' => $this->upload->display_errors('', ''),
            ];
            echo json_encode($response);
            return; // Exit after sending the response  
        } else {
            $data = $this->upload->data();
            $response = [
                'status' => 'success',
                'file_name' => $data['orig_name'],
                'file_path' => base_url('uploads/' . $data['orig_name']),
            ];
        }

        echo json_encode($response);
    }

    public function delete_file($file_name)
    {
        $file_path = './uploads/' . $file_name;

        // Check if the file exists  
        if (file_exists($file_path)) {
            // Attempt to delete the file  
            if (unlink($file_path)) {
                $response = [
                    'status' => 'success',
                    'message' => 'File deleted successfully.',
                ];
            } else {
                $response = [
                    'status' => 'error',
                    'message' => 'Failed to delete the file.',
                ];
            }
        } else {
            $response = [
                'status' => 'error',
                'message' => 'File does not exist.',
            ];
        }

        echo json_encode($response);
    }
}
