<?php
/*
Template Name: Candidates
*/

$page = get_page(get_the_ID());
$currentlang = pll_current_language();?>
<?php get_header(); ?>
<div class="con">
	<div class="col side-row">
		<div class="round-block" style="background-image:url(img/can.png)">
			<span class="middle"><?=$page->post_title?></span>
		</div>
	</div>

	<div class="col middle-row">
		<div class="top-block">
			<?=apply_filters('the_content', $page->post_content)?>
		</div>
		<div class="mid-con">
			<?the_field("lower_text");?>
		</div>
		<form id="candidateForm" method="post" enctype="multipart/form-data">
			<div id="sendCV"></div>
			<input class="text-input formDataCheck" type="text" placeholder="<?=($currentlang=='eng'?'Name, Surname':'Eesnimi, Perekonnanimi')?>" id="candidateFormName" />
			<textarea class="textarea formDataCheck" placeholder="<?=($currentlang=='eng'?'Message':'Sõnum')?>" id="candidateFormMessage"></textarea>
			<input class="formDataCheck" type="file" rel="candidateFormFileCVButton" name="fileCV" id="candidateFormFileCV" style="display:none;" />
			<input class="" type="file" rel="candidateFormFileMLButton" name="fileML" id="candidateFormFileML" style="display:none;" />
			<button class="upload wider formButton" id="candidateFormFileCVButton" onClick="addCV()"><?=($currentlang=='eng'?'Upload your CV':'Saada CV')?></button><label id="candidateFormFileCVLabel"></label><br />
			<button class="upload wider formButton" id="candidateFormFileMLButton" onClick="addML()"><?=($currentlang=='eng'?'Motivation letter':'Motivatsioonikiri')?></button><label id="candidateFormFileMLLabel"></label>
			<button class="bttn1 wider formButton"  id="candidateFormSubmitButton"><?=($currentlang=='eng'?'Submit':'Saada vorm')?></button>
		</form>
		<script>
			if(window.location.hash == '#SendCV'){
				 $('html,body').animate({scrollTop: $("#candidateForm").offset().top},'slow');
			}
			function addCV(){
				document.getElementById('candidateFormFileCV').click();
				$("#candidateFormFileCVButton").css("border", "");
			}
			function addML(){
				document.getElementById('candidateFormFileML').click();
				$("#candidateFormFileMLButton").css("border", "");
			}
			$(".formButton").click(function(e){e.preventDefault()})
			$("#candidateFormFileCV").change(function(e){
				var filename = $(this).val().replace(/C:\\fakepath\\/i, '')
				$("#candidateFormFileCVLabel").html(filename)
				console.log(e.target.files);
			})
			$("#candidateFormFileML").change(function(){
				var filename = $(this).val().replace(/C:\\fakepath\\/i, '')
				$("#candidateFormFileMLLabel").html(filename)
			})
			$("#candidateForm .formDataCheck").keyup(function(){
				if($(this).val() != ''){
					$(this).css("border", "");
				}else{
					$(this).css("border", "1px solid red");
				}
			})
			$("#candidateFormSubmitButton").click(function(){
				var send = true;
				$("#candidateForm .formDataCheck").each(function(){
					if($(this).val() == ""){
						send = false;
						if($(this).attr("type") == 'file'){
							$("#"+$(this).attr('rel')).css("border", "1px solid red");
						}else{
							$(this).css("border", "1px solid red");
						}
					}
				})
				if(send === true){
					var formData = new FormData();
					CVfiles = $('#candidateFormFileCV');
					$.each(CVfiles[0]['files'], function(key, value){formData.append(0, value);});
					FLfiles = $('#candidateFormFileML');
					$.each(FLfiles[0]['files'], function(key, value){formData.append(1, value);});
					formData.append('name', $("#candidateFormName").val());
					formData.append('message', $("#candidateFormMessage").val());
					console.log(formData)
					$.ajax({
						url: 'forCandidatesForm.php',
						type: 'POST',
						data: formData,
						success: function (data) {
							if(data == '1'){
								$("#candidateForm").html("<p style='color:red; padding-left:30px;'><?=($currentlang=='eng'?'Thank you. Your message has been sent.':'Aitäh. Sinu sõnum on saadetud.')?></p>");
							}else{
								$("#candidateForm").prepend("<p style='color:red; padding-left:30px;'><?=($currentlang=='eng'?'There was a problem sending your message. Please try later.':'Tekkis probleem saates sõnum. Palun proovige hiljem.')?></p>");
							}
						},
						cache: false,
						processData: false, 
						contentType: false
					});
				}
				return false
			})
		</script>
	</div>
	<?get_sidebar();?>
</div>
<?php get_footer(); ?>