<?php
class Login extends CI_Controller{
    function __construct(){
        parent:: __construct();
        $this->load->model('m_login');
        $this->load->model('m_siswa');
        $this->load->model('m_guru');
    }
    function index(){
        $user = $this->session->user;
        if (!empty($user)) return redirect('elearning/home');
        $this->load->view('elearning/v_login');
    }
    function auth(){
        $u=strip_tags(str_replace("'", "", $this->input->post('username')));
        $p=strip_tags(str_replace("'", "", $this->input->post('password')));
        $login=$this->m_login->login_elearning($u, $p);
        if($login->num_rows() > 0){
            $this->session->set_userdata('masuk', true);
            $this->session->set_userdata('user', $u);
            $this->session->set_userdata('status', $login->first_row()->pengguna_status);
            if ($this->session->status == '2') {
                $kelas_id = $this
                            ->m_siswa
                            ->get_siswa_kelas($this->session->user)
                            ->result()[0]
                            ->siswa_kelas_id;
                $siswa_id = $this
                            ->m_siswa
                            ->get_siswa_id($this->session->user)
                            ->result()[0]
                            ->pengguna_siswa;
                $this->session->set_userdata('kelas', $kelas_id);
                $this->session->set_userdata('id', $siswa_id);
            } else {
                $guru_id = $this
                            ->m_guru
                            ->get_guru_id($this->session->user)
                            ->result()[0]
                            ->pengguna_guru;
                $this->session->set_userdata('id', $guru_id);
            }
            redirect('elearning/home');
       } else {
            echo $this->session->set_flashdata('msg', '<div class="alert alert-danger" role="alert"><button type="button" class="close" data-dismiss="alert"><span class="fa fa-close"></span></button> Username Atau Password Salah</div>');
            redirect('elearning/login');
       }
    }

   function logout(){
            $this->session->sess_destroy();
            redirect('elearning/login');
    }
}
