<?php 
class Ref_karyawan_model extends CI_Model {
	
	function cekNIK($nik) {
		
		$this->db->select("id");
		$this->db->from("ref_karyawan");
		$this->db->where("upper(nik)",strtoupper($nik));
		$num = $this->db->get()->num_rows();
		return $num;
		
	}
	
	function cekNIKUpdate($nik,$id) {
		
		//get old role 
		$this->db->select("nik");
		$this->db->from("ref_karyawan");
		$this->db->where("id",$id);
		$oldNik = $this->db->get()->row()->nik;
		if (strtoupper($oldNik) == strtoupper($nik)) {
			$num = 0;
		} else {
			
			$this->db->select("id");
			$this->db->from("ref_karyawan");
			$this->db->where("upper(nik)",strtoupper($nik));
			$num = $this->db->get()->num_rows();
			return $num;
			
		}		
		return $num;
		
	}
	
	function get_jabatan() {
		$this->db->select('*');
		$this->db->from('ref_jabatan');
		$data = $this->db->get()->result_array();
		return $data;
	}
	
	function get_value_edit($id) {
		$this->db->select('*');
		$this->db->from('ref_karyawan');
		$this->db->where('id',$id);
		$data = $this->db->get()->row();
		return $data;
	}
	
	function delete_data($id){
		$this->db->where('id', $id);
		$return = $this->db->delete('ref_karyawan');
		return $return;
	}
	
	function get_list_data() {
	
		$param = $this->input->post(array('page', 'rows', 'sort', 'order', 'filterRules'));
        $offset = intval(($param['page'] - 1) * $param['rows']);
		
		// clause filter, array not null
        $cond = '';
        if (count($param['filterRules']) > 0) {
            $filter = json_decode($param['filterRules']);
            $loop = 0;
            foreach ($filter as $json) {
                // convert to array
                $rule = get_object_vars($json);
                // declare variable
                $field = $rule['field'];
                $opt = $rule['op'];
                $value = $rule['value'];
                if ($loop == 0) {
                    // user where
                    if (!empty($value)) {
                        if ($opt == 'contains') {
                            $cond .= "where ($field like '%$value%')";
                        } else if ($opt == 'greater') {
                            $cond .= "where $field > '$value'";
                        } else if ($opt == 'less') {
                            $cond .= "where $field < '$value'";
                        } else if ($opt == 'notequal') {
                            $cond .= "where $field != '$value'";
                        } else if ($opt == 'equal') {
                            $cond .= "where $field = '$value'";
                        }
                        $loop++; // flag where
                    }
                } else {
                    // user and
                    if (!empty($value)) {
                        if ($opt == 'contains') {
                            $cond .= " and ($field like '%$value%')";
                        } else if ($opt == 'greater') {
                            $cond .= " and $field > '$value'";
                        } else if ($opt == 'less') {
                            $cond .= " and $field < '$value'";
                        } else if ($opt == 'notequal') {
                            $cond .= " and $field != '$value'";
                        } else if ($opt == 'equal') {
                            $cond .= " and $field = '$value'";
                        }
                    }
                }
            }
        }
		
		$response = array();
		 $table = 'ref_karyawan'; 
		 
        if (empty($param['sort'])) {
            if (empty($cond)) {
                $sql = "select * from $table  order by id limit ?, ?";
                $sqlcount = "select * from $table";
            } else {
                $sql = "select * from $table  $cond order by id limit ?, ?";
                $sqlcount = "select * from $table $cond";
            }
            $result_array = $this->db->query($sql, array($offset, intval($param['rows'])));
            $response['total'] = $this->db->query($sqlcount)->num_rows();
            $response['rows'] = $result_array->result();
        } else {
            $sort = $param['sort'];
            $order = $param['order'];
            if (empty($cond)) {
                $sql = "select * from $table order by $sort $order limit ?, ?";
                $sqlcount = "select id from $table";
            } else {
                $sql = "select * from $table  $cond order by $sort $order limit ?, ?";
                $sqlcount = "select id from $table  $cond";
            }
            $result_array = $this->db->query($sql, array($offset, intval($param['rows'])));
            $response['total'] = $this->db->query($sqlcount)->num_rows();
            $response['rows'] = $result_array->result();
        }
        return $response;
	}
	
	function update_data() {
		
		$id = $this->input->post('id');	
		
		/*$t_lahir = $this->input->post('tanggal_lahir');
		$t_lahir = date_create_from_format('d-m-Y', $t_lahir);		
		$tanggal_lahir = date_format($t_lahir, 'Y-m-d');*/
		$tanggal_lahir = $this->input->post('tanggal_lahir');
		
		/*$t_masuk = $this->input->post('tanggal_masuk');
		$t_masuk = date_create_from_format('d-m-Y', $t_masuk);*/		
		$tanggal_masuk = $this->input->post('tanggal_masuk');
		
		
		$data['masa_kerja'] 	= $this->input->post('masa_kerja');
		$data['agama'] 			= $this->input->post('agama');
		$data['jabatan'] 		= $this->input->post('jabatan');
		$data['nama_karyawan'] 	= $this->input->post('nama_karyawan');
		$data['jenis_kelamin'] 	= $this->input->post('jenis_kelamin');
		$data['pendidikan'] 	= $this->input->post('pendidikan');
		$data['nik'] 			= $this->input->post('nik');
		$data['bidang_studi'] 	= $this->input->post('bidang_studi');
		$data['tempat_lahir'] 	= $this->input->post('tempat_lahir');
		$data['unit_kerja'] 	= $this->input->post('unit_kerja');
		$data['unit_kerja'] 	= $this->input->post('unit_kerja');
		$data['alamat'] 		= $this->input->post('alamat');
		$data['telepon'] 		= $this->input->post('telepon');
		$data['tanggal_lahir'] 	= $tanggal_lahir;
		$data['tanggal_masuk'] 	= $tanggal_masuk;
			
		$data['modified_date']	= date("Y-m-d H:i:s");
		$data['modified_by']	= $this->session->userdata('username');
		
		$this->db->where("id",$id);
		$q = $this->db->update("ref_karyawan",$data);
		return $q;	
		
	}
	
	function insert_data() {
		
		/*$t_lahir = $this->input->post('tanggal_lahir');
		$t_lahir = date_create_from_format('d-m-Y', $t_lahir);		
		$tanggal_lahir = date_format($t_lahir, 'Y-m-d');*/
		$tanggal_lahir = $this->input->post('tanggal_lahir');
		
		/*$t_masuk = $this->input->post('tanggal_masuk');
		$t_masuk = date_create_from_format('d-m-Y', $t_masuk);*/		
		$tanggal_masuk = $this->input->post('tanggal_masuk');
		
		$data['masa_kerja'] 	= $this->input->post('masa_kerja');
		$data['agama'] 			= $this->input->post('agama');
		$data['jabatan'] 		= $this->input->post('jabatan');
		$data['nama_karyawan'] 	= $this->input->post('nama_karyawan');
		$data['jenis_kelamin'] 	= $this->input->post('jenis_kelamin');
		$data['pendidikan'] 	= $this->input->post('pendidikan');
		$data['nik'] 			= $this->input->post('nik');
		$data['bidang_studi'] 	= $this->input->post('bidang_studi');
		$data['tempat_lahir'] 	= $this->input->post('tempat_lahir');
		$data['unit_kerja'] 	= $this->input->post('unit_kerja');
		$data['unit_kerja'] 	= $this->input->post('unit_kerja');
		$data['alamat'] 		= $this->input->post('alamat');
		$data['telepon'] 		= $this->input->post('telepon');
		$data['tanggal_lahir'] 	= $tanggal_lahir;
		$data['tanggal_masuk'] 	= $tanggal_masuk;
		
	
		$data['created_date']	= date("Y-m-d H:i:s");
		$data['created_by']	= $this->session->userdata('username');
		$q = $this->db->insert("ref_karyawan",$data);
		return $q;	
		
	}
	
}