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

include '../languages/en.php' ;

require( '../extensions/mailer.php' ) ;
require( '../extensions/sms.php' ) ;


// check if post exists
if ( checkExists($_POST) ){
	$status             = '300' ;

    $array              = resetRequest( $_POST ) ;
    $visited_dated_from = $array['visited_dated_from'] ;;
    $visited_dated_to   = $array['visited_dated_to'] ;;
    $category           = $array['category'] ;
    $name               = $array['name'] ;
    $dial   	        = $array['dial'] ;
    $mobile    	        = $array['mobile'] ;
    $email              = $array['email'] ;
    $identity           = $array['identity'] ;
    $nationality        = $array['nationality'] ;
    $visitor_company    = $array['visitor_company'] ;
    $car_plate          = $array['car_plate'] ;
    $branch             = $array['branch'] ;
    $remark             = $array['remark'] ;
    $contact_person     = $array['contact_person'] ;
    $reason             = $array['reason'] ;
    $question1          = $array['question1'] ;
    $question2          = $array['question2'] ;
    $question3          = $array['question3'] ;
    $question4          = $array['question4'] ;
    $question5          = $array['question5'] ;
    $question6          = $array['question6'] ;

    if ( $visited_dated_from != '' && 
    	 $visited_dated_to   != '' && 
    	 $name               != '' && 
    	 $dial               != '' && 
    	 $mobile             != '' && 
    	 $email              != '' && 
    	 $identity           != '' && 
    	 $visitor_company    != '' && 
    	 $branch             != '' && 
    	 $contact_person     != '' && 
    	 $reason             != '' ){

	    $status               = '295' ;

    	if ( $visited_dated_from >= TODAYDAY && $visited_dated_to >= $visited_dated_from ){

	    	$status           = '260' ;
      
	    	$mobile           = $dial . $mobile ;
	    	
	    	if ( $mysqli->query( "INSERT INTO visitor
	    					     ( `name`, `mobile`, `email`, `identity`, `nationality`, `visitor_company`, `car_plate`, `branch`, `remark`, `contact_person`, `reason`, `question1`, `question2`, `question3`, `question4`, `question5`, `question6`, `visited_at`, `visited_at_to`, `category` ) VALUES
	    					     ( '".$name."', '".$mobile."', '".$email."', '".$identity."', '".$nationality."', '".$visitor_company."', '".$car_plate."', '".$branch."', '".$remark."', '".$contact_person."', '".$reason."', '".$question1."', '".$question2."', '".$question3."', '".$question4."', '".$question5."', '".$question6."', '".$visited_dated_from."', '".$visited_dated_to."', '".$category."' )" ) ){

	    		$status              = '200' ;

	    		$visitor_id          = $mysqli->insert_id ;

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




		    	$body 			  = 'Dear valued visitor, good day. Thank you for your visit request submission, we will review and get back to you.<br /><br />by ' . COMPANY . $branch_email_footer ;
		    	$body_sms         = 'Dear valued visitor, good day. Thank you for your visit request submission, we will review and get back to you.' ;

				$mailer           = new Mailer() ;
                $mailer->from     = $branch_hr_email ;
                $mailer->fromname = COMPANY ;
			    $mailer->to       = [ $email ] ;
	            if ( count($branch_hr_cc) > 0 ){
	                $mailer->cc   = $branch_hr_cc ;
	            }
			    $mailer->subject  = 'Visitor Form Submission' ;
			    $mailer->body     = $body ;
			    $mailer->send() ;

			    if ( substr( $mobile, 0, 2 ) == '60' || substr( $mobile, 0, 3 ) == '+60' ||
             		 substr( $mobile, 0, 2 ) == '65' || substr( $mobile, 0, 3 ) == '+65' ){
				    $sms          = new Sms() ;
					$sms->to      = $mobile ;
					$sms->message = $body_sms ;
					$sms->send() ;
			    }

	    		header( "Location : qrcode.php?visitor_id=".$visitor_id.'&token='.setSecret( $visitor_id ) ) ;
	    		exit ;

	    	}

    	}

    }
    
    $_SESSION['error'] = $status ;
    header('Refresh: 0') ;
    exit ;
}


$dial_code    = '
["+60","+65","+1","+7","+12","+13","+15","+16","+20","+21","+22","+23","+24","+25","+26","+27","+29","+30","+31","+32","+33","+34","+35","+36","+37","+38","+39","+40","+41","+42","+43","+44","+45","+46","+47","+48","+49","+50","+51","+52","+53","+54","+55","+56","+57","+58","+59","+61","+62","+63","+64","+66","+67","+68","+69","+73","+81","+82","+84","+85","+86","+87","+88","+90","+91","+92","+93","+94","+95","+96","+97","+98","+99"]' ;
$get_dial_code = json_decode( $dial_code, true ) ;

include '../requires/page_header.php' ;
include 'requires.php' ;

$more_scripts = showMessage( $_SESSION['error'], $message ) ;
?>

<div class="modal fade" id="myModal" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-body">
				<div class="modal-top">

				</div>
				<div class="modal-center">
					
					<div class="logo-box">
						<img class="logo" src="../images/logo.png" />
					</div>

					<div class="title-box">
						<h3 class="title"><?= COMPANYSHORT ?> <?=$lang['Visitor Registration Form']?></h3>
						<div class="description">
							<h5><?= str_replace( 'XXXX', COMPANYSHORT, $lang['Dear Guest, Welcome to XXXX Group! We are honored to have you in our plant. Kindly fill up this Visitor Registration Form for us to be well-prepared for your visit. Looking forward to meeting you in person soon!'] ) ?></h5>
						</div>
					</div>

					<form method="POST" class="input-form" id="submit-register" >

						<div class="input-box">
							<span class="input-title"><?=$lang['Appointment Date From']?></span><br/>
							<input type="datetime-local" name="visited_dated_from" class="input" required />
						</div>
						<div class="input-box">
							<span class="input-title"><?=$lang['Appointment Date To']?></span><br/>
							<input type="datetime-local" name="visited_dated_to" class="input" required />
						</div>

						<div class="input-box">
							<span class="input-title"><?=$lang['Branch To Visit']?></span><br/>
							<select name="branch" class="input" required >
								<option value=""><?= $lang['select'] ?></option>
								<?php 
								$mysqli_query  = "SELECT branch_id, branch_name FROM branch
                          						  WHERE deleted_at IS NULL AND branch_show = 'yes'" ;
                        		$mysqli_branch = $mysqli->query($mysqli_query) ;

                        		if ( $mysqli_branch->num_rows > 0 ){
                        			while ( $row_branch = $mysqli_branch->fetch_assoc() ){
                        				echo '<option value="'.dataFilter( $row_branch['branch_id'] ).'">'.dataFilter( $row_branch['branch_name'] ).'</option>' ;
                        			}
                        		}
								?>
							</select>
						</div>
						<div class="input-box">
							<span class="input-title"><?=$lang['Visitor Category']?></span><br/>
							<select name="category" class="input" required >
								<option value=""><?= $lang['select'] ?></option>
								<option value="<?= $lang['VIP Visitor'] ?>"><?= $lang['VIP Visitor'] ?></option>
								<option value="<?= $lang['Visitor'] ?>"><?= $lang['Visitor'] ?></option>
								<option value="<?= $lang['Supplier/Contractor'] ?>"><?= $lang['Supplier/Contractor'] ?></option>
								<option value="<?= COMPANYSHORT . ' ' .$lang['Staff'] ?>"><?= COMPANYSHORT . ' ' .$lang['Staff'] ?></option>
								<option value="<?= COMPANYSHORT . ' ' .$lang['Corporate Staff'] ?>"><?= COMPANYSHORT . ' ' .$lang['Corporate Staff'] ?></option>
								<option value="<?= $lang['Interview'] ?>"><?= $lang['Interview'] ?></option>
							</select>
						</div>
						<div class="input-box">
							<span class="input-title"><?= $lang['Visitor Name'] ?></span><br/>
							<input type="text" name="name" class="input" required />
						</div>
						<div class="input-box">
							<span class="input-title"><?= $lang['Contact Number'] ?></span><br/>
							<div class="input-mobile clearfix">
								<select name="dial" class="input-mobile-select" required >
									<option value=""><?= $lang['select'] ?></option>
									<?php foreach ( $get_dial_code as $k => $v ){ ?>
										<option value="<?= $v ?>"><?= $v ?></option>
									<?php } ?>
								</select>
								<input type="text" name="mobile" class="input" required />
							</div>
						</div>
						<div class="input-box">
							<span class="input-title"><?= $lang['Email'] ?></span><br/>
							<input type="text" name="email" class="input" required />
						</div>
						<div class="input-box">
							<span class="input-title"><?= $lang['NRIC / Passport No'] ?></span><br/>
							<input type="text" name="identity" class="input" required />
						</div>
						<div class="input-box">
							<span class="input-title"><?= $lang['Nationality'] ?></span><br/>
							<input type="text" name="nationality" class="input" />
						</div>
						<div class="input-box">
							<span class="input-title"><?= $lang['Visitor Company'] ?></span><br/>
							<input type="text" name="visitor_company" class="input" required />
						</div>
						<div class="input-box">
							<span class="input-title"><?= $lang['Car Plate'] ?></span><br/>
							<input type="text" name="car_plate" class="input" required />
						</div>
						<div class="input-box">
							<span class="input-title"><?= $lang['Reason To Visit'] ?></span>
							<input type="text" name="reason" class="input" required />
						</div>
						<div class="input-box">
							<span class="input-title"><?= COMPANYSHORT . ' ' . $lang['Contact Person'] ?></span>
							<input type="text" name="contact_person" class="input" required />
						</div>

						<div class="input-box">
							<button type="submit" class="input-submit"><?=$lang['submit']?></button>
						</div>

						<div class="input-box" style="display: flex;justify-content: space-between;">
							<span class="input-title input-title-2">* <?=$lang['If any technical issue kindly contact us']?><br /><?= SUPPORTNAME ?> @ <a href="tel:<?= SUPPORTMOBILE ?>"><?= SUPPORTMOBILE ?></a></span>
						</div>

						<!-- <div class="input-box">
							<div class="language" style="display: flex;justify-content: space-between;">
					            <a href="language.php?lang=en&link=//<?= $_SERVER['HTTP_HOST'].urlencode($_SERVER['REQUEST_URI']) ?>" class="<?= $get_lang == 'en' ? 'active' :'' ?>">EN</a>
					            <a href="language.php?lang=cn&link=//<?= $_SERVER['HTTP_HOST'].urlencode($_SERVER['REQUEST_URI']) ?>" class="<?= $get_lang == 'cn' ? 'active' :'' ?>">CN</a>
					            <a href="language.php?lang=ma&link=//<?= $_SERVER['HTTP_HOST'].urlencode($_SERVER['REQUEST_URI']) ?>" class="<?= $get_lang == 'ma' ? 'active' :'' ?>">BM</a>
					        </div>
						</div> -->

					</form>	

				</div>
				<div class="modal-bottom">

				</div>
			</div>
		</div>
	</div>
</div>

<?php
include 'requires/page_footer.php' ;
?>