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

class Adm_user extends CI_Controller {
	
	var $gparam = array();
	
	public function __construct() {
       parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('user_model', 'user');
		$this->gparam['privilage'] = getPrivilage($this->gparam['controller']);
		$this->gparam['restrict'] = 'You Cannot Access This Menu';
    }
	
	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.userid+')\" 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.userid+')\" href=\"javascript:void(0)\" group=\"\" data-toggle=\"tooltip\" title=\"Delete\">\
										<i class=\"fa fa-trash-o  fa-lg\"></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('user_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->user->get_value_edit($id);					
					$data['userid'] 		= $id;
					$data['username'] 		= $edit->username;
					$data['alias'] 			= $edit->alias;
					
					$valRole = '';
					$get_row = $this->user->getRole();
					foreach ($get_row as $valRow) {
						if ($edit->role_id == $valRow['role_id']) {
							$valRole .= '<option value="'.$valRow['role_id'].'" selected>'.$valRow['role_name'].'</option>';
						} else {
							$valRole .= '<option value="'.$valRow['role_id'].'">'.$valRow['role_name'].'</option>';
						}						 
					}
					
					$role = '<label class="select">
									<select name="role_id" id="role_id">
										<option value="">--Select--</option>
										'.$valRole.'									
									</select> <i></i> 
								</label>';	
								
					$valKar = '';
					$getKar = $this->user->getKar();
					foreach ($getKar as $kalRow) {
						if ($edit->id_karyawan == $kalRow['id']) {
							$valKar .= '<option value="'.$kalRow['id'].'" selected>'.$kalRow['nama_karyawan'].'</option>';					
						} else {
							$valKar .= '<option value="'.$kalRow['id'].'">'.$kalRow['nama_karyawan'].'</option>';					
						}
					}
					
					$karyawan = '<label class="select">
									<select name="id_karyawan" id="id_karyawan">
										<option value="">--Select--</option>
										'.$valKar.'									
									</select> <i></i> 
								</label>';
								
					if ($edit->user_status == 'Y') {
						$status = '<label class="select">
									<select name="user_status" id="user_status">
										<option value="">--Select--</option>
										<option value="Y" selected>Valid</option>
										<option value="N">Invalid</option>									
									</select> <i></i> 
								</label>';	
					
					} else if ($edit->user_status == 'N') {
						$status = '<label class="select">
									<select name="user_status" id="user_status">
										<option value="">--Select--</option>
										<option value="Y">Valid</option>
										<option value="N" selected>Invalid</option>									
									</select> <i></i> 
								</label>';	
					
					} else {	
						$status = '<label class="select">
										<select name="user_status" id="user_status">
											<option value="">--Select--</option>
											<option value="Y">Valid</option>
											<option value="N">Invalid</option>									
										</select> <i></i> 
									</label>';	
					}
					$data['header'] 		= 'Edit User';
					$data['submit'] 		= 'update';	
				} else {
					echo $this->gparam['restrict']; die();
				}	
			} else {
				if ($this->gparam['privilage']->privilage_create == 'Y') {	
					
					$valRole = '';
					$get_row = $this->user->getRole();
					foreach ($get_row as $valRow) {
						$valRole .= '<option value="'.$valRow['role_id'].'">'.$valRow['role_name'].'</option>'; 
					}
					
					$role = '<label class="select">
									<select name="role_id" id="role_id">
										<option value="">--Select--</option>
										'.$valRole.'									
									</select> <i></i> 
								</label>';	
								
					$valKar = '';
					$getKar = $this->user->getKar();
					foreach ($getKar as $kalRow) {
						$valKar .= '<option value="'.$kalRow['id'].'">'.$kalRow['nama_karyawan'].'</option>';
					}
					
					$karyawan = '<label class="select">
									<select name="id_karyawan" id="id_karyawan">
										<option value="">--Select--</option>
										'.$valKar.'									
									</select> <i></i> 
								</label>';	
					
					$status = '<label class="select">
									<select name="user_status" id="user_status">
										<option value="">--Select--</option>
										<option value="Y">Valid</option>
										<option value="N">Invalid</option>									
									</select> <i></i> 
								</label>';					
					
					$data['header'] = 'Add User';
					$data['submit'] = 'create';
				} else {
					echo $this->gparam['restrict']; die();
				}	
			}
			
			$data['id_karyawan'] 	 = $karyawan;
			$data['role_id'] 	 	 = $role;
			$data['user_status'] 	 = $status;
			$this->load->view('crud_user',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}
	
	public function crud() {
		
		$crud = $this->input->post('submit');
		if ($crud == 'create') {
			
			$insert = $this->user->insert_data();
			if ($insert) {
				$json = true;
			}
			
		} else if ($crud == 'update') {
			$update = $this->user->update_data();
			if ($update) {
				$json = true;			
			}
				
		}
		
		echo json_encode($json);
		
	}
	
	public function delete() {
		
		$delete = $this->user->delete_data();
		if ($delete) {
			$json = true;
		}
		echo json_encode($json);	
	}

	
	function cek_karyawan() {
		$userid 		= "";
		$id_karyawan 	= $this->input->post('id_karyawan');
		$userid 		= $this->input->post('userid');
		
		//echo $role."--".$id; die(); 
		if ($userid == "") {
			$cek = $this->user->cekKaryawan($id_karyawan);			
			if ($cek > 0) {
				$json = false;
			} else {
				$json = true;
			}
		} else {
			$cek = $this->user->cekKaryawanUpdate($id_karyawan,$userid);			
			if ($cek > 0) {
				$json = false;
			} else {
				$json = true;
			}
		}
		
		echo json_encode($json);	
	}

	function cek_username() {
		$userid 		= "";
		$username 		= $this->input->post('username');
		$userid 		= $this->input->post('userid');
		
		//echo $role."--".$id; die(); 
		if ($userid == "") {
			$cek = $this->user->cekUsername($username);			
			if ($cek > 0) {
				$json = false;
			} else {
				$json = true;
			}
		} else {
			$cek = $this->user->cekUsernameUpdate($username,$userid);			
			if ($cek > 0) {
				$json = false;
			} else {
				$json = true;
			}
		}
		
		echo json_encode($json);		
	}	
	
	function load_data() {
		header('Content-Type: application/jsonp');
        $list = $this->user->get_list_data();
		
        echo json_encode($list);
	}
	
	
}
