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

class Serviceformbak extends MY_Controller
{
	function __construct()
	{
		parent::__construct();

		if($this->logged_user = $this->session->userdata('user')){
			$this->login->checkModuleAccess(4);
			
			$this->logged_module = $this->session->userdata('module_data');
			
			$this->load->model('hr/model_form');
			$this->load->model('admin/model_company');
			$this->load->model('hr/model_service_form','model_service_form');


		}else{
			redirect(base_url());
		}

		// die(debug($this->session->userdata()));
	}
	
	function index()
	{
		$data['forms']      = $this->model_form->getForms();
		$data['page_setup'] = $this->template->pageSetup('Service Form', '', '');
		
// 		if(!empty($data['forms']))
// 		{
// 			foreach ($data['forms'] as $key => $forms) 
// 			{
// 				$calendar = 'calendar';
// 				$data['forms'][$key]['calendar'] = $calendar;
// 			}
// 		}
// debug($data['forms']);

		$this->template->view('hr/policyprocedure/service_form', $data);
	}

	public function onlineForm($formName = null)
	{
		if($formName === null){
			$this->index();
		}else{
		/* $data['stationeryDetails'] = $this->model_service_form->getServiceStationery();
			//die(debug($data['stationeryDetails']));

			$data['page_setup'] = $this->template->pageSetup('Online Form', '', $formName);
			$this->template->view('hr/policyprocedure/form/'.$formName.'_list', $data); */ 
			redirect(base_url('hr/serviceformbak/'.$formName));
		}
		
	}

	function stationery_listBAK(){
		$this->load->model('hr/model_stationery'); 
		$data['rsStationeryRequestList'] = $this->model_stationery->getStationeryRequestList();
		// echo $this->db->last_query();
		// die(debug($data['rsStationeryRequestList']));

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Stationery Request List');
		$this->template->view('hr/policyprocedure/form/stationery_listBAK', $data);
	}

	function stationery_form($ID=0)
	{
		$this->load->model('hr/model_stationery','model_stationery');
		

		$data['ID'] 				= $ID; 
		// die(debug($data['ID']));

		$data['rsMasterStationeryReq'] = $this->model_stationery->getMasterStationeryReq($ID);
		$data['stationeryCategoryDetails'] 	= $this->model_stationery->getStationeryDetails($ID);
		$data['rsStationeryItem'] = $this->model_stationery->getAllStationeryItem($ID);
		// $data['rsStationeryFormID'] = $this->model_stationery->getStationeryDetail($ID);

		
		//else $data['stationeryCategoryDetails'] 	= $this->model_stationery->getStationeryCategoryDetails();
		//die(debug($data['rsMasterStationeryReq']));

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Stationery Form');
		$this->template->view('hr/policyprocedure/form/stationery', $data);
	}

	function stationery_save()
	{
		$this->load->model('hr/model_stationery'); 
		
		$insertData = array(
			'user_id'    						=> $this->input->post('reqName_id'),
			'department_id'  					=> $this->input->post('department_id'),
			'date_taken'						=>$this->input->post('datetaken'),
			'createdTS'							=>$this->input->postt('datereq'),
			'createdBY' 						=> $this->logged_user['id']
		);
		
		$newStationery = $this->model_general->insert('sv__stationery',$insertData);

		// die(debug($newStationery));

		if($newStationery){

			$insertDataDetail = array(
						'sv_stationery_id'    				=> $newStationery,
					);

			$newStationeryDetail = $this->model_general->insert('sv__stationery__detail',$insertDataDetail);
		} else {
			return false;
		}

		//die(debug($newStationery));

		redirect(base_url('/hr/serviceformbak/stationery_list'));
	}

	function get_stationery_list()
	{
		$this->load->model('hr/model_stationery');

		$input = $this->input->post();

		// die(debug($input));

		$data['stationeryCategoryDetails']	= $this->model_stationery->getStationeryCategoryDetails($input['categoryID']);

		$data['stationeryPackage'] = $this->model_stationery->getStationeryPackageDetails($input['packageGroupID']);
		//die(debug($data['stationeryCategoryDetails']));

		
		if($data['stationeryCategoryDetails'])
		{
			print json_encode(array(
				"status" 	=> "Success",
				"msg"	 	=> "Successfully get stationery details .",
				"details" 	=> $data
			)); 
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"	 => "System failed to get stationery details."
			));
		}

	}


	function stationery_details_save()
	{
		$this->load->model('hr/model_stationery');
		$stationeryID = $this->input->post('stationery_id');

		// die(debug($stationeryID));

		$insertData = array(
			'sv_stationery_category_detail_id'=> $this->input->post('listStationeryType'),
			'quantity'					 	=> $this->input->post ('quantity'),
			'category_package_id' 			=> $this->input->post ('package_group'),
			'sv_stationery_id'				=> $this->input->post ('stationery_id'),
			'createdBY' 						=> $this->logged_user['id']
		);

		$newStationeryDetails = $this->model_general->insert('sv__stationery__detail', $insertData);

		redirect(base_url('/hr/serviceformbak/stationery_form/'.$stationeryID));
	}

	public function stationery_edit_details_save()
	{
		$this->load->model('hr/model_stationery');

		$stationeryDetailID = $this->input->post('stationeryDetailIDstat');
		$data['stationeryBookingList'] = $this->model_stationery->getStationeryBookingList($stationeryDetailID);

	//die(debug($data['stationeryBookingList'] ));

		$sets = array
		(
			'sv_stationery_category_detail_id'	=> $this->input->post('description_id'),
			'description' 						=> $this->input->post('description'),
			'quantity' 							=> $this->input->post ('quantity1'),
			'category_package_id' 				=> $this->input->post ('package_group_id'),
			'package'							=> $this->input->post('package'),
			'createdBY' 						=> $this->logged_user['id']
		);

		$where = array 
		(
			'ID' => $stationeryID,
		);

		$affected_rows = $this->model_stationery->update_stationery_details($sets,$where);
		//die(debug($affected_rows));
		
		if($affected_rows)
		{
			print json_encode(array(
				"status" 		=> "Success",
				"msg"  			=> "Successfully update item ."
			)); 
		}
		else
		{
			print json_encode(array(
				"status" 		=> "Error",
				"msg"  			=> "System failed to update item."
			));
		}
		redirect(base_url('hr/serviceformbak/stationery_form/'.$stationeryID));
	}

	public function stationery_details_edit()
	{
		$this->load->model('hr/model_stationery');

		$stationeryID = $this->input->post('stationeryID');

		// die(debug($carbookID));

		$sets = array
		(
			'sv_stationery_category_detail_id'	=> $this->input->post('listStationeryType'),
			'quantity' 							=> $this->input->post ('quantity'),
			'category_package_id' 				=> $this->input->post ('package_group'),
			'updatedBY'     					=> $this->logged_user['id'],
			'updatedTS'     					=> date('Y-m-d H:i:s'),
		);

		// die(debug($updateData));

		$where = array 
		(
			'ID' => $stationeryID,
		);

		$affected_rows = $this->model_stationery->update_stationery_details($sets,$where);
		//die(debug($affected_rows));
		if($affected_rows)
		{
			print json_encode(array(
				"status" => "Success",
				"msg"  => "Successfully update booking ."
			)); 
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"  => "System failed to update booking."
			));
		}
		redirect(base_url('hr/serviceformbak/stationery_list'));
	}

	public function deleteStationeryListForm()
	{
		$this->load->model('hr/model_stationery');

		$input    = $this->input->post();
		$stationery_id   = $this->input->post('stationery_id');

		// die(debug($stationery_id));

		$sets = array
		(
			'is_delete'   => 1,
			'updatedBY'     => $this->logged_user['id'],
			'updatedTS'   => date('Y-m-d H:i:s'),
		);

		$where = array
		(
			'ID' => $stationery_id,
		);

		$affected_rows = $this->model_stationery->update_stationery_details($sets,$where);

		if($affected_rows)
		{
			print json_encode(array(
				"status" => "Success",
				"msg"  => "Successfully deleted booking ."
			)); 
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"  => "System failed to delete booking."
			));
		}
	}

	public function deleteStationeryList()
	{
		$this->load->model('hr/model_stationery');

		$input 		= $this->input->post();
		$stationeryID 	= $this->input->post('stationeryID');

		$sets = array
		(
			'is_delete'  	=> 1,
			'updatedBY'    	=> $this->logged_user['id'],
			'updatedTS'	 	=> date('Y-m-d H:i:s'),
		);

		$where = array
		(
			'ID' => $stationeryID,
		);

		$affected_rows = $this->model_stationery->update_stationery($sets,$where);

		if($affected_rows)
		{
			print json_encode(array(
				"status" => "Success",
				"msg"	 => "Successfully deleted request item ."
			)); 
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"	 => "System failed to delete request item."
			));
		}
	}

	function stationery_list_save()
	{
		$this->load->model('hr/model_stationery'); 
		
		$insertData = array(
			'user_id'               => $this->input->post('reqName_id'),
			'department_id'         => $this->input->post('department_id'),
			'date_taken'            => $this->input->post('date_taken'),
			'createdTS '            => $this->input->post('datereq'),
			'createdBY'             => $this->logged_user['id']
		);
		
		$newStationery = $this->model_general->insert('sv__stationery',$insertData);

			//die(debug($newStationery));

		redirect(base_url('/hr/serviceformbak/stationery_list'));
	}

	function delivery_list()
	{
		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Delivery Request List');
		$this->template->view('hr/policyprocedure/form/delivery_list', $data);
	}

	function delivery_form()
	{
		//$this->load->model('hr/model_stationery','model_stationery');

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Delivery Request Form');
		$this->template->view('hr/policyprocedure/form/delivery', $data);
	}

	function maintenance_list()
	{
		$this->load->model('hr/model_maintenance'); 
		$MaintenanceList = $this->model_maintenance->getMaintenanceRequestList();

		$fc_res 	= array();
		$fc_event 	= array();
		$resource 	= array();

		if(!empty($MaintenanceList))
		{
			foreach ($MaintenanceList as $key => $detail) 
			{
				$createdTS  = date('Y-m-d',strtotime($detail['createdTS']));
							$fixedDate    = $detail['date_fixed'];
				
				$tmpRes 					= array(); $tmpEvent = array();
				$tmpRes['id']  				= $detail['ID'];
				$tmpRes['title'] 			= $detail['dept_name'].'/'.$detail['branchName'];

				$tmpEvent['id'] 			= $key;
				$tmpEvent['resourceId'] 	= $detail['user_id'];
				$tmpEvent['start'] 			= $createdTS;
				$tmpEvent['end'] 			= date('Y-m-d',strtotime($fixedDate."+1 days"));
				$tmpEvent['title'] 			= $detail['dept_name'].'/'.$detail['branchName'];
				$tmpEvent['description']	= $detail['dept_name'].' request to perform "'.$detail['complain_category'].'" maintenance.';

				if($detail['complain_category'] == 'Lights')
				{
								$tmpEvent['color']			= '#0033cc';
				}

				if($detail['complain_category'] == 'Aircond')
				{
								$tmpEvent['color']			= '#0099ff';
				}

				if($detail['complain_category'] == 'Washroom')
				{
								$tmpEvent['color']			= '#00b359';
				}

				if($detail['complain_category'] == 'Copier Machine')
				{
								$tmpEvent['color']			= '#009900';
				}

				if($detail['complain_category'] == 'Furniture')
				{
								$tmpEvent['color']			= '#ff9900';
				}

				if($detail['complain_category'] == 'Others General')
				{
								$tmpEvent['color']			= '#9900cc';
				}

				if($detail['complain_category'] == 'Cleanliness Floor')
				{
								$tmpEvent['color']			= '#ff3300';
				}

				if($detail['complain_category'] == 'Other')
				{
								$tmpEvent['color']			= '#990099';
				}

				$tmpEvent['textColor']		= '#ffffff';

				array_push($fc_res, $tmpRes);
				array_push($fc_event, $tmpEvent);
				array_push($resource, $detail);
			}
		}

		$complain = array
		(
			$datas = array
			(
				'Complaint' 	=> 'Lights',
				'color_code' 	=> '#0033cc',
				'short_name'	=> 'LT',
			),

			$datas = array
			(
				'Complaint' 	=> 'Aircond',
				'color_code' 	=> '#0099ff',
				'short_name'	=> 'A/C',
			),

			$datas = array
			(
				'Complaint' 	=> 'Washroom',
				'color_code' 	=> '#00b359',
				'short_name'	=> 'WC',
			),

			$datas = array
			(
				'Complaint' 	=> 'Copier Machine',
				'color_code' 	=> '#009900',
				'short_name'	=> 'PSC',
			),

			$datas = array
			(
				'Complaint' 	=> 'Furniture',
				'color_code' 	=> '#ff9900',
				'short_name'	=> 'FURN',
			),

			$datas = array
			(
				'Complaint' 	=> 'Cleanliness Floor',
				'color_code' 	=> '#ff3300',
				'short_name'	=> 'CLN FLR',
			),

			$datas = array
			(
				'Complaint' 	=> 'Others General',
				'color_code' 	=> '#9900cc',
				'short_name'	=> 'OTH GNRL',
			),

			$datas = array
			(
				'Complaint' 	=> 'Other',
				'color_code' 	=> '#990099',
				'short_name'	=> 'OTH',
			),

		);

		$data = array
		(
			'page_setup'		=> $this->template->pageSetup('Online Form', '', 'Maintenance Request List'),
			'rsMaintenanceList'	=> $this->model_maintenance->getMaintenanceRequestList(),
			'complain'			=> $complain,
			'countMaintenance'	=> $this->model_maintenance->getCountMaintenanceList(),
			'fc_res'			=> json_encode($fc_res),
			'fc_event'			=> json_encode($fc_event),
			'resource'			=> json_encode($resource),
		);

		$this->template->view('hr/policyprocedure/form/maintenance_list', $data);
	}

	function maintenance_calendar()
	{
		$this->load->model('hr/model_maintenance');

		$maintenanceDetail	= $this->model_maintenance->getMaintenanceRequestList();
		$fc_res 	= array();
		$fc_event 	= array();
		$resource 	= array();

		if(!empty($maintenanceDetail))
		{
			foreach ($maintenanceDetail as $key => $detail) 
			{
				$createdTS  = date('Y-m-d',strtotime($detail['createdTS']));
								$fixedDate    = $detail['date_fixed'];
				
				$tmpRes 					= array(); $tmpEvent = array();
				$tmpRes['id']  				= $detail['ID'];
				$tmpRes['title'] 			= $detail['dept_name'].'/'.$detail['branchName'];

				$tmpEvent['id'] 			= $key;
				$tmpEvent['resourceId'] 	= $detail['user_id'];
				$tmpEvent['start'] 			= $createdTS;
				$tmpEvent['end'] 			= date('Y-m-d',strtotime($fixedDate."+1 days"));
				$tmpEvent['title'] 			= $detail['dept_name'].'/'.$detail['branchName'];
				$tmpEvent['description']	= $detail['dept_name'].' request to perform "'.$detail['complain_category'].'" maintenance.';

				if($detail['complain_category'] == 'Lights')
				{
									$tmpEvent['color']			= '#0033cc';
				}

				if($detail['complain_category'] == 'Aircond')
				{
									$tmpEvent['color']			= '#0099ff';
				}

				if($detail['complain_category'] == 'Washroom')
				{
									$tmpEvent['color']			= '#00b359';
				}

				if($detail['complain_category'] == 'Copier Machine')
				{
									$tmpEvent['color']			= '#009900';
				}

				if($detail['complain_category'] == 'Furniture')
				{
									$tmpEvent['color']			= '#ff9900';
				}

				if($detail['complain_category'] == 'Others General')
				{
									$tmpEvent['color']			= '#9900cc';
				}

				if($detail['complain_category'] == 'Cleanliness Floor')
				{
									$tmpEvent['color']			= '#ff3300';
				}

				if($detail['complain_category'] == 'Other')
				{
									$tmpEvent['color']			= '#990099';
				}

				$tmpEvent['textColor']		= '#ffffff';

				array_push($fc_res, $tmpRes);
				array_push($fc_event, $tmpEvent);
				array_push($resource, $detail);
			}
		}

		$complain = array
		(
			$datas = array
			(
				'Complaint' 	=> 'Lights',
				'color_code' 	=> '#0033cc',
			),

			$datas = array
			(
				'Complaint' 	=> 'Aircond',
				'color_code' 	=> '#0099ff',
			),

			$datas = array
			(
				'Complaint' 	=> 'Washroom',
				'color_code' 	=> '#00b359',
			),

			$datas = array
			(
				'Complaint' 	=> 'Copier Machine',
				'color_code' 	=> '#009900',
			),

			$datas = array
			(
				'Complaint' 	=> 'Furniture',
				'color_code' 	=> '#ff9900',
			),

			$datas = array
			(
				'Complaint' 	=> 'Cleanliness Floor',
				'color_code' 	=> '#ff3300',
			),

			$datas = array
			(
				'Complaint' 	=> 'Others General',
				'color_code' 	=> '#9900cc',
			),

			$datas = array
			(
				'Complaint' 	=> 'Other',
				'color_code' 	=> '#990099',
			),

		);
			
		$data = array
		(
			'page_setup' 		=> $this->template->pageSetup('Online Form', '', 'Maintenance Request Calendar'),
			'calendar_detail'	=> $fc_event,
			'complain'			=> $complain,
			'fc_res'			=> json_encode($fc_res),
			'fc_event'			=> json_encode($fc_event),
			'resource'			=> json_encode($resource),
		);

		// debug($data['complain']);
		// die();
		
		$this->template->view('hr/policyprocedure/calendar/maintenance_calendar', $data);
		
	}

	function maintenance_form($ID=null){


		$this->load->model('hr/model_maintenance'); 
		if($ID >0) 	$data['maintenanceDetails'] 	= $this->model_maintenance->getMaintenanceDetails($ID);
					$data['listAdmin'] 				= $this->model_maintenance->getAdminList();


		// die(debug($data['listAdmin']));

		// $data['rsMaintenanceList'] = $this->model_maintenance->getMaintenanceRequestList();
		$data['ID'] = $ID; 
		//$this->load->model('hr/model_stationery'); 
		//$data['ID'] = $ID; 
		//$data['rsPaper'] = $this->db->get('sv__stationery__category_detail')->result_array();
		//$data['stationeryDetails'] = $this->model_stationery->getStationeryDetails($ID);
		//die(debug($data['stationeryDetails']));

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Maintenance Request Form');
		$this->template->view('hr/policyprocedure/form/maintenance', $data);
	}

	public function maintenance_save()
	{

		$input = $this->input->post();
		// die(debug($input));

		$insertData = array(
			'user_id'    		=> $this->input->post('reqName_id'),
			'department_id'  	=> $this->input->post('department_id'),
			'createdTS'  		=> $this->input->post('reqDate'),
			'office_unit'  		=> $this->input->post('officeUnit'),
			'floor'  			=> $this->input->post('maintenanceFloor'),
			'complain_category' => $this->input->post('complaintCategory'),
			'problem'  			=> $this->input->post('prob'),
			'createdBY' 		=> $this->logged_user['id']
		);

		$newMaintenance = $this->model_general->insert('sv__maintenance',$insertData);

		$this->load->library('File_upload');

		$upload_directory = 'assets/upload/hr/Form/maintenanceForm/';

		$upload_status = $this->file_upload
			->set_file_type(FILE_TYPE_UPLOAD)
			->set_directory_public()
			->set_upload_directory($upload_directory)
			->uploads('picture_upload');

			// die(debug($upload_status));

		if(is_array($upload_status)) {
		$uploadPhoto = array
		(
			'photo_title'         	=> (!empty($upload_status['file_name']))? $upload_status['file_name']:'',
			'sv_maintenance_id'     => $newMaintenance, 
			// 'filename'       		=> (!empty($upload_status['file_location']))?$upload_status['file_location']:'',
			'file_location'     	=> (!empty($upload_status['file_name']))?$upload_directory:'', 
			'is_delete'         	=> 0,
		);
		// die(debug($uploadPhoto));

		$newMaintenance2 = $this->model_general->insert('sv__maintenance__photo',$uploadPhoto);

		// die(debug($newMaintenance2));
		
		} else {
			'tidak berjaya';
		}

		redirect(base_url('hr/serviceformbak/maintenance_list'));
	}

	public function maintenance_edit()
	{
		$this->load->model('hr/model_maintenance');

		$input = $this->input->post();

		$maintID 	= $this->input->post('maintenanceID');
		$pictID 	= $this->input->post('picture_id');

		// die(debug($input));
		
		$updateData = array(
			
			'office_unit'  		=> $this->input->post('officeUnit'),
			'floor'  			=> $this->input->post('maintenanceFloor'),
			'complain_category' => $this->input->post('complaintCategory'),
			'problem'  			=> $this->input->post('prob'),
			'date_fixed'		=> $this->input->post('fixDate'),
			'approvedBy'  		=> $this->input->post('approveBy'),
			'updatedBY' 		=> $this->logged_user['id'],
			'updatedTS'	 		=> date('Y-m-d H:i:s'),
		);

		$where = array
		(
			'ID' => $maintID,
		);

		$affected_rows = $this->model_maintenance->update_maintenance($updateData,$where);


		if(!empty($_FILES['picture_upload']['name']))
				{
			$this->load->library('File_upload');

			$upload_directory = 'upload/hr/Form/maintenanceForm';

			$upload_status = $this->file_upload
				->set_file_type(FILE_TYPE_UPLOAD)
				->set_directory_public()
				->set_upload_directory('assets/'.$upload_directory)
				->uploads('picture_upload');

				// die(debug($upload_status));

			if(is_array($upload_status)) {
				$uploadPhoto = array
				(
					'photo_title'         	=> (($upload_status['file_name']))?$upload_status['file_name']:$input['picture_upload'],
					// 'filename'       		=> (!empty($upload_status['file_location']))?$upload_status['file_location']:''
					'file_location'     	=> (!empty($upload_status['file_name']))?$upload_directory:'', 
					'is_delete'         	=> 0,
				);

				// die(debug($uploadPhoto));

				$where = array
				(
					'ID' => $pictID,
				);
				// die(debug($where));

				$affected_rows = $this->model_maintenance->update_picture($uploadPhoto, $where);

			// die(debug($affected_rows));
			
			} else {
				'tidak berjaya';
			}
		}

		if($affected_rows)
			{
				print json_encode(array(
					"status" => "Success",
					"msg"	 => "Successfully update maintenance ."
				)); 
			}
			else
			{
				print json_encode(array(
					"status" => "Error",
					"msg"	 => "System failed to update maintenance."
				));
			}
		redirect(base_url('hr/serviceformbak/maintenance_list'));
	}

	public function deleteMaintenanceList()
	{
		$this->load->model('hr/model_maintenance');

		$input 		= $this->input->post();
		$maintID 	= $this->input->post('maintenanceID');

		$sets = array
		(
			'is_delete'  	=> 1,
			'updatedBY'    	=> $this->logged_user['id'],
			'updatedTS'	 	=> date('Y-m-d H:i:s'),
		);

		$where = array
		(
			'ID' => $maintID,
		);

		$affected_rows = $this->model_maintenance->update_maintenance($sets,$where);

		if($affected_rows)
			{
				print json_encode(array(
					"status" => "Success",
					"msg"	 => "Successfully deleted maintenance ."
				)); 
			}
			else
			{
				print json_encode(array(
					"status" => "Error",
					"msg"	 => "System failed to delete maintenance."
				));
			}
	}


	 function readFile()
			{
				// $this->load->model('hr/model_maintenance')
			 //    $this->load->helper('download');

			 //    $dataForm =  $this->input->post();   

			 //    $where = array(
			 //        'ID' 		=> $dataForm['uploadUniqueFile'],
			 //        'is_delete' => 0
			 //    );

			 //    $dataFile = $this->model_maintenance->get_maintenance_file($where);
			 //    $path = 'assets/'.$dataFile[0]['file_location'].'/'.$dataFile[0]['file_name'];
			 //    $contentType = $dataFile[0]['file_type'];
			 //    force_download($path,null);
			}

	

	function meeting_list()
	{
		$this->load->model('hr/model_booking_room','room');

		$bookingDetail	= $this->room->getBookingRoom(null);

		$fc_res 	= array();
		$fc_event 	= array();
		$resource 	= array();

		if(!empty($bookingDetail))
		{
			foreach ($bookingDetail as $key => $booking) 
			{
				$createdTS  = date('Y-m-d',strtotime($booking['createdTS']));
								$startDate  = $booking['start_date'];
								$endDate    = $booking['end_date'];
				
				$tmpRes 					= array(); $tmpEvent = array();
				$tmpRes['id']  				= $booking['ID'];
				$tmpRes['title'] 			= $booking['dept_name'];

				$tmpEvent['id'] 			= $key;
				$tmpEvent['resourceId'] 	= $booking['staff_id'];
				$tmpEvent['start'] 			= $startDate;
				$tmpEvent['end'] 			= date('Y-m-d',strtotime($endDate."+1 days"));
				$tmpEvent['title'] 			= $booking['dept_name'].'/'.$booking['branchName'];
				
				if(!empty($booking['other_room'])):
									$tmpEvent['description']	= $booking['dept_name'].' request to book "'.$booking['other_room'].'"';
										// $tmpEvent['color']			= '#660066';
								else:
										$tmpEvent['description']	= $booking['dept_name'].' request to book "'.$booking['room'].'"';
								endif;
									$tmpEvent['color']			= '#'.$booking['color_code'];

				$tmpEvent['textColor']		= '#ffffff';

				array_push($fc_res, $tmpRes);
				array_push($fc_event, $tmpEvent);
				array_push($resource, $booking);
			}
		// debug($booking);
		}
		
		$data = array
		(
			'page_setup' 	=> $this->template->pageSetup('Online Form', '', 'Meeting & Training Booking List'),
			'booking_room'	=> $this->room->getBookingRoom(null),
			'room_type'		=> $this->room->getBookingRoomType2(),
			'departments'	=> $this->model_company->getDepartments(),
			'fc_res'		=> json_encode($fc_res),
			'fc_event'		=> json_encode($fc_event),
			'resource'		=> json_encode($resource),
		);

		if(!empty($data['room_type']))
		{
			foreach ($data['room_type'] as $key => $room_type) 
			{
				if($room_type['ID'] == 1) //melur
				{
					$short_name = "MLR";

					$data['room_type'][$key]['short_name'] = $short_name;
				}

				if($room_type['ID'] == 2) //teratai
				{
					$short_name = "TRT";

					$data['room_type'][$key]['short_name'] = $short_name;
				}

				if($room_type['ID'] == 3) //mawar
				{
					$short_name = "MWR";

					$data['room_type'][$key]['short_name'] = $short_name;
				}

				if($room_type['ID'] == 4) //bunga raya
				{
					$short_name = "BRY";

					$data['room_type'][$key]['short_name'] = $short_name;
				}

				if($room_type['ID'] == 5) //melati
				{
					$short_name = "MLT";

					$data['room_type'][$key]['short_name'] = $short_name;
				}

				if($room_type['ID'] == 6) //mph
				{
					$short_name = "MPH";

					$data['room_type'][$key]['short_name'] = $short_name;
				}

				if($room_type['ID'] == 7) //other
				{
					$short_name = "OTH";

					$data['room_type'][$key]['short_name'] = $short_name;
				}
			}
		}

		// debug($data['room_type']);

		$this->template->view('hr/policyprocedure/form/meeting_list', $data);
	}

	function meeting_calendar()
	{
		$this->load->model('hr/model_booking_room','room');

		$bookingDetail	= $this->room->getBookingRoom(null);

		$fc_res 	= array();
		$fc_event 	= array();
		$resource 	= array();

		if(!empty($bookingDetail))
		{
			foreach ($bookingDetail as $key => $booking) 
			{
				$createdTS  = date('Y-m-d',strtotime($booking['createdTS']));
								$startDate  = $booking['start_date'];
								$endDate    = $booking['end_date'];
				
				$tmpRes 					= array(); $tmpEvent = array();
				$tmpRes['id']  				= $booking['ID'];
				$tmpRes['title'] 			= $booking['dept_name'].'/'.$booking['branchName'];

				$tmpEvent['id'] 			= $key;
				$tmpEvent['resourceId'] 	= $booking['staff_id'];
				$tmpEvent['start'] 			= $startDate;
				$tmpEvent['end'] 			= date('Y-m-d',strtotime($endDate."+1 days"));
				$tmpEvent['title'] 			= $booking['dept_name'];
				
				if(!empty($booking['other_room'])):
									$tmpEvent['description']	= $booking['dept_name'].' request to book "'.$booking['other_room'].'"';
										// $tmpEvent['color']			= '#660066';
								else:
										$tmpEvent['description']	= $booking['dept_name'].' request to book "'.$booking['room'].'"';
								endif;
									$tmpEvent['color']			= '#'.$booking['color_code'];

				$tmpEvent['textColor']		= '#ffffff';

				array_push($fc_res, $tmpRes);
				array_push($fc_event, $tmpEvent);
				array_push($resource, $booking);
			}
		}
			// debug($fc_event);die();
		$data = array
		(
			'page_setup' 	=> $this->template->pageSetup('Online Form', '', 'Meeting & Training Booking Calendar'),
			'room_type'		=> $this->room->getBookingRoomType(),
			'fc_res'		=> json_encode($fc_res),
			'fc_event'		=> json_encode($fc_event),
			'resource'		=> json_encode($resource),
		);
		
		// debug($data['booking_room']);

		$this->template->view('hr/policyprocedure/calendar/meeting_calendar', $data);
	}

	function meeting_form($booking_id = null)
	{
		$this->load->model('hr/model_booking_room','room');
		// $data['ID'] = $booking_id;

		// die(debug($data['ID']));
		//$this->load->model('hr/model_stationery','model_stationery');

		$data = array
		(
			'ID'			=> $booking_id,
			'page_setup' 	=> $this->template->pageSetup('Online Form', '', 'Meeting & Training Booking Form'),
			'room_type'		=> $this->room->getBookingRoomType(),
			'booking_room'	=> $this->room->getBookingRoom($booking_id),
		);

		// die(debug($data['booking_room']));

		if(!empty($data['room_type']))
		{
			foreach ($data['room_type'] as $key => $room_type) 
			{
				if($room_type['room_id'] == 1) //melur
				{
					$img_link = "https://lh3.googleusercontent.com/pjEAP_vngtS990SptwjW7uyCq4cpkOuLgh0-9xDY1rKc5yCzj8BrAgrIX28WVHmx9VANNhDw0KCQyY_9GC_Hm3slmJFI6ymx3V15VBV0dIYLCBZ1Od5t2mWDSBtH=w260";

					$data['room_type'][$key]['img_link'] = $img_link;
				}

				if($room_type['room_id'] == 2) //teratai
				{
					$img_link = "https://lh5.googleusercontent.com/3Hfgwgi4D8ETS0s02gSZxb1OcayGE5PLmVrzJlQHfPDBKvMDBQXaSWKzZFeWFpqCEb6e87UkYs489mT5YI9jS73nf6DCBdtBEYpWbjVOEyx7ESbsN-V72mPGQZSi=w260";

					$data['room_type'][$key]['img_link'] = $img_link;
				}

				if($room_type['room_id'] == 3) //mawar
				{
					$img_link = "https://lh6.googleusercontent.com/zc_cOPyGrb16FATPTa4E-_K8nVBl-Ttk69Sq5qsQSvxkxN-FC4gQkdJJplPjlQVwdOgYrqynWCMclNWec_WvSK6MigErbkJj9Sseq5r4Ai9pKiZK9YWrfyDeaIx0=w260";

					$data['room_type'][$key]['img_link'] = $img_link;
				}

				if($room_type['room_id'] == 4) //bunga raya
				{
					$img_link = "https://lh3.googleusercontent.com/jCVeZxDtgNbEdgQE_r3GtefDCFJ9DKtXKcWVEDig4oLLA6-twmQjBvEAIx-itqF52Xa8j0Qem7cj0elYu2yFWxDKbc3ny8T--cKOMNhw88anrVFLrlR0rXFtCjiQ=w260";

					$data['room_type'][$key]['img_link'] = $img_link;
				}

				if($room_type['room_id'] == 5) //melati
				{
					$img_link = "https://lh3.googleusercontent.com/-WbF3ilqzUZQ5msWiB1PI0mz27uole-TixCXx7BANcgJLzeRIgWpCn70GoHzrWMmupAMlQcqSywSywELBzlGl6hR60OaeGRkIsrldhGzCx3PeadA4QzRcNF6SbKC=w260";

					$data['room_type'][$key]['img_link'] = $img_link;
				}

				if($room_type['room_id'] == 6) //mph
				{
					$img_link = "https://lh5.googleusercontent.com/EDnELEXIbiamoPIOWi9x36QOo5g2vBa0ZzrNXIC0IdtuDflT4m7j8lacanvMKGDjz_Dehg5-XUpHle2bEmc28Dm1A3kLyePj5wBPr1EB363R5Y48bNMdvFmTtglv=w260";

					$data['room_type'][$key]['img_link'] = $img_link;
				}

				if($room_type['room_id'] == 7) //other
				{
					$img_link = "";

					$data['room_type'][$key]['img_link'] = $img_link;
				}
			}
		}

		$this->template->view('hr/policyprocedure/form/meeting', $data);
	}

	function meeting_form_save()
	{
		$input = $this->input->post();
		// debug($input);
		// die();
		$insert = array
		(
			// 'room_id'			=> $input['']
			'requester_id'		=> $input['requester_id'],
			'department_id'		=> $input['department_id'],
			'package'			=> $input['package_id'],
			'event_title'		=> $input['event_title'],
			'client'			=> $input['client_name'],
			'start_date'		=> $input['start_date'],
			'start_time'		=> $input['start_time'],
			'end_date'			=> $input['end_date'],
			'end_time'			=> $input['end_time'],
			'pax'				=> $input['no_pax'],
			'createdTS'			=> $input['reqDate'],
			'createdBY'			=> $input['requester_id'],
		);
		if(!empty($input['other_room_id']))
		{
			$insert['room_id'] 		= 7;
			$insert['other_room'] 	= $input['other_room_id'];
		} else {
			$insert['room_id'] 		= $input['room_id'];
		}

		// if(!empty($input['room_id'])) $insert['room_id'] = $input['room_id'];
		if(!empty($input['table_setup'])) $insert['table_setup'] = $input['table_setup'];
		if(!empty($input['equipment_id'])) $insert['equipment'] = $input['equipment_id'];
		if(!empty($input['breakfast'])) $insert['breakfast'] = $input['breakfast'];
		if(!empty($input['lunch'])) $insert['lunch'] = $input['lunch'];
		if(!empty($input['tea'])) $insert['hi_tea'] = $input['tea'];
		if(!empty($input['other_menu'])) $insert['lunch'] = $input['other_menu'];
		if(!empty($input['special_request'])) $insert['special_request'] = $input['special_request'];
		if(!empty($input['vip'])) $insert['vip'] = $input['vip'];
// debug($insert);
// die();
		$addroombooking = $this->model_general->insert('sv__booking__room', $insert);
		
		if($addroombooking)
		{
			print json_encode(array(
				"status" => "Success",
				"msg"	 => "Successfully booking room."
			)); 
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"	 => "Failed to booking room. Please try again."
			));
		}
		redirect(base_url('hr/serviceformbak/meeting_list'));
	}

	function meeting_form_edit()
	{
		$input = $this->input->post();
		$this->load->model('hr/model_booking_room','room');

		$meetingID = $input['meetingID'];
		// die(debug($meetingID));
		
		$update = array
		(
			'requester_id'		=> $input['requester_id'],
			'department_id'		=> $input['department_id'],
			'package'			=> $input['package_id'],
			'event_title'		=> $input['event_title'],
			'client'			=> $input['client_name'],
			'start_date'		=> $input['start_date'],
			'start_time'		=> $input['start_time'],
			'end_date'			=> $input['end_date'],
			'end_time'			=> $input['end_time'],
			'pax'				=> $input['no_pax'],
			'updatedBY' 		=> $this->logged_user['id'],
			'updatedTS'	 		=> date('Y-m-d H:i:s'),
		);

		if(!empty($input['room_id'])) $update['room_id'] = $input['room_id'];
		if(!empty($input['table_setup'])) $update['table_setup'] = $input['table_setup'];
		if(!empty($input['equipment_id'])) $update['equipment'] = $input['equipment_id'];
		if(!empty($input['other_room_id'])) $update['other_room'] = $input['other_room_id'];
		if(!empty($input['breakfast'])) $update['breakfast'] = $input['breakfast'];
		if(!empty($input['lunch'])) $update['lunch'] = $input['lunch'];
		if(!empty($input['tea'])) $update['hi_tea'] = $input['tea'];
		if(!empty($input['other_menu'])) $update['other_menu'] = $input['other_menu'];
		if(!empty($input['special_request'])) $update['special_request'] = $input['special_request'];
		if(!empty($input['vip'])) $update['vip'] = $input['vip'];

		$where = array
		(
			'ID' => $meetingID,
		);

		$addroombooking = $this->room->updateBookingRoom($update, $where);
		
		if($addroombooking)
		{
			print json_encode(array(
				"status" => "Success",
				"msg"	 => "Successfully updated booking room."
			)); 
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"	 => "Failed to update booking room. Please try again."
			));
		}
		redirect(base_url('hr/serviceformbak/meeting_list'));
	}

	public function deleteMeetingList()
	{
		$this->load->model('hr/model_booking_room','room');

		$input 		= $this->input->post();
		$meetingID 	= $this->input->post('meetingsID');;

		$sets = array
		(
			'is_delete'  	=> 1,
			'updatedBY'    	=> $this->logged_user['id'],
			'updatedTS'	 	=> date('Y-m-d H:i:s'),
		);

		$where = array
		(
			'ID' => $meetingID,
		);

		// die(debug($where));

		$affected_rows = $this->room->updateBookingRoom($sets,$where);

		// die(debug($affected_rows));

		if($affected_rows)
			{
				print json_encode(array(
					"status" => "Success",
					"msg"	 => "Successfully deleted meeting booking."
				)); 
			}
			else
			{
				print json_encode(array(
					"status" => "Error",
					"msg"	 => "System failed to delete meeting booking."
				));
			}
	}

	function car_booking_list()
	{

		$this->load->model('hr/model_car_booking'); 
		//$data['rsCarBookingList'] = $this->model_car_booking->getCarRequestList();
		$data['rsCarRequestList'] = $this->model_car_booking->getCarRequestList();
		//die(debug($data['rsCarRequestList']));

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Car Booking List');
		$this->template->view('hr/policyprocedure/form/car_booking_list', $data);
	}

	 function car_booking_form($ID=null)
	 {

		$this->load->model('hr/model_car_booking'); 
		if($ID >0)$data['rsCarGetList'] = $this->model_car_booking->getCarListForm($ID);
		// die(debug($data));

		$this->load->model('hr/model_car_booking');
		$data['ID']         = $ID;

		//$data['carBookingDetails']  = $this->model_car_booking->getCarBookingDetails();


		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Car Booking Form');
		$this->template->view('hr/policyprocedure/form/car_booking', $data);
	 }

	public function carbooking_save()
	{
		$insertData = array(
			'car_id' 		=> $this->input->post('car_id'),
			'user_id' 		=> $this->input->post('reqName_id'),
			'location' 		=> $this->input->post('location'),
			'purpose' 		=> $this->input->post('purpose'),
			'department_id' => $this->input->post('department_id'),
			'start_date' 	=> $this->input->post('start_date'),
			'start_time' 	=> $this->input->post('start_time'),
			'end_date' 		=> $this->input->post('end_date'),
			'end_time' 		=> $this->input->post('end_time'),
			'createdBY' 	=> $this->logged_user['id']
		);

		$newCarbooking = $this->model_general->insert('sv__car_booking', $insertData);

		//die(debug($newCarbooking));

		redirect(base_url('hr/serviceformbak/car_booking_list'));
	}

	public function carbooking_edit()
	{
		$this->load->model('hr/model_car_booking');

		$carbookID = $this->input->post('carBookingID');

		$input = $this->input->post();
		// die(debug($input));

		// die(debug($carbookID));

		$sets = array
		(
			'car_id'    	=> $this->input->post('car_id'),
			'user_id'     	=> $this->input->post('reqName_id'),
			'location'    	=> $this->input->post('location'),
			'purpose'     	=> $this->input->post('purpose'),
			'department_id' => $this->input->post('department_id'),
			'start_date'  	=> $this->input->post('start_date'),
			'start_time'  	=> $this->input->post('start_time'),
			'end_date'    	=> $this->input->post('end_date'),
			'end_time'    	=> $this->input->post('end_time'),
			'updatedBY'     => $this->logged_user['id'],
			'updatedTS'     => date('Y-m-d H:i:s'),
		);

		// die(debug($updateData));

		$where = array 
		(
			'ID' => $carbookID,
		);

		$affected_rows = $this->model_car_booking->update_carBooking($sets,$where);
		//die(debug($affected_rows));
		if($affected_rows)
		{
			print json_encode(array(
				"status" => "Success",
				"msg"  => "Successfully update booking ."
			)); 
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"  => "System failed to update booking."
			));
		}
		redirect(base_url('hr/serviceformbak/car_booking_list'));
	}

	public function deleteCarBookingList()
	{
		$this->load->model('hr/model_car_booking');

		$input    = $this->input->post();
		$carBookID  = $this->input->post('carBookingID');

		$sets = array
		(
			'is_delete'   => 1,
			'updatedBY'     => $this->logged_user['id'],
			'updatedTS'   => date('Y-m-d H:i:s'),
		);

		$where = array
		(
			'ID' => $carBookID,
		);

		$affected_rows = $this->model_car_booking->update_carBooking($sets,$where);

		if($affected_rows)
		{
			print json_encode(array(
				"status" => "Success",
				"msg"  => "Successfully deleted booking ."
			)); 
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"  => "System failed to delete booking."
			));
		}
	}


	function meeting_crm_list()
	{
		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Meeting Request List');
		$this->template->view('hr/policyprocedure/form/meeting_crm_list', $data);
	}

	function meeting_crm_form()
	{
		//$this->load->model('hr/model_stationery','model_stationery');

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Meeting Request Form');
		$this->template->view('hr/policyprocedure/form/meeting_crm', $data);
	}

	function hotel_list()
	{
		$this->load->model('hr/model_hotel');
		$data['rsHotelList'] = $this->model_hotel->getHotelBookingList();

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Hotel List');
		$this->template->view('hr/policyprocedure/form/hotel_list', $data);
	}

	function hotel_form($ID = null)
	{
		$this->load->model('hr/model_hotel');
		if($ID>0)$data['rsHotelList'] = $this->model_hotel->getHotelListForm($ID);

		$this->load->model('hr/model_hotel');
		$data['ID'] = $ID;

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Hotel Form');
		$this->template->view('hr/policyprocedure/form/hotel', $data);
	}

	public function hotel_save()
	{

		$insertData = array(
			'user_id'    		=> $this->input->post('name_id'),
			'department_id' 	=> $this->input->post('department_id'),
			'hotel_name'     	=> $this->input->post('hotelname'),
			'location'    		=> $this->input->post('location'),
			'room_type'     	=> $this->input->post('typeroom'),
			'phoneNo'     		=> $this->input->post('contactno'),
			'ic'     			=> $this->input->post('gic'),
			'room_no'     		=> $this->input->post('room_no'),
			'no_pax'  			=> $this->input->post('nopax'),
			'checkin_date '  	=> $this->input->post('checkin_date'),
			'checkin_time'    	=> $this->input->post('checkin_time'),
			'checkout_date'    	=> $this->input->post('checkout_date'),
			'checkout_time'    	=> $this->input->post('checkout_time'),
			'createdBY'     	=> $this->logged_user['id']
		);

		
		$newHotelBooking = $this->model_general->insert('sv__hotel', $insertData);

		//die(debug($newHotelBooking));

		//redirect(base_url('hr/serviceformbak/hotel_list'));
	}

	public function hotel_edit()
	{
		$this->load->model('hr/model_hotel');

		$hotelID = $this->input->post('hotelID');

		// die(debug($hotelID));

		$sets = array
		(
			'user_id'       => $this->input->post('name_id'),
			'department_id' => $this->input->post('department_id'),
			'hotel_name'    => $this->input->post('hotelname'),
			'location'      => $this->input->post('location'),
			'room_type'     => $this->input->post('typeroom'),
			'phoneNo'     	=> $this->input->post('contactno'),
			'ic'     		=> $this->input->post('gic'),
			'room_no'       => $this->input->post('room_no'),
			'no_pax'        => $this->input->post('nopax'),
			'checkin_date ' => $this->input->post('checkin_date'),
			'checkin_time'  => $this->input->post('checkin_time'),
			'checkout_date' => $this->input->post('checkout_date'),
			'checkout_time' => $this->input->post('checkout_time'),
			'updatedBY'     => $this->logged_user['id'],
			'updatedTS'     => date('Y-m-d H:i:s'),
		);

		$where = array 
		(
			'ID' => $hotelID,
		);

		$affected_rows = $this->model_hotel->update_hotel($sets,$where);
		//die(debug($affected_rows));
		if($affected_rows)
		{
			print json_encode(array(
				"status" => "Success",
				"msg"  => "Successfully update booking ."
			)); 
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"  => "System failed to update booking."
			));
		}
		redirect(base_url('hr/serviceformbak/hotel_list'));
	}

	public function deleteHotelList()
	{
		$this->load->model('hr/model_hotel');

		$input    = $this->input->post();
		$hotelID  = $this->input->post('hotelID');

		$sets = array
		(
			'is_delete'   => 1,
			'updatedBY'   => $this->logged_user['id'],
			'updatedTS'   => date('Y-m-d H:i:s'),
		);

		$where = array
		(
			'ID' => $hotelID,
		);

		$affected_rows = $this->model_hotel->update_hotel($sets,$where);

		if($affected_rows)
		{
			print json_encode(array(
				"status" => "Success",
				"msg"  => "Successfully deleted booking ."
			)); 
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"  => "System failed to delete booking."
			));
		}
	}

	function flight_list()
	{
		$this->load->model('hr/model_flight_booking');

		$data = array
		(
			'listFlightBooking' => $this->model_flight_booking->getListFlightBooking(),
		);

		// die(debug($data['listFlightBooking']));

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Flight List');
		$this->template->view('hr/policyprocedure/form/flight_list', $data);
	}

	function flight_form($ID = null)
	{

		$this->load->model('hr/model_flight_booking');
		$data['ID']         = $ID;

		$data = array
		(
			'listName' 					=> $this->model_flight_booking->getlistName($_SESSION['user']['department_id']),
			'ID'	   					=> $ID,
			'getListFlightBookingByID' 	=> $this->model_flight_booking->getListFlightBookingByID($ID),
		);

		// die(debug($data['getListFlightBookingByID']));

		// ['listName'] = $this->model_flight_booking->getlistName($_SESSION['user']['department_id']);

		// die(debug($data['listName']));

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Flight Form');
		$this->template->view('hr/policyprocedure/form/flight', $data);
	}

	function save_flight_form()
	{
		$input = $this->input->post();

		//die(debug($input));
		$flight = array
		(
			'baggage' 			=> (!empty($input['other_baggege_id']))?$input['other_baggege_id']:$input['baggage'],
			'requester_id'		=> $input['name_id'],
			'no_of_passengers'	=> $input['passenger'],
			'guest_name'		=> $input['gname'],
			'phone_no'			=> $input['gphone'],
			'passport'			=> $input['passport'],
			'ic_no'				=> $input['gic'],
			'origin'			=> $input['origin'],
			'destination'		=> $input['destination'],
			'departure_date'	=> $input['depart_date'],
			'departure_time'	=> $input['depart_time'],
			'return_date'		=> $input['return_date'],
			'return_time'		=> $input['return_time'],
			'flight_name'		=> (!empty($input['other_flight_name']))?$input['other_flight_name']:$input['flightName'],
			'flight_class'		=> $input['flightClass'],
			'createdBY'			=> $this->logged_user['id'],
		);

		$newFlightBooking = $this->model_general->insert('sv__flight__booking', $flight);

		if($newFlightBooking)
		{
			print json_encode(array(
				"status" => "Success",
				"msg"	 => "Successfully request for booking flight."
			)); 
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"	 => "Failed to request booking flight. Please try again."
			));
		}
		redirect(base_url('hr/serviceformbak/flight_list'));
	}

	public function edit_flight_form()
	{
		$this->load->model('hr/model_flight_booking');

		$input = $this->input->post();

		$flightID 	= $this->input->post('flightID');

		// die(debug($flightID));
		
		$updateData = array(
			
			'baggage' 			=> (!empty($input['other_baggage_id']))?$input['other_baggage_id']:$input['baggage'],
			'requester_id'		=> $input['name_id'],
			'no_of_passengers'	=> $input['passenger'],
			'guest_name'		=> $input['gname'],
			'phone_no'			=> $input['gphone'],
			'passport'			=> $input['passport'],
			'ic_no'				=> $input['gic'],
			'origin'			=> $input['origin'],
			'destination'		=> $input['destination'],
			'departure_date'	=> $input['depart_date'],
			'departure_time'	=> $input['depart_time'],
			'return_date'		=> $input['return_date'],
			'return_time'		=> $input['return_time'],
			'flight_name'		=> (!empty($input['other_flight_name']))?$input['other_flight_name']:$input['flightName'],
			'flight_class'		=> $input['flightClass'],
			'updatedBY' 		=> $this->logged_user['id'],
			'updatedTS'	 		=> date('Y-m-d H:i:s'),
		);

		$where = array
		(
			'ID' => $flightID,
		);

		// die(debug($where));

		$flightBooking = $this->model_flight_booking->update_flight($updateData,$where);

		// die(debug($flightBooking));

		if($flightBooking)
		{
			print json_encode(array(
				"status" => "Success",
				"msg"	 => "Successfully request for booking flight."
			)); 
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"	 => "Failed to request booking flight. Please try again."
			));
		}
		redirect(base_url('hr/serviceformbak/flight_list'));
	}


	public function deleteFlightList()
	{
		$this->load->model('hr/model_flight_booking');

		$input 		= $this->input->post();
		$flightID 	= $this->input->post('flightID');

		// debug($flightID);

		$sets = array
		(
			'is_delete'  	=> 1,
			'updatedBY'    	=> $this->logged_user['id'],
			'updatedTS'	 	=> date('Y-m-d H:i:s'),
		);

		$where = array
		(
			'ID' => $flightID,
		);

		$affected_rows = $this->model_flight_booking->update_flight($sets,$where);

		if($affected_rows)
			{
				print json_encode(array(
					"status" => "Success",
					"msg"	 => "Successfully deleted flight booking ."
				)); 
			}
			else
			{
				print json_encode(array(
					"status" => "Error",
					"msg"	 => "System failed to delete flight booking."
				));
			}
	}

	// function employee_list()
	// {
	// 	$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Car Booking List');
	// 	$this->template->view('hr/policyprocedure/form/employee_list', $data);
	// }

	function employee_form()
	{
		//$this->load->model('hr/model_stationery','model_stationery');

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Employee Particulars Update for year 2020');
		$this->template->view('hr/policyprocedure/form/employee', $data);
	}

	function request_list()
	{
		$this->load->model('hr/model_training_request');

		$data = array
		(
			'listTrainingRequest' => $this->model_training_request->getListTrainingRequest(),
		);

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Training Request List');
		$this->template->view('hr/policyprocedure/form/request_list', $data);
	}

	function request_form($ID=null)
	{
		$data['ID']         = $ID;
		$this->load->model('admin/model_company');
		$this->load->model('hr/model_training_request');

		$data['rsBusiness_entities'] = $this->model_company->getBusinesses(1);
		if($ID >0) 	$data['trainingRequestDetails'] 	= $this->model_training_request->getTrainingRequestDetails($ID);
		//die(debug($ID));

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Training Request Form');
		$this->template->view('hr/policyprocedure/form/request', $data);
	}

	function training_request_save()
	{
		$input = $this->input->post();

		//die(debug($input));
		$training_request = array
		(
			'user_id'				=> $input['reqName_id'],
			'department_id'			=> $input['department_id'],
			'business_entity_id'	=> $input['business_entity'],
			'topic'					=> $input['topic'],
			'venue'					=> $input['venue'],
			'organizer'				=> $input['organizer'],
			'time'					=> $input['time'],
			'cost'					=> $input['cost'],
			'participant'			=> $input['participant'],
			'proposed_date'			=> $input['proposed_date'],
			'createdBY'				=> $this->logged_user['id']
		);

		$newTrainingRequest = $this->model_general->insert('sv__training_request', $training_request);

		$this->load->library('File_upload');

		$upload_directory = 'assets/upload/hr/Form/trainingRequest/';

		$upload_status = $this->file_upload
			->set_file_type(FILE_TYPE_UPLOAD)
			->set_directory_public()
			->set_upload_directory($upload_directory)
			->uploads('picture_upload');

			// die(debug($upload_status));

		if(is_array($upload_status)) {
			$uploadFile = array
			(
				'photo_title'			=> (!empty($upload_status['file_name']))? $upload_status['file_name']:'',
				'sv_trainingRequest_id' => $newTrainingRequest,
				'file_location'			=> (!empty($upload_status['file_name']))?$upload_directory:'', 
				'is_delete'				=> 0,
			);
			// die(debug($uploadPhoto));

			$newTrainingRequestUpload = $this->model_general->insert('sv__training_request__upload',$uploadFile);

			// die(debug($newMaintenance2));
		} else {
			'tidak berjaya';
		}

		redirect(base_url('hr/serviceformbak/request_list'));
	}

	public function training_request_edit()
	{
		$this->load->model('hr/model_training_request');

		$input = $this->input->post();

		$trainingRequestID 	= $this->input->post('trainingRequestID');
		$uploadID 			= $this->input->post('upload_id');

		// die(debug($input));
		
		$updateData = array(
			'user_id'				=> $input['reqName_id'],
			'department_id'			=> $input['department_id'],
			'business_entity_id'	=> $input['business_entity'],
			'topic'					=> $input['topic'],
			'venue'					=> $input['venue'],
			'organizer'				=> $input['organizer'],
			'time'					=> $input['time'],
			'cost'					=> $input['cost'],
			'participant'			=> $input['participant'],
			'proposed_date'			=> $input['proposed_date'],
			'updatedBY' 			=> $this->logged_user['id'],
			'updatedTS'	 			=> date('Y-m-d H:i:s'),
		);

		$where = array
		(
			'ID' => $trainingRequestID,
		);

		$affected_rows = $this->model_training_request->update_training_request($updateData,$where);


		if(!empty($_FILES['picture_upload_update']['name']))
				{
			$this->load->library('File_upload');

			$upload_directory = 'upload/hr/Form/trainingRequest';

			$upload_status = $this->file_upload
				->set_file_type(FILE_TYPE_UPLOAD)
				->set_directory_public()
				->set_upload_directory('assets/'.$upload_directory)
				->uploads('picture_upload_update');

				// die(debug($upload_status));

			if(is_array($upload_status)) 
			{
				$uploadFile = array
				(
					'photo_title'         	=> (($upload_status['file_name']))?$upload_status['file_name']:$input['picture_upload_update'],
					// 'filename'       		=> (!empty($upload_status['file_location']))?$upload_status['file_location']:''
					'file_location'     	=> (!empty($upload_status['file_name']))?$upload_directory:'', 
					'is_delete'         	=> 0,
					'updatedBY' 			=> $this->logged_user['id'],
					'updatedTS'	 			=> date('Y-m-d H:i:s'),
				); 

				// die(debug($uploadPhoto));

				$where = array
				(
					'ID' => $uploadID,
				);
				// die(debug($where));

				$affected_rows = $this->model_training_request->update_training_request_upload($uploadFile, $where);

			// die(debug($affected_rows));
			
			} else {
				'tidak berjaya';
			}
		}

		if($affected_rows)
			{
				print json_encode(array(
					"status" => "Success",
					"msg"	 => "Successfully update maintenance ."
				)); 
			}
			else
			{
				print json_encode(array(
					"status" => "Error",
					"msg"	 => "System failed to update maintenance."
				));
			}
		redirect(base_url('hr/serviceformbak/request_list'));
	}

	public function deleteTrainingRequestList()
	{
		$this->load->model('hr/model_training_request');

		$input 				= $this->input->post();
		$trainingRequestID 	= $this->input->post('trainingRequestID');

		$sets = array
		(
			'is_delete'  	=> 1,
			'updatedBY'    	=> $this->logged_user['id'],
			'updatedTS'	 	=> date('Y-m-d H:i:s'),
		);

		$where = array
		(
			'ID' => $trainingRequestID,
		);

		$affected_rows = $this->model_training_request->update_training_request($sets,$where);

		if($affected_rows)
		{
			print json_encode(array(
				"status" => "Success",
				"msg"	 => "Successfully deleted training request."
			)); 
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"	 => "System failed to delete training request."
			));
		}
	}


	function evaluation_list()
	{
		$this->load->model('hr/model_training_evaluation');

		$data = array
		(
			'listTrainingEvaluation' => $this->model_training_evaluation->getListTrainingEvaluation()
		);
		//die(debug($data['listTrainingEvaluation']));

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Training Evaluation List');
		$this->template->view('hr/policyprocedure/form/evaluation_list', $data);
	}

	function evaluation_form($ID=0)
	{
		$this->load->model('hr/model_training_evaluation');

		
		//die(debug($data['ID']));

		$data = array
		(
			'rsEvaluationQuestion' 		=> $this->model_training_evaluation->getEvaluationQuestionDetails($ID),
			'rsDepartments' 			=> $this->model_training_evaluation->name_with_branch(),
			
		);
		if($ID >0) 	$data['rsEvaluationDetails'] = $this->model_training_evaluation->getAllEvaluationDetails($ID);
					$data['rsUserEvaluationDetails'] = $this->model_training_evaluation->getUserEvaluationDetails($ID);
					$data['rsEvaluationAnswer'] = $this->model_training_evaluation->getEvaluationAnswerDetails($ID);
		//die(debug($data['detailsTrainingEvaluation']));
		$data['ID'] 		= $ID;

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Training Evaluation Form');
		$this->template->view('hr/policyprocedure/form/evaluation', $data);
	}

	function evaluation_form_save()
	{
		$input = $this->input->post();


		// $evaluationID = $this->input->post('evaluation_id');

		//debug($input);

		$userData = array
		(
			'user_id'				=> $input['user_id'],
			'department_id'			=> $input['department_id'],
			'createdBY'				=> $this->logged_user['id']
		);

		$newUser = $this->model_general->insert('sv__evaluation', $userData);

		if(!empty($newUser)){
			$evaluationData = array
			(
				'training_title'		=> $input['title'],
				'packages'				=> $input['packages'],
				'venue'					=> $input['venue'],
				'training_date'			=> $input['date_training'],
				'time'					=> $input['time'],
				'respective_dept'		=> $input['respective_dept'],
				'cpd_hours'				=> $input['hour'],
				'trainer_name'			=> $input['trainer_name'],
				'createdBY'				=> $this->logged_user['id']
			);
		}

		$newTrainingEvaluation = $this->model_general->insert('sv__evaluation_training', $evaluationData);

		//die($newTrainingEvaluation);
		if($newUser && $newTrainingEvaluation)
		{
			foreach($input['question_id'] as $question_id){
				echo '<br>no soalan',$question_id; 
				//echo '<br>jawapan'.(!empty($input['answerRadio'][$question_id]))?$input['answerRadio'][$question_id] :null;
				echo '<br>type'.$input['question_type'][$question_id];
	
				$answerData = array 
				(
					'sv_evaluation_id'			=> $newUser,
					'sv_evaluation_training_id'	=> $newTrainingEvaluation,
					'sv_evaluation_question_id'	=> $question_id,
					// 'answer_radio'				=> (!empty($input['answerRadio'][$question_id]))?$input['answerRadio'][$question_id] :'',
					// 'answer_text'				=> (!empty($input['answerText'][$question_id]))?$input['answerText'][$question_id] :'',
					'createdBY'					=> $this->logged_user['id']
				);
	
				if($input['question_type'][$question_id] == 0){
					$answerData['answer_radio'] = (!empty($input['answerRadio'][$question_id]))?$input['answerRadio'][$question_id] :'';
				}
				elseif($input['question_type'][$question_id] == 1){
					$answerData['answer_text'] = (!empty($input['answerText'][$question_id]))?$input['answerText'][$question_id] :'';
				}else{
					return false;
				}
	
				$newAnswer = $this->model_general->insert('sv__evaluation__answer', $answerData);
			}
		}
		// if($newUser && $newTrainingEvaluation)
		// {
		// 	print json_encode(array(
		// 		"status" => "Success",
		// 		"msg"	 => "Successfully request for booking flight."
		// 	)); 
		// }
		// else
		// {
		// 	print json_encode(array(
		// 		"status" => "Error",
		// 		"msg"	 => "Failed to request booking flight. Please try again."
		// 	));
		// }
		redirect(base_url('hr/serviceformbak/evaluation_list'));
	}
	

	function it_equipment_list()
	{
		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'AV/IT Equipment Request List');
		$this->template->view('hr/policyprocedure/form/it_equipment_list', $data);
	}

	function it_equipment_form()
	{
		//$this->load->model('hr/model_stationery','model_stationery');

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'AV/IT Equipment Request Form');
		$this->template->view('hr/policyprocedure/form/it_equipment', $data);
	}

	function it_service_list()
	{
		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'AV/IT Service Request List');
		$this->template->view('hr/policyprocedure/form/it_service_list', $data);
	}

	function it_service_form()
	{
		//$this->load->model('hr/model_stationery','model_stationery');

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'AV/IT Service Request Form');
		$this->template->view('hr/policyprocedure/form/it_service', $data);
	}

	function policereport_list()
	{
		$this->load->model('hr/model_police_report');

		$data = array
		(
			'listPoliceReport' => $this->model_police_report->getListPoliceReport()
		);

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Police Report List');
		$this->template->view('hr/policyprocedure/form/policereport_list', $data);
	}

	function police_report_form($ID=null)
	{
		$data['ID']			= $ID;
		$this->load->model('hr/model_police_report');

		if($ID >0) 	$data['policeReportDetails'] 	= $this->model_police_report->getPoliceReportDetails($ID);
		// die(debug($ID));

		$data['page_setup'] = $this->template->pageSetup('Online Form', '', 'Police Report Form');
		$this->template->view('hr/policyprocedure/form/police_report', $data);
	}

	function police_report_save()
	{
		$input = $this->input->post();

		//die(debug($input));
		$police_report = array
		(
			'user_id'				=> $input['reqName_id'],
			'department_id'			=> $input['department_id'],
			'position'				=> $input['position'],
			'phone'					=> $input['phone'],
			'location'				=> $input['location'],
			'incident_date'			=> $input['incident_date'],
			'incident_time'			=> $input['incident_time'],
			'time_reported'			=> $input['time_reported'],
			'supervisor'			=> $input['supervisor'],
			'equipment_damaged'		=> $input['equipment_damaged'],
			'equipment_number'		=> $input['equipment_number'],
			'equipment_location'	=> $input['equipment_location'],
			'reason_damage'			=> $input['reason_damage'],
			'description_damage'	=> $input['description_damage'],
			'repair_cost'			=> $input['repair_cost'],
			'responsible_person'	=> $input['responsible_person'],
			'police_report'			=> $input['police_report'],
			'police_file'			=> $input['police_file'],
			'officer_incharge'		=> $input['officer_incharge'],
			'station'			    => $input['station'],
			'contact_no'			=> $input['contact_no'],
			'createdBY'				=> $this->logged_user['id']
						
		);

		$newPoliceReport = $this->model_general->insert('sv__police_report', $police_report);

		$this->load->library('File_upload');

		$upload_directory = 'assets/upload/hr/Form/policeReport/';

		$upload_status = $this->file_upload
			->set_file_type(FILE_TYPE_UPLOAD)
			->set_directory_public()
			->set_upload_directory($upload_directory)
			->uploads('file_upload');

			// die(debug($upload_status));

		if(is_array($upload_status)) {
			$uploadFile = array
			(
				'file_title'			=> (!empty($upload_status['file_name']))? $upload_status['file_name']:'',
				'sv_policeReport_id' 	=> $newPoliceReport,
				'file_location'			=> (!empty($upload_status['file_name']))?$upload_directory:'', 
				'is_delete'				=> 0,
			);
			// die(debug($uploadPhoto));

			$newTrainingRequestUpload = $this->model_general->insert('sv__police_report__upload',$uploadFile);

			// die(debug($newMaintenance2));
		} else {
			'tidak berjaya';
		}

		redirect(base_url('hr/serviceformbak/policereport_list'));
	}

	function police_report_edit()
	{
		$this->load->model('hr/model_police_report');

		$input = $this->input->post();

		// die(debug($input));
		$policeReportID 	= $this->input->post('policeReportID');
		$uploadID 			= $this->input->post('upload_id');

		
		$updateData = array
		(
			'user_id'				=> $input['reqName_id'],
			'department_id'			=> $input['department_id'],
			'position'				=> $input['position'],
			'phone'					=> $input['phone'],
			'location'				=> $input['location'],
			'incident_date'			=> $input['incident_date'],
			'incident_time'			=> $input['incident_time'],
			'time_reported'			=> $input['time_reported'],
			'supervisor'			=> $input['supervisor'],
			'equipment_damaged'		=> $input['equipment_damaged'],
			'equipment_number'		=> $input['equipment_number'],
			'equipment_location'	=> $input['equipment_location'],
			'reason_damage'			=> $input['reason_damage'],
			'description_damage'	=> $input['description_damage'],
			'repair_cost'			=> $input['repair_cost'],
			'responsible_person'	=> $input['responsible_person'],
			'police_report'			=> $input['police_report'],
			'police_file'			=> $input['police_file'],
			'officer_incharge'		=> $input['officer_incharge'],
			'station'				=> $input['station'],
			'contact_no'			=> $input['contact_no'],
			'updatedBY' 			=> $this->logged_user['id'],
			'updatedTS'	 			=> date('Y-m-d H:i:s'),
		);
		
		$where = array
		(
			'ID' => $policeReportID,
		);

		$affected_rows = $this->model_police_report->update_police_report($updateData,$where);
		//die(debug($affected_rows));

		if(!empty($_FILES['file_upload_update']['name']))
		{
			$this->load->library('File_upload');

			$upload_directory = 'upload/hr/Form/policeReport';

			$upload_status = $this->file_upload
				->set_file_type(FILE_TYPE_UPLOAD)
				->set_directory_public()
				->set_upload_directory('assets/'.$upload_directory)
				->uploads('file_upload_update');

				// die(debug($upload_status));

			if(is_array($upload_status)) 
			{
				$uploadFile = array
				(
					'file_title'         	=> (($upload_status['file_name']))?$upload_status['file_name']:$input['file_upload_update'],
					// 'filename'       		=> (!empty($upload_status['file_location']))?$upload_status['file_location']:''
					'file_location'     	=> (!empty($upload_status['file_name']))?$upload_directory:'', 
					'is_delete'         	=> 0,
					'updatedBY' 			=> $this->logged_user['id'],
					'updatedTS'	 			=> date('Y-m-d H:i:s'),
				); 

				// die(debug($uploadPhoto));

				$where = array
				(
					'ID' => $uploadID,
				);
				// die(debug($where));

				$affected_rows = $this->model_police_report->update_police_report_upload($uploadFile, $where);

			// die(debug($affected_rows));
			
			} else {
				'tidak berjaya';
			}
		}

		if($affected_rows)
			{
				print json_encode(array(
					"status" => "Success",
					"msg"	 => "Successfully update maintenance ."
				)); 
			}
			else
			{
				print json_encode(array(
					"status" => "Error",
					"msg"	 => "System failed to update maintenance."
				));
			}
		redirect(base_url('hr/serviceformbak/policereport_list'));
	}

	public function deletePoliceReportList()
	{
		$this->load->model('hr/model_police_report');

		$input				= $this->input->post();
		$policeReportID 	= $this->input->post('policeReportID');

		$sets = array
		(
			'is_delete'		=> 1,
			'updatedBY'		=> $this->logged_user['id'],
			'updatedTS'	 	=> date('Y-m-d H:i:s'),
		);

		$where = array
		(
			'ID' => $policeReportID,
		);

		$affected_rows = $this->model_police_report->update_police_report($sets,$where);

		if($affected_rows)
		{
			print json_encode(array(
				"status" => "Success",
				"msg"	 => "Successfully deleted police report list."
			)); 
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"	 => "System failed to delete police report list."
			));
		}
	}

	public function filter()
	{
		$this->load->model('hr/model_booking_room','room');

		$this->load->model('hr/model_maintenance','maintenance');

			$input  = $this->input->post();

			switch ($input['filter']) 
			{
				case 'booking_room':  
						$rs = $this->room->getBookingRoom(null,$input['date'],$input['department_id'],$input['room_id'],$input['vip_id']);
								break;

				case 'maintenance_list':  
						$rs = $this->maintenance->getMaintenanceRequestList(array(),$input['date'],$input['office_unit_id'],$input['floor_id'],$input['category']);
								break;

				default: $rs = null;
			}
	// debug($this->db->last_query());die();

			// debug($rs);
			
			if($rs) 
			{
					print json_encode(array
					(
							"status"        => "success",
							"recordSet"     => $rs,
					));
			}else {
					print json_encode(array
					(
							"status"        => "Failed",
							"recordSet"     => 'data takde'
					));
			}
	}
}
