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

class ControllerDataLogin 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_login')->result();
        $data_role_level = $this->db->get_where('sistem_role_level')->result();
        $jml_data        = count($data_table);
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'database/sistem/login',
            'page'                => 'index',
            'judul'               => 'Kelola Database Bagian Sistem Login || Bagian Pengurus',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Isi data
            'jml_data'            => $jml_data,
            'data_table'          => $data_table,
            'role_level'          => $data_role_level
        );
        $this->load->view('owner/index', $data);
    }
    // Crud
    public function crud()
    {
        if (isset($_POST['updateDatabaseTerbaru'])) :
            // CEK OLD DATA
            $this->_cekOldDATA();
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Merubah Data!</div>');
            redirect('owner/database/sistem/login');
        endif;
        if (isset($_POST['simpanDataLogin'])) :
            // Verifikasi Email
            $email = htmlspecialchars($this->input->post('email'));
            $this->_verifikasiEmail($email);
            // Verifikasi Username
            $username = htmlspecialchars($this->input->post('username'));
            $this->_verifikasiUsername($username);
            // Verifikasi Nomor HP
            $no_hp = htmlspecialchars($this->input->post('no_hp'));
            $this->_verifikasiNomorHP($no_hp);
            $data = array(
                'role_id'         => htmlspecialchars($this->input->post('role_id')),
                'email'           => htmlspecialchars($this->input->post('email')),
                'username'        => htmlspecialchars($this->input->post('username')),
                'no_hp'           => htmlspecialchars($this->input->post('no_hp')),
                'nm_panggilan'    => htmlspecialchars($this->input->post('nm_panggilan')),
                'password'        => md5(htmlspecialchars($this->input->post('email'))),
                'status_akun'     => htmlspecialchars($this->input->post('status_akun')),
                'gambar_profil'   => htmlspecialchars($this->input->post('gambar_profil'))
            );
            // echo '<pre>';
            // var_dump($data);
            // die;
            $this->db->insert('sistem_login', $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/login');
        endif;
    }
    // Verifikasi Email
    private function _verifikasiEmail($email)
    {
        $cek_mail = $this->db->get_where('sistem_login', ['email' => $email])->row_array();
        if ($cek_mail != NULL) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Email Sudah di Gunakan!</div>');
            redirect('owner/database/sistem/login');
        endif;
    }
    // Cek Username
    private function _verifikasiUsername($username)
    {
        $cek_username = $this->db->get_where('sistem_login', ['username' => $username])->row_array();
        if ($cek_username != NULL) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Username Sudah di Gunakan!</div>');
            redirect('owner/database/sistem/login');
        endif;
    }
    // Cek Nomor HP
    private function _verifikasiNomorHP($no_hp)
    {
        $cek_nohp = $this->db->get_where('sistem_login', ['no_hp' => $no_hp])->row_array();
        if ($cek_nohp != NULL) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Nomor HP Sudah di Gunakan!</div>');
            redirect('owner/database/sistem/login');
        endif;
    }
    // Cek Old Data
    private function _cekOldDATA()
    {
        $cek_mail = $this->db->get_where('sistem_login', ['id' => htmlspecialchars($this->input->post('login_id')), 'email' => htmlspecialchars($this->input->post('email'))])->row_array();
        if ($cek_mail == NULL) :
            $email = htmlspecialchars($this->input->post('email'));
            $this->_verifikasiEmail($email);
        endif;
        $cek_username = $this->db->get_where('sistem_login', ['id' => htmlspecialchars($this->input->post('login_id')), 'username' => htmlspecialchars($this->input->post('username'))])->row_array();
        if ($cek_username == NULL) :
            $username = htmlspecialchars($this->input->post('username'));
            $this->_verifikasiUsername($username);
        endif;
        $cek_hp = $this->db->get_where('sistem_login', ['id' => htmlspecialchars($this->input->post('login_id')), 'no_hp' => htmlspecialchars($this->input->post('no_hp'))])->row_array();
        if ($cek_hp == NULL) :
            $no_hp = htmlspecialchars($this->input->post('no_hp'));
            $this->_verifikasiNomorHP($no_hp);
        endif;
        $cek_alldata = $this->db->get_where('sistem_login', ['id' => htmlspecialchars($this->input->post('login_id')), 'no_hp' => htmlspecialchars($this->input->post('no_hp')), 'email' => htmlspecialchars($this->input->post('email')), 'username' => htmlspecialchars($this->input->post('username'))])->row_array();
        if ($cek_alldata == NULL) :
            $data = array(
                'role_id'       => htmlspecialchars($this->input->post('role_id')),
                'email'         => htmlspecialchars($this->input->post('email')),
                'username'      => htmlspecialchars($this->input->post('username')),
                'no_hp'         => htmlspecialchars($this->input->post('no_hp')),
                'nm_panggilan'  => htmlspecialchars($this->input->post('nm_panggilan')),
                'status_akun'   => htmlspecialchars($this->input->post('status_akun')),
                'gambar_profil' => htmlspecialchars($this->input->post('gambar_profil')),
                'update_at'     => date('Y-m-d H:is')
            );
        else:
            $data = array(
                'role_id'       => htmlspecialchars($this->input->post('role_id')),
                'nm_panggilan'  => htmlspecialchars($this->input->post('nm_panggilan')),
                'status_akun'   => htmlspecialchars($this->input->post('status_akun')),
                'gambar_profil' => htmlspecialchars($this->input->post('gambar_profil')),
                'update_at'     => date('Y-m-d H:is')
            );
        endif;
        $this->db->where('id', htmlspecialchars($this->input->post('login_id')));
        $this->db->update('sistem_login', $data);
    }
}

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