<?php

// default config setting
$boolean_ssl_lock = true ;

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



// get all branch
$branch_all     = [] ;
$get_branch = $mysqli->query( "SELECT branch_id, branch_name FROM branch
                               WHERE deleted_at IS NULL " ) ;
if ( $get_branch->num_rows > 0 ){
    while ( $row_branch = $get_branch->fetch_assoc() ){
        $branch_all[$row_branch['branch_id']] = $row_branch['branch_name'] ;
    }
}




$month_3_ago      = date( "Y-m", strtotime( '-2 months' ) ) ;
$month_2_ago      = date( "Y-m", strtotime( '-1 months' ) ) ;
$month_1_current  = date( "Y-m", time() ) ;

$mysqli_staff     = $mysqli->query("SELECT staff_id, staff_name, staff_idno, staff_date_resigned, branch_id FROM staff
				    				WHERE deleted_at IS NULL AND staff_idno IS NOT NULL AND ( staff_date_resigned LIKE '%".$month_3_ago."%' OR staff_date_resigned LIKE '%".$month_2_ago."%' OR staff_date_resigned LIKE '%".$month_1_current."%' )
				    				ORDER BY staff_date_resigned ASC") ;

$staff_list 	  = [] ;
if ( $mysqli_staff->num_rows > 0 ){
	while ( $row_staff = $mysqli_staff->fetch_assoc() ){
		
		$reset_month = date( 'Y-m', strtotime( $row_staff['staff_date_resigned'] ) ) ;
		$staff_list[$row_staff['branch_id']][$reset_month][] = $row_staff ;

	}
}

$html 			  = '' ;
if ( count($staff_list) ){

	foreach ( $staff_list as $kbranch => $vbranch ){

		$html    .= '
		<div style="font-size:20px;"><b>'.$branch_all[$kbranch].'</b></div>' ;

		foreach ( $vbranch as $kdate => $vdate ){

			$count     = 0 ;
			$content   = '' ; 


			$html     .= '
			<div style="margin-top:10px; margin-bottom:3px;"><b>'.date( 'M Y', strtotime( $kdate.'-01' ) ).'</b></div>' ;

			foreach ( $vdate as $kstaff => $vstaff ){

				$count++ ;
				$staff_idno = ucwords( dataFilter($vstaff['staff_idno']) ) ;

				$content .= '
				<tr>
					<td>'.$count.'</td>
					<td>'.dataFilter($vstaff['staff_idno']).'</td>
					<td>'.dataFilter($vstaff['staff_name']).'</td>
					<td>'.dataFilter($vstaff['staff_date_resigned']).'</td>
				</tr>' ;

			}

			$html        .= '
			<table border="1" cellpadding="5" cellspacing="0" width="800px" style="font-size:13px;">
				<thead>
					<tr>
						<th width="50" style="text-align:left;">No.</th>
						<th width="100" style="text-align:left;">ID No.</th>
						<th style="text-align:left;">Staff Name</th>
						<th width="100" style="text-align:left;">Resigned Date</th>
					</tr>
				</thead>
				<tbody>
					'.$content.'
				</tbody>
			</table>' ;

		}

		$html       .= '
		<br />
		<br />
		<br />' ;
		
	}


	$title = 'Staff Resignation Within 3 Month' ;
	foreach ( $EMAILCRON as $vv ){
		sendEmail( $vv, EMAILNOREPLY, $title, $html ) ;
	}
}

?>