<?php
$must_login             = true ;
$require_path           = '../../../' ;
$require_sub            = '../../' ;
require( $require_sub.'header.php' ) ;

if ( $boolean_login ){
	$status   		    = '200' ;

	$adjustments 		= [ 'plus' => [], 'minus' => [] ] ;
	$staffs             = [] ;
	$departments        = [] ;
	$staff_departments  = [] ;
	$all_tier 			= getAllTier( $array['lang'] ) ;



	// select all adjustment
	$query_setting      = " AND set_tier LIKE '%|".$staff_info['staff_tier']."|%'" ;
	switch ( $staff_info['staff_tier_is_adjustment'] ){
		case 'yes' :
			// do nothing
		break ;
		default :
        	$query_setting = " AND a.adjustment_mode = 'fixed'" ;
	}

	$select  		    = $mysqli->query( "SELECT a.adjustment_id, a.adjustment_type, a.adjustment_mode, a.point, 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 = '".$array['lang']."' AND a.adjustment_site = 'frontend' ".$query_setting." ORDER BY a.sortable" ) ;
	if ( $select->num_rows > 0 ){
		while ( $row    = $select->fetch_assoc() ){
			$adjustments[$row['adjustment_type']][]  = [
				'id'    => $row['adjustment_id'],
				'mode'  => $row['adjustment_mode'],
				'title' => dataFilter( $row['title'] ),
				'point' => $row['point']
			] ;
		}
	}

	// select all staff
	$staff_tier 	    = [] ;
	$staff_tier 	    = getRelatedTierID( 'no', $staff_info['staff_tier_level'] ) ;

	$where_staff 		= '' ;
	$where_branch	    = '' ;
	if ( $staff_info['staff_settings']['adjustmentbranch'] != 'yes' ){
		$where_staff   .= " AND a.branch_id = '".$array['branch_id']."'" ;
		$where_branch  .= " AND a.branch_id = '".$array['branch_id']."'" ;
	}

	// select all staff
	$select  		    = $mysqli->query( "SELECT a.staff_id, a.branch_id, a.staff_idno, a.staff_name, a.staff_shortname, a.staff_tier FROM staff a
										   WHERE a.deleted_at IS NULL AND a.staff_id != '".$staff_info['staff_id']."' AND a.staff_tier IN ( '".implode("', '", $staff_tier)."' ) AND ( a.staff_date_resigned >= '".date("Y-m-d",time())."' OR a.staff_date_resigned = '0000-00-00' OR a.staff_date_resigned IS NULL ) " . $where_staff ) ;

	if ( $select->num_rows > 0 ){
		while ( $row        = $select->fetch_assoc() ){
			$get_staff_tier = $all_tier[$row['staff_tier']] ;

			$staffs[$row['staff_id']]  = [
				'id'        => $row['staff_id'],
				'branch_id' => $row['branch_id'],
				'title'     => $row['staff_shortname'] . ' ('.$row['staff_idno'].' / '.strtoupper( $get_staff_tier['title'] ).')',
				'name'      => $row['staff_name'],
				'tier'      => $get_staff_tier['level']
			] ;
		}
	}



	// select all department
	$select  		    = $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 = '".$array['lang']."'" ) ;
	if ( $select->num_rows > 0 ){
		while ( $row    = $select->fetch_assoc() ){
			$departments[] = $row ;
		}
	}




	// select all branch
	$branches  			= [] ;
	$select_branch  	= $mysqli->query( "SELECT branch_id, branch_name FROM branch a
										   WHERE a.deleted_at IS NULL" . $where_branch ) ;
	if ( $select_branch->num_rows > 0 ){
		while ( $row_branch = $select_branch->fetch_assoc() ){
			$branches[] = [
				'id' 	=> $row_branch['branch_id'],
				'title' => dataFilter( $row_branch['branch_name'] )
			] ;
		}
	}



	// select all staff department
	$select  		    = $mysqli->query( "SELECT a.staff_id, a.department_id FROM staff_department a
										   WHERE a.deleted_at IS NULL" ) ;
	if ( $select->num_rows > 0 ){
		while ( $row    = $select->fetch_assoc() ){
			if ( $staffs[ $row['staff_id'] ] != null && $staffs[ $row['staff_id'] ] != undefined ){
				$staff      = $staffs[ $row['staff_id'] ] ;
				$staff_departments[$staff['branch_id']][$row['department_id']][] = $staff ;
			}
		}
	}


	$reset_branches  = [] ;
	foreach ( $branches as $kbranch => $vbranch ){
		// reset
		$reset_departments   = [] ;
		$reset_departments[] = [
			'id'        => '0',
			'title'     => 'Cross Department',
			'staffs'    => []
		] ;
		foreach ( $departments as $k => $v ){
			if ( $staff_departments[$vbranch['id']][$v['department_id']] != null && $staff_departments[$vbranch['id']][$v['department_id']] != undefined ){
				$reset_departments[] = [
					'id'        => $v['department_id'],
					'title'     => $v['department_desc'],
					'staffs'    => $staff_departments[$vbranch['id']][$v['department_id']]
				] ;
			}
		}

		$reset_branches[] = [
			'id' 		  => $vbranch['id'],
			'title' 	  => $vbranch['title'],
			'departments' => $reset_departments
		] ;
	}



	$data			    = [
		'adjustments'    => $adjustments,
		'default_branch' => $array['branch_id'],
		'branches' 		 => $reset_branches
	] ;
}

require( $require_sub.'footer.php' ) ;
?>