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

        class absen  extends CI_Controller {

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

            public function index(){
               
                $data['query']=$this->absen_model->absensidpn(); 
                $data['absen']=$this->absen_model->absensidpn(); 
                $this->load->view('absen',$data);
            
               
             }
             public function kelola(){
                if($this->session->userdata('LOGIN')=='TRUE'){
                $data['main']='Absen';
                $data['judul']='Kelola Absen'; 
                $data['query']=$this->absen_model->absensi();
                $data['karyawan']=$this->karyawan_model->all(); 
                $data['view'] = 'absen/kelola'; 
                $this->load->view('index',$data);
                }else{
                     redirect('beranda'); 
                }
               
             }
            
            public function tambah() {
                if($this->session->userdata('LOGIN')=='TRUE' && $this->session->userdata('LEVEL')==0){
                $this->_validate();
                $tgl=$this->input->post('tgl');
                $id_kar=$this->input->post('id_kar');
                if($this->input->post('jenis')=='masuk'){
                    $jam='masuk';
                }else{
                    $jam='keluar';
                }
                $data = array(
                    'id_kar' => $this->input->post('id_kar'),
                    'tgl' => $this->input->post('tgl'),
                    $jam => $this->input->post('jam'),
                     );
               $this->absen_model->save($tgl,$id_kar,$data);
                echo json_encode(array("status" => TRUE));
                }else{
                     redirect('beranda'); 
                }
                
            }
            public function cekmasuk(){
                
                $id_kar=$this->input->post('id_kar');
                $pass=md5($this->input->post('pass'));
                $result=$this->karyawan_model->cek($id_kar,$pass);
                if ($result==1) {
                    $data = array(
                        'id_kar' => $id_kar, 
                        'masuk' =>date('H:i:s'),
                        'tgl' =>date('y-m-d'),);
                    $this->absen_model->jammasuk($data);
                    echo 1;
                    
                }if($result==0){
                    echo 0;
                }
                
            }
            public function cekkeluar(){
                
                $id_kar=$this->input->post('id_kar');
                $tgl=date('y-m-d');
                $jam=date('H:i:s');
                $pass=md5($this->input->post('pass'));
                
                $result=$this->karyawan_model->cek($id_kar,$pass);
                if ($result==1) {
                    $data = array(
                        'keluar' =>date('H:i:s'),);
                    $this->absen_model->jammkeluar($id_kar,$tgl,$data);
                    echo 1;
                    
                }if($result==0){
                    echo 0;
                }
            
            }
            public function edit($id_ab){
            $data = $this->absen_model->pilih($id_ab);
            echo json_encode($data);
            }

            public function update()
            {    if($this->session->userdata('LOGIN')=='TRUE' && $this->session->userdata('LEVEL')==0){
                $id_ab=$this->input->post('id_ab');
                if($this->input->post('keluar')==''){
                    $keluar=null;
                }else{
                    $keluar=$this->input->post('keluar');
                }
                $this->_validateup();
                $data = array(
                    'id_ab' => $this->input->post('id_ab'),
                    'id_kar' => $this->input->post('id_kar'),
                    'tgl' => $this->input->post('tgl'),
                    'masuk' => $this->input->post('masuk'),
                    'keluar' => $keluar,
                     );
                $this->absen_model->update($id_ab, $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_ab=$this->uri->segment(3);
                 $this->absen_model->delete($id_ab);
                 redirect('absen/kelola','refresh'); 
                }else{
                    redirect('beranda');   
                }
                
                
            }
            private function _validateup()
            {
                $data = array();
                $data['error_string'] = array();
                $data['inputerror'] = array();
                $data['status'] = TRUE;
               
                if($this->input->post('id_kar') == '')
                {
                    $data['inputerror'][] = 'id_kar';
                    $data['error_string'][] = 'Nik harap diisi';
                    $data['status'] = FALSE;
                }
                if($this->input->post('tgl') == '')
                {
                    $data['inputerror'][] = 'tgl';
                    $data['error_string'][] = 'Tanggal harap diisi';
                    $data['status'] = FALSE;
                }
                if($this->input->post('masuk') == '')
                {
                    $data['inputerror'][] = 'masuk';
                    $data['error_string'][] = 'Jam Masuk harap diisi';
                    $data['status'] = FALSE;
                }
                if($data['status'] === FALSE)
                {
                    echo json_encode($data);
                    exit();
                }
            }  

            private function _validate()
            {
                $data = array();
                $data['error_string'] = array();
                $data['inputerror'] = array();
                $data['status'] = TRUE;
               
                if($this->input->post('jenis') == '')
                {
                    $data['inputerror'][] = 'jenis';
                    $data['error_string'][] = 'Jenis harap diisi';
                    $data['status'] = FALSE;
                }
                if($this->input->post('id_kar') == '')
                {
                    $data['inputerror'][] = 'id_kar';
                    $data['error_string'][] = 'Nik harap diisi';
                    $data['status'] = FALSE;
                }
                if($this->input->post('tgl') == '')
                {
                    $data['inputerror'][] = 'tgl';
                    $data['error_string'][] = 'Tanggal harap diisi';
                    $data['status'] = FALSE;
                }
                
                if($data['status'] === FALSE)
                {
                    echo json_encode($data);
                    exit();
                }
            }  
            
    }
