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


$select = $mysqli->query( "SELECT task_id, task_so, task_type, created_by, assigned_by, date_start FROM task
						   WHERE deleted_at IS NULL AND status IN ( 'pending', 'assigned', 'resubmit', 'progress' )" ) ;
if ( $select->num_rows > 0 ){
	
	while ( $row     = $select->fetch_assoc() ){

		// '1time','daily','weekly','monthly','yearly'

		$task_so         = $row['task_so'] ;
		$title           = 'Task Reminder' ;
		$message         = 'Friendly reminder task ( '.$task_so.' ) haven\'t completed yet' ;

		$date_start      = strtotime( $row['date_start'] ) ;

		$boolean         = false ;
		switch ( $row['task_type'] ){
			case '1time'   :
			case 'daily'   :
				$boolean = true ;
			break ;
			case 'weekly'  :
				if ( date( 'w', $date_start ) == date( 'w', time() ) ){
					$boolean = true ;
				}
			break ;
			case 'monthly' :

				if ( date( 'd', $date_start ) >= date( 't', time() ){
					$boolean = true ;
				}else{
					if ( date( 'd', $date_start ) == date( 'd', time() ) ){
						$boolean = true ;
					}
				}

			break ;
			case 'yearly'  :
				if ( date( 'm-01', $date_start ) == date( 'm-d', time() ) ){
					$boolean = true ;
				}
			break ;
		}

		if ( $boolean ){
			$staffs      = [] ;
			$staffs[]    = $row['created_by'] ;
			$staffs[]    = $row['assigned_by'] ;

			$select_join = $mysqli->query( "SELECT staff_id FROM task_joinstaff WHERE task_id = '".$row['task_id']."'" ) ;
			if ( $select_join->num_rows > 0 ){
				while ( $row_join = $select_join->fetch_assoc() ){
					$staffs[] = $row_join['staff_id'] ;
				}
			}

			foreach ( $staffs as $k => $staff ){
				pushToUserCron( 'task', $row['task_id'], $staff, $title, $message ) ;
			}
		}

	}

}



?>