<?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'];

        // 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_awal));

        // 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);



        $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"]        = $tgl_akhir;
        $this->load->view('dashboard/header', $data);
        $this->load->view('dashboard/dashboardlayananan', $data);
    }

    public function download() {}
}
