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

class ControllerDataIdentitas 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();
        // Data Login
        $data_login            = $this->userlogin->user_login(); // Menampilkan siapa yang login
        $data_id_login         = $this->session->userdata('isloggedin'); // Menampilkan data session
        // Data Isian Konten
        // Query SQL Data atau isi data page
        // Menampilkan saldo pertama koperasi 
        $data_table = $this->db->get_where('sistem_identitas')->result();
        $jml_data   = count($data_table);
        $login_data = $this->db->get_where('sistem_login')->result();
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'database/sistem/identitas',
            'page'                => 'index',
            'judul'               => 'Kelola Database Bagian Sistem Identitas || Bagian Pengurus',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Isi data
            'jml_data'            => $jml_data,
            'data_table'          => $data_table,
            'login_data'          => $login_data
        );
        $this->load->view('owner/index', $data);
    }
    // Crud
    public function crud()
    {
        if (isset($_POST['updateDatabaseTerbaru'])) :
            $data = array(
                'endpoint_url'    => htmlspecialchars($this->input->post('endpoint_url')),
                'api_key'         => htmlspecialchars($this->input->post('api_key')),
                'ket_wa'          => htmlspecialchars($this->input->post('ket_wa')),
                'no_hp'           => htmlspecialchars($this->input->post('no_hp')),
                'status'          => htmlspecialchars($this->input->post('status')),
                'uji_coba'        => htmlspecialchars($this->input->post('uji_coba')),
                'format_wa'       => htmlspecialchars($this->input->post('format_wa'))
            );
            $this->db->where('id', htmlspecialchars($this->input->post('wa_id')));
            $this->db->update('sistem_wa', $data);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Merubah Data!</div>');
            redirect('owner/database/sistem/whatsapp');
        endif;
        if (isset($_POST['simpanDataIndentitas'])) :
            // Cek data sudah ada apa belu
            $this->_cekIdentitas();
            $data = array(
                'login_id'     => htmlspecialchars($this->input->post('login_id')),
                'nik'          => htmlspecialchars($this->input->post('nik')),
                'jk'           => htmlspecialchars($this->input->post('jk')),
                'tempat_lahir' => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir'    => htmlspecialchars($this->input->post('tgl_lahir')),
                'alamat_id'    => htmlspecialchars($this->input->post('alamat_id')),
                'foto_ktp'     => 'Default.jpg',
                'sb'           => htmlspecialchars($this->input->post('sb')),
                'sm'           => htmlspecialchars($this->input->post('sm')),
                'nm_bank'      => htmlspecialchars($this->input->post('nm_bank')),
                'norek'        => htmlspecialchars($this->input->post('norek')),
                'atas_nama'    => htmlspecialchars($this->input->post('atas_nama')),
                'atas_nama'    => htmlspecialchars($this->input->post('atas_nama')),
                'tgl_gabung'   => htmlspecialchars($this->input->post('tgl_gabung'))
            );
            $this->db->insert('sistem_identitas', $data);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Menambah Data!</div>');
            redirect('owner/database/sistem/identitas');
        endif;
    }
    // Cek identitas
    private function _cekIdentitas()
    {
        // Cek Identitas
        $cek_identitas = $this->db->get_where('sistem_identitas', ['login_id' => htmlspecialchars($this->input->post('login_id'))])->row_array();
        if ($cek_identitas != NULL) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Kode Login Sudah di Gunakan!</div>');
            redirect('owner/database/sistem/identitas');
        endif;
        // Cek NIK
        $cek_nik = $this->db->get_where('sistem_identitas', ['nik' => htmlspecialchars($this->input->post('nik'))])->row_array();
        if ($cek_nik != NULL) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">NIK Sudah di Gunakan!</div>');
            redirect('owner/database/sistem/identitas');
        endif;
        // Cek alamat 
        $cek_alamat = $this->db->get_where('sistem_alamat', ['id' => htmlspecialchars($this->input->post('alamat_id'))])->row_array();
        if ($cek_alamat != NULL) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Alamat Tidak Tersedia!</div>');
            redirect('owner/database/sistem/identitas');
        endif;
    }
}

/* End of file ControllerDataIdentitas.php and path \application\controllers\owner\database\sistem\ControllerDataIdentitas.php */