<?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');
    }

    public function data_user() {
        if ($this->input->post('edit')) {
          $this->session->set_flashdata('id', $this->input->post('edit'));
          redirect('/akun/edit_user');
        }
        $this->data['halaman'] = "Data User";
        $this->data['user_role'] = $this->m_akun->getRole();
        $this->data['analis_role'] = $this->m_akun->getAnalisRole();

        $total_data = $this->m_akun->getUser(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->getUser(false, $per_page, $page);
        //END CONFIG PAGINATION

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

    public function edit_user() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'required');
            $this->form_validation->set_rules('email', 'Email', 'required');
        if ($this->input->post('newPass')) {
            $this->form_validation->set_rules('newPass', 'Password Baru', 'required');
            $this->form_validation->set_rules('confirmPass', 'Ulangi Password', 'matches[newPass]');
        }
        if ($this->input->post()) {
            $this->data['data'] = $this->input->post();
            if ($this->form_validation->run()) {
                if ($this->m_akun->edit()) {
                    redirect($this->module . '/data_user');
                }
            }
        } else if ($this->session->flashdata('id')) {
            $this->data['data'] = $this->m_akun->getUser(false, null, null, $this->session->flashdata('id'));
        } else {
            redirect($this->module . '/data_user');
        }
        $this->render('edit_user');
    }

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

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

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

        $url = site_url('akun/data_personil');
        $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->getPersonil(false, $per_page, $page);
        //END CONFIG PAGINATION

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

    public function create_edit() {
        $data = $this->input->get();
        $type = $data['type'];
        if ($type === "user") {
            $this->data['halaman'] = "Tambah Data User";
            $this->create_edit_user($data);
        } else if ($type === "personil") {
            $this->data['halaman'] = "Tambah Data Personil";
            $this->create_edit_personil($data);
        }
    }

    function create_edit_user($data) {
      $this->data['user_role'] = $this->m_akun->getRole();
      $this->data['analis_role'] = $this->m_akun->getAnalisRole();

      if (!empty($data['id_user']) && isset($data['id_user'])) {
          $this->data['halaman'] = "Edit Data User";
          $this->data['id_user'] = $id = $data['id_user'];
          $this->data['edit'] = $this->m_akun->getUser(false, null, null, $data['id_user']);
      }

      if($this->input->post()){
        $data = $this->input->post();

        $role_analis = '|';
        if(!empty($data['role_analis'])){
          foreach ($data['role_analis'] as $r) {
            $role_analis .= $r.'|';
          }
        }
        $data['role_analis'] = $role_analis;

        if($data['id_user'] != '' && !is_null($data['id_user'])){
          //echo 'sukses'; die();
            $status = $this->m_akun->setUser($data, $data['id_user']);
        }else{
          //echo 'gagal'; die();
            $status = $this->m_akun->setUser($data);
        }

        if (isset($status)) {
            $msg = 'Data berhasil Disimpan';
            if (!empty($id))
                $msg = 'Data berhasil diubah';
            $this->session->set_flashdata('msg', $msg);
        }

        redirect('akun/data_user');

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

    function create_edit_personil($data) {
      $this->data['jabatan'] = $this->m_master->getDataMaster('jabatan');
      if (!empty($data['id_personil']) && isset($data['id_personil'])) {
          $this->data['halaman'] = "Edit Data Personil";
          $this->data['id_personil'] = $id = $data['id_personil'];
          $this->data['edit'] = $this->m_akun->detailPersonil($data['id_personil']);
      }
      if($this->input->post()){
        $data = $this->input->post();

        $jabatan= "|";
        foreach($data['id_jabatan'] as $j){
          $jabatan .= $j."|";
        }
        $data['id_jabatan'] = $jabatan;
        if (!empty($data['klik'])) {
          $status = $this->m_akun->setPersonil($data);
          if (isset($status)) {
              $msg = 'Data berhasil Disimpan';
              if (!empty($id))
                  $msg = 'Data berhasil diubah';
              $this->session->set_flashdata('msg', $msg);
          }

          redirect("akun/data_personil");
        }
      }
      $this->render('create_edit_personil');
    }

    function hapus_user(){
      $data['id'] = $this->input->get('id_user');
      $data['id_key'] = 'id_user';
      $data['table'] = 'user';

      $status = $this->m_akun->delete_user_personil($data);
      if($status){
        $this->session->set_flashdata('msg', 'Data user berhasil dihapus');
      }

      redirect('akun/data_user');
    }

    function hapus_personil(){
      $data['id'] = $this->input->get('id_personil');
      $data['id_key'] = 'id_personil';
      $data['table'] = 'personil';

      $status = $this->m_akun->delete_user_personil($data);
      if($status){
        $this->session->set_flashdata('msg', 'Data user berhasil dihapus');
      }

      redirect('akun/data_personil');
    }

}
