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

class ControllerAnggotaPengurus extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        //validasi jika user belum login
        check_not_login();
        if ($this->session->userdata('level') != 1) {
            redirect();
        }
        $this->set_timezone();
        $this->load->library('encrypt');
        $this->load->library('form_validation');
        $this->load->library('Mycustom');
        $this->load->library('pdfgenerator');
        $this->load->library('Notifikasi');
        $this->load->model('bagian_owner/insert_model');
        $this->load->model('bagian_owner/select_model');
        $this->load->model('bagian_owner/update_model');
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }
    public function index()
    {
        $notif                = $this->notifikasi->notif_owner();
        $provinsi             = file_get_contents('https://open-api.my.id/api/wilayah/provinces');
        $data_provinsi        = json_decode($provinsi, true);
        // Data Login
        $data_login                   = $this->userlogin->user_login();
        $data_id_login                = $this->session->userdata('isloggedin');
        // Query SQL Data atau isi data page
        $data_anggota_pengurus = $this->select_model->vd_anggotaPengurus();
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder' => 'anggota/pengurus',
            'page'   => 'index',
            'judul'  => 'Kelola Data Pengurus Koperasi || Bagian Pengurus',
            // Isi data
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            'provinsi'            => $data_provinsi,
            // Data isian konten
            'anggota_pengurus'    => $data_anggota_pengurus
        );
        $this->load->view('owner/index', $data);
    }
    public function crud()
    {
        if (isset($_POST['simpanAnggoKoperasi'])) :
            $cek_email = $this->db->get_where('sistem_login', ['email' => htmlspecialchars($this->input->post('email'))])->row_array();
            if ($cek_email > 0) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Email Sudah Terdaftar!</div>');
                redirect('owner/anggota/pengurus');
            endif;
            $cek_nik   = $this->db->get_where('sistem_identitas', ['nik' => htmlspecialchars($this->input->post('nik'))])->row_array();
            if ($cek_nik > 0) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">NIK Sudah Terdaftar!</div>');
                redirect('owner/anggota/pengurus');
            endif;
            if ($cek_nomor > 0) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Nomor HP Sudah Terdaftar!</div>');
                redirect('owner/anggota/pengurus');
            endif;
            // Konversi Alamat
            $konversi_alamat    = $this->_KKodeAlamatKeHuruf();
            $data_inputan       = $this->_dataInputanAnggotaKoperasi($konversi_alamat);
            $alamat_id          = $this->_cekDataAlamat($konversi_alamat);
            $this->insert_model->simpanDataAnggotaPengurus($data_inputan);
            $cek_login = $this->db->get_where('sistem_login', ['email' => htmlspecialchars($this->input->post('email'))])->row_array();
            $login_id           = $cek_login['id'];
            $this->insert_model->simpanIdentitasDataAnggotaPengurus($data_inputan, $alamat_id, $login_id);
            redirect('owner/anggota/pengurus');
        endif;
    }
    // Privat Function
    // Melihat alamat lengkap Heboh
    private function _KKodeAlamatKeHuruf()
    {
        // Cek Provinsi
        $provinsi               = file_get_contents('https://open-api.my.id/api/wilayah/provinces/');
        $data_provinsi          = json_decode($provinsi, true);
        foreach ($data_provinsi as $Provinsi) :
            if ($Provinsi['id'] == htmlspecialchars($this->input->post('province_id'))) :
                $prov = array(
                    'id'   => $Provinsi['id'],
                    'name' => $Provinsi['name']
                );
            endif;
        endforeach;
        // Cek Kabupaten
        $kab_kota               = file_get_contents('https://open-api.my.id/api/wilayah/regencies/' . $prov['id']);
        $data_kab_kota          = json_decode($kab_kota, true);
        foreach ($data_kab_kota as $Kabupaten) :
            if ($Kabupaten['id'] == htmlspecialchars($this->input->post('kot_kab'))) :
                $kab = array(
                    'id'   => $Kabupaten['id'],
                    'name' => $Kabupaten['name']
                );
            endif;
        endforeach;
        // Cek Kecamatan
        $kecamatan               = file_get_contents('https://open-api.my.id/api/wilayah/districts/' . $kab['id']);
        $data_kecamatan          = json_decode($kecamatan, true);
        foreach ($data_kecamatan as $Kec) :
            if ($Kec['id'] == htmlspecialchars($this->input->post('nm_kec'))) :
                $nm_kec = array(
                    'id'   => $Kec['id'],
                    'name' => $Kec['name']
                );
            endif;
        endforeach;
        // Cek Nama Desa atau Kelurahan
        $deskel               = file_get_contents('https://open-api.my.id/api/wilayah/villages/' . $nm_kec['id']);
        $data_deskel          = json_decode($deskel, true);
        foreach ($data_deskel as $Deskel) :
            if ($Deskel['id'] == htmlspecialchars($this->input->post('nm_desa'))) :
                $desa_kelurahan = array(
                    'id'   => $Deskel['id'],
                    'name' => $Deskel['name']
                );
            endif;
        endforeach;
        $data_alamat = array(
            'prov'         => $prov['name'],
            'kot_kab'      => $kab['name'],
            'nm_kecamatan' => $nm_kec['name'],
            'nm_desa'      => $desa_kelurahan['name'],
        );
        return $data_alamat;
    }
    // Validasi Inputan
    private function _dataInputanAnggotaKoperasi($konversi_alamat)
    {
        $data_inputan = array(
            'nik'            => htmlspecialchars($this->input->post('nik_anggota')),
            'email'          => htmlspecialchars($this->input->post('email')),
            'no_hp'          => htmlspecialchars($this->input->post('no_hp')),
            'nm_anggota'     => htmlspecialchars($this->input->post('nm_anggota')),
            'jk'             => htmlspecialchars($this->input->post('jk')),
            'kd_pos'         => htmlspecialchars($this->input->post('kd_pos')),
            'nm_desa'        => $konversi_alamat['nm_desa'],
            'nm_kecamatan'   => $konversi_alamat['nm_kecamatan'],
            'kot_kab'        => $konversi_alamat['kot_kab'],
            'prov'           => $konversi_alamat['prov'],
            'tempat_lahir'   => htmlspecialchars($this->input->post('tempat_lahir')),
            'tgl_lahir'      => htmlspecialchars($this->input->post('tgl_lahir')),
            'pekerjaan'      => 1,
            'foto_ktp'       => htmlspecialchars($this->input->post('foto_ktp'))
        );
        return $data_inputan;
    }
    // Mencari ID alamat database Heboh
    private function _cekDataAlamat($konversi_alamat)
    {
        $cek_alamat = $this->db->get_where('sistem_alamat', ['kd_pos' => htmlspecialchars($this->input->post('kd_pos')), 'nm_desa' => $konversi_alamat['nm_desa'], 'nm_kecamatan' => $konversi_alamat['nm_kecamatan'], 'kot_kab' => $konversi_alamat['kot_kab'], 'prov' => $konversi_alamat['prov']])->row_array();
        if ($cek_alamat > 0) :
            $alamat_id = $cek_alamat['id'];
            return $alamat_id;
        else :
            $data_alamat = array(
                'nm_desa'      => $konversi_alamat['nm_desa'],
                'nm_kecamatan' => $konversi_alamat['nm_kecamatan'],
                'kd_pos'       => htmlspecialchars($this->input->post('kd_pos')),
                'kot_kab'      => $konversi_alamat['kot_kab'],
                'prov'         => $konversi_alamat['prov']
            );
            $this->db->insert('sistem_alamat', $data_alamat);
            $alamat = $this->db->get_where('sistem_alamat', ['nm_desa' => $konversi_alamat['nm_desa'], 'nm_kecamatan' => $konversi_alamat['nm_kecamatan'], 'kot_kab' => $konversi_alamat['kot_kab'], 'prov' => $konversi_alamat['prov']])->row_array();
            $alamat_id = $alamat['id'];
            return $alamat_id;
        endif;
    }
}

/* End of file ControllerAnggotaPengurus.php and path \application\controllers\owner\anggota\ControllerAnggotaPengurus.php */