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

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

	$staffs             = [] ;
	$departments        = [] ;
	$staff_departments  = [] ;
	$difficultys        = [] ;
	$all_tier 			= getAllTier( $array['lang'] ) ;



	// select all staff
	$select  		    = $mysqli->query( "SELECT a.staff_id, a.staff_idno, a.staff_name, a.staff_shortname, a.staff_tier FROM staff a
										   WHERE a.deleted_at IS NULL AND a.branch_id = '".$array['branch_id']."' 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 ) " ) ;

	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'],
				'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 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[$row['department_id']][] = $staff ;
			}
		}
	}





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







	$task_types 	    = [
		[
			'id' 	    => '1time',
			'title' 	=> 'One Time Only'
		],
		[
			'id' 		=> 'daily',
			'title' 	=> 'Daily Update'
		],
		[
			'id' 		=> 'weekly',
			'title' 	=> 'Weekly Update'
		],
		[
			'id' 		=> 'monthly',
			'title' 	=> 'Monthly Update'
		],
		[
			'id' 		=> 'yearly',
			'title' 	=> 'Yearly Update'
		]
	] ;
	



	// select all department
	$select  		    = $mysqli->query( "SELECT a.code, b.title FROM setting_difficulty a
						   				   LEFT JOIN setting_difficulty_translation b ON ( a.difficulty_id = b.difficulty_id )
					  					   WHERE a.deleted_at IS NULL AND b.lang = '".$array['lang']."' AND set_tier LIKE '%|".$staff_info['staff_tier']."|%'
					  					   ORDER BY a.sortable" ) ;
	if ( $select->num_rows > 0 ){
		while ( $row    = $select->fetch_assoc() ){
			$difficultys[] = [
				'id' 		=> $row['code'],
				'title' 	=> $row['title'],
			] ;
		}
	}



	$data			    = [
		'task_types'    => $task_types,
		'departments'   => $reset_departments,
		'difficultys'   => $difficultys
	] ;
}

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