<?php ?>
<div class ="container">
<div id="contact">
<?php 
$path_to_root ="../../.."; 
include_once($path_to_root."/config_db.php");
// include_once($path_to_root."/config.php");
global $db ,$current_db;

if(isset($_GET['id'])){
	$_POST['id']=$_GET['id'];
}elseif(!isset($_POST['id'])){
	exit();
}

if(!isset($db_connections[unserialize(base64_decode($_POST['id']))])){
	exit();
}
$current_db =$db_connections[unserialize(base64_decode($_POST['id']))];

$db = new mysqli($current_db['host'],$current_db['dbuser'],$current_db['dbpassword'],$current_db['dbname']);

$sql = 'SELECT option_value fROM '.$current_db['tbpref'].'kv_empl_option WHERE option_name="web_candidate_options"';
$query = mysqli_query($db, $sql);
$row =mysqli_fetch_array($query,MYSQLI_ASSOC);
$canditate_options =unserialize(base64_decode($row['option_value']));


$sql = 'SELECT value fROM '.$current_db['tbpref'].'sys_prefs WHERE name="curr_default"';
$query = mysqli_query($db, $sql);
$row =mysqli_fetch_array($query,MYSQLI_ASSOC);

$curr_code =$row['value'];
$date =date("Y-m-d");

if(isset($_POST['submit'])){

	$error =0;
	if($canditate_options['captcha_private_k']){
		if(isset($_POST['g-recaptcha-response'])){
	      $captcha=$_POST['g-recaptcha-response'];
	    }
	    if(!$captcha){
	    	echo '<div class="captcha_error">'._("Captcha Not Validated").'</div>';
	    	$error =1;
		}else{
			$secretKey =$canditate_options['captcha_private_k'];
			$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) .  '&response=' . urlencode($captcha);
			$response = file_get_contents($url);
	        $responseKeys = json_decode($response,true);
	        // should return JSON with success as true
	        if($responseKeys["success"]) {
	        	
			}else{
				echo '<div class="captcha_error">'._("Captcha Not Validated").'</div>';
				$error =1;
	        }
		}
	}
	if(strlen(trim($_POST['first_name'])) ==0 ){
		$first_name_error ="cannot be empty";
		$error =1;
	}
	if(strlen(trim($_POST['phone'])) ==0 ){
		$mobile_phone_error ="cannot be empty";
		$error =1;
	}
	if(strlen(trim($_POST['email'])) ==0 ){
		$email_error ="cannot be empty";
		$error =1;
	}
	if(strlen(trim($_POST['qualification'])) ==0 ){
		$qualification_error =_("Qualification cannot be empty.");
		$error =1;
	}
	if(strlen(trim($_POST['position'])) ==0 ){
		$position_error =_("Position cannot be empty.");
		$error =1;
	}
	if(strlen(trim($_POST['salary'])) ==0 ){
		$salary_error =_("Salary cannot be empty.");
		$error =1;
	}
	if(strlen(trim($_POST['last_company'])) ==0 ){
		$last_company_error =_("Last company worked cannot be empty.");
		$error =1;
	}


	if(empty($_POST["kv_attach_name"])){
		$resume_error =_("* Upload Your Resume");
		$error =1;
	}



 	if(!isset($max_image_size))
			$max_image_size = 5000;
		$upload_file = "";

		if (isset($_FILES['kv_attach_name']) && $_FILES['kv_attach_name']['name'] != '') {
			$_POST['unique_name'] =  '';
			$result = $_FILES['kv_attach_name']['error'];
			$upload_file = 'Yes'; 
			$attr_dir = $path_to_root.'/company/'.$_POST['id'].'/attachments'; 
			if (!file_exists($attr_dir)){
				
				mkdir($attr_dir);
			}

			$attach_dir = $attr_dir.'/empldocs/';
			if (!file_exists($attach_dir)){
				mkdir($attach_dir);
			}	
			$filename = basename($_FILES['kv_attach_name']['name']);
			$tmp = explode('.', $filename);
			$ext = strtolower(end($tmp));
		
			if(in_array($ext, array('docx','doc', 'pdf'))){		
				if(isset($_POST['unique_name']) && $_POST['unique_name'] == '')
					$kv_file_name = uniqid();			
				else
					$kv_file_name = $_POST['unique_name']; 
					
				$filename = $attach_dir."/".$kv_file_name; 
				if ( $_FILES['kv_attach_name']['size'] > ($max_image_size * 1024)) { //File Size Check
					$upload_file ='No';
				} 

			}


			$actual_file_nam = $_FILES['kv_attach_name']['name'];
// echo $actual_file_nam.'__';

			if(!$error){

					echo $filename;
					$result  =  move_uploaded_file($_FILES['kv_attach_name']['tmp_name'], $filename);
					if($result){
					$sql ='INSERT INTO '.$current_db['tbpref'].'kv_empl_candidates 
				(
					fname,
					lname,
					email,
					qualification,
					position,
					salary,
					experience,
					phone,
					status,
					last_company,
					comments,
					filename,
					unique_name	
				)
				VALUES ('.db_escape($_POST['first_name']).','.db_escape($_POST['last_name']).','.db_escape($_POST['email']).','.db_escape($_POST['qualification']).','.db_escape($_POST['position']).','.db_escape($_POST['salary']).','.db_escape($_POST['experience']).','.db_escape($_POST['phone']).','.db_escape($_POST['status']).','.db_escape($_POST['last_company']).','.db_escape($_POST['comments']).','.db_escape($actual_file_nam).','.db_escape($kv_file_name).')';  //,'.db_escape($actual_file_nam).','.db_escape($kv_file_name).'
				mysqli_query($db, $sql);
				// echo $sql;
				echo '<div class="success">'.$canditate_options['success_txt'].'</div>';

					}
			}

	}

}


function db_escape($value = "", $nullify = false)
{
	return '"'.$value.'"';
}
?>
<?php if($canditate_options['captcha_public_k']){ ?>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<?php }?>
<style type="text/css">
	
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-font-smoothing: antialiased;
  -moz-font-smoothing: antialiased;
  -o-font-smoothing: antialiased;
  font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body {
  font-family: "Roboto", Helvetica, Arial, sans-serif;
  font-weight: 100;
  font-size: 12px;
  line-height: 30px;
  color: #777;
  background: #4CAF50;
}

.container {
  max-width: 400px;
  width: 100%;
  margin: 0 auto;
  position: relative;
}

#contact input[type="text"],
#contact input[type="email"],
#contact input[type="tel"],
#contact input[type="url"],
#contact textarea,
#contact button[type="submit"] {
  font: 400 12px/16px "Roboto", Helvetica, Arial, sans-serif;
}

#contact {
  background: #F9F9F9;
  padding: 25px;
  margin: 50px 0;
  box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2), 0 5px 5px 0 rgba(0, 0, 0, 0.24);
}

#contact h2 {
  text-align: center;
  display: block;
  font-size: 30px;
  font-weight: 300;
  margin-bottom: 10px;
}

#contact h3 {
  display: block;
  font-size: 30px;
  font-weight: 300;
  margin-bottom: 10px;
}

#contact h4 {
  margin: 5px 0 15px;
  display: block;
  font-size: 13px;
  font-weight: 400;
}

fieldset {
  border: medium none !important;
  margin: 0 0 10px;
  min-width: 100%;
  padding: 0;
  width: 100%;
}

#contact input[type="text"],
#contact input[type="email"],
#contact input[type="tel"],
#contact input[type="url"],
#contact textarea ,
#contact select {
  width: 100%;
  border: 1px solid #ccc;
  background: #FFF;
  margin: 0 0 5px;
  padding: 10px;
}

#contact input[type="text"]:hover,
#contact input[type="email"]:hover,
#contact input[type="tel"]:hover,
#contact input[type="url"]:hover,
#contact textarea:hover {
  -webkit-transition: border-color 0.3s ease-in-out;
  -moz-transition: border-color 0.3s ease-in-out;
  transition: border-color 0.3s ease-in-out;
  border: 1px solid #aaa;
}

#contact textarea {
  height: 100px;
  max-width: 100%;
  resize: none;
}

#contact button[type="submit"] {
  cursor: pointer;
  width: 100%;
  border: none;
  background: #4CAF50;
  color: #FFF;
  margin: 0 0 5px;
  padding: 10px;
  font-size: 15px;
}

#contact button[type="submit"]:hover {
  background: #43A047;
  -webkit-transition: background 0.3s ease-in-out;
  -moz-transition: background 0.3s ease-in-out;
  transition: background-color 0.3s ease-in-out;
}

#contact button[type="submit"]:active {
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5);
}

.copyright {
  text-align: center;
}

#contact input:focus,
#contact textarea:focus {
  outline: 0;
  border: 1px solid #aaa;
}

::-webkit-input-placeholder {
  color: #888;
}

:-moz-placeholder {
  color: #888;
}

::-moz-placeholder {
  color: #888;
}

:-ms-input-placeholder {
  color: #888;
}

#contact .error {
	margin-left: 20px;
	color: red;
}
#contact div.success {
	color: green;
	text-align: center;
	margin-bottom: 20px;
}
#contact div.captcha_error {
	color: red;
	text-align: center;
	margin-bottom: 20px;
}
.g-recaptcha{
	text-align: center;
	display: flex;
	justify-content: center;
}
</style>

<?php if(file_exists($path_to_root."/modules/ExtendedHRM/form/style.css")){ ?>
<link rel="stylesheet" type="text/css" href="style.css">
<?php } ?>

<h2><?php echo $canditate_options['form_name'];?></h2>
<form action="<?php echo $_SERVER["PHP_SELF"].'?id='.$_POST['id'];?>"  method="post" enctype="multipart/form-data">
	<!-- user info -->
<!--  -->

	<label for="first_name" value="<?php echo isset($_POST['first_name'])?$_POST['first_name']:'';?>">First Name</label><span class="error">* <?php echo $first_name_error;?></span>
	<input type="text" name="first_name" value="<?php echo isset($_POST['first_name'])?$_POST['first_name']:'';?>">
	<br>

	<label for="last_name">Last Name</label><span class="error"><?php echo $last_name_error;?></span>
	<input type="text" name="last_name" value="<?php echo isset($_POST['last_name'])?$_POST['last_name']:'';?>">
	<br>

	<label for="email">Email</label><span class="error">* <?php echo $email_error;?></span>
	<input type="email"  name="email" value="<?php echo isset($_POST['email'])?$_POST['email']:'';?>"> 
	<br>
	<!-- contact info -->
	<label for="qualification">Qualification</label><span class="error"><?php echo $qualification_error;?></span>
	<input type="text"  name="qualification" value="<?php echo isset($_POST['qualification'])?$_POST['qualification']:'';?>">
	<br>

	<label for="position">Position</label><span class="error"><?php echo $position_error;?></span>
	<input type="text"  name="position" value="<?php echo isset($_POST['position'])?$_POST['position']:'';?>">
	<br>

	<label for="salary">Salary requirements</label><span class="error"><?php echo $salary_error;?></span>
	<input type="text"  name="salary" value="<?php echo isset($_POST['salary'])?$_POST['salary']:'';?>">
	<br>

	<label for="experience">Working Experience</label><span class="error"><?php echo $experience_error;?></span>
	<input type="text"  name="experience" value="<?php echo isset($_POST['experience'])?$_POST['experience']:'';?>">
	<br>

	<label for="phone">Phone number</label><span class="error">* <?php echo $phone_error;?></span>
	<input type="tel"  name="phone" value="<?php echo isset($_POST['phone'])?$_POST['phone']:'';?>">
	<br>

<label for="phone">Employemnt Status*	</label><span class="error">* <?php echo $status_error;?></span><br>
	  <input type="radio" id="employed" name="status" value="employed">
	  <label for="employed">Employed</label>
	  <input type="radio" id="female" name="status" value="un_employed">
	  <label for="un_employed">Un Employed</label>
	  <input type="radio" id="other" name="status" value="self_employed">
	  <label for="self_employed">Self Employed</label>
	  <input type="radio" id="other" name="status" value="student">
	  <label for="student">Student</label>
	  <br>
	<label for="last_company">Last worked company </label><span class="error"><?php echo $last_company_error;?></span>
	<input type="text"  name="last_company" value="<?php echo isset($_POST['last_company'])?$_POST['last_company']:'';?>">
	<br>

	<label for="comments">Reference / Comments / Questions</label>
	<textarea name="comments"><?php echo isset($_POST['comments'])?$_POST['comments']:'';?></textarea><br>

	<br>
	<label for="kv_attach_name">Resume</label><span class="error"><?php echo $resume_error;?></span>
	<input type="file" name="kv_attach_name" id="kv_attach_name">
	<br><br>


	<?php if($canditate_options['captcha_public_k']){ ?>
	<div class="g-recaptcha" data-sitekey="<?php echo $canditate_options['captcha_public_k'];?>"></div>;
	<?php }?>

	<br>

	<input type="hidden" name="id" value="<?php echo $_POST['id'];?>">
	<button type="submit" name="submit" value="<?php echo $canditate_options['btn_txt'];?>"><?php echo $canditate_options['btn_txt'];?></button>

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