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

        class karyawan  extends CI_Controller {

            public function __construct()
            {
                parent::__construct();
                $this->load->library(array('form_validation', 'pagination')); 
                $this->load->helper('url');
                $this->load->model("karyawan_model"); 
            }

            public function index(){
                 if($this->session->userdata('LOGIN')=='TRUE' && $this->session->userdata('LEVEL')==0){
                $data['main']='Karyawan';
                $data['judul']='Kelola Karyawan';  
                $data['query']=$this->karyawan_model->all(); 
                $data['view'] = 'karyawan/kelola'; 
                $this->load->view('index',$data);
               }else if($this->session->userdata('LOGIN')=='TRUE' && $this->session->userdata('LEVEL')==1){
                $data['main']='Karyawan';
                $data['judul']='Kelola Profile';  
                $data['query']=$this->karyawan_model->mykaryawan(); 
                $data['view'] = 'karyawan/profile'; 
                $this->load->view('index',$data);
                }
             }
            
            public function tambah() {
               if($this->session->userdata('LOGIN')=='TRUE' && $this->session->userdata('LEVEL')==0){
                $this->_validate();
                $data = array(
                    'nik' => $this->input->post('nik'),
                    'nama' => $this->input->post('nama'),
                    'jk' => $this->input->post('jk'),
                    'jabatan' => $this->input->post('jabatan'),
                    'username' => $this->input->post('username'),
                    'password' => md5($this->input->post('password')),
                     );
               $this->karyawan_model->save($data);
                echo json_encode(array("status" => TRUE));
                }else{
                     redirect('beranda'); 
                }
                
            }
           
            public function edit($id_kar){
                 if($this->session->userdata('LOGIN')=='TRUE' && $this->session->userdata('LEVEL')==0){
            $data = $this->karyawan_model->get_by_id($id_kar);
            echo json_encode($data);
            }else{
                     redirect('beranda'); 
                }
            }

            public function update()
            {
                 if($this->session->userdata('LOGIN')=='TRUE' && $this->session->userdata('LEVEL')==0){
                $this->_validate();
                $data = array(
                    'nik' => $this->input->post('nik'),
                    'nama' => $this->input->post('nama'),
                    'jk' => $this->input->post('jk'),
                    'jabatan' => $this->input->post('jabatan'),
                    'username' => $this->input->post('username'),
                    'password' => md5($this->input->post('password')),
                     );
                $this->karyawan_model->update(array('id_kar' => $this->input->post('id_kar')), $data);
                echo json_encode(array("status" => TRUE));
                }else{
                     redirect('beranda'); 
                }
               
                }
             public function updateprofile()
            {
                 if($this->session->userdata('LOGIN')=='TRUE'){
                $this->_validateprofile();
                $data = array(
                    'nik' => $this->input->post('nik'),
                    'nama' => $this->input->post('nama'),
                    'jk' => $this->input->post('jk'),
                    'jabatan' => $this->input->post('jabatan'),
                     );
                $this->karyawan_model->update(array('id_kar' => $this->input->post('id_kar')), $data);
                echo json_encode(array("status" => TRUE));
                }else{
                     redirect('beranda'); 
                }
               
                } public function updateakun()
            {
                 if($this->session->userdata('LOGIN')=='TRUE'){
                $this->_validateakun();
                $data = array(
                   
                    'username' => $this->input->post('username'),
                    'password' => md5($this->input->post('password')),
                     );
                $this->karyawan_model->update(array('id_kar' => $this->input->post('id_kar')), $data);
                echo json_encode(array("status" => TRUE));
                }else{
                     redirect('beranda'); 
                }
               
                }
            public function delete()
            {
                 if($this->session->userdata('LOGIN')=='TRUE' && $this->session->userdata('LEVEL')==0){
                $id_kar=$this->uri->segment(3);
                $this->karyawan_model->delete($id_kar);
                 redirect('karyawan','refresh');
               }else{
                     redirect('beranda'); 
                }  
                
            }
            private function _validate()
            {
                $data = array();
                $data['error_string'] = array();
                $data['inputerror'] = array();
                $data['status'] = TRUE;
                $username = $this->input->post('username');
                $resultuser = $this->karyawan_model->checkusername($username);
               
                if($this->input->post('nik') == '')
                {
                    $data['inputerror'][] = 'nik';
                    $data['error_string'][] = 'NIK harap diisi';
                    $data['status'] = FALSE;
                }
                if($this->input->post('nama') == '')
                {
                    $data['inputerror'][] = 'nama';
                    $data['error_string'][] = 'nama harap diisi';
                    $data['status'] = FALSE;
                }

                if($this->input->post('jk') == '')
                {
                    $data['inputerror'][] = 'jk';
                    $data['error_string'][] = 'jenis kelamin harap diisi';
                    $data['status'] = FALSE;
                }
                if($this->input->post('jabatan') == '')
                {
                    $data['inputerror'][] = 'jabatan';
                    $data['error_string'][] = 'jabatan harap diisi';
                    $data['status'] = FALSE;
                }
                if ($resultuser == '1') 
                {
                    $data['inputerror'][] = 'username';
                    $data['error_string'][] = 'username diambil orang lain';
                    $data['status'] = FALSE;
                }
                if($this->input->post('username') == '')
                {
                    $data['inputerror'][] = 'password';
                    $data['error_string'][] = 'password harap diisi';
                    $data['status'] = FALSE;
                }
                if($this->input->post('password') == '')
                {
                    $data['inputerror'][] = 'password';
                    $data['error_string'][] = 'password harap diisi';
                    $data['status'] = FALSE;
                }
                if($data['status'] === FALSE)
                {
                    echo json_encode($data);
                    exit();
                }
            }  
             private function _validateprofile()
            {
                $data = array();
                $data['error_string'] = array();
                $data['inputerror'] = array();
                $data['status'] = TRUE;
                
               
                if($this->input->post('nik') == '')
                {
                    $data['inputerror'][] = 'nik';
                    $data['error_string'][] = 'NIK harap diisi';
                    $data['status'] = FALSE;
                }
                if($this->input->post('nama') == '')
                {
                    $data['inputerror'][] = 'nama';
                    $data['error_string'][] = 'nama harap diisi';
                    $data['status'] = FALSE;
                }

                if($this->input->post('jk') == '')
                {
                    $data['inputerror'][] = 'jk';
                    $data['error_string'][] = 'jenis kelamin harap diisi';
                    $data['status'] = FALSE;
                }
                if($this->input->post('jabatan') == '')
                {
                    $data['inputerror'][] = 'jabatan';
                    $data['error_string'][] = 'jabatan harap diisi';
                    $data['status'] = FALSE;
                }
                
                if($data['status'] === FALSE)
                {
                    echo json_encode($data);
                    exit();
                }
            }  
           
         private function _validateakun()
            {
                $data = array();
                $data['error_string'] = array();
                $data['inputerror'] = array();
                $data['status'] = TRUE;
                
               
                if($this->input->post('username') == '')
                {
                    $data['inputerror'][] = 'username';
                    $data['error_string'][] = 'username harap diisi';
                    $data['status'] = FALSE;
                }
                if($this->input->post('password') == '')
                {
                    $data['inputerror'][] = 'password';
                    $data['error_string'][] = 'Password harap diisi harap diisi';
                    $data['status'] = FALSE;
                }
                
                if($data['status'] === FALSE)
                {
                    echo json_encode($data);
                    exit();
                }
            }     
    }
