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

class ControllerDataReferral extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        check_not_login();
        if ($this->session->userdata('level') != 4) {
            redirect();
        }
        $this->load->library('encrypt');
        $this->load->library('mycustom');
        $this->load->library('form_validation');
        $this->load->model('bagian_arisan/insert_model');
        $this->load->model('bagian_arisan/select_model');
        $this->load->model('bagian_arisan/update_model');
        $this->set_timezone();
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }
    public function index()
    {
        // NOtifikasi
        $notifikasi_pengajuan = $this->db->get_where('tbl_arisan_pengajuan')->result();
        $jml_pengajuan        = count($notifikasi_pengajuan);
        // End_notifikasi
        $data_login       = $this->userlogin->user_login();
        $data_id_login    = $this->session->userdata('isloggedin');
        // Query SQL Data atau isi data page
        $data_anggota_referral = $this->select_model->getDataReferralAnggota();
        // echo '<pre>';
        // var_dump($data_pengajuan_anggota);
        // die;
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'            => 'keloladata/referral',
            'page'              => 'index',
            'judul'             => 'Anggota Refferal Arisan || Bagian Arisan',
            'data_id_login'     => $data_id_login,
            'data_login'        => $data_login,
            // Isi data konten
            'anggota_referral'  => $data_anggota_referral,
            'jml_pengajuan'       => $jml_pengajuan
        );
        $this->load->view('arisan/index', $data);
    }
    public function crud()
    {
        if (isset($_POST['simpanAnggotaReferral'])) {
            $data_inputan = $this->_formInputanAnggota();
            $this->_validasiInputanAnggota($data_inputan);
            $this->_cekDataInputanAnggota($data_inputan);
            $this->insert_model->simpanAnggotaReferral($data_inputan);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Referral Anggota Berhasil di Tambah!</div>');
            redirect('arisan/data/referral_arisan');
        }
    }
    // Data function
    private function _formInputanAnggota()
    {
        $acak = $this->_randomString();
        $data_inputan = array(
            'nik'   => htmlspecialchars($this->input->post('nik')),
            'email' => htmlspecialchars($this->input->post('email')),
            'nm_anggota' => htmlspecialchars($this->input->post('nm_anggota')),
            'jk' => htmlspecialchars($this->input->post('jk')),
            'alamat_lengkap' => htmlspecialchars($this->input->post('alamat_lengkap')),
            'tempat_lahir' => htmlspecialchars($this->input->post('tempat_lahir')),
            'tgl_lahir' => htmlspecialchars($this->input->post('tgl_lahir')),
            'no_hp' => htmlspecialchars($this->input->post('no_hp')),
            'username' => $acak
        );
        return $data_inputan;
    }
    private function _validasiInputanAnggota($data_inputan)
    {
        // Cek NIK
        if (strlen($data_inputan['nik']) < 16 && strlen($data_inputan['nik']) > 16) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">NIK Harus berisi 16 karakter!</div>');
            redirect('arisan/data/referral_arisan');
        endif;
        // Cek Jenis Kelamin
        if ($data_inputan['jk'] == 'ok') :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Pilih Jenis Kelamin Dengan Benar!</div>');
            redirect('arisan/data/referral_arisan');
        endif;
        // Cek Nomor HP substr($data_inputan['no_hp'])
        if (substr($data_inputan['no_hp'], 0, 1) != '0') :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Nomor HP diawali dengan 0!</div>');
            redirect('arisan/data/referral_arisan');
        endif;
    }
    private function _cekDataInputanAnggota($data_inputan)
    {
        // Menyamakan NIK Anggota Baru
        $cek_nik    = $this->db->get_where('sistem_identitas', ['nik' => $data_inputan['nik']])->row_array();
        $cek_hp     = $this->db->get_where('sistem_identitas', ['no_hp' => $data_inputan['no_hp']])->row_array();
        $cek_email  = $this->db->get_where('sistem_login', ['email' => $data_inputan['email']])->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('arisan/data/referral_arisan');
        endif;
        if ($cek_hp > 0) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Nomor HP Sudah Terdaftar!</div>');
            redirect('arisan/data/referral_arisan');
        endif;
        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('arisan/data/referral_arisan');
        endif;
    }
    private function _randomString()
    {
        $str        = "";
        $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789';
        $max        = strlen($characters) - 1;
        for ($i = 0; $i <= 5; $i++) {
            $rand = mt_rand(0, $max);
            $str .= $characters[$rand];
        }
        return $str;
    }
}

/* End of file ControllerDataReferral.php and path \application\controllers\arisan\keloladata\ControllerDataReferral.php */