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

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

	$is_show  			 = 'no' ;
	$popup  			 = '' ;
	$department_list     = [] ;
	$departments         = [] ;
	$position 	         = '' ;
	$section   	         = '' ;
	$announcements 	     = [] ;
	$services  		     = [ 'hr' => [], 'service' => [] ] ;
	$count_inbox   		 = 0 ;

	$staff_settings      = $staff_info['staff_settings'] ;



	// get branch name 
	$branch 			 = '' ;
	$select_branch       = $mysqli->query( "SELECT branch_name FROM branch 
											WHERE deleted_at IS NULL AND branch_id = '".$array['branch_id']."' LIMIT 1" ) ;
	if ( $select_branch->num_rows > 0 ){
		$row_branch      = $select_branch->fetch_assoc() ;
		$branch          = dataFilter( $row_branch['branch_name'] ) ;
	}



	// get popup
	$select  			 = $mysqli->query( "SELECT * FROM setting_popup
											WHERE branch LIKE '%/".$array['branch_id']."/%' AND setting_popup_id = '1' AND file != '' AND status = 'active' LIMIT 1" ) ;
	if ( $select->num_rows > 0 ){
		$row       		 = $select->fetch_assoc() ;
		$popup 			 = PATH.'uploads/Pop-up/b/'.dataFilter($row['file']) ;
	}





	// select all department
	$select  		       = $mysqli->query( "SELECT a.department_id, a.department_code, 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() ){
			$department_list[$row['department_id']] = dataFilter( $row['department_desc'] ) ;
		}
	}




	// select staff department
	$select  		         = $mysqli->query( "SELECT a.department_id FROM staff_department a
										        WHERE a.deleted_at IS NULL AND a.staff_id = '".$staff_info['staff_id']."'" ) ;
	if ( $select->num_rows   > 0 ){
		while ( $row         = $select->fetch_assoc() ){
			$departments[]   = $department_list[ $row['department_id'] ] ;
		}
	}
	$departments             = implode( ', ', $departments ) ;




	// select staff position
	$select  		         = $mysqli->query( "SELECT 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 = '".$array['lang']."' AND a.job_position_id = '".$staff_info['job_position_id']."'" ) ;
	if ( $select->num_rows   > 0 ){
		$row   		         = $select->fetch_assoc() ;
		$position            = dataFilter( $row['job_position_desc'] ) ;
	}



	// select staff section
	$select  		         = $mysqli->query( "SELECT 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 = '".$array['lang']."' AND a.job_section_id = '".$staff_info['job_section_id']."'" ) ;
	if ( $select->num_rows   > 0 ){
		$row   		         = $select->fetch_assoc() ;
		$section             = dataFilter( $row['job_section_desc'] ) ;
		$section 		 	 = ( $section != 'NONE' ? $section : '' ) ;
	}




	// select announcement
	$select                  = $mysqli->query( "SELECT a.announcement_id, a.is_hide, a.file, b.title FROM announcement a
								   		  		LEFT JOIN announcement_translation b ON ( a.announcement_id = b.announcement_id )
						                        WHERE a.deleted_at IS NULL AND a.status = 'active' AND b.lang = '".$array['lang']."' AND a.branch LIKE '%".$array['branch_id']."%' AND a.file != ''
						                        ORDER BY a.announcement_id DESC
						                        LIMIT 8" ) ;
	if ( $select->num_rows   > 0 ){
		while ( $row         = $select->fetch_assoc() ){
			$row['id']       = dataFilter( $row['announcement_id'] ) ;
			$row['title']    = ( $row['is_hide'] == 'no' ? dataFilter( $row['title'] ) : '' ) ;
			$row['uri']      = ( $row['file'] != '' ? PATH.'uploads/Announcement/b/'.$row['file'] : '' ) ;
			$row['url']      = ( $row['file'] != '' ? PATH.'uploads/Announcement/b/'.$row['file'] : '' ) ;
			$announcements[] = $row ;
		}
	}












	// count

	// count adjustment
	$count_adjustment      = 0 ;
	$query                 = "SELECT COUNT( adjustment_id ) as total FROM staff_adjustment
                              WHERE deleted_at IS NULL AND ( created_branch_id = '".$array['branch_id']."' AND created_by = '".$staff_info['staff_id']."' ) " ;
    $mysqli_query          = $mysqli->query( $query ) ;
    if ( $mysqli_query->num_rows > 0 ){
    	$row 		       = $mysqli_query->fetch_assoc() ;
    	$count_adjustment  = $row['total'] ;
    }

	// count task
	$count_task 	       = 0 ;
	$query                 = "SELECT COUNT( a.task_id ) as total FROM task a
                              WHERE a.deleted_at IS NULL AND ( a.created_branch_id = '".$array['branch_id']."' AND a.created_by = '".$staff_info['staff_id']."' OR a.assigned_by = '".$staff_info['staff_id']."' OR EXISTS ( SELECT b.staff_id FROM task_joinstaff b WHERE a.task_id = b.task_id AND b.staff_id = '".$staff_info['staff_id']."' LIMIT 1 ) ) AND status IN ( 'pending', 'assigned', 'resubmit', 'progress' ) " ;
    $mysqli_query          = $mysqli->query( $query ) ;
    if ( $mysqli_query->num_rows > 0 ){
    	$row 		       = $mysqli_query->fetch_assoc() ;
    	$count_task        = $row['total'] ;
    }

	// count redeem
    $count_redeem          = 0 ;
    $query                 = "SELECT COUNT( redeem_id ) as total FROM staff_redeem
                              WHERE deleted_at IS NULL AND branch_id = '".$array['branch_id']."' AND staff_id = '".$staff_info['staff_id']."' AND status = 'pending'" ;
    $mysqli_query          = $mysqli->query( $query ) ;

    if ( $mysqli_query->num_rows > 0 ){
        $row               = $mysqli_query->fetch_assoc() ;
        $count_redeem      = $row['total'] ;
    }

	// count catalog
	$count_catalog         = 0 ;
	$query                 = "SELECT COUNT( catalog_id ) as total FROM catalog
                              WHERE deleted_at IS NULL" ;
    $mysqli_query          = $mysqli->query( $query ) ;
    if ( $mysqli_query->num_rows > 0 ){
    	$row 		       = $mysqli_query->fetch_assoc() ;
    	$count_catalog     = $row['total'] ;
    }

	// count handbook
	$count_handbook        = 0 ;
	$query                 = "SELECT COUNT( handbook_id ) as total FROM handbook
                              WHERE deleted_at IS NULL AND ( ( receiver_type IN ( '0' ) ) OR ( receiver_type IN ( '1', '2' ) AND staff_id LIKE '%/".$staff_info['staff_id']."/%' ) )" ;
    $mysqli_query          = $mysqli->query( $query ) ;
    if ( $mysqli_query->num_rows > 0 ){
    	$row 		       = $mysqli_query->fetch_assoc() ;
    	$count_handbook    = $row['total'] ;
    }

    // count suggestion, grievance, request
    // count suggestion
	$count_suggestion      = 0 ;
	$query                 = "SELECT COUNT( suggestion_id ) as total FROM suggestion
                              WHERE deleted_at IS NULL AND branch_id = '".$array['branch_id']."' AND staff_id = '".$staff_info['staff_id']."' AND status = 'pending'" ;
    $mysqli_query          = $mysqli->query( $query ) ;
    if ( $mysqli_query->num_rows > 0 ){
    	$row 		       = $mysqli_query->fetch_assoc() ;
    	$count_suggestion  = $row['total'] ;
    }

    // count request
    $count_request         = 0 ;
    $query                 = "SELECT COUNT( request_id ) as total FROM request
                              WHERE deleted_at IS NULL AND branch_id = '".$array['branch_id']."' AND staff_id = '".$staff_info['staff_id']."' AND status = 'pending'" ;
    $mysqli_query          = $mysqli->query( $query ) ;
    if ( $mysqli_query->num_rows > 0 ){
        $row               = $mysqli_query->fetch_assoc() ;
        $count_request    += $row['total'] ;
    }

    // count grievance
	$count_grievance       = 0 ;
    $query                 = "SELECT COUNT( grievance_id ) as total FROM grievance
                              WHERE deleted_at IS NULL AND branch_id = '".$array['branch_id']."' AND staff_id = '".$staff_info['staff_id']."' AND status = 'pending'" ;
    $mysqli_query          = $mysqli->query( $query ) ;
    if ( $mysqli_query->num_rows > 0 ){
    	$row 		       = $mysqli_query->fetch_assoc() ;
    	$count_grievance  += $row['total'] ;
    }

    // count training
	$count_training        = 0 ;
	$query                 = "SELECT COUNT( training_id ) as total FROM staff_training
                              WHERE deleted_at IS NULL AND branch_id = '".$array['branch_id']."' AND staff_id = '".$staff_info['staff_id']."' AND status = 'pending'" ;
    $mysqli_query          = $mysqli->query( $query ) ;

    if ( $mysqli_query->num_rows > 0 ){
    	$row 		       = $mysqli_query->fetch_assoc() ;
    	$count_training    = $row['total'] ;
    }

    // count association
	$count_association     = 0 ;
	$query                 = "SELECT COUNT( association_id ) as total FROM staff_association
                              WHERE deleted_at IS NULL AND branch_id = '".$array['branch_id']."' AND staff_id = '".$staff_info['staff_id']."' AND status = 'pending'" ;
    $mysqli_query          = $mysqli->query( $query ) ;
    if ( $mysqli_query->num_rows > 0 ){
    	$row 		       = $mysqli_query->fetch_assoc() ;
    	$count_association = $row['total'] ;
    }

    // count form
	$count_form     	   = 0 ;
	$query                 = "SELECT COUNT( form_id ) as total FROM form
                              WHERE deleted_at IS NULL AND branch LIKE '%/".$array['branch_id']."/%' AND ( ( receiver_type IN ( '0' ) ) OR ( receiver_type IN ( '1', '2' ) AND staff_id LIKE '%/".$staff_info['staff_id']."/%' ) )" ;
    $mysqli_query          = $mysqli->query( $query ) ;
    if ( $mysqli_query->num_rows > 0 ){
    	$row 		       = $mysqli_query->fetch_assoc() ;
    	$count_form        = $row['total'] ;
    }
    
    // count recruitment
	$count_recruitment     = 0 ;
	$query                 = "SELECT COUNT( employment_id ) as total FROM staff_employment
                              WHERE employment_trash = '0' AND employment_status = 'Processing' AND employment_branch = '".$array['branch_id']."' AND employment_incharge_staff_id = '".$staff_info['staff_id']."'" ;
    $mysqli_query          = $mysqli->query( $query ) ;
    if ( $mysqli_query->num_rows > 0 ){
    	$row 		       = $mysqli_query->fetch_assoc() ;
    	$count_recruitment = $row['total'] ;
    }


	// select service
	$select                = $mysqli->query( "SELECT a.type, a.module, a.colour, a.file, b.title FROM app_service a 
                          					  LEFT JOIN app_service_translation b ON ( a.service_id = b.service_id )
						                      WHERE a.deleted_at IS NULL AND b.lang = '".$array['lang']."' AND a.on_off = 'on'
						                      ORDER BY a.sortable" ) ;
	if ( $select->num_rows > 0 ){
		while ( $row       = $select->fetch_assoc() ){
			$boolean_show  = true ;

			$count 		   = 0 ;
			switch ( $row['module'] ){
				case 'Punch'         : 
					if ( $staff_settings['punch'] != 'yes' ){
						$boolean_show = false ;
					}
				break ;
				case 'Adjustment'    :
					switch ( $staff_info['staff_tier_is_adjustment'] ){
						case 'yes'   :
						case 'fixed' :
							$count  = $count_adjustment ;
						break ;
						default 	 :
							$boolean_show = false ;
					}
				break ;
				case 'Task'          : $count   = $count_task ; 		break ;
				case 'Redeem'        : $count   = $count_redeem ; 		break ;
				case 'Catalog'       : $count   = $count_catalog ; 		break ;
				case 'Handbook'    		    : 
				case 'HandbookMainCategory' : 
					$count   = $count_handbook ; 		
				break ;
				case 'LocalInbox'    : $count   = ( $count_suggestion + $count_grievance + $count_request ) ; break ;
				case 'Training'      : $count   = $count_training ; 	break ;
				case 'Association'   : $count   = $count_association ; 	break ;
				case 'Hr' 		     : $is_show = 'yes' ; 				break ;
				case 'HrBranch'      : $is_show = 'yes' ; 				break ;
				case 'HrManual'      : $is_show = 'manual' ; 			break ;
				case 'Recruitment' 	 :
					if ( $staff_info['staff_settings']['checkrecruitment'] != 'yes' ){
						$boolean_show = false ;
					}else{
						$count   	  = $count_recruitment ;
					}
				break ;
				case 'Visitor' 		 :
					if ( $staff_info['staff_settings']['approvevisitation'] != 'yes' ){
						$boolean_show = false ;
					}
				break ;
				case 'Form' 		 : $count   = $count_form ; 	    break ;
			}
			$row['title']   = dataFilter( $row['title'] ) ;
			$row['count']   = ( $count > 999 ? '999+' : $count ) ;
			$row['file']    = ( $row['file'] != '' ? PATH.'uploads/AppService/'.$row['file'] : '' ) ;
			$service_type   = $row['type'] ;
			unset( $row['type'] ) ;

			if ( $boolean_show ){
				$services[$service_type][] = $row ;
			}
		}
	}



	// select attendance
	$attendances        = [ 'presents' => 0, 'late' => 0, 'absent' => 0 ] ;





    $mysqli_query      = $mysqli->query( "SELECT COUNT( a.view_id ) as total FROM staff_inbox_view a
                          				  WHERE a.deleted_at IS NULL AND a.staff_id = '".$staff_info['staff_id']."' AND a.is_read = '0'" ) ;

    if ( $mysqli_query->num_rows > 0 ){
    	$row 		   = $mysqli_query->fetch_assoc() ;
    	$count_inbox   = $row['total'] ;
    }


	$data			        = [
		'is_show' 		=> $is_show,
		'branch'    	=> $branch,
		'position'      => $position,
		'section'       => $section,
		'popup' 		=> $popup,
		'departments'   => $departments,
		'announcements' => $announcements,
		'services'      => $services,
		'attendances'   => $attendances,
		'count_inbox'   => $count_inbox
	] ;
}

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