<?php
$current_page = 'employment_document.php' ;
include 'connect/cms-config.php' ;
include 'requires/function.php' ;

// include the class
include 'requires/class_resize.php' ;

$page = escapeString($_GET['id']);
// $success = escapeString($_GET['success']);
$doctype = escapeString($_GET['doctype']);
$branch_id = escapeString($_GET['branch']);

$mysqli_ck_branch = $mysqli->query("SELECT branch_id FROM branch");
if ($mysqli_ck_branch->num_rows >0) {
	while ($row_ck_branch = $mysqli_ck_branch->fetch_array()) {
		$array_branch_id_list[] = $row_ck_branch['branch_id'];
	}
}
// print_r($array_branch_id_list);exit;
$boolean_ck_branch = in_array($branch_id, $array_branch_id_list) ;

if ($branch_id == '' || $boolean_ck_branch == false) {
	echo '
	<script>
		alert("Invalid branch code!") ;
	</script>' ;
	exit;
}

// print_r($_POST['application_signature']);exit;

if (isset($doctype) && $doctype == 'sign_form' && $_POST['hidden'] == 1 && $_POST['offer_letter_sign_form'] == 'offer_letter_sign_form'){

	if ($_POST['application_signature'] != $_POST['application_signature_hidden'] && $_POST['application_signature'] != '') {
		// signature
		$application_signature = escapeString($_POST['application_signature']) ;
		$application_signature_date = TODAYDATE ;
	}else{
		$offer_form_con = jsonEncodeDecode('decode', $row_page['employment_offer_sign_detail']) ;
		if($application != ''){
			$application_signature = $offer_form_con['signature'] ;
			$application_signature_date = $offer_form_con['date'] ;
		}else{
			$application_signature = escapeString($_POST['application_signature']) ;
			$application_signature_date = TODAYDATE ;
		}
		
	}

	$array_offer_form = array('signature' => $application_signature, 
								'date' => $application_signature_date);
	$array_offer_form = jsonEncodeDecode('encode', $array_offer_form) ;


	if($mysqli->query("UPDATE staff_employment SET employment_offer_sign_detail = '".$array_offer_form."' WHERE employment_id = '".$page."'")){
		$descrition = 'Candidate had sign the offer letter form. ('.TODAYDATE.')' ;
		$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-offer-form', '200', 'AF-".$page."', '".$_SESSION["system_id"]."', '".$descrition."', '".$record."', NOW())");
	}

}



$mysqli_page = $mysqli->query("SELECT * FROM staff_employment
							   WHERE employment_id = '".$page."' AND employment_trash = '0' AND employment_status = 'Offer' LIMIT 1");
if ($mysqli_page->num_rows > 0){
	// set query in array
	$row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
}

$offer_form_con = jsonEncodeDecode('decode', $row_page['employment_offer_sign_detail']) ;


if(($doctype == 'offer_letter' || $doctype == 'ieagreement') && $page != ''){

	switch($doctype){
		case 'offer_letter' :
			$print_filename = 'Offer Letter' ;
			$title = 'Offer Letter' ;
		break ;	
		case 'ieagreement' :
			$print_filename = 'IEA' ;
			$title = 'Individual Employment Agreement between an Employer and an Employee' ;
		break ;
	}

	$mysqli_page = $mysqli->query("SELECT * FROM staff_employment
								   WHERE employment_id = '".$page."' AND employment_trash = '0' AND employment_status = 'Offer' LIMIT 1");
	if ($mysqli_page->num_rows > 0){
		
		// set query in array
		$row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
		
		// employment position
		$mysqli_position = $mysqli->query("SELECT post_title FROM system_post
										   WHERE post_id = '".$row_page['employment_position']."' AND post_type = 'hr-position' AND post_categories = 'hr-position' LIMIT 1") ; 
		if ($mysqli_position->num_rows > 0){
			$row_position = $mysqli_position->fetch_array(MYSQLI_ASSOC) ;
			$position = dataFilter($row_position['post_title']) ;	
		}
		
		// incharge person
		$mysqli_query = "SELECT * FROM system_user
						 WHERE user_id = '".$row_page['employment_user_id']."' AND (user_permission = 'admin' OR user_permission = 'hr') AND user_trash = '0' LIMIT 1" ;
		$mysqli_incharge_by = $mysqli->query($mysqli_query) ;
		if ($mysqli_incharge_by->num_rows > 0){
			$row_incharge_by = $mysqli_incharge_by->fetch_array(MYSQLI_ASSOC) ;
			$incharge_by = dataFilter($row_incharge_by['user_call']).' . '.dataFilter($row_incharge_by['user_fullname']) ;
		}
		
		// assigned by
		$mysqli_query = "SELECT * FROM system_user a
						 LEFT JOIN system_post b ON (a.user_position = b.post_id)
						 WHERE user_id = '".$row_page['employment_assign_by']."' AND (user_permission = 'admin' OR user_permission = 'hr') AND user_trash = '0' ORDER BY user_name" ;
		$mysqli_assign_by = $mysqli->query($mysqli_query) ;
		if ($mysqli_assign_by->num_rows > 0){
			$row_assign_by = $mysqli_assign_by->fetch_array(MYSQLI_ASSOC) ;
			$assign_by = dataFilter($row_assign_by['user_call']).' . '.dataFilter($row_assign_by['user_fullname']) ;
			$assign_by_position = dataFilter($row_assign_by['post_title']) ;
		}
		
		$status_text = jsonEncodeDecode('decode', $row_page['employment_status_text']) ;
		$offer_status = $status_text['offer_status'] ;
		
		$new_worker = dataFilter($row_page['employment_call']).' . '.strtoupper(dataFilter($row_page['employment_name']));
		
		$letter_head = getOwnerCompanyLetterHead($branch_id) ;

		// set body content
		$html = '
		'.$letter_head['header'].'
		<table width="100%" border="0" cellpadding="0" cellspacing="0" class="employment_table" >
			<tr><td colspan="4">&nbsp;</td></tr>
			<tr>
				<td colspan="4" width="100%" style="font-size:24px; padding:10px 0; text-align:center; border:1px solid #000; background:#000; color:white;text-transform:uppercase;text-align:center;">
					<strong>'.$title.'</strong>
				</td>
			</tr>
			<tr><td colspan="4">&nbsp;</td></tr>' ;
	}

	if($doctype == 'offer_letter'){
		include_once 'HR/letter-offer.php' ;
	}else if ($doctype == 'ieagreement'){
		include_once 'HR/letter-iea-temp.php' ;
				
		// page footer
		$footer = '
		<table border="0" cellpadding="0" cellspacing="0" width="960" style="font-size:18px;">
			<tr>
				<td align="center">{PAGENO}</td>
			</tr>
		</table>' ;
	}

	$html .= '
		</table>'.$html_offer ;
		
		// page header
		$header = '' ;

		include_once 'MPDF/mpdf.php' ;

		$mpdf = new mPDF('utf-8', 'A4', '', 'freesans', 15, 15, 15, 15, 5, 5) ;
		ini_set("memory_limit","999999999999999999999999999999999999999999M");

		// Use different Odd/Even headers and footers and mirror margins
		$mpdf->mirrorMargins = 1 ;

		
		// set mpdf header
		$mpdf->SetHTMLHeader($header) ;
		$mpdf->SetHTMLHeader($header,'E') ;
		
		// set mpdf footer
		$mpdf->SetHTMLFooter($footer) ;
		$mpdf->SetHTMLFooter($footer,'E') ;

		// write in html
		$mpdf->WriteHTML($html) ;
		
		// set filename
		$filename = 'Offer Letter-'.strPad(3, $page) ; // Your Filename whit local date and time
		$filename_save = $filename.'.pdf' ;
		$filename_temp = $filename ;
		
		// turns all headers/footers off from new page onwards
		$mpdf->useAdobeCJK = true;
		
		// check output type
		$page_type = ($_GET['page_type']) ;
		$page_type_output = 'I' ;
		
		//$mpdf->SetAutoFont(AUTOFONT_ALL);
		$mpdf->Output($filename_save, $page_type_output);
}else if ($doctype == 'sign_form' && $page!='' && $row_page['employment_status'] == 'Offer') {
	$letter_head = getOwnerCompanyLetterHead($branch_id) ;

	$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) ;
    if ( $mysqli_position->num_rows > 0 ){
       $row_position = $mysqli_position->fetch_array(MYSQLI_ASSOC);
    }

    $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) ;
    if ($mysqli_branch->num_rows > 0){
    	$row_branch = $mysqli_branch->fetch_array(MYSQLI_ASSOC);
    }

	echo'
	<!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>Offer Letter Sign Form - '.COMPANY.'</title>
	<meta name="description" content="">
	<meta name="author" content="JImmyLimSengHang">
	<meta name="creator" content="JImmyLimSengHang">
	<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>
		<div style="max-width: 960px ; margin: auto;">
			<form id="ofsign_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">
		                	'.$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>OFFER  LETTER  SIGNATURE  FORM</strong>
		                            </td>
		                        </tr>
		                    </table>
		                </td>
		            </tr>
		            <tr><td colspan="4">&nbsp;</td></tr>';
		            if($offer_form_con['date'] != ''){
		            	echo'
		            	<tr>
			                <td colspan="4" style="background:green;color:white;padding:5px">
			                	You have submitted the form.
			                </td>
		                </tr>
		            	';
		            }
		       echo'<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;">'.$row_position['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>
	                <tr><td colspan="4">&nbsp;</td></tr>
	                <tr>
	                	<td colspan="2" style="width:20%; font-weight:bold; font-size:16px;text-transform: uppercase;vertical-align:top;">Signature:
	                	</td>';
	                if($offer_form_con['signature'] == '' && $offer_form_con['date'] == ''){
	                	echo'
	                	<td colspan="2" style="font-weight:bold; font-size:16px;text-transform: uppercase;"> <div class="clearfix" style="max-width:960px; width:100%; margin:0 auto;">
                            <div style="width:350px;float:left;">
                            	<div id="protective-layer" style="position: absolute;width: inherit;height: 150px; text-align: center;font-size: large;font-weight: bolder;padding: 50px 0px;">Click To Sign Here</div>
                                <div id="signature-pad" style="width:350px;" class="signature-pad">
                                    <div class="signature-pad--body">
                                        <canvas style="border: 1px solid #ccc; width:350px !important; height:150px !important;"></canvas>
                                    </div>
                                    <div class="signature-pad--footer">
                                        <div class="signature-pad--actions">
                                            <div>
                                                <button type="button" class="button clear" data-action="clear">'.$lang['Clear'].'</button>
                                            </div>
                                        </div>
                                    </div>
                                    <input type="hidden" name="application_signature" id="signature_pad" required="required" />
                                    <input type="hidden" name="application_signature_hidden" id="signature_pad_hidden" required="required" />
                                </div>
                            </div>
                            <div style="width:260px; float:left;">&nbsp;</div>
                            <div style="width:350px; float:left;">&nbsp;</div>
	                	</td>';
	                }else{
	                	echo'
	                	<td colspan="2" style="font-weight:bold; font-size:16px;text-transform: uppercase;"> <div class="clearfix" style="max-width:960px; width:100%; margin:0 auto;">
	                		<img src='.$offer_form_con['signature'].' />
	                	</td>';
	                }
	                
	           	echo'</tr>';

	          	if($offer_form_con['date'] == ''){
	           	echo'
	                <tr>
		                <td colspan="4">
		                    <input type="hidden" name="hidden" value="1" />
		                    <input type="hidden" name="branch" value="'.$branch_id.'" />
						  	<input type="hidden" name="offer_letter_sign_form" value="offer_letter_sign_form" />
		                    <input id="ofsign_sub_btn" type="submit" class="cd-btn custom_button float_right">
		                </td>
		            </tr>';
		        }
		       echo'
		        </table>
			</form>
		</div>
	<script src="scripts/jquery-1.8.3.js"></script>
	<script src="'.PATH.'scripts/signature_pad.umd.js"></script>
	<script src="'.PATH.'scripts/signature.js?v='.filemtime( $_SERVER["DOCUMENT_ROOT"].'/scripts/signature.js').'"></script>
	<script>
		$("#ofsign_sub_btn").click(function(){
			event.preventDefault();
		  	if(confirm("Please make sure you have signed the form?")==true){
		    	$("#ofsign_form").submit();
		  	}
		});
	</script>
	<script src="scripts/jquery.validate.js"></script>
	<script src="scripts/all_employment.js"></script>
	</body>
	</html>
	';
}else{
	echo'<script>alert("Sorry, you don\'t have permission to this page. If you have any queries, please contact our admin.");</script>';
}
?>