<?php

$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" ;
$mysqli_position   	     = $mysqli->query($mysqli_query) ;
$job_position_desc 	     = '' ;
if ( $mysqli_position->num_rows > 0 ){
	$row_position 	     = $mysqli_position->fetch_assoc() ;
	$job_position_desc   = $row_position['job_position_desc'] ;
}

$mysqli_query            = "SELECT * FROM branch 
			         	    WHERE deleted_at IS NULL AND branch_id = '".$row_page['employment_branch']."' 
			         	    LIMIT 1" ;
$mysqli_branch 		     = $mysqli->query($mysqli_query) ;
$branch_name 		     = '' ;
$branch_hr_email 		 = '' ;
$branch_hr_cc 		     = [] ;
$branch_email_footer     = '' ;
if ( $mysqli_branch->num_rows > 0 ){
	$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'] ) ) ;
}

$mail_type 				 = escapeString( $_GET['mail_type'] ) ;
$hod 					 = base64_decode(escapeString( $_GET['hod'])) ;
$attach 				 = base64_decode(escapeString( $_GET['attach']));

if ( $hod != '' ) {
	$temp_emailcc        = explode(",", str_replace(" ","",$hod));
	foreach ($temp_emailcc as $key => $value) {
		if ( filter_var($value, FILTER_VALIDATE_EMAIL) ) {
			array_push($branch_hr_cc, $value);
		}
	}
}

if ( $attach != '' ) {
	$link_attach_resume = '<a href="'.PATH.'uploads/Employment_Resume/'.$attach.'">Candidate\'s Resume</a>' ;
}
$link 					= PATH.'hr-employment-schedule-interview-date.php?page='.$page.'&branch='.$row_page['branch'].'&sign='.md5( $page.$row_page['branch'].APIKEY) ;
$link_reschedule 		= '<a href="'.$link.'">'.$link.'</a>' ;

switch ( $mail_type ){
	case 'interview'    :
		$title 	         = "Invitation For Interview" ;
		$content 		 = "
		<table>
			<tr><td>Dear ".strtoupper($row_page['employment_name']).",</td></tr>
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>Thank you for your application regarding the position of <span style='border-bottom:1px solid black; font-weight:bold;'>".$job_position_desc."</span> at <span style='border-bottom:1px solid black; font-weight:bold;'>".$branch_name."</span>.</td></tr>
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>We are impressed with your qualifications and would like to meet with you to have a round of discussion. You are advised to attend for the interview on <span style='border-bottom:1px solid black; font-weight:bold;'>".date('Y-m-d H:i', strtotime($row_page['employment_interview_date']))."</span> at our office.</td></tr>
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>If you have any queries, please feel free to contact the office. We look forward to meet you soon at our office.</td></tr>
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>Please Note That:</td></tr>
			<tr><td style='height:20px;'>1) This is an interview call for the job applied and does not guarantee employment with us.</td></tr>
			<tr><td style='height:20px;'>2) No TA / DA will be provided to candidates appearing for the interview.</td></tr>
			<tr><td style='height:20px;'>3) Bring this letter with you on the above-mentioned date and time of the interview.</td></tr>
			<tr><td style='height:20px;'>4) Bring photocopies and originals of your academic & other credentials along with a recent snap.</td></tr>
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>Reschedule Interview Date (If you are inconvenience with the date provided): </td></tr>
			<tr><td>".$link_reschedule."</td></tr>
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>*Please revert back to this email to confirm for your interview attendance.</td></tr>
		</table>" . $branch_email_footer ;
	break ;
	case 'reschedule'   :
		$title           = "Reschedule for Interview Date" ;
		$content         = "
		<table>
			<tr><td>Dear ".strtoupper($row_page['employment_name']).",</td></tr>
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>After discussion and consideration, we would like to inform you that your interview was rescheduled and will be held on <span style='border-bottom:1px solid black; font-weight:bold;'>".($row_page['employment_r_interview_date'] == '0000-00-00 00:00:00' ? date('Y-m-d H:i', strtotime($row_page['employment_interview_date'])) : date('Y-m-d H:i', strtotime($row_page['employment_r_interview_date'])) )."</span> at our office.</td></tr>
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>If you have any queries, please feel free to contact the office. We look forward to meet you soon at our office.</td></tr>
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>Please Note That:</td></tr>
			<tr><td style='height:20px;'>1) This is an interview call for the job applied and does not guarantee employment with us.</td></tr>
			<tr><td style='height:20px;'>2) No TA / DA will be provided to candidates appearing for the interview.</td></tr>
			<tr><td style='height:20px;'>3) Bring this letter with you on the above-mentioned date and time of the interview.</td></tr>
			<tr><td style='height:20px;'>4) Bring photocopies and originals of your academic & other credentials along with a recent snap.</td></tr>
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>Reschedule Interview Date (If you are inconvenience with the date provided): </td></tr>
			<tr><td>".$link_reschedule."</td></tr>
			<tr><td style='height:20px;'> </td></tr>";
			if ( $attach != '' ) {
				$content.="
				<tr><td>Attachment:</td></tr>
				<tr><td>".$link_attach_resume."</td></tr>
				<tr><td style='height:20px;'> </td></tr>";
			}
			$content.="
			<tr><td>*This is a system generated email. Please do not reply to it.</td></tr>
		</table>" . $branch_email_footer ;
	break ;
	case 'offer_letter' :
		$title  	       = "Offer Letter" ;
		$link_offer_letter = '<a href="'.PATH.'employment_document.php?id='.$page.'&doctype=offer_letter&branch='.$_SESSION['url_get_branch_admin'].'">'.PATH.'employment_document.php?id='.$page.'&doctype=offer_letter&branch='.$_SESSION['url_get_branch_admin'].'</a>' ;

		$link_agreement    = '<a href="'.PATH.'employment_document.php?id='.$page.'&doctype=ieagreement&branch='.$_SESSION['url_get_branch_admin'].'">'.PATH.'employment_document.php?id='.$page.'&doctype=ieagreement&branch='.$_SESSION['url_get_branch_admin'].'</a>' ;

		$link_sign_form    = '<a href="'.PATH.'employment_document.php?id='.$page.'&doctype=sign_form&branch='.$_SESSION['url_get_branch_admin'].'">'.PATH.'employment_document.php?id='.$page.'&doctype=sign_form&branch='.$_SESSION['url_get_branch_admin'].'</a>' ;

		$content           = "
		<table>
			<tr><td>Dear ".strtoupper($row_page['employment_name']).",</td></tr>
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>Congratulations! We would like to inform you that your application of <span style='border-bottom:1px solid black; font-weight:bold;'>".$job_position_desc."</span> at <span style='border-bottom:1px solid black; font-weight:bold;'>".$branch_name."</span> had been approved. Please read the offer letter provided and sign the form provided below to confirm the offer.</td></tr>
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>If you have any queries, please feel free to contact the office. We look forward to meet you soon at our office.</td></tr>
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>Attachment:</td></tr>
			<tr><td style='height:20px;'>Official Offer Letter:</td></tr>
			<tr><td style='height:20px;'>".$link_offer_letter."</td></tr>";

			if ( $offer_status['sent_ol_date'] != '' && $offer_status['sent_ol'] == 'OLA' ){
				$content.="
				<tr><td style='height:20px;'> </td></tr>
				<tr><td style='height:20px;'>Individual Employment Agreement</td></tr>
				<tr><td style='height:20px;'>".$link_agreement."</td></tr>" ;
			}

			$content.= "
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>Offer Letter Form (Signed for Confirmation):</td></tr>
			<tr><td>".$link_sign_form."</td></tr>
			<tr><td style='height:20px;'> </td></tr>
			<tr><td>*This is a system generated email. Please do not reply to it.</td></tr>
		</table>" . $branch_email_footer ;
	break ;
	case 'reject' :
		$title 	         = "Reject Applicant Interview" ;
		$content         = "
        <table>
            <tr><td>Dear ".ucwords($row_page['employment_name']).",</td></tr>
            <tr><td style='height:20px;'> </td></tr>
            <tr><td>Thank you for applying to the job opening at ".$branch_name.". After carefully reviewing your qualifications, we have decided to pursue other candidates whom we feel more closely meeting our needs at this time.</td></tr>
            <tr><td style='height:20px;'> </td></tr>
            <tr><td>We appreciate your interest in our company and the time it took to apply with us. Please feel free to apply for open positions with us in the future.</td></tr>
            <tr><td style='height:20px;'> </td></tr>
            <tr><td>Again, thank you for considering us as a potential employer. We wish you success in your career pursuit.</td></tr>
            <tr><td style='height:20px;'> </td></tr>
        </table>" . $branch_email_footer ;
	break ;
}


$mailer 		  = new Mailer() ;
$mailer->from     = $branch_hr_email ;
$mailer->fromname = COMPANY ;
$mailer->to       = [ $row_page['employment_email'] ] ;
if ( count($branch_hr_cc) > 0 ){
	$mailer->cc   = $branch_hr_cc ;
}
$mailer->subject  = $title ;
$mailer->body     = $content ;
$mailer->send() ;

echo '<script>alert("'.ucfirst($mail_type).' Letter is Sent!")</script>' ;
echo '<script>window.open("?page_mode=edit_status&page='.$page.'&success=1&select_worker=Local", "_self");</script>' ;

?>