<?php

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

class Akun extends MY_Controller {

    protected $template = "app";
    protected $module = "akun";
    protected $per_page = 20;

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

    public function akun_saya() {
        $this->data['halaman'] = "Akun Saya";

        $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_akun->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_akun->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_akun->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('akun_saya');
    }


    //--------------------------------------------------------
    // NEW METHOD
    //--------------------------------------------------------

    public function data_user_list()
    {
        $this->data['halaman'] = "Data User";

        $total_data = $this->m_akun->get_data_user_list(true);
        $this->data['total_data'] = $total_data;

        //CONFIG PAGINATION
        $this->load->library('pagination');

        $url = site_url('akun/data_user');
        $per_page = $this->per_page;
        $uri_segment = 3;

        $this->data['link_paging'] = $this->paging->set_paging2($url, $total_data, $per_page, $uri_segment);
        if ($this->uri->segment($uri_segment)) {
            $this->cur_page = $page = $this->uri->segment($uri_segment);
        } else {
            $this->cur_page = $page = 0;
        }

        $this->data['per_page'] = $per_page;
        $this->data['data'] = $this->m_akun->get_data_user_list(false, $per_page, $page);
        //END CONFIG PAGINATION

        $this->render('data_user');
    }

    public function data_personil_list()
    {
        $this->data['halaman'] = "Data Personil";

        $total_data = $this->m_akun->get_data_personil_list(true);
        $this->data['total_data'] = $total_data;

        //CONFIG PAGINATION
        $this->load->library('pagination');

        $url = site_url('akun/data_user');
        $per_page = $this->per_page;
        $uri_segment = 3;

        $this->data['link_paging'] = $this->paging->set_paging2($url, $total_data, $per_page, $uri_segment);
        if ($this->uri->segment($uri_segment)) {
            $this->cur_page = $page = $this->uri->segment($uri_segment);
        } else {
            $this->cur_page = $page = 0;
        }

        $this->data['per_page'] = $per_page;
        $this->data['data'] = $this->m_akun->get_data_personil_list(false, $per_page, $page);
        //END CONFIG PAGINATION

        $this->render('data_personil');
    }

    public function tambah_akun($type)
    {
        if($type == 'user'){
            $this->proses_user();
        }
        if($type == 'personil'){
            $this->proses_personil();
        }
    }

    public function edit_akun($type, $id)
    {
        if($type == 'user'){
            $this->proses_user($id);
        }
        if($type == 'personil'){
            $this->proses_personil($id);
        }
    }

    public function proses_user($id='')
    {   
        $title  = "TAMBAH AKUN USER";
        $url    = base_url("save_akun/user");
        if($id != ''){
            $title  = "EDIT AKUN USER";
            $url    = base_url("update_akun/user/$id");
            $this->data['edit'] = $this->m_akun->get_user_by_id($id);
        }

        $this->data['role_user']= $this->m_akun->get_role_user();
        $this->data['role_analis']= $this->m_akun->get_role_analis();
        $this->data['url']      = $url;
        $this->data['halaman']  = $title;
        $this->render('create_edit_user');
    }

    public function proses_personil($id='')
    {
        $title  = "TAMBAH AKUN PERSONIL";
        $url    = base_url("save_akun/personil");
        if($id != ''){
            $title  = "EDIT AKUN PERSONIL";
            $url    = base_url("update_akun/personil/$id");
            $this->data['edit'] = $this->m_akun->get_personil_by_id($id);
        }
        if($id != ''){
            $this->data['role_jabatan']= $this->m_akun->get_data_jabatan(false, 1, $id);
        }else{
            $this->data['role_jabatan']= $this->m_akun->get_data_jabatan(false, 1);
        }
        
        $this->data['url']      = $url;
        $this->data['halaman']  = $title;
        $this->render('create_edit_personil');
    }

    public function save_akun($type='')
    {
        $i_post = $this->input->post();
        if($type == 'user'){
            if(!empty($i_post['role_analis'])){
                $data['role_analis']    = json_encode($i_post['role_analis']);
            }
            $data['id_login_group'] = json_encode($i_post['role_user']);
            $data['email']          = $i_post['email'];
            $data['username']       = $i_post['username'];
            $data['password']       = hash('sha256', $i_post['password']);
            $data['last_modified_user'] = $this->id_user;
            $id_user = $this->m_akun->save_akun($type, $data);

            $data = [];
            $data['nama']    = $i_post['nama'];
            $data['id_user'] = $id_user;
            $this->m_akun->save_akun('personil', $data);

            $this->session->set_flashdata('msg', 'Data berhasil diubah');
            redirect('data_user');
        }
        if($type == 'personil'){
            $data['nama'] = $i_post['nama'];
            $data['id_jabatan'] = json_encode($i_post['id_jabatan']);
            $data['nip'] = $i_post['nip'];
            $data['niptk'] = $i_post['niptk'];
            $data['alamat'] = $i_post['alamat'];
            $data['no_telp'] = $i_post['no_telp'];
            if(!empty($_FILES['file_foto']['tmp_name'])){
                $data['file_foto'] = 'files/foto_profil/'.strtotime(date('ymdhis')).'.jpg';
                move_uploaded_file($_FILES['file_foto']['tmp_name'], $data['file_foto']);
            }
            $this->m_akun->save_akun('personil', $data);

            $this->session->set_flashdata('msg', 'Data berhasil diubah');
            redirect('data_personil');
        }
    }

    public function update_akun($type='', $id='')
    {
        $i_post = $this->input->post();
        if($type == 'user'){
            $data['role_analis']    = json_encode($i_post['role_analis']);
            $data['id_login_group'] = json_encode($i_post['role_user']);
            $data['email']          = $i_post['email'];
            $data['username']       = $i_post['username'];
            $data['last_modified_user'] = $this->id_user;

            if($i_post['password'] != ''){
                $data['password']   = hash('sha256', $i_post['password']);
            }
            $this->m_akun->update_akun($type, $data, $id);

            $this->session->set_flashdata('msg', 'Data berhasil diubah');
            redirect('data_user');
        }
        if($type == 'personil'){
            $data['nama']       = $i_post['nama'];
            $data['id_jabatan'] = json_encode($i_post['id_jabatan']);
            $data['nip']        = $i_post['nip'];
            $data['niptk']      = $i_post['niptk'];
            $data['no_telp']    = $i_post['no_telp'];
            $data['email']      = $i_post['email'];
            $data['alamat']     = $i_post['alamat'];
            $data['last_modified_user'] = $this->id_user;

            if(!empty($_FILES['file_foto']['tmp_name'])){
                $data['file_foto'] = 'files/foto_profil/'.strtotime(date('ymdhis')).'.jpg';
                move_uploaded_file($_FILES['file_foto']['tmp_name'], $data['file_foto']);
            }else{
                $data['file_foto'] = ($i_post['file_foto_lama'])?$i_post['file_foto_lama']:null;
            }
            $this->m_akun->update_akun($type, $data, $id);

            $this->session->set_flashdata('msg', 'Data berhasil diubah');
            redirect('data_personil');
        }
        
    }

    public function hapus_akun($type='', $id='')
    {
        if($type == 'user'){
            $data['is_active'] = '0';
            $data['last_modified_user'] = $this->id_user;

            $this->m_akun->hapus_akun($type, $data, $id);
            $this->session->set_flashdata('msg', 'Data berhasil dihapus');
            redirect('data_user');
        }
        if($type == 'personil'){
            $data['is_active'] = '0';
            $data['last_modified_user'] = $this->id_user;

            $this->m_akun->hapus_akun($type, $data, $id);
            $this->session->set_flashdata('msg', 'Data berhasil dihapus');
            redirect('data_personil');
        }
    }

}
