<?php
include 'connect/cms-config.php' ;
include 'requires/function.php' ;
include 'requires/session.php' ;

// include the class
include 'requires/class_resize.php' ;

// keep parameter in value
$page 			  = escapeString($_GET['page']) ;
$page_mode 		  = escapeString($_GET['page_mode']) ;
$type 			  = escapeString($_GET['type']) ;
$search 		  = escapeString($_GET['search']) ;
$today_year       = date('Y', time()) ;
$today_minus_1    = date('Y-m-d', strtotime('-1 days')) ;


$staff_all    = [] ; 
$mysqli_staff = $mysqli->query("SELECT staff_name, staff_id FROM staff WHERE deleted_at IS NULL") ;
if ( $mysqli_staff->num_rows > 0 ){
	while($row_staff = $mysqli_staff->fetch_array(MYSQLI_ASSOC)){
		$staff_all[$row_staff['staff_id']] = $row_staff['staff_name'];
	}
}

// get all position
$position 	  = [] ;
$get_position = $mysqli->query("SELECT a.job_position_id, a.job_position_code, b.job_position_desc FROM setting_job_position a
		 						LEFT JOIN setting_job_position_translation b ON ( a.job_position_id = b.job_position_id )
					    		WHERE a.deleted_at IS NULL AND b.lang = 'en'") ;
if ( $get_position->num_rows > 0 ){
	while ( $row_position = $get_position->fetch_assoc() ){
		$position[$row_position['job_position_id']] = dataFilter( $row_position['job_position_code'] ) . ' ( ' . dataFilter( $row_position['job_position_desc'] ) . ' )' ;
	}
}

// get all section
$section 	  = [] ;
$get_section  = $mysqli->query("SELECT a.job_section_id, a.job_section_code, b.job_section_desc FROM setting_job_section a
                                LEFT JOIN setting_job_section_translation b ON ( a.job_section_id = b.job_section_id )
							    WHERE a.deleted_at IS NULL AND b.lang = 'en'") ;
if ( $get_section->num_rows > 0 ){
	while ( $row_section = $get_section->fetch_assoc() ){
		$section[$row_section['job_section_id']] = dataFilter( $row_section['job_section_code'] ) . ' ( ' . dataFilter( $row_section['job_section_desc'] ) . ' )' ;
	}
}

// get all requires 
$department_list   = [] ;
$mysqli_department = $mysqli->query("SELECT a.department_id, b.department_desc FROM setting_department a
						   			 LEFT JOIN setting_department_translation b ON ( a.department_id = b.department_id )
									 WHERE a.deleted_at IS NULL AND b.lang = 'en' ORDER BY b.department_desc ASC") ;
if ( $mysqli_department->num_rows > 0 ){
	while ( $row_department = $mysqli_department->fetch_assoc() ){
		$department_list[]  = $row_department ;
	}
}

// active menu bar
$active_main_menu = 'hr' ;
$active_sub_menu  = 'hr-merit-points' ;

// mode type | all list | new | edit
switch($page_mode){

	case 'movement'  :
	
		// check permission
		if ( !permissionCheck($row_user, 'hr-merit-points-movement-view') && !permissionCheck($row_user, 'foreign-only') ){
			echo '<script>alert("Sorry You Don\'t Have The Permission.")</script>';

			header('Location: index.php') ;
			exit ;
		}

		$active_menu 	  	= 'hr-merit-points-movement' ;
		$search_name 		= escapeString($_GET['search_name']) ;
		$search_idno 		= escapeString($_GET['search_idno']) ;
		$search_mobile 		= escapeString($_GET['search_mobile']) ;
		$search_mail 		= escapeString($_GET['search_mail']) ;
		$search_date 		= ( $_GET['search_date']!= '' ? date('Y-m-d', strtotime($_GET['search_date'])) : '' ) ;
		$search_action 		= escapeString($_GET['search_action']) ;
		$search_type 		= escapeString($_GET['search_type']) ;
		$search_remark 		= escapeString($_GET['search_remark']) ;
		$export  			= escapeString($_GET['export']) ;

		$search_query 		= '';

		if( $search_name != ''){
			$search_query .= " AND b.staff_name LIKE '%".$search_name."%'" ;
		}
		if( $search_idno != ''){
			$search_query .= " AND b.staff_idno LIKE '%".$search_idno."%'" ;
		}
		if( $search_mobile != ''){
			$search_query .= " AND b.staff_mobileno LIKE '%".$search_mobile."%'" ;
		}
		if( $search_mail != ''){
			$search_query .= " AND b.staff_email LIKE '%".$search_mail."%'" ;
		}
		if ( $search_date != '' ){
		    $search_query .= " AND a.created_at LIKE '%".$search_date."%' " ;
		}
		if ( $search_action != '' ){
		    $search_query .= " AND a.from_table = '".$search_action."' " ;
		}
		if ( $search_type == 'positive' ){
		    $search_query .= " AND a.amount >= '0' " ;
		}elseif ( $search_type == 'negative' ){
		    $search_query .= " AND a.amount < '0' " ;
		}
		if ( $search_remark != '' ){
		    $search_query .= " AND a.remark LIKE '%".$search_remark."%' " ;
		}

		// page query
		$mysqli_query = "SELECT 
							a.created_at, a.balance, a.before_amount, a.amount, a.from_table, a.remark, 
							b.staff_id, b.staff_idno, b.staff_name, b.job_position_id, b.job_section_id
						 FROM staff_point_movement a
						 LEFT JOIN staff b ON (a.staff_id = b.staff_id)
						 WHERE a.deleted_at IS NULL " . $search_query .$user_branch_permission_sql_b ;

		// pagination
		if (isset($page) && !empty($page)) { $product_page = $page ; } else { $product_page = 1 ; } // next and prev page (5 thing need to change)
		$start_from = ($product_page - 1) * LIMIT ; //end next and prev page

		// set search url
		$search_url = 'search='.$search.'&search_name='.$search_name.'&search_date='.$search_date.'&page_mode='.$page_mode.'&search_idno='.$search_idno.'&search_mobile='.$search_mobile.'&search_mail='.$search_mail.'&search_action='.$search_action.'&search_type='.$search_type.'&search_remark='.$search_remark ;

		switch ( $export ){
			case 'export-excel' :
				include 'PhpExcel/PHPExcel.php' ;
				$page_filename 		 = 'MeritPoint-'.date('Ymd', time()) ;
				$objPHPExcel   		 = new PHPExcel() ;
				$objPHPExcel->getProperties()->setCreator(COMPANY)->setTitle(COMPANY)->setSubject(COMPANY)->setDescription(COMPANY)->setKeywords(COMPANY)->setCategory(COMPANY) ;
				$objPHPExcel->getActiveSheet()->setTitle( $page_filename ) ;
				$objPHPExcel->setActiveSheetIndex(0);
				$objWriter     		 = PHPExcel_IOFactory::createWriter( $objPHPExcel, 'Excel5' ) ;
 
				$count     			 = 1 ;
				$firstChar 			 = 'A' ;
				$firstChar2 		 = $firstChar ;
 
				$array_title 	     = [] ;
				$array_title[] 		 = 'Date' ;
				$array_title[] 		 = 'Time' ;
				$array_title[] 		 = 'Staff IDNo' ;
				$array_title[] 		 = 'Staff Name' ;
				$array_title[] 		 = 'Department' ;
				$array_title[] 		 = 'Designation' ;
				$array_title[] 		 = 'Section' ;
				$array_title[] 		 = 'Action' ;
				$array_title[] 		 = 'Before' ;
				$array_title[] 		 = 'Amount' ;
				$array_title[] 		 = 'Total' ;
				$array_title[] 		 = 'Remark' ;
	            foreach ( $array_title as $ktitle => $vtitle ){
	                $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar2.$count, $vtitle ) ;
	                $firstChar2++ ;
	            }
	            $count++ ;

	            $mysqli_page 		 = $mysqli->query( $mysqli_query." ORDER by a.created_at DESC" ) ;
	            if( $mysqli_page->num_rows > 0 ){

					$staff_lists       = [] ;
					$staff_ids         = [] ;
	            	while( $row_page   = $mysqli_page->fetch_assoc() ){
						$staff_lists[] = $row_page ;
						$staff_ids[$row_page['staff_id']]   	    = $row_page['staff_id'] ;
					}

					$staff_departments = [] ;
					$select_departments = $mysqli->query( "SELECT a.staff_id, b.department_desc FROM staff_department a
														   LEFT JOIN setting_department_translation b ON ( a.department_id = b.department_id )
														   WHERE a.deleted_at IS NULL AND b.lang = 'en' AND a.staff_id IN ( ".implode(', ', $staff_ids)." )" ) ;
					if ( $select_departments->num_rows > 0 ){
						while ( $row_departments = $select_departments->fetch_assoc() ){
							$staff_departments[$row_departments['staff_id']][] = $row_departments['department_desc'] ;
						}
					}

					foreach ( $staff_lists as $k => $row_page ){
						$firstChar2  = $firstChar ;

						if( $row_page['before_amount'] >= $row_page['balance'] ){
							$movement_action = 'minus';
						}else{
							$movement_action = 'add';
						}

						$temp  		 = [
							'Date'       			=> date('Y-m-d', strtotime( $row_page['created_at'])),
							'Time'       			=> date('H:i:s', strtotime( $row_page['created_at'])),
							'StaffIDNo'       		=> dataFilter($row_page['staff_idno']),
							'StaffName'       		=> dataFilter($row_page['staff_name']),
							'Department'       		=> ( count($staff_departments[$row_page['staff_id']]) > 0 ? ( implode( "\n", $staff_departments[$row_page['staff_id']] ) ) : '' ),
							'Designation'       	=> $position[$row_page['job_position_id']],
							'Section'       		=> $section[$row_page['job_section_id']],
							'Action'       			=> ucfirst($row_page['from_table']),
							'Before'       			=> dataFilter($row_page['before_amount']),
							'Amount'       			=> ($movement_action == 'add' ? '+' : '').dataFilter($row_page['amount']),
							'Total'       			=> dataFilter($row_page['balance']),
							'Remark'       			=> dataFilter($row_page['remark'])
						] ;

						foreach ( $temp as $ktemp => $vtemp ){
							$objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar2.$count, $vtemp ) ; 
							$firstChar2++ ;
						}

						$count++ ;
					}
				}

				// Submission from
				header( 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) ; 
				header( 'Content-Disposition: attachment;filename="'.$page_filename.'.xls"' ) ;
				header( 'Cache-Control: max-age=0' ) ;
				// save to pc
				$objWriter->save('php://output') ;
				exit ;
			break ;
		}

		$mysqli_page = $mysqli->query($mysqli_query." ORDER by a.created_at DESC LIMIT $start_from, " . LIMIT) ;

			// load pagination
		$page_pagination = nextPrevious($product_page, LIMIT, $search_url, $mysqli_query) ;

		include 'requires/page_header.php';
		include 'requires/page_top.php';
	?>
	<div class="warper container-fluid">
	<div class='container' style="background-color: white; border-radius: 10px;">
		<div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;">
			<div class="row">
	        	<div class="pull-left col">
					<h1>Merit Points <small>Movement</small></h1>
				</div>
				<div class="pull-right col">
                	<a href="<?= '?'.$search_url.'&export=export-excel' ?>" class="btn" style="color:white;margin:5px;background-color: #5e5bd0;margin-top: 5px;" target="_blank">Export Excel</a>
                </div>
            </div>
		</div>

	    <div class="panel panel-default" id="basic-table-title">
	        <div class="panel-heading">Search</div>
	        <div class="panel-body">
	            <form method="get" class="form-horizontal">
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['Name'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_name" value="<?= $search_name ?>" class="form-control" />
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['ID No'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_idno" value="<?= $search_idno ?>" class="form-control" />
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['Mobile'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_mobile" value="<?= $search_mobile ?>" class="form-control" />
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['email'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_mail" value="<?= $search_mail ?>" class="form-control" />
	                    </div>
	                </div>
                    <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['date'] ?></label>
	                    <div class="col-sm-9">
	                        <input class="form-control" name="search_date" type="date" value="<?= $search_date ?>" placeholder="Date Resigned">
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['Action'] ?></label>
	                    <div class="col-sm-9">
	                        <select name="search_action" class="form-control">
	                            <option value=""><?= $lang['select']?></option>
	                            <option value="system" <?= ( $search_action == 'system' ? 'selected' : '' ) ?>>System</option>
	                            <option value="task" <?= ( $search_action == 'task' ? 'selected' : '' ) ?>>Task</option>
	                            <option value="redeem" <?= ( $search_action == 'redeem' ? 'selected' : '' ) ?>>Redeem</option>
	                            <option value="adjustment" <?= ( $search_action == 'adjustment' ? 'selected' : '' ) ?>>Adjustment</option>
	                        </select>
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['type'] ?></label>
	                    <div class="col-sm-9">
	                        <select name="search_type" class="form-control">
	                            <option value=""><?= $lang['select']?></option>
	                            <option value="positive" <?= ( $search_type == 'positive' ? 'selected' : '' ) ?>>Positive</option>
	                            <option value="negative" <?= ( $search_type == 'negative' ? 'selected' : '' ) ?>>Negative</option>
	                        </select>
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['remark'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_remark" value="<?= $search_remark ?>" class="form-control" />
	                    </div>
	                </div>
	                <div class="form-group">
	                    <div class="col-sm-11">
	                        <input type="hidden" name="page_mode" value="<?= $page_mode ?>" />
	                        <input type="hidden" name="search" value="<?= $search ?>" />
	                        <button type="submit" class="btn" style="color:white;float:right;background-color: #5e5bd0;margin-top: 5px;width: 100px;"><?= $lang['submit'] ?></button>
	                    </div>
	                </div>
	            </form>
	        </div>
	    </div>

	    <form method="post">
	        <div class="panel panel-default">
	            <div class="panel-heading">listing</div>
	            <div class="panel-body">
					<table cellpadding="0" cellspacing="0" border="0" class="listing-table responsive table table-striped table-bordered" id="basic-datatable">
						<thead>
							<tr>
	                            <th><?= $lang['date'] ?></th>
								<th><?= $lang['Staff'] ?></th>
								<th><?= $lang['Action'] ?></th>
								<th><?= $lang['before'] ?></th>
								<th><?= $lang['amount'] ?></th>
								<th><?= $lang['Total'] ?></th>
								<th><?= $lang['remark'] ?></th>
							</tr>
						</thead>
						<tbody>
							<?php
							if( $mysqli_page->num_rows > 0 ){
								while( $row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ){

									if($row_page['before_amount'] >= $row_page['balance']){
										$movement_action = 'minus';
									}else{
										$movement_action = 'add';
									}

									echo '
									 <tr class="odd gradeX">
										<td class="align_center">'.resetDateTimeFormat($row_page['created_at']).'</td>
										<td class="align_center">'.dataFilter($row_page['staff_name']).'</td>
										<td class="align_center">'.ucfirst($row_page['from_table']).'</td>
										<td class="align_center">'.dataFilter($row_page['before_amount']).'</td>
										<td class="align_center">'.($movement_action == 'add' ? '+' : '').dataFilter($row_page['amount']).'</td>
										<td class="align_center">'.dataFilter($row_page['balance']).'</td>
										<td class="align_center">'.dataFilter($row_page['remark']).'</td>
									</tr>';
								}
							}else{
								echo '
								<tr class="odd gradeX">
									<td class="border_none">'.$lang['no_data'].'</td>
									<td class="border_none"></td>
									<td class="border_none"></td>
									<td class="border_none"></td>
									<td class="border_none"></td>
									<td class="border_none"></td>
									<td class="border_none"></td>
								</tr>' ;
							}
							?>
						</tbody>
					</table>
					<?= $page_pagination['page_pagination'] ?>
	            </div>
	        </div>
		</form>
	</div>
	</div>
<?php
	break;

	case 'adjustment'  :
	
		// check permission
		if ( !permissionCheck($row_user, 'hr-merit-points-adjustment-view') && !permissionCheck($row_user, 'foreign-only') ){
			echo '<script>alert("Sorry You Don\'t Have The Permission.")</script>';

			header('Location: index.php') ;
			exit ;
		}
		
		$active_menu 	  	= 'hr-merit-points-adjustment' ;
		$search_name 		= escapeString($_GET['search_name']) ;
		$search_idno 		= escapeString($_GET['search_idno']) ;
		$search_mobile 		= escapeString($_GET['search_mobile']) ;
		$search_mail 		= escapeString($_GET['search_mail']) ;
		$search_date 		= ( $_GET['search_date']!= '' ? date('Y-m-d', strtotime($_GET['search_date'])) : '' ) ;
		$search_action 		= escapeString($_GET['search_action']) ;
		$search_type 		= escapeString($_GET['search_type']) ;
		$search_remark 		= escapeString($_GET['search_remark']) ;
		$export  			= escapeString($_GET['export']) ;

		$search_query 		= '';

		if( $search_name != ''){
			$search_query .= " AND d.staff_name LIKE '%".$search_name."%'" ;
		}
		if( $search_idno != ''){
			$search_query .= " AND d.staff_idno LIKE '%".$search_idno."%'" ;
		}
		if( $search_mobile != ''){
			$search_query .= " AND d.staff_mobileno LIKE '%".$search_mobile."%'" ;
		}
		if( $search_mail != ''){
			$search_query .= " AND d.staff_email LIKE '%".$search_mail."%'" ;
		}
		if ( $search_date != '' ){
		    $search_query .= " AND a.created_at like '%".$search_date."%' " ;
		}
		if ( $search_action != '' ){
		    $search_query .= " AND c.title like '%".$search_action."%' " ;
		}
		if ( $search_type == 'positive' ){
		    $search_query .= " AND a.adjustment_type = 'plus' " ;
		}elseif ( $search_type == 'negative' ){
		    $search_query .= " AND a.adjustment_type = 'minus' " ;
		}
		if ( $search_remark != '' ){
		    $search_query .= " AND a.remark LIKE '%".$search_remark."%' " ;
		}

		// page query
		$mysqli_query = "SELECT 
							a.adjustment_so, a.adjustment_type, a.created_by, a.point, a.remark, a.created_at, a.point, 
							c.title, 
							d.staff_id, d.staff_idno, d.staff_name, d.job_position_id, d.job_section_id,
							e.staff_id as createdby_staff_id, e.staff_idno as createdby_staff_idno, e.staff_name as createdby_staff_name, e.job_position_id as createdby_job_position_id, e.job_section_id as createdby_job_section_id
						 FROM staff_adjustment a
						 LEFT JOIN staff_adjustment_point b ON (a.adjustment_id = b.adjustment_id)
						 LEFT JOIN setting_adjustment c ON (a.setting_adjustment_id = c.adjustment_id)
						 LEFT JOIN staff d ON (b.staff_id = d.staff_id)
						 LEFT JOIN staff e ON (a.created_by = e.staff_id)
						 WHERE a.deleted_at IS NULL " . $search_query .$user_branch_permission_sql_d ;

		// pagination
		if (isset($page) && !empty($page)) { $product_page = $page ; } else { $product_page = 1 ; } // next and prev page (5 thing need to change)
		$start_from  = ($product_page - 1) * LIMIT ; //end next and prev page

		// set search url
		$search_url  = 'search='.$search.'&search_name='.$search_name.'&search_date='.$search_date.'&page_mode='.$page_mode.'&search_idno='.$search_idno.'&search_mobile='.$search_mobile.'&search_mail='.$search_mail.'&search_action='.$search_action.'&search_type='.$search_type.'&search_remark='.$search_remark ;

		switch ( $export ){
			case 'export-excel' :
				include 'PhpExcel/PHPExcel.php' ;
				$page_filename 		 = 'MeritPoint-'.date('Ymd', time()) ;
				$objPHPExcel   		 = new PHPExcel() ;
				$objPHPExcel->getProperties()->setCreator(COMPANY)->setTitle(COMPANY)->setSubject(COMPANY)->setDescription(COMPANY)->setKeywords(COMPANY)->setCategory(COMPANY) ;
				$objPHPExcel->getActiveSheet()->setTitle( $page_filename ) ;
				$objPHPExcel->setActiveSheetIndex(0);
				$objWriter     		 = PHPExcel_IOFactory::createWriter( $objPHPExcel, 'Excel5' ) ;
 
				$count     			 = 1 ;
				$firstChar 			 = 'A' ;
				$firstChar2 		 = $firstChar ;
 
				$array_title 	     = [] ;
				$array_title[] 		 = 'Date' ;
				$array_title[] 		 = 'Time' ;
				$array_title[] 		 = 'Created By Staff IDNo' ;
				$array_title[] 		 = 'Created By Staff Name' ;
				$array_title[] 		 = 'Created By Department' ;
				$array_title[] 		 = 'Created By Designation' ;
				$array_title[] 		 = 'Created By Section' ;
				$array_title[] 		 = 'Staff IDNo' ;
				$array_title[] 		 = 'Staff Name' ;
				$array_title[] 		 = 'Department' ;
				$array_title[] 		 = 'Designation' ;
				$array_title[] 		 = 'Section' ;
				$array_title[] 		 = 'Action' ;
				$array_title[] 		 = 'Amount' ;
				$array_title[] 		 = 'Remark' ;
	            foreach ( $array_title as $ktitle => $vtitle ){
	                $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar2.$count, $vtitle ) ;
	                $firstChar2++ ;
	            }
	            $count++ ;

	            $mysqli_page 		 = $mysqli->query( $mysqli_query." ORDER by a.created_at DESC" ) ;
	            if( $mysqli_page->num_rows > 0 ){

					$staff_lists       = [] ;
					$staff_ids         = [] ;
	            	while( $row_page   = $mysqli_page->fetch_assoc() ){
						$staff_lists[] = $row_page ;
						$staff_ids[$row_page['staff_id']]   	    = $row_page['staff_id'] ;
						$staff_ids[$row_page['createdby_staff_id']] = $row_page['createdby_staff_id'] ;
					}

					$staff_departments = [] ;
					$select_departments = $mysqli->query( "SELECT a.staff_id, b.department_desc FROM staff_department a
														   LEFT JOIN setting_department_translation b ON ( a.department_id = b.department_id )
														   WHERE a.deleted_at IS NULL AND b.lang = 'en' AND a.staff_id IN ( ".implode(', ', $staff_ids)." )" ) ;
					if ( $select_departments->num_rows > 0 ){
						while ( $row_departments = $select_departments->fetch_assoc() ){
							$staff_departments[$row_departments['staff_id']][] = $row_departments['department_desc'] ;
						}
					}

					foreach ( $staff_lists as $k => $row_page ){
						$firstChar2  = $firstChar ;

						$temp  		 = [
							'Date'       			=> date('Y-m-d', strtotime( $row_page['created_at'])),
							'time'       			=> date('H:i:s', strtotime( $row_page['created_at'])),
							'CreatedByStaffIDNo'    => dataFilter($row_page['createdby_staff_idno']),
							'CreatedByStaffName' 	=> dataFilter($row_page['createdby_staff_name']),
							'CreatedByDepartment' 	=> ( count($staff_departments[$row_page['createdby_staff_id']]) > 0 ? ( implode( "\n", $staff_departments[$row_page['createdby_staff_id']] ) ) : '' ),
							'CreatedByDesignation'  => $position[$row_page['createdby_job_position_id']],
							'CreatedBySection'      => $section[$row_page['createdby_job_section_id']],
							'StaffIDNo'       		=> dataFilter($row_page['staff_idno']),
							'StaffName'       		=> dataFilter($row_page['staff_name']),
							'Department'       		=> ( count($staff_departments[$row_page['staff_id']]) > 0 ? ( implode( "\n", $staff_departments[$row_page['staff_id']] ) ) : '' ),
							'Designation'       	=> $position[$row_page['job_position_id']],
							'Section'       		=> $section[$row_page['job_section_id']],
							'Action'       			=> ucfirst($row_page['title']),
							'Amount'       			=> ($row_page['adjustment_type'] == 'plus' ? '+' : '').dataFilter($row_page['point']),
							'Remark'       			=> dataFilter($row_page['remark'])
						] ;

						foreach ( $temp as $ktemp => $vtemp ){
							$objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar2.$count, $vtemp ) ; 
							$firstChar2++ ;
						}

						$count++ ;
					}
				}

				// Submission from
				header( 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) ; 
				header( 'Content-Disposition: attachment;filename="'.$page_filename.'.xls"' ) ;
				header( 'Cache-Control: max-age=0' ) ;
				// save to pc
				$objWriter->save('php://output') ;
				exit ;
			break ;
		}

		$mysqli_page = $mysqli->query($mysqli_query."ORDER by a.created_at DESC LIMIT $start_from, " . LIMIT) ;

			// load pagination
		$page_pagination = nextPrevious($product_page, LIMIT, $search_url, $mysqli_query) ;

		include 'requires/page_header.php';
		include 'requires/page_top.php';
	?>
	<div class="warper container-fluid">
	<div class='container' style="background-color: white; border-radius: 10px;">
		<div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;">
			<div class="row">
	        	<div class="pull-left col">
					<h1>Merit Points <small>Adjustment</small></h1>
				</div>
				<div class="pull-right col">
                <a href="<?= '?'.$search_url.'&export=export-excel' ?>" target="_blank" class="btn" style="color:white;margin:5px;background-color: #5e5bd0;margin-top: 5px;">Export Excel</a>
            	</div>
            </div>
		</div>

	    <div class="panel panel-default" id="basic-table-title">
	        <div class="panel-heading"></div>
	        <div class="panel-body">
	            <form method="get" class="form-horizontal">
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['Name'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_name" value="<?= $search_name ?>" class="form-control" />
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['ID No'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_idno" value="<?= $search_idno ?>" class="form-control" />
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['Mobile'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_mobile" value="<?= $search_mobile ?>" class="form-control" />
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['email'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_mail" value="<?= $search_mail ?>" class="form-control" />
	                    </div>
	                </div>
                    <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['date'] ?></label>
	                    <div class="col-sm-9">
	                        <input class="form-control" name="search_date" type="date" value="<?= $search_date ?>" placeholder="Date Resigned">
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['Action'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_action" value="<?= $search_action ?>" class="form-control" />
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['type'] ?></label>
	                    <div class="col-sm-9">
	                        <select name="search_type" class="form-control">
	                            <option value=""><?= $lang['select']?></option>
	                            <option value="positive" <?= ( $search_type == 'positive' ? 'selected' : '' ) ?>>Positive</option>
	                            <option value="negative" <?= ( $search_type == 'negative' ? 'selected' : '' ) ?>>Negative</option>
	                        </select>
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['remark'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_remark" value="<?= $search_remark ?>" class="form-control" />
	                    </div>
	                </div>
	                <div class="form-group">
	                    <div class="col-sm-11">
	                        <input type="hidden" name="page_mode" value="<?= $page_mode ?>" />
	                        <input type="hidden" name="search" value="<?= $search ?>" />
	                        <button type="submit" class="btn" style="color:white;float:right;background-color: #5e5bd0;margin-top: 5px;width: 100px;"><?= $lang['submit'] ?></button>
	                    </div>
	                </div>
	            </form>
	        </div>
	    </div>

	    <form method="post">
	        <div class="panel panel-default">
	            <div class="panel-heading"></div>
	            <div class="panel-body">
					<table cellpadding="0" cellspacing="0" border="0" class="listing-table responsive table table-striped table-bordered" id="basic-datatable">
						<thead>
							<tr>
	                            <th><?= $lang['date'] ?></th>
								<th><?= $lang['Created By'] ?></th>
								<th><?= $lang['Staff'] ?></th>
								<th><?= $lang['Action'] ?></th>
								<th><?= $lang['amount'] ?></th>
								<th><?= $lang['remark'] ?></th>
							</tr>
						</thead>
						<tbody>
							<?php
							if( $mysqli_page->num_rows > 0 ){
								while( $row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ){
									echo '
									 <tr class="odd gradeX">
										<td class="align_center">'.resetDateTimeFormat($row_page['created_at']).'</td>
										<td class="align_center">'.dataFilter($row_page['createdby_staff_name']).' ('.dataFilter($row_page['createdby_staff_idno']).')</td>
										<td class="align_center">'.dataFilter($row_page['staff_name']).' ('.dataFilter($row_page['staff_idno']).')</td>
										<td class="align_center">'.ucfirst($row_page['title']).'</td>
										<td class="align_center">'.($row_page['adjustment_type'] == 'plus' ? '+' : '').dataFilter($row_page['point']).'</td>
										<td class="align_center">'.dataFilter($row_page['remark']).'</td>
									</tr>';
								}
							}else{
								echo '
								<tr class="odd gradeX">
									<td class="border_none">'.$lang['no_data'].'</td>
									<td class="border_none"></td>
									<td class="border_none"></td>
									<td class="border_none"></td>
									<td class="border_none"></td>
									<td class="border_none"></td>
								</tr>' ;
							}
							?>
						</tbody>
					</table>
					<?= $page_pagination['page_pagination'] ?>
	            </div>
	        </div>
		</form>
	</div>
	</div>
<?php
	break;
	case 'task'  :
	
		// check permission
		if ( !permissionCheck($row_user, 'hr-merit-points-task-view') && !permissionCheck($row_user, 'foreign-only') ){
			echo '<script>alert("Sorry You Don\'t Have The Permission.")</script>';

			header('Location: index.php') ;
			exit ;
		}
		
		$active_menu 	  = 'hr-merit-points-task' ;
		$search_name = escapeString($_GET['search_name']) ;
		$search_idno = escapeString($_GET['search_idno']) ;
		$search_mobile = escapeString($_GET['search_mobile']) ;
		$search_mail = escapeString($_GET['search_mail']) ;
		$search_date = ( $_GET['search_date']!= '' ? date('Y-m-d', strtotime($_GET['search_date'])) : '' ) ;
		$search_action = escapeString($_GET['search_action']) ;
		$search_type = escapeString($_GET['search_type']) ;
		$search_remark = escapeString($_GET['search_remark']) ;
		$search_level = escapeString($_GET['search_level']) ;

		$search_query = '';

		if( $search_name != ''){
			$search_query .= " AND b.staff_name LIKE '%".$search_name."%'" ;
		}
		if( $search_idno != ''){
			$search_query .= " AND b.staff_idno LIKE '%".$search_idno."%'" ;
		}
		if( $search_mobile != ''){
			$search_query .= " AND b.staff_mobileno LIKE '%".$search_mobile."%'" ;
		}
		if( $search_mail != ''){
			$search_query .= " AND b.staff_email LIKE '%".$search_mail."%'" ;
		}
		if ( $search_date != '' ){
		    $search_query .= " AND a.created_at >= '".$search_date."' " ;
		}
		if ( $search_level != '' ){
		    $search_query .= " AND c.difficulty = '".$search_level."' " ;
		}
		if ( $search_action != '' ){
		    $search_query .= " AND c.title like '%".$search_action."%' " ;
		}
		if ( $search_type == 'positive' ){
		    $search_query .= " AND a.amount >= '0' " ;
		}elseif ( $search_type == 'negative' ){
		    $search_query .= " AND a.amount < '0' " ;
		}
		if ( $search_remark != '' ){
		    $search_query .= " AND a.remark LIKE '%".$search_remark."%' " ;
		}

		// page query
		$mysqli_query = "SELECT a.created_at, a.amount, a.remark, b.staff_name, c.assigned_by, c.difficulty, c.title FROM staff_point_movement a
						 LEFT JOIN staff b ON (a.staff_id = b.staff_id)
						 LEFT JOIN task c ON (a.from_id = c.task_id)
						 WHERE a.deleted_at IS NULL and a.from_table = 'task' " . $search_query .$user_branch_permission_sql_b ;

		// pagination
		if (isset($page) && !empty($page)) { $product_page = $page ; } else { $product_page = 1 ; } // next and prev page (5 thing need to change)
		$start_from = ($product_page - 1) * LIMIT ; //end next and prev page

		// set search url
		$search_url = 'search='.$search.'&search_name='.$search_name.'&search_date='.$search_date.'&page_mode='.$page_mode.'&search_idno='.$search_idno.'&search_mobile='.$search_mobile.'&search_mail='.$search_mail.'&search_action='.$search_action.'&search_type='.$search_type.'&search_remark='.$search_remark.'&search_level='.$search_level ;

		$mysqli_page = $mysqli->query($mysqli_query."ORDER by a.created_at DESC LIMIT $start_from, " . LIMIT) ;

			// load pagination
		$page_pagination = nextPrevious($product_page, LIMIT, $search_url, $mysqli_query) ;

		include 'requires/page_header.php';
		include 'requires/page_top.php';
	?>
	<div class="warper container-fluid">
	<div class='container' style="background-color: white; border-radius: 10px;">
		<div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;"><h1>Merit Points <small>Task</small></h1></div>

	    <div class="panel panel-default" id="basic-table-title">
	        <div class="panel-heading">Search</div>
	        <div class="panel-body">
	            <form method="get" class="form-horizontal">
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['Name'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_name" value="<?= $search_name ?>" class="form-control" />
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['ID No'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_idno" value="<?= $search_idno ?>" class="form-control" />
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['Mobile'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_mobile" value="<?= $search_mobile ?>" class="form-control" />
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['email'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_mail" value="<?= $search_mail ?>" class="form-control" />
	                    </div>
	                </div>
                    <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['date'] ?></label>
	                    <div class="col-sm-9">
	                        <input class="form-control" name="search_date" type="date" value="<?= $search_date ?>" placeholder="Date Resigned">
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['level'] ?></label>
	                    <div class="col-sm-9">
	                        <select name="search_level" class="form-control">
	                            <option value=""><?= $lang['select']?></option>
	                            <option value="normal" <?= ( $search_level == 'normal' ? 'selected' : '' ) ?>>Normal</option>
	                            <option value="easy" <?= ( $search_level == 'easy' ? 'selected' : '' ) ?>>Easy</option>
	                            <option value="medium" <?= ( $search_level == 'medium' ? 'selected' : '' ) ?>>Medium</option>
	                            <option value="hard" <?= ( $search_level == 'hard' ? 'selected' : '' ) ?>>Hard</option>
	                            <option value="master" <?= ( $search_level == 'master' ? 'selected' : '' ) ?>>Master</option>
	                        </select>
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['title'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_action" value="<?= $search_action ?>" class="form-control" />
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['type'] ?></label>
	                    <div class="col-sm-9">
	                        <select name="search_type" class="form-control">
	                            <option value=""><?= $lang['select']?></option>
	                            <option value="positive" <?= ( $search_type == 'positive' ? 'selected' : '' ) ?>>Positive</option>
	                            <option value="negative" <?= ( $search_type == 'negative' ? 'selected' : '' ) ?>>Negative</option>
	                        </select>
	                    </div>
	                </div>
	                <div class="form-group">
	                    <label class="col-sm-2 control-label"><?= $lang['remark'] ?></label>
	                    <div class="col-sm-9">
	                        <input type="text" name="search_remark" value="<?= $search_remark ?>" class="form-control" />
	                    </div>
	                </div>
	                <div class="form-group">
	                    <div class="col-sm-11">
	                        <input type="hidden" name="page_mode" value="<?= $page_mode ?>" />
	                        <input type="hidden" name="search" value="<?= $search ?>" />
	                        <button type="submit" class="btn" style="color:white;float:right;background-color: #5e5bd0;margin-top: 5px;width: 100px;"><?= $lang['submit'] ?></button>
	                    </div>
	                </div>
	            </form>
	        </div>
	    </div>

	    <form method="post">
	        <div class="panel panel-default">
	            <div class="panel-heading">listing</div>
	            <div class="panel-body">
					<table cellpadding="0" cellspacing="0" border="0" class="listing-table responsive table table-striped table-bordered" id="basic-datatable">
						<thead>
							<tr>
	                            <th><?= $lang['date'] ?></th>
								<th><?= $lang['Staff'] ?></th>
								<th>Assigned By</th>
								<th><?= $lang['level'] ?></th>
								<th><?= $lang['title'] ?></th>
								<th><?= $lang['amount'] ?></th>
								<th><?= $lang['remark'] ?></th>
							</tr>
						</thead>
						<tbody>
							<?php
							if( $mysqli_page->num_rows > 0 ){
								while( $row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ){

									if($row_page['before_amount'] >= $row_page['balance']){
										$movement_action = 'minus';
									}else{
										$movement_action = 'add';
									}

									echo '
									 <tr class="odd gradeX">
										<td class="align_center">'.resetDateTimeFormat($row_page['created_at']).'</td>
										<td class="align_center">'.dataFilter($row_page['staff_name']).'</td>
										<td class="align_center">'.dataFilter($staff_all[$row_page['assigned_by']]).'</td>
										<td class="align_center">'.ucfirst($row_page['difficulty']).'</td>
										<td class="align_center">'.ucfirst($row_page['title']).'</td>
										<td class="align_center">'.($movement_action == 'add' ? '+' : '').dataFilter($row_page['amount']).'</td>
										<td class="align_center">'.dataFilter($row_page['remark']).'</td>
									</tr>';
								}
							}else{
								echo '
								<tr class="odd gradeX">
									<td class="border_none">'.$lang['no_data'].'</td>
									<td class="border_none"></td>
									<td class="border_none"></td>
									<td class="border_none"></td>
									<td class="border_none"></td>
									<td class="border_none"></td>
									<td class="border_none"></td>
								</tr>' ;
							}
							?>
						</tbody>
					</table>
					<?= $page_pagination['page_pagination'] ?>
	            </div>
	        </div>
		</form>
	</div>
	</div>
<?php
	break;
}
?>