<?php
defined('BASEPATH') or exit('No direct script access allowed');
// created by : KuduSinau_Hendrik_081275532220 (TERIMAKASIH UNTUK MENDAPATKAN KEBERKAHAN, DENGAN TIDAK MERUBAH NAMA HAK CIPTA, PLAGIAT, DAN TIDAK MEMPERJUALBELIKAN APLIKASI INI, SEMOGA KITA SEMUA MENDAPAT MANFAAT DAN KEBERKAHAN DARI APLIKASI INI)
// created by : KuduSinau_Hendrik_081275532220 (UNTUK MASUKAN DAN SARAN SERTA PENGEMBANGAN BISA MENGHUBUNGI NOMOR)
class Login extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        $data = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        if (!is_null($data)) {
            if ($data['kd_level']       == 1) {
                redirect('admin');
            } elseif ($data['kd_level'] == 2) {
                redirect('kamad');
            } elseif ($data['kd_level'] == 3) {
                redirect('walas');
            } elseif ($data['kd_level'] == 4) {
                redirect('guru');
            } elseif ($data['kd_level'] == 5) {
                redirect('siswa');
            }
        }
        $data['title'] = 'D-Rapor | Login';
        $this->load->view('auth/masuk', $data);
        // created by : KuduSinau_Hendrik_081275532220
    }
    public function daftar()
    {
        $data = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        if (!is_null($data)) {
            if ($data['kd_level']       == 1) {
                redirect('admin');
            } elseif ($data['kd_level'] == 2) {
                redirect('kamad');
            } elseif ($data['kd_level'] == 3) {
                redirect('walas');
            } elseif ($data['kd_level'] == 4) {
                redirect('guru');
            } elseif ($data['kd_level'] == 5) {
                redirect('siswa');
            }
        }
        $data['jenjang'] = $this->db->get('t_jenjang')->result_array();
        $data['status'] = $this->db->get('t_status')->result_array();
        $this->form_validation->set_rules('nm_user', 'Nama User', 'trim|required|min_length[3]', [
            'required'      => 'Nama Admin Wajib diisi !',
            'min_length'    => 'Nama Minimal 3 Karakter !',
        ]);
        $this->form_validation->set_rules('email_user', 'Email User', 'trim|required|valid_email|is_unique[t_user.email_user]', [
            'required'      => 'Email Admin Wajib diisi !',
            'valid_email'   => 'Penulisan Email Belum Benar !',
            'is_unique'     => 'Email sudah terdaftar !'
        ]);
        $this->form_validation->set_rules('nsm_lembaga', 'NSM Lembaga', 'trim|required|numeric|min_length[12]', [
            'required'      => 'NSM Lembaga Wajib diisi !',
            'numeric'       => 'NSM Lembaga Berupa Angka 12 Digit !',
            'min_length'    => 'NSM Lembaga 12 Digit Angka !'
        ]);
        $this->form_validation->set_rules('npsn_lembaga', 'NPSN Lembaga', 'trim|required|numeric|min_length[8]', [
            'required'      => 'NPSN Lembaga Wajib diisi !',
            'numeric'       => 'NPSN Lembaga Berupa Angka 8 Digit !',
            'min_length'    => 'NPSN Lembaga 8 Digit Angka !'
        ]);
        $this->form_validation->set_rules('kd_jenjang', 'Jenjang Lembaga', 'trim|required', [
            'required'      => 'Jenjang Lembaga Wajib diisi !'
        ]);
        $this->form_validation->set_rules('kd_status', 'Status Lembaga', 'trim|required', [
            'required'      => 'Status Lembaga Wajib diisi !'
        ]);
        $this->form_validation->set_rules('nm_lembaga', 'Nama Lembaga', 'trim|required', [
            'required'      => 'Nama Lembaga Wajib diisi !'
        ]);
        if ($this->form_validation->run() == false) {
            $data['title'] = 'D-Rapor | Registrasi';
            $this->load->view('auth/daftar', $data);
        } else {
            $nsm = htmlspecialchars($this->input->post('nsm_lembaga', true));
            $sql = $this->db->query("SELECT nsm_lembaga FROM t_lembaga where nsm_lembaga = '$nsm'");
            $cek_nsm = $sql->num_rows();
            if ($cek_nsm > 0) {
                $this->session->set_flashdata('messageError', 'NSM Sudah Terdaftar !');
                redirect('login/daftar');
            } else {
                $npsn = htmlspecialchars($this->input->post('npsn_lembaga', true));
                $sql = $this->db->query("SELECT npsn_lembaga FROM t_lembaga where npsn_lembaga = '$npsn'");
                $cek_npsn = $sql->num_rows();
                if ($cek_npsn > 0) {
                    $this->session->set_flashdata('messageError', 'NPSN Sudah Terdaftar !');
                    redirect('login/daftar');
                } else {
                    $nsm = htmlspecialchars($this->input->post('nsm_lembaga', true));
                    $npsn = htmlspecialchars($this->input->post('npsn_lembaga', true));
                    $jenjang = htmlspecialchars($this->input->post('jenjang_lembaga', true));
                    $data = [
                        'npsn_lembaga'  => htmlspecialchars($this->input->post('npsn_lembaga', true)),
                        'nsm_lembaga'   => htmlspecialchars($this->input->post('nsm_lembaga', true)),
                        'kd_jenjang'    => htmlspecialchars($this->input->post('kd_jenjang', true)),
                        'kd_status'     => htmlspecialchars($this->input->post('kd_status', true)),
                        'nm_lembaga'    => ucwords(strtolower(htmlspecialchars($this->input->post('nm_lembaga', true)))),
                        'image_user'    => 'lembaga.png',
                        'kop_lembaga'   => 'kop_lembaga.png'
                    ];
                    $this->db->insert('t_lembaga', $data);
                    $sql = $this->db->query("SELECT id_lembaga FROM t_lembaga where nsm_lembaga = '$nsm'")->row_array();
                    $newIdLembaga = $sql['id_lembaga'];
                    $email = $this->input->post('email_user', true);
                    $nmLembaga = htmlspecialchars($this->input->post('nm_lembaga', true));
                    $dataUser = [
                        'kd_user'        => $newIdLembaga,
                        'nm_user'        => htmlspecialchars($this->input->post('nm_user', true)),
                        'email_user'     => htmlspecialchars($email),
                        'username'       => htmlspecialchars($email),
                        'password'       => password_hash('kudusin@u', PASSWORD_DEFAULT),
                        'kd_jenjang'     => htmlspecialchars($this->input->post('kd_jenjang', true)),
                        'no_level'       => 1,
                        'kd_level'       => 1,
                        'status_user'    => 0,
                        'status_lembaga' => htmlspecialchars($this->input->post('kd_status', true)),
                        'tgl_user'       => time(),
                        'image_user'     => 'user.png'
                    ];
                    $token = base64_encode(random_bytes(32));
                    $user_token = [
                        'email'         => $email,
                        'token'         => $token,
                        'date_created'  => time()
                    ];
                    $this->db->insert('t_user', $dataUser);
                    $this->db->insert('t_token', $user_token);
                    $this->_sendEmail($token, 'verify');
                    $this->session->set_flashdata('messageSuccess', 'Registrasi ' . $nmLembaga . ' berhasil, silahkan aktivasi akun melalui email yang sudah didaftarkan !');
                    redirect('login');
                    // created by : KuduSinau_Hendrik_081275532220
                }
            }
        }
    }
    public function verify()
    {
        $email = $this->input->get('email');
        $token = $this->input->get('token');
        $user = $this->db->get_where('t_user', ['email_user' => $email])->row_array();
        if ($user) {
            $user_token = $this->db->get_where('t_token', ['token' => $token])->row_array();
            if ($user_token) {
                if (time() - $user_token['date_created'] < (60 * 60 * 24)) {
                    $this->db->set('status_user', 1);
                    $this->db->where('email_user', $email);
                    $this->db->update('t_user');
                    $this->db->delete('t_token', ['email' => $email]);
                    $this->session->set_flashdata('messageSuccess', 'Akun email ' . $email . ' Sudah aktif, silahkan login menggunakan email !');
                    redirect('login');
                } else {
                    $this->db->delete('t_user', ['email_user' => $email]);
                    $this->db->delete('t_token', ['email' => $email]);
                    $this->session->set_flashdata('messageError', 'Aktivasi akun gagal, token Sudah kadaluarsa !');
                    redirect('login');
                }
            } else {
                $this->session->set_flashdata('messageError', 'Aktivasi akun gagal, token tidak sesuai !');
                redirect('login');
            }
        } else {
            $this->session->set_flashdata('messageError', 'Aktivasi akun gagal, email tidak sesuai !');
            redirect('login');
            // created by : KuduSinau_Hendrik_081275532220
        }
    }
    public function reset_password()
    {
        $email = $this->input->get('email');
        $token = $this->input->get('token');
        $user = $this->db->get_where('t_user', ['email_user' => $email])->row_array();
        if ($user) {
            $user_token = $this->db->get_where('t_token', ['token' => $token])->row_array();
            if ($user_token) {
                $this->session->set_userdata('reset_email', $email);
                $this->ganti_password();
            } else {
                $this->session->set_flashdata('messageError', 'Reset password gagal, token tidak sesuai !');
                redirect('login');
            }
        } else {
            $this->session->set_flashdata('messageError', 'Reset password gagal, email tidak sesuai !');
            redirect('login');
            // created by : KuduSinau_Hendrik_081275532220
        }
    }
    public function ganti_password()
    {
        if (!$this->session->userdata('reset_email')) {
            redirect('login');
        }
        $this->form_validation->set_rules('password1', 'Password', 'trim|required|min_length[6]|matches[password2]', [
            'required' => 'Email Admin Wajib diisi !',
            'min_length' => 'Password minimal 6 karakkter !',
            'matches' => 'Password tidak sama !',
        ]);
        $this->form_validation->set_rules('password2', 'Ulangi Password', 'trim|required|min_length[6]|matches[password1]', [
            'required' => 'Email Admin Wajib diisi !',
            'min_length' => 'Password minimal 6 karakkter !',
            'matches' => 'Password tidak sama !',
        ]);
        if ($this->form_validation->run() == false) {
            $data['title'] = 'D-Rapor | Ganti Password';
            $this->load->view('auth/ganti_password', $data);
        } else {
            $password = password_hash($this->input->post('password1'), PASSWORD_DEFAULT);
            $email = $this->session->userdata('reset_email');
            $this->db->set('password', $password);
            $this->db->where('email_user', $email);
            $this->db->update('t_user');
            $this->session->unset_userdata('reset_email');
            $this->db->delete('t_token', ['email' => $email]);
            $this->session->set_flashdata('messageSuccess', 'Password berhasil diganti, Silahkan login !');
            redirect('login');
            // created by : KuduSinau_Hendrik_081275532220
        }
    }
    public function cek_login()
    {
        $username = htmlspecialchars($this->input->post('username', true));
        $password = htmlspecialchars($this->input->post('password', true));
        $user = $this->db->get_where('t_user', ['username' => $username])->row_array();
        $newlembaga = $user['kd_user'];
        $newId = $user['id_user'];
        $this->db->select('kd_user');
        $this->db->from('t_user');
        $this->db->where('id_user', $newId);
        $newKdLembaga = $this->db->get()->row_array();
        $cek = $this->m_user->cek_login($username);
        if (!empty($cek)) {
            if ($user['status_user'] == 1) {
                if (password_verify($password, $user['password'])) {
                    if (($user['no_level'] == 1) && ($user['kd_level'] == 1)) {
                        if ($user['kd_jenjang'] == 1) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "11";
                        } else if ($user['kd_jenjang'] == 2) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "12";
                        } else if ($user['kd_jenjang'] == 3) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "13";
                        } else if ($user['kd_jenjang'] == 4) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "14";
                        } else if ($user['kd_jenjang'] == 5) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "15";
                        }
                    } else if (($user['no_level'] == 2) && ($user['kd_level'] == 2)) {
                        if ($user['kd_jenjang'] == 1) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "21";
                        } else if ($user['kd_jenjang'] == 2) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "22";
                        } else if ($user['kd_jenjang'] == 3) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "23";
                        } else if ($user['kd_jenjang'] == 4) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "24";
                        } else if ($user['kd_jenjang'] == 5) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "25";
                        }
                    } else if (($user['no_level'] == 2) && ($user['kd_level'] == 3)) {
                        if ($user['kd_jenjang'] == 1) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "31";
                        } else if ($user['kd_jenjang'] == 2) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "32";
                        } else if ($user['kd_jenjang'] == 3) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "33";
                        } else if ($user['kd_jenjang'] == 4) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "34";
                        } else if ($user['kd_jenjang'] == 5) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "35";
                        }
                    } else if (($user['no_level'] == 2) && ($user['kd_level'] == 4)) {
                        if ($user['kd_jenjang'] == 1) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "41";
                        } else if ($user['kd_jenjang'] == 2) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "42";
                        } else if ($user['kd_jenjang'] == 3) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "43";
                        } else if ($user['kd_jenjang'] == 4) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "44";
                        } else if ($user['kd_jenjang'] == 5) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "45";
                        }
                    } else if (($user['no_level'] == 3) && ($user['kd_level'] == 5)) {
                        if ($user['kd_jenjang'] == 1) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "51";
                        } else if ($user['kd_jenjang'] == 2) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "52";
                        } else if ($user['kd_jenjang'] == 3) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "53";
                        } else if ($user['kd_jenjang'] == 4) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "54";
                        } else if ($user['kd_jenjang'] == 5) {
                            foreach ($cek as $user) {
                                $session_data = array(
                                    'id_user'           => $user->id_user,
                                    'username'          => $user->username,
                                    'kd_jenjang'        => $user->kd_jenjang,
                                    'kd_level'          => $user->kd_level,
                                    'status_user'       => $user->status_user,
                                    'status_lembaga'    => $user->status_lembaga,
                                    'kd_user'           => $user->kd_user,
                                    'email_user'        => $user->email_user,
                                );
                                $this->session->set_userdata($session_data);
                            }
                            echo "55";
                        }
                    } else if (($user['no_level'] == 0) && ($user['kd_level'] == 0)) {
                        foreach ($cek as $user) {
                            $session_data = array(
                                'id_user'   => $user->id_user,
                                'username'  => $user->username,
                                'kd_jenjang' => $user->kd_jenjang,
                                'kd_level' => $user->kd_level,
                                'status_user' => $user->status_user,
                                'status_lembaga' => $user->status_lembaga,
                                'kd_user' => $user->kd_user,
                                'email_user' => $user->email_user,
                            );
                            $this->session->set_userdata($session_data);
                        }
                        echo "56";
                    }
                } else {
                    echo "salah";
                }
            } else {
                echo "noActive";
            }
        } else {
            echo "error";
        }
        // created by : KuduSinau_Hendrik_081275532220
    }
    private function _sendEmail($token, $type)
    {
        $config = [
            'protocol'  => 'smtp',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_user' => 'kudusinau2021@gmail.com',
            'smtp_pass' => 'kudusinau111011',
            'smtp_port' =>  465,
            'mailtype'  => 'html',
            'charset'   => 'utf-8',
            'newline'   => "\r\n"
        ];
        $this->load->library('email', $config);
        $this->email->from('kudusinau2021@gmail.com', 'Digital Rapor by Kudu Sinau');
        $this->email->to($this->input->post('email_user', true));
        if ($type == 'verify') {
            $this->email->subject('Aktivasi Akun');
            $this->email->message('Silahkan klik link berikut ini untuk aktivasi akun : <a href="' . base_url() . 'login/verify?email=' . $this->input->post('email_user') . '&token=' . urlencode($token) . '">Aktivasi Akun</a><br>Password default anda adalah : kudusin@u');
        } else if ($type == 'forgot') {
            $this->email->subject('Reset Password');
            $this->email->message('Silahkan klik link berikut ini untuk reset password : <a href="' . base_url() . 'login/reset_password?email=' . $this->input->post('email_user') . '&token=' . urlencode($token) . '">Reset Password</a>');
        }
        if ($this->email->send()) {
            return true;
        } else {
            $this->session->set_flashdata('messageWarning', 'Gagal koneksi, Cek koneksi jaringan internet anda !');
            redirect('login');
            // created by : KuduSinau_Hendrik_081275532220
        }
    }
    public function logout()
    {
        $this->session->unset_userdata('username');
        $this->session->unset_userdata('id_user');
        $this->session->unset_userdata('kd_level');
        $this->session->unset_userdata('kd_jenjang');
        $this->session->unset_userdata('status_user');
        $this->session->unset_userdata('sts_lembaga');
        $this->session->unset_userdata('kd_user');
        $this->session->unset_userdata('email_user');
        $this->session->set_flashdata('messageLogout', 'Terima kasih, anda berhasil keluar !');
        redirect('login');
        // created by : KuduSinau_Hendrik_081275532220
    }
    public function block()
    {
        $data['title'] = 'D-Rapor | Registrasi';
        $this->load->view('auth/block', $data);
        // created by : KuduSinau_Hendrik_081275532220
    }
    public function lupa_password()
    {
        $this->form_validation->set_rules('email_user', 'Email User', 'trim|required|valid_email', [
            'required' => 'Email Admin Wajib diisi !',
            'valid_email' => 'Penulisan Email Belum Benar !',
        ]);
        if ($this->form_validation->run() == false) {
            $data['title'] = 'D-Rapor | Forgot Password';
            $this->load->view('auth/lupa_password', $data);
        } else {
            $email = $this->input->post('email_user');
            $user = $this->db->get_where('t_user', ['email_user' => $email, 'status_user' => 1])->row_array();
            if ($user) {
                $token = base64_encode(random_bytes(32));
                $user_token = [
                    'email'         => $email,
                    'token'         => $token,
                    'date_created'  => time()
                ];
                $this->db->insert('t_token', $user_token);
                $this->_sendEmail($token, 'forgot');
                $this->session->set_flashdata('messageSuccess', 'Silahkan cek email untuk me-reset password !');
                redirect('login/lupa_password');
            } else {
                $this->session->set_flashdata('messageError', 'Email tidak terdaftar atau tidak aktif !');
                redirect('login/lupa_password');
                // created by : KuduSinau_Hendrik_081275532220
            }
        }
    }
}
// created by : KuduSinau_Hendrik_081275532220 (TERIMAKASIH UNTUK MENDAPATKAN KEBERKAHAN, DENGAN TIDAK MERUBAH NAMA HAK CIPTA, PLAGIAT, DAN TIDAK MEMPERJUALBELIKAN APLIKASI INI, SEMOGA KITA SEMUA MENDAPAT MANFAAT DAN KEBERKAHAN DARI APLIKASI INI)
