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

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

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

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

        // Atur tanggal akhir secara otomatis (misalnya, 1 hari setelah tanggal awal)


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

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


        $curl = curl_init();

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

        $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 => array(
                'TANGGAL_AWAL' => $tanggal_awal,
                'TANGGAL_AKHIR' => $tanggal_akhir // Tambahkan tanggal akhir ke body request
            ),

        ));

        $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 => array(
                'TANGGAL_AWAL' => $tanggal_awal,
                'TANGGAL_AKHIR' => $tanggal_akhir // Tambahkan tanggal akhir ke body request
            ),
        ));

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

    public function download() {}
}
