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

$page 					= escapeString($_GET['page']) ;
$branch_id 				= escapeString($_GET['branch']) ;
$sign 				    = escapeString($_GET['sign']) ;
$branch_name 			= '' ;
$weekdays 			 	= [] ;
$times 			  		= [] ;

if ( md5($page.$branch_id.APIKEY) != $sign ){
	echo '<script>alert("Sorry, you don\'t have permission to view the page");</script>' ;
	exit ;
}

$mysqli_page 			= $mysqli->query( "SELECT * FROM staff_employment 
										   WHERE employment_id = '".$page."' AND employment_trash = '0' LIMIT 1" ) ;
if ( $mysqli_page->num_rows == 0 ){
	echo '<script>alert("Sorry, you don\'t have permission to view the page");</script>' ;
	exit ;
}
$row_page 		 		= $mysqli_page->fetch_assoc() ;


$branch_hr_email        = '' ;
$branch_hr_cc           = [] ;
$branch_email_footer    = '' ;
$mysqli_branch 			= $mysqli->query( "SELECT * FROM branch 
										   WHERE deleted_at IS NULL AND branch_id = '".$row_page['employment_branch']."' LIMIT 1" ) ;
if ( $mysqli_branch->num_rows == 0 ){
	echo '<script>alert("Sorry, you don\'t have permission to view the page");</script>' ;
	exit ;
}
$row_branch 			= $mysqli_branch->fetch_assoc() ;
$branch_name 			= dataFilter( $row_branch['branch_name'] ) ; 
$branch_hr_email        = dataFilter( $row_branch['branch_hr_email'] ) ;
$branch_hr_cc           = explodeToArray( $row_branch['branch_hr_cc'] ) ;
$branch_email_footer    = entityDecode( dataFilter( $row_branch['branch_email_footer'] ) ) ;

$mysqli_schedule_date   = $mysqli->query("SELECT weekdays FROM setting_employment_schedule
										  WHERE deleted_at IS NULL LIMIT 1") ;
if ( $mysqli_schedule_date->num_rows == 0 ){
	echo '<script>alert("Sorry, you don\'t have permission to view the page");</script>' ;
	exit ;
}
$row_schedule_date      = $mysqli_schedule_date->fetch_assoc() ;
$weekdays 		        = json_decode( $row_schedule_date['weekdays'] ) ;						  

// get position
$mysqli_position 		= $mysqli->query("SELECT a.job_position_id, 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 = 'en' AND a.job_position_id = '".$row_page['employment_position']."' LIMIT 1") ;
$job_position_desc 		= '' ;
if ( $mysqli_position->num_rows > 0 ){
	$row_position 		= $mysqli_position->fetch_assoc() ;
	$job_position_desc 	= $row_position['job_position_desc'] ;
}

// page header
$letter_head 			= getOwnerCompanyLetterHead($branch_id) ;
$header 				= $letter_head['header'] ;

if ( $_POST['hidden']  != '' ){
	$employment_interview_date 	= escapeString($_POST['employment_interview_date']) ;
	$is_reschedule 				= escapeString($_POST['is_reschedule']) ;
	$error 						= 'Selected date was select by other candidate' ;

	$select_check_date  = $mysqli->query( "SELECT * FROM staff_employment
										   WHERE employment_id != '".$page."' AND employment_trash = '0' AND ( employment_interview_date = '".$employment_interview_date."' OR employment_r_candidate = '".$employment_interview_date."' ) LIMIT 1" ) ;

	if ( $select_check_date->num_rows == 0 ){
		$error 			= 'Cannot set schedule date' ;

		$is_sendemail   = '' ;
		switch ( $is_reschedule ){
			case 'no'  :
				$error  = 'Failed to update' ;

				if ( $mysqli->query( "UPDATE staff_employment SET 
									  employment_status 			= 'Processing Interview Slot', 
									  employment_interview_date 	= '".$employment_interview_date."'
									  WHERE employment_id 			= '".$page."'" ) ){
					$descrition 	= 'Candidate schedule interview date to ' . $employment_interview_date . '.' ;
					$mysqli->query( "INSERT INTO system_log_employment (log_table, log_action, log_page_id, log_page_name, log_user_id, log_description, log_record, log_date) VALUES
									( 'employment', 'update-status', '200', 'AF-".$page."', '', '".$descrition."', '', NOW())" ) ;

					$is_sendemail   = 'selected' ;

					$error 			= 'Success' ;
				}

			break ;
			case 'yes' :
				$error  = 'Failed to update' ;

				if ( $mysqli->query( "UPDATE staff_employment SET 
									  employment_status 			= 'Reschedule', 
									  employment_r_interview_date 	= '".$employment_interview_date."', 
									  employment_r_candidate 		= '1' 
									  WHERE employment_id 			= '".$page."'" ) ){
					$descrition 	= 'Candidate reschedule interview date from ' . $row_page['employment_interview_date'] . ' to ' . $employment_interview_date ;
					$mysqli->query( "INSERT INTO system_log_employment (log_table, log_action, log_page_id, log_page_name, log_user_id, log_description, log_record, log_date) VALUES
									( 'employment', 'update-status', '200', 'AF-".$page."', '', '".$descrition."', '', NOW())" ) ;

					$is_sendemail   = 'reschedule' ;

					$error 			= 'Success' ;
				}

			break ;
		}

		if ( $is_sendemail ){
            $mailer->from     = $branch_hr_email ;
            $mailer->fromname = COMPANY ;
            $mailer->to       = [ $branch_hr_email ] ;
            if ( count($branch_hr_cc) > 0 ){
                $mailer->cc   = $branch_hr_cc ;
            }
            $mailer->subject  = 'Reminder for Recruitment' ;
            $mailer->body     = 'Dear HR, '.ucwords($row_page['employment_name']).' has '.$is_sendemail.' interview slot for '.$job_position_desc.'. Please log in to review.
                                 <br /><br />
                                 * This is an auto-generated message, please do not reply.' ;
            $mailer->send() ;
		}

	}

	$_SESSION['system_error'] = $error ;
	header( "Refresh:0;" ) ;
	exit ;
}
?>

<!DOCTYPE html>
<html lang="en" class="">
<head>
	<meta charset="utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<meta name="viewport" content="width=device-width, initial-scale=1" />
	<title>Schedule Interview Date- <?= COMPANY ?></title>
	<meta name="description" content="" />
	<meta name="robots" content="noindex,nofollow" />
	<link type="image/ico" rel="shortcut icon" href="images/favicon.ico?v=1" />
	<link rel="stylesheet" href="css/css_employment.css" />
</head>
<body>

<?php
$is_allow  = false ;
switch ( $row_page['employment_status'] ){
	case 'Processing Confirmed'      :
	case 'Processing Interview Slot' :
	case 'Interview' 			     :
	case 'Reschedule' 			     :
		$is_allow    	   			   = true ;
		$is_reschedule 	   			   = false ;
		$title  		   			   = 'SCHEDULE' ;
		$desc_date 		   			   = 'Schedule Date' ;
		$employment_interview_date 	   = '' ;

		if ( $row_page['employment_status'] == 'Processing Interview Slot' ){
			$is_reschedule 			   = true ;
		}else{
			if ( $row_page['employment_interview_date'] != '0000-00-00 00:00:00' ){
				$is_reschedule 			   = true ;
				$title  	   			   = 'RESCHEDULE' ;
				$desc_date 	   			   = 'Reschedule Date' ;
				$employment_interview_date = ( $row_page['employment_r_interview_date'] != '0000-00-00 00:00:00' ? $row_page['employment_r_interview_date'] : '' ) ;
			}
		}

		?>
		<div style="max-width: 960px ; margin: auto; padding:10px;">
			<form method="POST" enctype="multipart/form-data">
				<table border="0" cellpadding="0" cellspacing="0" class="employment_table" style="max-width:960px; width:100%; padding:0; margin:0 auto;">
		        	<tr>
		            	<td colspan="4">
		                	<?= str_replace( 'width:960px', 'max-width:960px;', $letter_head['header'] ) ?>
		                </td>
		            </tr>
		            <tr><td colspan="4">&nbsp;</td></tr>
		            <tr>
		                <td colspan="4">
		                    <table style="max-width:960px; width:100%;" border="0" cellpadding="0" cellspacing="0">
		                        <tr>
		                            <td style="max-width:960px; width:100%;font-size:20px; padding:2px 0; text-align:center; border:1px solid #000;">
		                                <strong><?= $title ?> INTERVIEW DATE</strong>
		                            </td>
		                        </tr>
		                    </table>
		                </td>
		            </tr>
		            <?php if ( $_SESSION['system_error'] != '' ){ ?>
			            <tr><td colspan="4">&nbsp;</td></tr>
						<tr>
			                <td colspan="4">
			                    <table style="max-width:960px; width:100%;" border="0" cellpadding="0" cellspacing="0">
			                        <tr>
			                        	<?php if ( $_SESSION['system_error'] == 'Success' ){ ?>
				                            <td style="max-width:960px; width:100%;font-size:16px; padding:2px; text-align:left; border:1px solid #000; background: green;color:white">
				                                <strong>Your application to schedule interview date was submitted.</strong>
				                            </td>
			                        	<?php }else{ ?>
				                            <td style="max-width:960px; width:100%;font-size:16px; padding:2px; text-align:left; border:1px solid #000; background: #f00;color:white">
				                                <strong><?= $_SESSION['system_error'] ?></strong>
				                            </td>
			                        	<?php } ?>
			                        </tr>
			                    </table>
			                </td>
			            </tr>
		            <?php unset( $_SESSION['system_error'] ) ; } ?>
		            <tr><td colspan="4">&nbsp;</td></tr>
		            <tr><td colspan="4">&nbsp;</td></tr>
	            	<tr>
	                	<td colspan="2" style="width:20%; font-weight:bold; font-size:16px;text-transform: uppercase;">Name :
	                	</td>
	                	<td colspan="2" style="font-weight:bold; font-size:16px;text-transform: uppercase;"><?= $row_page['employment_name'] ?>
	                	</td>
	                </tr>
	                <tr><td colspan="4">&nbsp;</td></tr>
	                <tr>
	                	<td colspan="2" style="width:20%; font-weight:bold; font-size:16px;text-transform: uppercase;">Position Applied :
	                	</td>
	                	<td colspan="2" style="font-weight:bold; font-size:16px;text-transform: uppercase;"><?= $job_position_desc ?>
	                	</td>
	                </tr>
	                <tr><td colspan="4">&nbsp;</td></tr>
	                <tr>
	                	<td colspan="2" style="width:20%; font-weight:bold; font-size:16px;text-transform: uppercase;">Branch Applied :
	                	</td>
	                	<td colspan="2" style="font-weight:bold; font-size:16px;text-transform: uppercase;"><?= $row_branch['branch_name'] ?>
	                	</td>
	                </tr>

	                <?php if ( $is_reschedule ){ ?>
		                <tr><td colspan="4">&nbsp;</td></tr>
		                <tr>
		                	<td colspan="2" style="width:20%; font-weight:bold; font-size:16px;text-transform: uppercase;">
		                		Schedule Date :
		                	</td>
		                	<td colspan="2" style="font-weight:bold; font-size:16px;text-transform: uppercase;">
		                		<input type="text" style="border:1px solid black;width:100%;" readonly value="<?= $row_page['employment_interview_date'] ?>" />
		                	</td>
		                </tr>
	                <?php } ?>

	                <?php if ( $row_page['employment_status'] != 'Processing Interview Slot' ){ ?>
		                <tr><td colspan="4">&nbsp;</td></tr>
		                <tr>
		                	<td colspan="2" style="width:20%; font-weight:bold; font-size:16px;text-transform: uppercase;">
		                		<?= $desc_date ?> :
		                	</td>
		                	<td colspan="2" style="font-weight:bold; font-size:16px;text-transform: uppercase;">
		                		<input type="text" name="employment_interview_date" class="datetimepicker_with_time_employ" id="datetimepicker_with_time_employ" style="border:1px solid black;width:100%;" value="<?= $employment_interview_date ?>" readonly />
		                	</td>
		                </tr>
		            <?php } ?>

	                <?php if ( $row_page['employment_status'] != 'Processing Interview Slot' && $row_page['employment_status'] != 'Reschedule' ){ ?>
		                <tr>
			                <td colspan="4">
			                    <input type="hidden" name="hidden" value="1" />
			                    <input type="hidden" name="is_reschedule" value="<?= ( $is_reschedule ? 'yes' : 'no' ) ?>" />
			                    <input type="hidden" name="branch" value="<?= $branch_id ?>" />
			                    <input type="submit" class="cd-btn custom_button float_right" />
			                </td>
			            </tr>
			        <?php } ?>

		        </table>
			</form>
		</div>
		<?php
	break ;
}

if ( !$is_allow ){
	echo '<script>alert("Sorry, you don\'t have permission the page.");</script>' ;
}
?>

<script src="scripts/jquery-1.8.3.js"></script>
<script src="scripts/jquery.validate.js"></script>
<script src="scripts/jquery.datetimepicker.full.min.js"></script>
<script src="scripts/moment.min.js"></script>
<script src="scripts/all_employment.js"></script>
<script>
	$(document).ready(function(){
		<?php 
		$days    = [ 0 => [], 1 => [], 2 => [], 3 => [], 4 => [], 5 => [], 6 => [] ] ;
		if ( count($weekdays) > 0 ){ 
			foreach ( $weekdays as $k => $v ){
				$weekday 			 = explode( '/', $v ) ;
				$days[$weekday[0]][] = $weekday['1'] ;
			}
		}

		$disable = [] ;
		foreach ( $days as $k => $v ){
			if ( count($v) == 0 ){
				$disable[] = $k ;
			}
		}

		$default = $days[date('w', time())] ;
		?>

		$('#datetimepicker_with_time_employ').datetimepicker({
			format           : 'Y-m-d H:i',
			formatDate       : 'Y/m/d',
			scrollInput      : false,
			defaultTime      : "<?= count($default) > 0 ? $default[0] : '' ?>",
			allowTimes 		 : <?= json_encode( $default ) ?>,
			disabledWeekDays : <?= json_encode( $disable ) ?>,
			onSelectDate:function(dp,$input){
				switch ( dp.getDay() ){
					case 0 :
						this.setOptions({ defaultTime : "<?= $days[0][0] ?>", allowTimes: <?= json_encode( $days[0] ) ?> }) ;
					break ;
					case 1 :
						this.setOptions({ defaultTime : "<?= $days[1][0] ?>", allowTimes: <?= json_encode( $days[1] ) ?> }) ;
					break ;
					case 2 :
						this.setOptions({ defaultTime : "<?= $days[2][0] ?>", allowTimes: <?= json_encode( $days[2] ) ?> }) ;
					break ;
					case 3 :
						this.setOptions({ defaultTime : "<?= $days[3][0] ?>", allowTimes: <?= json_encode( $days[3] ) ?> }) ;
					break ;
					case 4 :
						this.setOptions({ defaultTime : "<?= $days[4][0] ?>", allowTimes: <?= json_encode( $days[4] ) ?> }) ;
					break ;
					case 5 :
						this.setOptions({ defaultTime : "<?= $days[5][0] ?>", allowTimes: <?= json_encode( $days[5] ) ?> }) ;
					break ;
					case 6 :
						this.setOptions({ defaultTime : "<?= $days[6][0] ?>", allowTimes: <?= json_encode( $days[6] ) ?> }) ;
					break ;
				}
			},
			beforeShowDay: function (dp) {
				switch ( dp.getDay() ){
					case 0 :
						return [ <?= ( count($days[0]) > 0 ? "true" : "false" ) ?>, "", "" ] ;
					break ;
					case 1 :
						return [ <?= ( count($days[1]) > 0 ? "true" : "false" ) ?>, "", "" ] ;
					break ;
					case 2 :
						return [ <?= ( count($days[2]) > 0 ? "true" : "false" ) ?>, "", "" ] ;
					break ;
					case 3 :
						return [ <?= ( count($days[3]) > 0 ? "true" : "false" ) ?>, "", "" ] ;
					break ;
					case 4 :
						return [ <?= ( count($days[4]) > 0 ? "true" : "false" ) ?>, "", "" ] ;
					break ;
					case 5 :
						return [ <?= ( count($days[5]) > 0 ? "true" : "false" ) ?>, "", "" ] ;
					break ;
					case 6 :
						return [ <?= ( count($days[6]) > 0 ? "true" : "false" ) ?>, "", "" ] ;
					break ;
				}
			}
		}) ;
	});
</script>
</body>
</html>


