<?php

class MY_Controller extends CI_Controller {

    protected $template = "app";
    protected $module   = "";
    protected $data     = [];
    protected $id_user  = "";
    protected $id_group = "";
    protected $role_analis = "";
    protected $uname    = "";

    public function __construct() 
    {
        parent::__construct();
        $this->id_user      = $this->session->userdata('id_user');
        $this->id_group     = $this->session->userdata("id_group");
        $this->role_analis  = $this->session->userdata("role_analis");

        if(!empty($this->input->post('login'))){
            $i_post   = $this->input->post();
            $username = $i_post['username'];
            $password = hash('sha256', $i_post['password']);

            $this->db->select("u.id, u.email, u.role_analis, u.id_login_group, p.nama");
            $this->db->where(['u.username'=>$username, 'u.password'=>$password]);
            $this->db->join('personil p', 'p.id_user = u.id', 'left');
            $result = $this->db->get('user u');

            if ($result->num_rows() == 0) {
                $message = 'Akun yang Anda masukkan salah, silahkan cek dan login kembali.';
                $this->session->set_userdata('out_message', $message);
                redirect("");
            } else {
                $row = $result->row_array();

                $this->session->set_userdata("username", $username);
                $this->session->set_userdata("nama", $row['nama']);
                $this->session->set_userdata("email", $row['email']);
                $this->session->set_userdata("role_analis", $row['role_analis']);

                $this->get_group_user($row['id'], $row['id_login_group'], $username, $password);
                redirect('index');
            }
        }
        if($this->id_user == ""){
            if(uri_string() != ''){
                $message = 'Waktu sesi Anda telah habis, silahkan login untuk masuk kembali.';
                $this->session->set_userdata('out_message', $message);
                redirect(base_url());
            }
            $this->login();
        }else{
            $this->set_user_data();
        }
    }

    protected function login(){
        if (!empty($this->session->userdata('out_message'))) {
            $this->data["message"] = $this->session->userdata('out_message');
            $this->data["notif"]   = 'danger';
            $this->session->unset_userdata('out_message');
        } 
        $this->template = 'login';
        $this->module   = 'index';
        $this->render('login');
    }

    protected function get_group($id){
        $this->db->select('id_group, nama_group');
        $query = $this->db->get_where('login_group', ['id_group'=>$id]);
        $result = $query->row_array();
        return $result;
    }

    protected function get_group_user($id_user, $id_login_group=[], $username, $password)
    {
        $login_group = json_decode($id_login_group);
        if(count($login_group) > 0){
            if(count($login_group) == 1){
                $data = $this->get_group($login_group[0]);

                $this->session->set_userdata("id_user", $id_user);
                $this->session->set_userdata("id_group", $data['id_group']);
                $this->session->set_userdata("nama_group", $data['nama_group']);

                $this->set_user_data();
            }else{
                $data = [];
                foreach($login_group as $lg){
                    $data[] = $this->get_group($lg);
                }
                var_dump($data);
                die();
                $this->session->set_userdata("user_id", $id_user);
                $this->session->set_userdata("user_temp", $id_user);
                $this->session->set_userdata("option_group", $data);
            }
        }else{
            $this->session->unset_userdata();
            redirect('');
        }

    }

    protected function set_user_data() {
        $this->data["id_user"] = $this->session->userdata("id_user");
        $this->data["username"] = $this->session->userdata("username");
        $nama = $this->session->userdata('username');
        if($this->session->userdata("nama") != ''){
            $nama = $this->session->userdata("nama");
        }
        $this->data["nama"] = $nama;
        $this->data["email"] = ($this->session->userdata("email") != '') ? $this->session->userdata("email") : '';
        $this->data["id_group"] = $this->session->userdata("id_group");
        $this->data["nama_group"] = $this->session->userdata("nama_group");
    }

    protected function unset_userdata() {
        $this->session->unset_userdata("id_user");
        $this->session->unset_userdata("username");
        $this->session->unset_userdata("nama");
        $this->session->unset_userdata("email");
        $this->session->unset_userdata("id_group");
        $this->session->unset_userdata("nama_group");
    }

    protected function render($filename = "", $sub_folder = "") 
    {   
        $module     = ($this->module != "") ? $this->module . "/" : "index/";
        $sub_folder = ($sub_folder != "") ? $sub_folder. "/" : "";
        $template   = $this->load->view("template/" . $this->template, $this->data, true);
        $content    = $this->load->view($module.$sub_folder.$filename, $this->data, true);

        exit(str_replace("{CONTENT}", $content, $template));
    }
}