<?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']) ;
$adjustment_id	    = escapeString($_GET['adjustment_id']) ;

// active menu bar
$active_main_menu   = 'hr' ;
$active_sub_menu    = 'hr-staff' ;
$active_menu 		= 'hr-staff-adjustment' ;

$get_user_tier      = userTierQuery( $row_user ) ;

// mode type | all list | new | edit
switch($page_mode){
	
	// edit redeem
	case 'new' :
	case 'edit' :

		// check query exsits
		$submit_type = 'new' ;

		// update database
		if ( $_POST['hide'] == 1 ){

			$error 					= 'Invalid Type' ;

			$current_branch_id      = $_SESSION['url_get_branch_admin'] ;
			$created_type           = escapeString( $_POST['created_type'] ) ;
			$created_name           = escapeString( $_POST['created_name'] ) ;
			$adjustment_type        = $_POST['adjustment_type'] ;
			$adjustment_type_plus   = escapeString( $_POST['adjustment_type_plus'] ) ;
			$adjustment_type_minus  = escapeString( $_POST['adjustment_type_minus'] ) ;
			$remark 		        = escapeString( $_POST['remark'] ) ;
			$point 		            = escapeString( $_POST['point'] ) ;


			if ( $adjustment_type != '' ){

				$error 					   = 'Invalid Setting Point Adjustment' ;

				$setting_adjustment_id     = '' ;
				if ( $adjustment_type     == 'plus' ){
					$setting_adjustment_id = $adjustment_type_plus ;
				}
				if ( $adjustment_type     == 'minus' ){
					$setting_adjustment_id = $adjustment_type_minus ;
				}

				switch ( $adjustment_type ){
		            case 'plus' :
		                $point  = ( $point > 0 ? $point : -($point) ) ;
		            break ;
		            case 'minus' :
		                $point  = ( $point > 0 ? -($point) : $point ) ;
		            break ;
		        }

				if ( $setting_adjustment_id != '' ){

					$error 				= 'Invalid staff' ;

					$staff_id 			= [] ;

					// delete all department & receiver
					$receiver_type		= dataFilter($_POST['receiver_type']) ;
					$receiver_to 		= $_POST['receiver_to'] ;
					$receiver_to_dept 	= $_POST['receiver_to_dept'] ;

					if ( $receiver_type == '1' ){
						if( !empty( $receiver_to ) ){
							for ( $i = 0 ; $i < count($receiver_to) ; $i++ ){
								if ( $receiver_to[$i] != '' ){
									$reset_staff      = $receiver_to[$i] ;
									$staff_id[$reset_staff] = $receiver_to[$i] ;
								}
							}
						}
					}else{
						if( !empty( $receiver_to_dept ) ){
							$array_depart = [] ;
							for ( $i = 0 ; $i < count($receiver_to_dept) ; $i++ ){

								$department_id = $receiver_to_dept[$i] ;
								if ( $department_id != '' ){

									// save into department 
									$selected_depart[]= $department_id ;

									// check department staff
									$reset_depart     = str_replace( ['(', ')'], '', $department_id ) ;
									$get_depart_staff = $mysqli->query( "SELECT a.staff_id FROM staff_department a
																		 LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
																		 WHERE a.deleted_at IS NULL AND b.deleted_at IS NULL AND a.department_id = '".$reset_depart."'" ) ;
									if ( $get_depart_staff->num_rows > 0 ){
										while ( $row_depart_staff = $get_depart_staff->fetch_assoc() ){
											if ( !in_array($row_depart_staff['staff_id'], $array_depart) ){
												$array_depart[]   = $row_depart_staff['staff_id'] ;
												$staff_id[$row_depart_staff['staff_id']] = $row_depart_staff['staff_id'] ;
											}
										}
									}

								}
							}
						}
					}

					if ( count($staff_id) > 0 ){
						$error 			    = 'Selected adjustment not exists.' ;

						// check adjustment exists
						$setting_adjustment = $mysqli->query( "SELECT a.adjustment_id, a.adjustment_type, b.title FROM setting_adjustment a
								   						       LEFT JOIN setting_adjustment_translation b ON ( a.adjustment_id = b.adjustment_id )
															   WHERE a.deleted_at IS NULL AND b.lang = 'en' AND a.adjustment_site = 'backend' AND a.adjustment_id = '".$setting_adjustment_id."' LIMIT 1" ) ;
						if ( $setting_adjustment->num_rows > 0 ){
							$error 			    = 'Failed To Submit' ;

							$row_setting_adjustment = $setting_adjustment->fetch_assoc() ;

							if ( $mysqli->query( "INSERT INTO staff_adjustment
		                                          ( `adjustment_type`, `created_branch_id`, `created_by`, `created_type`, `created_name`, `department_id`, `setting_adjustment_id`, `point`, `remark` ) VALUES
		                                          ( '".$adjustment_type."', '".$current_branch_id."', '".$_SESSION['system_id']."', '".$created_type."', '".$created_name."', '0', '".$setting_adjustment_id."', '".$point."', '".$remark."' )" ) ){
		                        $status         = '200' ;

		                        $error 			= 'success-updated' ;


		                        $adjustment_id  = $mysqli->insert_id ;
		                        $adjustment_so  = 'AD'.strPad( 6, $adjustment_id ) ;
		                        $mysqli->query( "UPDATE staff_adjustment SET adjustment_so = '".$adjustment_so."' WHERE adjustment_id = '".$adjustment_id."'" ) ;

		                        // set point movement
		                        // $remark         = ( $point > 0 ? 'Earn' : 'Deduct' ).' point from adjustment ( ' . $adjustment_so . ' )' ;
		                        $remark         = 'You have been '.( $point > 0 ? 'given' : 'deducted' ).' the point ('.$row_setting_adjustment['title'].') from backend (' . $adjustment_so . ')' ;
		                       
		                        foreach ( $staff_id as $k => $v ){
		                            $mysqli->query( "INSERT INTO staff_adjustment_point ( `adjustment_id`, `setting_adjustment_id`, `staff_id`, `point` ) VALUES ( '".$adjustment_id."', '".$setting_adjustment_id."', '".$v."', '".$point."' )" ) ;

		                            pointMovement( 'adjustment', $adjustment_id, $adjustment_type, 'normal', $v, $point, $remark ) ;
		                            pushToUserCron( 'staff_adjustment', $adjustment_id, $v, 'Point Adjustment', $remark ) ;
		                        }

		                    }
						}

					}

				}
			}

			// refresh page
			header("Location:hr-staff-adjustment.php?page_mode=all&success=1") ;
			$_SESSION['system_result'] = $error ;
			exit ;
		}

		if ( (!permissionCheck($row_user, 'staff-adjustment-new') ) ){
			header('Location: hr-staff-adjustment.php') ;
			exit ;
		}

		// staff
		$staff_list   = [] ;
		$mysqli_staff = $mysqli->query( "SELECT staff_id, staff_name, staff_idno FROM staff
										 WHERE deleted_at IS NULL ".$user_branch_permission_sql . ( $get_user_tier['check'] ? " AND staff_tier IN ( ".implode(', ', $get_user_tier['tiers'])." )" : '' ) ) ;
		if ( $mysqli_staff->num_rows > 0 ){
			while ( $row_staff = $mysqli_staff->fetch_assoc() ){
				$staff_list[$row_staff['staff_id']] = dataFilter($row_staff['staff_name']) . ' ( ' . dataFilter($row_staff['staff_idno']) . ' )' ;
			}
		}

		// 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'") ;
		if ( $mysqli_department->num_rows > 0 ){
			while ( $row_department = $mysqli_department->fetch_assoc() ){
				$department_list[$row_department['department_id']] = dataFilter($row_department['department_desc']) ;
			}
		}

		if ( ( $page_mode == 'new' && !permissionCheck($row_user, 'handbook-list-new') ) ||
			 ( $page_mode == 'edit' && !permissionCheck($row_user, 'handbook-list-edit') ) ){
			header('Location: app-training.php') ;
			exit ;
		}

		// get all selected staff & department
		$receiver_depart = ( $row_page['department_id'] != '' ? explode('/', $row_page['department_id']) : [] ) ;

		// setting
		$array_setting_adjustment = [] ;
		$select_setting 		  = $mysqli->query( "SELECT a.adjustment_id, a.adjustment_type, b.title FROM setting_adjustment a
						   						     LEFT JOIN setting_adjustment_translation b ON ( a.adjustment_id = b.adjustment_id )
													 WHERE a.deleted_at IS NULL AND b.lang = 'en' AND a.adjustment_site = 'backend'" ) ;
		if ( $select_setting->num_rows > 0 ){
			while ( $row_setting = $select_setting->fetch_assoc() ){
				$array_setting_adjustment[$row_setting['adjustment_type']][] = $row_setting ;
			}
		}

		// start header here
		include 'requires/page_header.php';
		include 'requires/page_top.php';
		?>

		<style>
		.stafflist_check, .departmentlist_check{
			margin-top:5px;
			margin-bottom:5px;
		}
		.form-group-pdf, .form-group-message, .form-group-video{
			display:none;
		}
		</style>

		<script>
			$(document).ready(function(){
				$(".click_to_show_name").change(function(){
					$(".div_show_box").show() ;
				}) ;

				$(".click_to_show_type").change(function(){
					if ( $(this).val() == 'plus' ){
						$("#adjustment_type_plus").show() ;
						$("#adjustment_type_minus").hide() ;
					}else{
						$("#adjustment_type_plus").hide() ;
						$("#adjustment_type_minus").show() ;
					}
				}) ;

				$("#checkAll").click(function(){
				$("#selectuser option").prop("selected","selected");
				$("#selectuser").trigger("change");
			});

			$("#unCheckAll").click(function(){
				$("#selectuser option").removeAttr("selected");
				$("#selectuser").trigger("change");
			});

			$("#checkAllDept").click(function(){
				$("#selectdept option").prop("selected","selected");
				$("#selectdept").trigger("change");
			});

			$("#unCheckAllDept").click(function(){
				$("#selectdept option").removeAttr("selected");
				$("#selectdept").trigger("change");
			});

			$('input[type=radio][name=receiver_type]').on('change', function(){
	            hideshowlist($(this).val()) ;
	        });

	        <?php if ( $submit_type == 'edit' ){ ?>
				hideshowlist('<?= $row_page['receiver_type'] ?>') ;
			<?php }else{ ?>
				hideshowlist( '1' ) ;
			<?php } ?>

			$('input[type=radio][name=receiver_type]').on('change', function(){
	            hideshowlist($(this).val()) ;
	        });

	        // function hideshowlist(id){
	        // 	alert(id );
	        // 	if( id == '0' ){
	        //         $('.stafflist').hide();
	        //         $('.departmentlist').hide();
	        //     }else if( id == '1' ){
	        //         $('.stafflist').show();
	        //         $('.departmentlist').hide();
	        //     }else{
	        //         $('.stafflist').hide();
	        //         $('.departmentlist').show();
	        //     }
	        // }

			function hideshowlist(id){
	        	if ( id == '0' ){
	                $('.stafflist').hide();
	                $('.stafflist').next(".select2-container").hide();
	                $('.departmentlist').hide();
	                $('.departmentlist').next(".select2-container").hide();
	            }else if ( id == '1' ){
	                $('.stafflist').show();
	                $('.stafflist').next(".select2-container").show();
	                $('.departmentlist').hide();
	                $('.departmentlist').next(".select2-container").hide();
	            }else{
	                $('.stafflist').hide();
	                $('.stafflist').next(".select2-container").hide();
	                $('.departmentlist').show();
	                $('.departmentlist').next(".select2-container").show();
	            }
	        }
		}) ;
		</script>
		<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>Point Adjustment <small><?= $lang[$page_mode] ?></small></h1></div>

            <div class="panel panel-default">
                <div class="panel-heading"></div>
                <div class="panel-body">
                    <form method="post" class="form-horizontal" id="quotationForm" enctype="multipart/form-data" action="hr-staff-adjustment.php?page_mode=edit&type=edit" novalidate="novalidate">

                    	<div class="form-group">
                            <div class="col-sm-2 control-label">Mode</div>
                            <div class="col-sm-9">
                                <label><input type="radio" name="created_type" value="HR Admin" required class="click_to_show_name" /> HR Admin</label>
                                <label><input type="radio" name="created_type" value="Training" class="click_to_show_name" /> Training</label>
                                <label><input type="radio" name="created_type" value="Management" class="click_to_show_name" /> Management</label>
                            </div>
                        </div>
                        <div class="form-group display_none div_show_box">
                            <div class="col-sm-2 control-label">Name</div>
                            <div class="col-sm-9">
                                <input type="text" name="created_name" required class="form-control ui-search-input" value="<?= dataFilter($row_page['created_name']) ?>" placeholder="Name" />
                            </div>
                        </div>

                        <hr />

                        <div class="display_none div_show_box">
	                    	<div class="form-group">
	                            <div class="col-sm-2 control-label">To</div>
	                            <div class="col-sm-9">

									<div class="div_radio">
					                	<label><input type="radio" name="receiver_type"value="1" checked required /> Individual</label>&nbsp;&nbsp;&nbsp;
					                	<label><input type="radio" name="receiver_type" value="2" required /> </i> Department</label>
					                </div>

					                <div class="div_radio_select">
					                	<div class="stafflist">
					                		<div class="stafflist_select">
												<select name="receiver_to[]" id="selectuser" multiple class="chosen-select form-control tab_index ui-dropdown-select ui-search-input select2-basic-single">
													<?php
													if ( count($staff_list) > 0 ){
														foreach ( $staff_list as $k => $v ){
														echo '<option value="'.$k.'" '.( $k == $row_page['staff_id'] ? 'selected' : '').'>'.$v.'</option>';
														}
													}
													?>
												</select>
											</div>

											<div class="stafflist_check">
												<a class="btn btn-xs btn-danger" id="checkAll"><i class="fa fa-check-circle"></i> </i> <?= $lang['Check All']?></a>
												<a class="btn btn-xs btn-danger" id="unCheckAll"><i class="fa fa-times-circle"></i> </i> <?= $lang['Uncheck All']?></a>
											</div>
										</div>

										<div class="departmentlist">
											<div class="departmentlist_select">
												<select name="receiver_to_dept[]" id="selectdept" class="chosen-select form-control tab_index ui-dropdown-select ui-search-input select2-basic-single" multiple>
													<?php
													if ( count($department_list) > 0 ){
														foreach ( $department_list as $k => $v ){
															echo '<option value="'.$k.'" '.( in_array( $k, $receiver_depart ) ? 'selected' : '').'>'.$v.'</option>' ;
														}
													}
													?>
												</select>
											</div>
											<div class="stafflist_check">
												<a class="btn btn-xs btn-danger" id="checkAllDept"><i class="fa fa-check-circle"></i> <?= $lang['Check All']?></a>
												<a class="btn btn-xs btn-danger" id="unCheckAllDept"><i class="fa fa-times-circle"></i> <?= $lang['Uncheck All']?></a>
											</div>
										</div>
					                </div>

	                            </div>
	                        </div>

	                    	<!-- <div class="form-group">
	                            <div class="col-sm-2 control-label">Staff</div>
	                            <div class="col-sm-9">
	                                <select name="staff_id[]" id="staff_id" multiple class="chosen-select form-control tab_index ui-dropdown-select ui-search-input select2-basic-single">
										<?php
										// if ( count($staff_list) > 0 ){
										// 	foreach ( $staff_list as $k => $v ){
										// 		echo '<option value="'.$k.'" '.( $k == $row_page['staff_id'] ? 'selected' : '').'>'.$v.'</option>' ;
										// 	}
										// }
										?>
									</select>
	                            </div>
	                        </div> -->

	                    	<div class="form-group">
	                            <div class="col-sm-2 control-label">Mode</div>
	                            <div class="col-sm-9">
	                                <label><input type="radio" name="adjustment_type" value="plus" class="click_to_show_type" /> Plus</label>
	                                <label><input type="radio" name="adjustment_type" value="minus" class="click_to_show_type" /> Minus</label>
	                            </div>
	                        </div>

	                        <div class="form-group">
	                            <div class="col-sm-2 control-label">Type</div>
	                            <div class="col-sm-9">
	                               
	                               	<select name="adjustment_type_plus" style="display:none;" id="adjustment_type_plus" class="form-control ui-search-input" >
	                               		<option value="">Select</option>
		                            	<?php foreach ( $array_setting_adjustment['plus'] as $k => $v ){ ?>
				                        	<option value="<?= $v['adjustment_id'] ?>" ><?= $v['title'] ?></option>
				                        <?php } ?>
				                    </select>

				                    <select name="adjustment_type_minus" style="display:none;" id="adjustment_type_minus" class="form-control ui-search-input" >
	                               		<option value="">Select</option>
		                            	<?php foreach ( $array_setting_adjustment['minus'] as $k => $v ){ ?>
				                        	<option value="<?= $v['adjustment_id'] ?>" ><?= $v['title'] ?></option>
				                        <?php } ?>
				                    </select>

	                            </div>
	                        </div>

	                        <div class="form-group">
	                            <div class="col-sm-2 control-label">Remark</div>
	                            <div class="col-sm-9">
	                                <input type="text" name="remark" class="form-control ui-search-input" value="<?= dataFilter($row_page['remark']) ?>" placeholder="Remark" />
	                            </div>
	                        </div>
	                        <div class="form-group">
	                            <div class="col-sm-2 control-label">Point</div>
	                            <div class="col-sm-9">
	                                <input type="text" name="point" class="form-control ui-search-input" value="<?= dataFilter($row_page['point']) ?>" placeholder="Point" />
	                            </div>
	                        </div>
	                        <div class="form-group">
	                            <div class="col-sm-2 control-label"></div>
	                            <div class="col-sm-9">
	                                <button type="submit" class="btn btn-purple" style="float:right"><?= $lang['submit']?></button>
	                                <input type="hidden" name="hide" value="1">
	                                <input type="hidden" name="page_status" value="<?= $submit_type ?>">
	                            </div>
	                        </div>   
	                    </div>
                    </form>
                </div>
            </div>
		</div>
		</div>

		<?php
		
	break ;












	// edit adjustment
	case 'view' :

		// check permission
		if ( !permissionCheck($row_user, 'staff-adjustment-view') ){
			header('Location: index.php') ;
			exit ;
		}

		$mysqli_query      = "SELECT * FROM staff_adjustment a
						      WHERE a.deleted_at IS NULL AND a.adjustment_id = '".$adjustment_id."' " . str_replace( 'a.branch_id', 'a.created_branch_id', $user_branch_permission_sql_a ) ;
		$mysqli_adjustment = $mysqli->query( $mysqli_query." LIMIT 1" ) ;
		if ( $mysqli_adjustment->num_rows == 0 ){
			exit ;
		}
		$row_adjustment    = $mysqli_adjustment->fetch_assoc() ;



		// get adjustment title
		$adjustment_title   = '' ;
		$setting_adjustment = $mysqli->query( "SELECT b.title FROM setting_adjustment a
								   			   LEFT JOIN setting_adjustment_translation b ON ( a.adjustment_id = b.adjustment_id )
								   			   WHERE a.deleted_at IS NULL AND b.lang = 'en' AND a.adjustment_id = '".$row_adjustment['setting_adjustment_id']."' LIMIT 1" ) ;
		if ( $setting_adjustment->num_rows > 0 ){
			$row_setting_adjustment = $setting_adjustment->fetch_assoc() ;
			$adjustment_title 		= $row_setting_adjustment['title'] ;
		}



		// 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   	   = 'page_mode='.$page_mode.'&adjustment_id='.$adjustment_id.'search='.$search ;

		$mysqli_query      = "SELECT a.point, a.created_at, b.staff_name FROM staff_adjustment_point a
							  LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
						      WHERE a.deleted_at IS NULL AND a.adjustment_id = '".$adjustment_id."'" ;
		$mysqli_page       = $mysqli->query( $mysqli_query." ORDER BY a.point_id DESC LIMIT $start_from, " . LIMIT ) ;

		// load pagination
		$page_pagination   = nextPrevious($product_page, LIMIT, $search_url, $mysqli_query) ;
		
		// start header here
		include 'requires/page_header.php' ;
		include 'requires/page_top.php' ;

		?>
		<div class="warper container-fluid">   	
			<div class="page-header"><h1>Related Point Adjustment <small>List</small></h1></div>

			<div class="panel panel-default">
                <div class="panel-heading">Staff Point Adjustment</div>
                <div class="panel-body">
					<table cellpadding="0" cellspacing="0" border="0" class="responsive table table-striped table-bordered" id="basic-datatable">
						<thead>
							<tr>
								<th>Staff</th>
								<th>Point</th>
								<th>Created At</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="text-center">'.dataFilter($row_page['staff_name']).'</td>
										<td class="text-center">'.dataFilter($row_page['point']).'</td>
										<td class="text-center">'.resetDateTimeFormat($row_page['created_at']).'</td>
									</tr>';
								}
							}
							?>
						</tbody>
					</table>
					<?= $page_pagination['page_pagination'] ?>
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-heading">Point Adjustment Details</div>
                <div class="panel-body">
                	<form method="post" class="form-horizontal">
						<div class="form-group">
						    <div class="col-sm-2 control-label">Created Type</div>
						    <div class="col-sm-9">
						        <input type="text" name="created_type" required class="form-control ui-search-input" value="<?= dataFilter($row_adjustment['created_type']) ?>" placeholder="Created Type" disabled />
						    </div>
						</div>
						<div class="form-group">
						    <div class="col-sm-2 control-label">Created Name</div>
						    <div class="col-sm-9">
						        <input type="text" name="created_name" required class="form-control ui-search-input" value="<?= dataFilter($row_adjustment['created_name']) ?>" placeholder="Created Name" disabled />
						    </div>
						</div>

						<hr />

					    <div class="form-group">
					        <div class="col-sm-2 control-label">Type</div>
					        <div class="col-sm-9">
					            <input type="text" name="type" class="form-control ui-search-input" value="<?= ucwords($row_adjustment['adjustment_type']) ?>" placeholder="Type" disabled />
					        </div>
					    </div>
					    <div class="form-group">
					        <div class="col-sm-2 control-label">Type</div>
					        <div class="col-sm-9">
					            <input type="text" name="title" class="form-control ui-search-input" value="<?= ucwords($adjustment_title) ?>" placeholder="Title" disabled />
					        </div>
					    </div>
					    <div class="form-group">
					        <div class="col-sm-2 control-label">Remark</div>
					        <div class="col-sm-9">
					            <input type="text" name="remark" class="form-control ui-search-input" value="<?= dataFilter($row_adjustment['remark']) ?>" placeholder="Remark" disabled />
					        </div>
					    </div>
					    <div class="form-group">
					        <div class="col-sm-2 control-label">Point</div>
					        <div class="col-sm-9">
					            <input type="text" name="point" class="form-control ui-search-input" value="<?= dataFilter($row_adjustment['point']) ?>" placeholder="Point" disabled />
					        </div>
					    </div>
					</form>
				</div>
            </div>
		</div>

		<?php
		
	break ;





	case 'all' :
	default :
		

		// check permission
		if ( !permissionCheck($row_user, 'staff-adjustment-view') ){
			header('Location: index.php') ;
			exit ;
		}
		// query type
		$search_query = '' ;
		
		// search query
		if ($search  != ''){
			$search_query  .= " AND ( remark LIKE '%".$search."%' )" ;
		}
		$search_query  	   .= ( $get_user_tier['check'] ? " AND ( a.created_by_userid LIKE '%|" . implode( "|%' OR a.created_by_userid LIKE '%|", $get_user_tier['tiers'] ) . "|%' )" : "" ) ;
		
		// 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   = 'page_mode='.$page_mode.'&search='.$search ;

		$mysqli_query = "SELECT a.adjustment_id, a.adjustment_so, a.adjustment_type, a.point, a.remark, a.created_at, b.title FROM staff_adjustment a
						 LEFT JOIN setting_adjustment b ON ( a.setting_adjustment_id = b.adjustment_id )
						 WHERE a.deleted_at IS NULL " . str_replace( 'a.branch_id', 'a.created_branch_id', $user_branch_permission_sql_a ) . $search_query ;
		$mysqli_page  = $mysqli->query( $mysqli_query." ORDER BY a.adjustment_id DESC LIMIT $start_from, " . LIMIT ) ;

		// load pagination
		$page_pagination = nextPrevious($product_page, LIMIT, $search_url, $mysqli_query) ;
		
		// start header here
		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>Point Adjustment <small>List</small></h1>
					</div>
					<div class="pull-right col">
	            	<?php if ( permissionCheck($row_user, 'staff-adjustment-new') ){ ?>
		                <a href="hr-staff-adjustment.php?page_mode=new" class="btn" target="_blank" style="color:white;margin:5px;background-color: #5e5bd0;margin-top: 5px;"><?= $lang['add_new'] ?></a>
					<?php } ?>
					</div>
				</div>
			</div>

			<?php
            if ($_SESSION['system_result'] != ''){
                switch($_SESSION['system_result']){
                    case 'success-updated' :
                        echo '
                        <div class="result_success">
                            '.$lang['Thank you details has been updated'].'
                        </div>' ;	
                    break ;
                    default :
                        echo '
                        <div class="result_error">
                            '.$_SESSION['system_result'].'
                        </div>' ;
                }
                unset($_SESSION['system_result']) ;
            }
            ?>

			<div class="panel panel-default">
                <div class="panel-heading">Staff Point Adjustment</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['Action']?></th>
								<th>So</th>
								<th>Type</th>
								<th>Title</th>
								<th>Point</th>
								<th>Remark</th>
								<th>Created At</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">
											<a title="Edit Current Point Adjustment" href="hr-staff-adjustment.php?page_mode=view&adjustment_id='.$row_page['adjustment_id'].'" target="_blank"><i class="fa fa-eye"></i></a>
									 	</td>
										<td class="text-center">'.dataFilter($row_page['adjustment_so']).'</td>
										<td class="text-center">'.dataFilter($row_page['adjustment_type']).'</td>
										<td class="text-center">'.dataFilter($row_page['title']).'</td>
										<td class="text-center">'.dataFilter($row_page['point']).'</td>
										<td class="text-center">'.dataFilter($row_page['remark']).'</td>
										<td class="text-center">'.resetDateTimeFormat($row_page['created_at']).'</td>
									</tr>';
								}
							}
							?>
						</tbody>
					</table>
					<?= $page_pagination['page_pagination'] ?>
                </div>
            </div>
        </div>
		</div>
		<?php
	break ;



}
// footer
include 'requires/page_footer.php' ;
?>