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

class ControllerKaryawan extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        //validasi jika user belum login
        check_not_login();
        if ($this->session->userdata('level') != 2) {
            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_manager/insert_model');
        $this->load->model('bagian_manager/select_model');
        $this->load->model('bagian_manager/update_model');
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }
    public function index()
    {
        // Data Login
        $data_login            = $this->userlogin->user_login();
        $data_id_login         = $this->session->userdata('isloggedin');
        $notif                 = $this->notifikasi->notif_manager();
        $data_kotkab           = $this->select_model->vd_kotkab();
        // Data Isian Konten
        $data_anggota_karyawan = $this->select_model->vd_anggotaKaryawan();
        $data_karyawan         = $this->select_model->vd_KaryawanAktifKerja();
        $role_level            = $this->select_model->getDataRoleLevel();
        // echo '<pre>';
        // var_dump($role_level);
        // die;
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'sistem/karyawan',
            'page'                => 'index',
            'judul'               => 'Kelola Data Karyawan || Bagian Manager',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            'data_kota'           => $data_kotkab,
            // Isi dalam konten
            'anggota_karyawan'    => $data_anggota_karyawan,
            'data_karyawan'       => $data_karyawan,
            'role_level'          => $role_level
        );
        $this->load->view('manager/index', $data);
    }
    // Cek Identitas Anggota Karyawan
    public function karyawan_detail($id)
    {
        // NOtifikasi
        $notif            = $this->notifikasi->notif_manager();
        $data_login       = $this->userlogin->user_login('isloggedin');
        $data_id_login    = $this->session->userdata('isloggedin');
        // End Notifikasi
        $login_id                  = $this->encrypt->decode($id);
        $data_login_karyawan       = $this->db->get_where('sistem_login', ['id' => $login_id])->row_array();
        $date_role_level           = $this->db->get_where('sistem_role_level', ['id' => $data_login_karyawan['role_id']])->row_array();
        $data_identitas            = $this->db->get_where('sistem_identitas', ['login_id' => $data_login_karyawan['id']])->row_array();
        $data_alamat               = $this->db->get_where('sistem_alamat', ['id' => $data_identitas['alamat_id']])->row_array();
        $data_kotkab               = $this->select_model->vd_kotkab();
        // Data KPI
        $data_kpi                  = $this->db->get_where('k_hasil', ['login_id' => $login_id])->result();
        // Data Gaji Karyawan
        $data_gaji                 = $this->db->get_where('k_gaji', ['login_id' => $login_id])->result();
        $tampil_tahun              = $this->select_model->getDataTahunTransaksi();
        // Data Review Konsumen
        $data_review           = $this->db->get_where('k_rkonsumen', ['login_id' => $login_id])->result();
        // KPI Nilai
        // echo '<pre>';
        // var_dump($data_kpi, $kpi_detail);
        // die;

        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'            => 'identitas/karyawan',
            'page'              => 'index',
            'judul'             => 'Data Identitas Anggota Karyawan || Bagian Manager',
            'data_id_login'     => $data_id_login,
            'data_login'        => $data_login,
            'notif'             => $notif,
            'data_kota'         => $data_kotkab,
            // Data Login      
            'tampil_tahun'      => $tampil_tahun,
            'login'             => $data_login_karyawan,
            'role_level'        => $date_role_level,
            'identitas'         => $data_identitas,
            'alamat'            => $data_alamat,
            'kpi'               => $data_kpi,
            'gaji'              => $data_gaji,
            'reviews'           => $data_review
        );
        $this->load->view('manager/index', $data);
    }
    public function crud()
    {
        if (isset($_POST['simpanAnggotaKaryawan'])) {
            $data_inputan = $this->_inputanDataKaryawan();
            $this->_cekEmail($data_inputan);
            $this->_cekNoHP($data_inputan);
            $this->_cekKtpKaryawan($data_inputan);
            $this->insert_model->simpanAnggotaKaryawan($data_inputan);
            redirect('manager/sistem/karyawan');
        }
        if (isset($_POST['updateDataKaryawan'])) {
            $data_inputan = $this->_dataInputanIdentitasKaryawan();
            $this->_cekKtpKaryawan2($data_inputan);
            $this->_cekEmailKaryawan($data_inputan);
            $this->_cekNomorHPKaryawan($data_inputan);
            $alamat = $this->db->get_where('sistem_alamat', ['nm_desa' => $data_inputan['nm_desa'], 'nm_kecamatan' => $data_inputan['nm_kecamatan'], 'kot_kab' => $data_inputan['kot_kab']])->row_array();
            $alamat_id = $alamat['id'];
            // Cek Identitas Karyawan
            $cek_identitas = $this->db->get_where('sistem_identitas', ['login_id' => $data_inputan['login_id']])->row_array();
            if ($cek_identitas > 0) :
                $this->update_model->updateDataKaryawanLama($data_inputan, $alamat_id);
            else :
                $this->insert_model->tambahDataIdentitasKaryawan($data_inputan, $alamat_id);
            endif;
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil merubah data karyawan koperasi!</div>');
            redirect('manager/identitas/karyawan/' . $this->encrypt->encode($data_inputan['login_id']));
        }
        if (isset($_POST['blokirDataKaryawan'])) {
            $data_inputan = $this->_dataInputanIdentitasKaryawan();
            $this->update_model->blokirDataKaryawan($data_inputan);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil blokir data karyawan koperasi!</div>');
            redirect('manager/identitas/karyawan/' . $this->encrypt->encode($data_inputan['login_id']));
        }
        if (isset($_POST['aktifDataKaryawan'])) {
            $data_inputan = $this->_dataInputanIdentitasKaryawan();
            $this->update_model->aktifDataKaryawan($data_inputan);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil aktifasi data karyawan koperasi!</div>');
            redirect('manager/identitas/karyawan/' . $this->encrypt->encode($data_inputan['login_id']));
        }
        // Ubah diri sendiri
        if (isset($_POST['updateDataDiriSendiri'])) {
            $data_inputan = $this->_dataInputanIdentitasKaryawan();
            $this->_cekKtpKaryawan22($data_inputan);
            $this->_cekEmailKaryawan22($data_inputan);
            $this->_cekNomorHPKaryawan22($data_inputan);
            $alamat = $this->db->get_where('sistem_alamat', ['nm_desa' => $data_inputan['nm_desa'], 'nm_kecamatan' => $data_inputan['nm_kecamatan'], 'kot_kab' => $data_inputan['kot_kab']])->row_array();
            $alamat_id = $alamat['id'];
            // Cek Identitas Karyawan
            $cek_identitas = $this->db->get_where('sistem_identitas', ['login_id' => $data_inputan['login_id']])->row_array();
            if ($cek_identitas > 0) :
                $this->update_model->updateDataKaryawanLama($data_inputan, $alamat_id);
            else :
                $this->insert_model->tambahDataIdentitasKaryawan($data_inputan, $alamat_id);
            endif;
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil merubah data karyawan koperasi!</div>');
            redirect('manager/identitas/myprofile');
        }
    }
    // Kumpulan funtion private
    private function _inputanDataKaryawan()
    {
        $length = 20;
        $username = substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 1, $length);
        $alamat = $this->db->get_where('sistem_alamat', [
            'nm_desa'      => htmlspecialchars($this->input->post('dk_ds')),
            'nm_kecamatan' => htmlspecialchars($this->input->post('kecamatan_baru')),
            'kot_kab'      => htmlspecialchars($this->input->post('kota_kabupaten'))
        ])->row_array();
        $data_inputan = array(
            // Data Login
            'role_id'        => htmlspecialchars($this->input->post('role_id')),
            'email'          => htmlspecialchars($this->input->post('email')),
            'username'       => $username,
            'no_hp'          => htmlspecialchars($this->input->post('no_hp')),
            'nm_panggilan'   => htmlspecialchars($this->input->post('nm_panggilan')),
            'password'       => $this->encrypt->encode($username),
            'status_akun'    => 'AKTIF',
            // Data Identitas
            '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'      => $alamat['id'],
            'tgl_gabung'     => htmlspecialchars($this->input->post('tgl_gabung'))
        );
        return $data_inputan;
    }
    private function _cekEmail($data_inputan)
    {
        $cek_email = $this->db->get_where('sistem_login', ['email' => $data_inputan['email']])->row_array();
        if ($cek_email > 0) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Alamat Email Sudah Di Gunakan Karyawan Lain!</div>');
            redirect('manager/sistem/karyawan');
        endif;
    }
    private function _cekNoHP($data_inputan)
    {
        $cek_email = $this->db->get_where('sistem_login', ['no_hp' => $data_inputan['no_hp']])->row_array();
        if ($cek_email > 0) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Nomor HP Sudah Di Gunakan Karyawan Lain!</div>');
            redirect('manager/sistem/karyawan');
        endif;
    }
    private function _cekKtpKaryawan($data_inputan)
    {
        $nik = $data_inputan['nik'];
        // Cek Apakah 16 karakter
        $hitung_nik      = strlen($nik);
        if ($hitung_nik != 16) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">NIK Harus 16 Karakter!</div>');
            redirect('manager/sistem/karyawan');
        endif;
        // Apakah NIK sudah terdaftar
        $cek_nik = $this->db->get_where('sistem_identitas', ['nik' => $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('manager/sistem/karyawan');
        endif;
    }
    // Update data karaywan
    // Data Inputan identitas Karyawan
    private function _dataInputanIdentitasKaryawan()
    {
        $data_inputan = array(
            'login_id'       => htmlspecialchars($this->input->post('login_id')),
            'nik'            => htmlspecialchars($this->input->post('nik')),
            'nik_lama'       => htmlspecialchars($this->input->post('nik_lama')),
            'nm_panggilan'   => htmlspecialchars($this->input->post('nm_panggilan')),
            'jk'             => htmlspecialchars($this->input->post('jk')),
            'kot_kab'        => htmlspecialchars($this->input->post('kota_kabupaten')),
            'nm_kecamatan'   => htmlspecialchars($this->input->post('kecamatan_baru')),
            'nm_desa'        => htmlspecialchars($this->input->post('dk_ds')),
            'tempat_lahir'   => htmlspecialchars($this->input->post('tempat_lahir')),
            'tgl_lahir'      => htmlspecialchars($this->input->post('tgl_lahir')),
            'no_hp_baru'     => htmlspecialchars($this->input->post('no_hp')),
            'no_hp_lama'     => htmlspecialchars($this->input->post('no_hp_lama')),
            'email_baru'     => htmlspecialchars($this->input->post('email_baru')),
            'email_lama'     => htmlspecialchars($this->input->post('email_lama')),
            'tgl_gabung'     => htmlspecialchars($this->input->post('tgl_gabung')),
            'foto_ktp_old'   => htmlspecialchars($this->input->post('foto_ktp_old'))
        );
        return $data_inputan;
    }
    private function _cekEmailKaryawan($data_inputan)
    {
        if ($data_inputan['email_baru'] != $data_inputan['email_lama']) :
            $cek_email = $this->db->get_where('sistem_login', ['email' => $data_inputan['email_baru']])->row_array();
            if ($cek_email > 0) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Alamat Email Sudah Di Gunakan Karyawan Lain!</div>');
                redirect('keuangan/identitas/karyawan/' . $this->encrypt->encode($data_inputan['login_id']));
            endif;
        endif;
    }
    private function _cekEmailKaryawan22($data_inputan)
    {
        if ($data_inputan['email_baru'] != $data_inputan['email_lama']) :
            $cek_email = $this->db->get_where('sistem_login', ['email' => $data_inputan['email_baru']])->row_array();
            if ($cek_email > 0) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Alamat Email Sudah Di Gunakan Karyawan Lain!</div>');
                redirect('manager/identitas/myprofile');
            endif;
        endif;
    }
    private function _cekNomorHPKaryawan($data_inputan)
    {
        if ($data_inputan['no_hp_baru'] != $data_inputan['no_hp_lama']) :
            $cek_email = $this->db->get_where('sistem_login', ['no_hp' => $data_inputan['no_hp_baru']])->row_array();
            if ($cek_email > 0) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Nomor HP Sudah Di Gunakan Karyawan Lain!</div>');
                redirect('keuangan/identitas/karyawan/' . $this->encrypt->encode($data_inputan['login_id']));
            endif;
        endif;
    }
    private function _cekNomorHPKaryawan22($data_inputan)
    {
        if ($data_inputan['no_hp_baru'] != $data_inputan['no_hp_lama']) :
            $cek_email = $this->db->get_where('sistem_login', ['no_hp' => $data_inputan['no_hp_baru']])->row_array();
            if ($cek_email > 0) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Nomor HP Sudah Di Gunakan Karyawan Lain!</div>');
                redirect('manager/identitas/myprofile');
            endif;
        endif;
    }
    private function _cekKtpKaryawan2($data_inputan)
    {

        if ($data_inputan['nik'] != $data_inputan['nik_lama']) :
            $nik = $data_inputan['nik'];
            // Cek Apakah 16 karakter
            $hitung_nik      = strlen($nik);
            if ($hitung_nik != 16) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">NIK Harus 16 Karakter!</div>');
                redirect('manager/identitas/karyawan/' . $this->encrypt->encode($data_inputan['login_id']));
            endif;
            // Apakah NIK sudah terdaftar
            $cek_nik = $this->db->get_where('sistem_identitas', ['nik' => $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('manager/identitas/karyawan/' . $this->encrypt->encode($data_inputan['login_id']));
            endif;
        endif;
    }
    private function _cekKtpKaryawan22($data_inputan)
    {
        if ($data_inputan['nik'] != $data_inputan['nik_lama']) :
            $nik = $data_inputan['nik'];
            // Cek Apakah 16 karakter
            $hitung_nik      = strlen($nik);
            if ($hitung_nik != 16) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">NIK Harus 16 Karakter!</div>');
                redirect('manager/identitas/myprofile');
            endif;
            // Apakah NIK sudah terdaftar
            $cek_nik = $this->db->get_where('sistem_identitas', ['nik' => $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('manager/identitas/myprofile');
            endif;
        endif;
    }
}

/* End of file ControllerKaryawan.php and path \application\controllers\manager\sistem\ControllerKaryawan.php */