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

class Ref_karyawan extends CI_Controller {
	
	var $gparam = array();
	
	public function __construct() {
        parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('ref_karyawan_model', 'ref_karyawan');
		$this->gparam['privilage'] = getPrivilage($this->gparam['controller']);
		$this->gparam['restrict'] = 'You Cannot Access This Menu';
    }
	
	function cek_nik() {
		
		$id 	= "";
		$nik 	= $this->input->post('nik');
		$id 	= $this->input->post('id');
		
		//echo $role."--".$id; die(); 
		if ($id == "") {
			$cek = $this->ref_karyawan->cekNIK($nik);			
			if ($cek > 0) {
				$json = false;
			} else {
				$json = true;
			}
		} else {
			$cek = $this->ref_karyawan->cekNIKUpdate($nik,$id);			
			if ($cek > 0) {
				$json = false;
			} else {
				$json = true;
			}
		}
		
		echo json_encode($json);		
	}
	
	public function index()
	{
		
		if ($this->gparam['privilage']->privilage_view == 'Y') {
			
			$gridopt = $this->input->get(array('psize', 'pnumber'));
			header('Content-Type: text/html');
			
			$create_button = '""';			
			$update_button = '';
			$delete_button = '';		
			
			if ($this->gparam['privilage']->privilage_create == 'Y') {
				//create button
				$create_button = '<a href="javascript:void(0)"  class="btn btn-primary btn-xs" onclick="create_view();" >
									<i class="fa fa-lg fa-fw fa-plus"></i>Create
								  </a>';
			}	
			
			if ($this->gparam['privilage']->privilage_update == 'Y') {
				$update_button = "<a style=\"margin:4px;\" class=\"btn btn-success btn-xs\" onclick=\"edit_view('+row.id+')\" href=\"javascript:void(0)\"\
									group=\"\" data-toggle=\"tooltip\" title=\"Update\"><i class=\"fa fa-lg fa-fw fa-edit\"></i>\
								  </a>";
			}

			if ($this->gparam['privilage']->privilage_delete == 'Y') {	
				$delete_button = "<a style=\"margin:4px;\" class=\"btn btn-danger btn-xs\" onclick=\"delete_data('+row.id+')\" href=\"javascript:void(0)\" group=\"\" data-toggle=\"tooltip\" title=\"Delete\">\
									<i class=\"fa fa-lg fa-fw fa-trash-o\"></i>\
								</a>"; 
			}		

			
			
			// pnumber, psize
			$data = array(
					'controller'  			=> $this->gparam['controller'],
					'psize'  				=> (empty($gridopt['psize'])?10:$gridopt['psize']),
					'pnumber'  				=> (empty($gridopt['pnumber'])?1:$gridopt['pnumber']),
					'create' 				=> $create_button,
					'update' 				=> $update_button,
					'delete'				=> $delete_button
			);
			
			$this->load->view('ref_karyawan_view',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}
	
	
	
	function form() {
		
		if ($this->gparam['privilage']->privilage_view == 'Y') {
			$data['controller'] = $this->gparam['controller'];				
			$data['action'] = base_url()."index.php/".$this->gparam['controller']."/crud";
			$id = $this->uri->segment(3);	
			
			if (!empty($id)) {
				if ($this->gparam['privilage']->privilage_update == 'Y') {				
					//get value for edit
					$edit = $this->ref_karyawan->get_value_edit($id);
					$t_lahir = strtotime($edit->tanggal_lahir);
					$t_masuk = strtotime($edit->tanggal_masuk);
					
					$get_jabatan = $this->ref_karyawan->get_jabatan();
					$jabatan = '<label class="select">
									<select name="jabatan">
										<option value="">--Select--</option>';
					
					foreach ($get_jabatan as $value_jabatan) {
						if ($edit->jabatan == $value_jabatan['jabatan']) {
							$jabatan .='<option value="'.$value_jabatan['jabatan'].'" selected>'.$value_jabatan['keterangan'].'</option>';
						} else {
							$jabatan .='<option value="'.$value_jabatan['jabatan'].'">'.$value_jabatan['keterangan'].'</option>';
						}					
						 
					}

					$jabatan .=	'</select> <i></i> 
								</label>';	
					
					$data['nama_karyawan'] 		= $edit->nama_karyawan;
					$data['nik'] 				= $edit->nik;					
					$data['bidang_studi'] 		= $edit->bidang_studi;					
					$data['agama'] 				= $edit->agama;					
					$data['tanggal_lahir'] 		= date('d-m-Y',$t_lahir);				
					$data['tanggal_masuk'] 		= date('d-m-Y',$t_masuk);				
					$data['tempat_lahir'] 		= $edit->tempat_lahir;					
					$data['unit_kerja'] 		= $edit->unit_kerja;					
					$data['masa_kerja'] 		= $edit->masa_kerja;					
					$data['alamat'] 			= $edit->alamat;					
					$data['telepon'] 			= $edit->telepon;					
					$data['id'] 				= $id;
					
					if ($edit->jenis_kelamin == 'L') {
						
						$jenis_kelamin = '<label class="select">
									<select name="jenis_kelamin">
										<option value="">--Select--</option>
										<option value="L" selected>Laki-Laki</option>
										<option value="P">perempuan</option>									
																		
									</select> <i></i> 
								</label>';		
						
					} else {
						$jenis_kelamin = '<label class="select">
									<select name="jenis_kelamin">
										<option value="">--Select--</option>
										<option value="L" >Laki-Laki</option>
										<option value="P" selected>perempuan</option>									
																		
									</select> <i></i> 
								</label>';
					}
					
					$pen = $edit->pendidikan;		
								
					$pendidikan = '<label class="select">
									<select name="pendidikan">
										<option value="">--Select--</option>';
										
					if ($pen == 'sd') {
						$pendidikan .='<option value="sd" selected>Sekolah Dasar</option>
										<option value="smp">Sekolah Menengah Pertama</option>									
										<option value="sma">Sekolah Menengah Atas</option>									
										<option value="s1">Strata 1</option>									
										<option value="s2">Strata 2</option>									
										<option value="s3">Strata 3</option>';
					} else if ($pen == 'smp') {
						$pendidikan .='<option value="sd">Sekolah Dasar</option>
										<option value="smp" selected>Sekolah Menengah Pertama</option>									
										<option value="sma">Sekolah Menengah Atas</option>									
										<option value="s1">Strata 1</option>									
										<option value="s2">Strata 2</option>									
										<option value="s3">Strata 3</option>';
					} else if ($pen == 'sma') {
						$pendidikan .='<option value="sd">Sekolah Dasar</option>
										<option value="smp">Sekolah Menengah Pertama</option>									
										<option value="sma" selected>Sekolah Menengah Atas</option>									
										<option value="s1">Strata 1</option>									
										<option value="s2">Strata 2</option>									
										<option value="s3">Strata 3</option>';
					} else if ($pen == 's1') {
						$pendidikan .='<option value="sd">Sekolah Dasar</option>
										<option value="smp">Sekolah Menengah Pertama</option>									
										<option value="sma" >Sekolah Menengah Atas</option>									
										<option value="s1" selected>Strata 1</option>									
										<option value="s2">Strata 2</option>									
										<option value="s3">Strata 3</option>';
					}else if ($pen == 's2') {
						$pendidikan .='<option value="sd">Sekolah Dasar</option>
										<option value="smp">Sekolah Menengah Pertama</option>									
										<option value="sma" >Sekolah Menengah Atas</option>									
										<option value="s1" >Strata 1</option>									
										<option value="s2" selected>Strata 2</option>									
										<option value="s3">Strata 3</option>';
					}else if ($pen == 's3') {
						$pendidikan .='<option value="sd">Sekolah Dasar</option>
										<option value="smp">Sekolah Menengah Pertama</option>									
										<option value="sma">Sekolah Menengah Atas</option>									
										<option value="s1">Strata 1</option>									
										<option value="s2">Strata 2</option>									
										<option value="s3" selected>Strata 3</option>';
					} else {
						$pendidikan .='<option value="sd">Sekolah Dasar</option>
										<option value="smp">Sekolah Menengah Pertama</option>									
										<option value="sma">Sekolah Menengah Atas</option>									
										<option value="s1">Strata 1</option>									
										<option value="s2">Strata 2</option>									
										<option value="s3">Strata 3</option>';
					}							
																			
					$pendidikan .='</select> <i></i> 
								</label>';			
					
					
					$data['header'] 			= 'Edit Karyawan';
					$data['submit'] 			= 'update';	
				} else {
					echo $this->gparam['restrict']; die();
				}	
			} else {
				if ($this->gparam['privilage']->privilage_create == 'Y') {	
					
					$data['header'] = 'Add Karyawan';
					$data['submit'] = 'create';
					
					$get_jabatan = $this->ref_karyawan->get_jabatan();
					$jabatan = '<label class="select">
									<select name="jabatan">
										<option value="">--Select--</option>';
					foreach ($get_jabatan as $value_jabatan) {
						$jabatan .='<option value="'.$value_jabatan['jabatan'].'">'.$value_jabatan['keterangan'].'</option>'; 
					}	
					
					$jabatan .=	'</select> <i></i> 
								</label>';
								
					$jenis_kelamin = '<label class="select">
									<select name="jenis_kelamin">
										<option value="">--Select--</option>
										<option value="L">Laki-Laki</option>
										<option value="P">perempuan</option>									
																		
									</select> <i></i> 
								</label>';				
								
					$pendidikan = '<label class="select">
									<select name="pendidikan">
										<option value="">--Select--</option>
										<option value="sd">Sekolah Dasar</option>
										<option value="smp">Sekolah Menengah Pertama</option>									
										<option value="sma">Sekolah Menengah Atas</option>									
										<option value="s1">Strata 1</option>									
										<option value="s2">Strata 2</option>									
										<option value="s3">Strata 3</option>									
									</select> <i></i> 
								</label>';			
				} else {
					echo $this->gparam['restrict']; die();
				}	
			}
			
			$data['jenis_kelamin'] 	= $jenis_kelamin;
			$data['jabatan'] 		= $jabatan;
			$data['pendidikan'] 	= $pendidikan;
			$this->load->view('crud_ref_karyawan',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}	
	
	
	public function crud() {
		
		$crud = $this->input->post('submit');
		if ($crud == 'create') {
			
			$insert = $this->ref_karyawan->insert_data();
			if ($insert) {
				$json = true;
			}
			
		} else if ($crud == 'update') {
			$update = $this->ref_karyawan->update_data();
			if ($update) {
				$json = true;			
			}
				
		}
		
		echo json_encode($json);
		
	}
	
	public function delete() {
		$id = $this->input->post("id");
		$delete = $this->ref_karyawan->delete_data($id);
		if ($delete) {
			$json = true;
		}
		echo json_encode($json);	
	}
	
	function load_data() {
		
		header('Content-Type: application/jsonp');
        $list = $this->ref_karyawan->get_list_data();
		
        echo json_encode($list);
	}
	
	
}
