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

class User extends CI_Controller
{
  function __construct()
  {
    parent::__construct();
    $this->load->model('User_model');
    $this->load->model('Jenis_form_model');
    $this->load->model('Kategori_admin_model');

    $this->data['module'] = 'User';
    if($this->session->userdata('status') != "login"){
      redirect(base_url("login"));
    }
  }

  public function index()
  {
    $this->data['title'] = 'Data '.$this->data['module'];
    $this->load->view('user/user_list', $this->data);
  }

  public function admin()
  {
    $this->data['title'] = 'Data '.$this->data['module'];
    $this->data['active'] = 'admin';
    $this->data['user'] = $this->User_model->get_admin('0');
    $this->load->view('user/admin', $this->data);
  }

  public function admin_supervisor()
  {
    $this->data['title'] = 'Data '.$this->data['module'].' Admin';
    $this->data['active'] = 'admin_supervisor';
    $this->data['user'] = $this->User_model->get_admin_supervisor();
    $this->load->view('user/admin_supervisor', $this->data);
  }

  public function user_pending()
  {
    $this->data['title'] = 'Data '.$this->data['module'].' Pending';
    $this->data['active'] = 'user_pending';
    $this->data['user'] = $this->User_model->get_user_by_status('0');
    $this->load->view('user/user', $this->data);
  }

  public function ajax_list()
	{
    //get_datatables terletak di model
    $list = $this->User_model->get_datatables();
    $data = array();
		$no = $_POST['start'];

    // Membuat loop/ perulangan
    foreach ($list as $data_user) {
			$no++;
			$row = array();
      $row[] = '<p style="text-align: center">'.$no.'</p>';
      $row[] = '<p style="text-align: left">'.$data_user->nama_user.'</p>';
      $row[] = '<p style="text-align: left">'.$data_user->username.'</p>';
      $row[] = '<p style="text-align: left">'.$data_user->id_user_group.'</p>';
      $row[] = '<p style="text-align: left">'.$data_user->jabatan.'</p>';
      $row[] = '<p style="text-align: left">'.$data_user->jobdesk.'</p>';

      // Penambahan tombol edit dan hapus
      if ($this->session->userdata('id_user_group') == "2"){
      $row[] = '
      <p style="text-align: center">
      	<a class="btn btn-sm btn-warning" onclick="open_modal(\'Edit Data User\',\'form_edit\',\''.encrypt($data_user->id_user).'\')" title="Edit"><i class="fa fa-pencil"></i></a>
        <a class="btn btn-sm btn-danger" href="'.base_url('user/delete/').encrypt($data_user->id_user).'" title="Hapus" onclick="javasciprt: return confirm(\'Apakah Anda yakin ingin menghapus ?\')"><i class="fa fa-remove"></i></a>
			</p>';
		}else{
			$row[] = '';
		}

      $data[] = $row;
    }

    $output = array(
              "draw" => $_POST['draw'],
              "recordsTotal" => $this->User_model->count_all(),
              "recordsFiltered" => $this->User_model->count_filtered(),
              "data" => $data
              );
    //output to json format
    echo json_encode($output);
  }

  
  public function add()
  {
      $data = array(
        'nama_user'          => $this->input->post('nama_user'),
        'username'  => $this->input->post('username'),
        'password'     => password_hash($this->input->post('password'), PASSWORD_BCRYPT),
        'jabatan'       => $this->input->post('jabatan'),
        'id_user_group'       => $this->input->post('id_user_group'),
        'jobdesk' 	      => $this->input->post('jobdesk')
      );
      // eksekusi query INSERT
      $insert = $this->User_model->insert($data);
      // set pesan data berhasil dibuat
        $this->session->set_flashdata('message', '
        <div class="alert alert-block alert-success"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button>
          <i class="ace-icon fa fa-bullhorn green"></i> Data berhasil disimpan
        </div>');
        redirect(base_url('user'));
  }

  public function update()
    {
      $id=decrypt($this->input->post('id_user'));
        if ($this->input->post('password')==""){
          $data = array(
	        'nama_user'          => $this->input->post('nama_user'),
	        'username'          => $this->input->post('username'),
          );

        }else{
          $data = array(
	        'nama_user'          => $this->input->post('nama_user'),
	        'username'          => $this->input->post('username'),
	        'password'     => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
          );

        }
        // eksekusi query
        $this->User_model->update($id, $data);
        // set pesan data berhasil dibuat
          $this->session->set_flashdata('message', '
          <div class="alert alert-block alert-success"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button>
            <i class="ace-icon fa fa-bullhorn green"></i> Data berhasil diperbaharui
          </div>');
		  echo "1";
          //redirect(base_url('user'));
    }


  public function approve()
    {
      $id=decrypt($this->input->post('id'));

          $data = array(
          'status'          => '1'
          );

        
        // eksekusi query
        $this->User_model->update($id, $data);
        // set pesan data berhasil dibuat
          $this->session->set_flashdata('message', '
          <div class="alert alert-block alert-success"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button>
            <i class="ace-icon fa fa-bullhorn green"></i> Data berhasil diperbaharui
          </div>');
          echo "1";
    }  
  public function delete()
  {
      $id = decrypt($this->input->post('id'));
      $this->User_model->delete($id);

      $this->session->set_flashdata('message', '
      <div class="alert alert-block alert-success"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button>
        <i class="ace-icon fa fa-bullhorn green"></i> Data berhasil dihapus
      </div>');
          echo "1";
      //redirect(base_url('user'));

  }

  public function form_open()
  {
    $form_open=$this->input->post('form_open');
    $id=decrypt($this->input->post('id'));
    $this->data['user'] = $this->User_model->get_by_id($id);
    $this->data['title'] = 'Data '.$this->data['module'];
    $this->load->view('user/'.$form_open, $this->data);

//    $this->load->view('user/'.$form_open, $id);
  }



  public function update_password()
    {
      $id=$this->session->userdata('id_user');
          $data = array(
          'password'     => password_hash($this->input->post('password_user'), PASSWORD_DEFAULT)
          );

        // eksekusi query
        $this->User_model->update($id, $data);
        // set pesan data berhasil dibuat
          $this->session->set_flashdata('message', '
          <div class="alert alert-block alert-success"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button>
            <i class="ace-icon fa fa-bullhorn green"></i> Data berhasil diperbaharui
          </div>');
          redirect(base_url());
    }
  
	function get_data(){
		$id = decrypt($this->input->post('id'));
		$data = $this->User_model->get_by_id($id);
		echo json_encode($data);
	}

    function get_data_kategori(){
      $id = $this->input->post('id_user');
      $data['id_user'] = $id;
      $data['jenis_form'] = $this->Jenis_form_model->get_dropdown_jenis_form();
      $this->load->view('user/data_jenis_form', $data);
    }


  public function add_admin_supervisor(){
    $username = $this->User_model->get_by_username($this->input->post('username'));
    if(!(empty($username))){
      echo 3;
      die();
    }
    if (!($this->input->post('password')==$this->input->post('repassword'))){
      echo 2;
      die();

    }
        $data = array(
          'nama_user'          => $this->input->post('nama_user'),
          'username'          => $this->input->post('username'),
          'password'          => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
          'level'          => 1,
        );
        // eksekusi query INSERT
        $id_user = $this->User_model->insert($data);

        $jenis_form = $this->Jenis_form_model->get_dropdown_jenis_form();

        foreach($jenis_form as $jenis_form){
          if($this->input->post($jenis_form->id_jenis_form)){
              $data_akses = array(
                  'id_user' => $id_user,
                  'id_jenis_form' => $jenis_form->id_jenis_form
                );
                $this->Kategori_admin_model->insert($data_akses);
          }
        }
    
      // set pesan data berhasil dibuat
        $this->session->set_flashdata('message', '
        <div class="alert alert-block alert-success"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button>
          <i class="ace-icon fa fa-bullhorn green"></i> Data berhasil disimpan
        </div>');
    echo "1";
    }


 public function update_admin_supervisor()
    {
      $id=decrypt($this->input->post('id_user'));
        if ($this->input->post('password')==""){
          $data = array(
          'nama_user'          => $this->input->post('nama_user'),
          'username'          => $this->input->post('username'),
          );

        }else{
          $data = array(
          'nama_user'          => $this->input->post('nama_user'),
          'username'          => $this->input->post('username'),
          'password'     => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
          );

        }
        // eksekusi query
        $this->User_model->update($id, $data);
        $this->Kategori_admin_model->delete_user($id);
        $id_user = $id;

        $jenis_form = $this->Jenis_form_model->get_dropdown_jenis_form();

        foreach($jenis_form as $jenis_form){
          if($this->input->post($jenis_form->id_jenis_form)){
              $data_akses = array(
                  'id_user' => $id_user,
                  'id_jenis_form' => $jenis_form->id_jenis_form
                );
                $this->Kategori_admin_model->insert($data_akses);
          }
        }

        // set pesan data berhasil dibuat
          $this->session->set_flashdata('message', '
          <div class="alert alert-block alert-success"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button>
            <i class="ace-icon fa fa-bullhorn green"></i> Data berhasil diperbaharui
          </div>');
      echo "1";
          //redirect(base_url('user'));
    }


}
