<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Index extends MY_Controller {

    protected $template = "app";

    public function __construct() {
        parent::__construct();
        $this->load->model('m_grafik');

    }

    public function index() {
        $this->data['halaman'] = "Dashboard";
        //$this->data['gafik_pad'] = $this->m_grafik->get_pad();
        $this->data['grafik_jl'] = $this->m_grafik->get_jenis_layanan();
        $this->data['grafik_pad'] = $this->m_grafik->get_pad();
        $this->data['grafik_uji_sampel'] = $this->m_grafik->get_uji_sampel();
        $this->data['grafik_jenis_pengujian'] = $this->m_grafik->get_jenis_pengujian();
        $this->render('index');
    }

    public function login(){
        $this->template = "login";

        $idGroup = $this->session->userdata('r_idGroup');
        if($idGroup || $idGroup !== null){
            redirect('index');
        }

        if ($this->input->get("access_without_login") == "true") {
            $this->data["errorMessage"] = "Session anda telah berakhir, silahkan login kembali untuk masuk ke dashboard.";
        } else if ($this->input->get("logout") == "true") {
            $this->data["successMessage"] = "Anda telah keluar.";
        } else if ($this->input->get("forgot_password") == "true") {
            $this->data["successMessage"] = "Password anda berhasil diperbarui. Silahkan login dengan password baru.";
        }

        $this->render("login");
    }

    public function profile() {
        $this->data["changePassword"] = $this->input->post("change_password");
        if ($this->input->post("submit-button")) {
            if ($this->input->post("username") == null || $this->input->post("username") == "") {
                $this->data["errorUsernameMessage"] = "Username tidak boleh kosong";
            }
            if ($this->input->post("email") == null || $this->input->post("email") == "") {
                $this->data["errorEmailMessage"] = "Email tidak boleh kosong";
            }

            if ($this->input->post("change_password") != null && $this->input->post("change_password") == 1) {
                if ($this->input->post("password") == null || $this->input->post("password") == "") {
                    $this->data["errorPasswordMessage"] = "Password tidak boleh kosong";
                }

                if ($this->input->post("confirm") == null || $this->input->post("confirm") == "") {
                    $this->data["errorConfirmMessage"] = "Password confirm cannot be blank";
                } else if ($this->input->post("confirm") != null && $this->input->post("confirm") != "" && $this->input->post("password") != $this->input->post("confirm")) {
                    $this->data["errorConfirmMessage"] = "Konfirmasi Password tidak sesuai dengan Password baru";
                }
            }

            if (!isset($this->data["errorUsernameMessage"]) && !isset($this->data["errorEmailMessage"]) && !isset($this->data["errorPasswordMessage"]) && !isset($this->data["errorConfirmMessage"])) {
                $status = false;
                if ($this->input->post("change_password") != null && $this->input->post("change_password") == 1) {

					$data = array(
						$this->input->post("email"),
						$this->input->post("real_name"),
						$this->input->post("password"),
						$this->session->userdata("r_userId")
						);

					$status = $this->m_index->update_my_account($data, true);

                } else {

					$data = array(
						$this->input->post("email"),
						$this->input->post("real_name"),
						$this->session->userdata("r_userId")
						);

					$status = $this->m_index->update_my_account($data, false);

				}

                if ($status) {
                    $this->data["successMessage"] = "Profile berhasil diperbarui";
                } else {
                    $this->data["errorMessage"] = "Update Profile Gagal, silahkan coba lagi.";
                }
            } else {
                $this->data["errorMessage"] = "Gagal melakukan update profile, silahkan periksa informasi pada field di bawah ini.";
            }
        }

        $my_account = $this->m_index->my_account(intval($this->session->userdata("r_userId")));
		$this->data["detail"] = array(
			"id" => $my_account->id_user,
			"group" => $my_account->group,
			"real_name" => $my_account->real_name,
			"username" => $my_account->username,
			"email" => $my_account->email,
		);

        $this->render("profile");
    }

    public function get_option() {
        $data['data'] = $this->session->userdata("r_optionGroup");
        $this->load->view('index/option', $data);
    }

    public function set_group($id_group) {
        $cek = $this->db->query("SELECT * FROM akses_group_modul WHERE id_group = ?",array($id_group));

        if($cek->num_rows() == 0){
            $this->session->unset_userdata("r_userId");
            $this->session->unset_userdata("r_username");
            $this->session->unset_userdata("r_realName");
            $this->session->unset_userdata("r_email");
            $this->session->unset_userdata("r_creationTime");
            $this->session->unset_userdata("r_isActive");
            redirect("login?no_module=true");
        }
        else {
            $groups = $this->db->query("SELECT lg.id_group, lg.nama_group
                                        FROM login_group lg
                                        WHERE lg.id_group = ?", array($id_group))->first_row();


            $this->session->set_userdata("r_idGroup", $id_group);
            $this->session->set_userdata("r_groupName", $groups->nama_group);
            $this->session->unset_userdata("r_optionGroup");
            $this->get_list_akses_modul_of_group();
            $this->setUserData();
            redirect("index");
        }
    }

    public function ganti_group($id) {
        $query = $this->db->query("SELECT lg.* FROM login_group lg WHERE lg.is_active = '1' AND lg.id_group = ?", $id);
        $result = $query->row();

        $this->session->unset_userdata("r_idGroup");
        $this->session->unset_userdata("r_groupName");

        $this->session->set_userdata("r_idGroup", $result->id_group);
        $this->session->set_userdata("r_groupName", $result->nama_group);
        $this->get_list_akses_modul_of_group();
        $this->setUserData();

        redirect();
    }

    public function logout() {
        $this->unSetUserData();

        redirect("login");
    }
}
