<?php 
include_once('../header.php');
include_once('../utility/common.php');
include_once(ROOT_PATH.'language/'.$lang_code_global.'/lang_add_rented.php');
if(!check_login()){
	header("Location: " . WEB_URL . "logout.php");
	die();
}
$success = "none";
$name = '';
$email = '';
$phone = '';
$business_name = '';
$business_phone = '';
$nature_of_business = "";

$title = "Add Tenant Information";
$button_text = $_data['save_button_text'];
$successful_msg = 'Tenant added successfully';
$form_url = WEB_URL . "tenant/addtenant.php";
$id="";
$hdnid = "0";
$image_rnt = WEB_URL . 'img/no_image.jpg';
$img_track = '';

if(isset($_POST['name'])){
	$image_url = uploadImage();
	$sql = "INSERT INTO tbl_tenant(`name`, email, phone, `address`, `business_name`, `business_phone`, `nature_of_business`, `image`) 
		values('$_POST[name]','$_POST[email]','$_POST[phone]', '$_POST[address]','$_POST[business_name]', '$_POST[business_phone]',
		'$_POST[nature_of_business]', '$image_url')";
	mysqli_query($link,$sql);
	
	mysqli_close($link);
	$url = WEB_URL . 'tenant/tenantlist.php?m=add';
	header("Location: $url");
		
	

	$success = "block";
}


//for image upload
function uploadImage(){
	if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
	  $filename = basename($_FILES['uploaded_file']['name']);
	  $ext = substr($filename, strrpos($filename, '.') + 1);
	  if(($ext == "jpg" && $_FILES["uploaded_file"]["type"] == 'image/jpeg') || ($ext == "png" && $_FILES["uploaded_file"]["type"] == 'image/png') || ($ext == "gif" && $_FILES["uploaded_file"]["type"] == 'image/gif')){   
	  	$temp = explode(".",$_FILES["uploaded_file"]["name"]);
	  	$newfilename = NewGuid() . '.' .end($temp);
		move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], ROOT_PATH . '/img/upload/' . $newfilename);
		return $newfilename;
	  }
	  else{
	  	return '';
	  }
	}
	return '';
}
function NewGuid() { 
    $s = strtoupper(md5(uniqid(rand(),true))); 
    $guidText = 
        substr($s, 0,8) . '-' . 
        substr($s, 8,4) . '-' . 
        substr($s, 12,4). '-' . 
        substr($s, 16,4). '-' . 
        substr($s, 20); 
    return $guidText;
}

?>
<!-- Content Header (Page header) -->

<section class="content-header">
  <h1><?= $title;?></h1>
  <ol class="breadcrumb">
    <li><a href="<?= WEB_URL ?>dashboard.php"><i class="fa fa-dashboard"></i><?= $_data['home_breadcam'];?></a></li>
    <li class="active"><?= $_data['add_new_renter_information_breadcam']; ?></li>
    <li class="active"><?= $title; ?></li>
  </ol>
</section>
<!-- Main content -->
<section class="content">
<!-- Full Width boxes (Stat box) -->
<div class="row">
  <div class="col-md-12">
    <div align="right" style="margin-bottom:1%;"> </div>
    <div class="box box-success">
      <div class="box-header">
        <h3 class="box-title"><?= $_data['add_new_renter_entry_form'];?></h3>
      </div>
      <form onSubmit="return validateMe();" action="<?= $form_url; ?>" method="post" enctype="multipart/form-data" id="frm_tenant_entry">
        <div class="box-body row">
			<div class="form-group col-md-6">
				<label for="name"><span class="errorStar">*</span> <?= $_data['add_new_form_field_text_1'];?> :</label>
				<input type="text" name="name" value="<?= $name; ?>" id="name" class="form-control" />
			</div>
			<div class="form-group col-md-6">
				<label for="email"><span class="errorStar">*</span> <?= $_data['add_new_form_field_text_2'];?> :</label>
				<input type="text" name="email" value="<?= $email; ?>" id="email" class="form-control" />
			</div>
			<div class="form-group col-md-6">
				<label for="phone"><span class="errorStar">*</span> Phone :</label>
				<input type="text" name="phone" value="<?= $phone; ?>" id="phone" class="form-control" />
			</div>
			<div class="form-group  col-md-12">
				<label for="address"><span class="errorStar">*</span> Address :</label>
				<textarea name="address" id="address" class="form-control"><?= $address;?></textarea>
			</div>
			<div class="form-group col-md-6">
				<label for="business_name"><span class="errorStar">*</span> Business Name :</label>
				<input type="text" name="business_name" value="<?= $business_name; ?>" id="business_name" class="form-control" />
			</div>
			<div class="form-group col-md-6">
				<label for="business_phone"><span class="errorStar">*</span> Business Phone :</label>
				<input type="text" name="business_phone" value="<?= $business_phone; ?>" id="business_phone" class="form-control" />
			</div>
			<div class="form-group  col-md-6">
				<label for="nature_of_business"><span class="errorStar">*</span> Nature of Business :</label>
				<input type="text" name="nature_of_business" value="<?= $nature_of_business; ?>" id="nature_of_business" class="form-control" />
			</div>
			<div class="form-group col-md-12">
				<label for="Prsnttxtarea"><?= $_data['add_new_form_field_text_15'];?> :</label>
				<img class="form-control" src="<?= $image_rnt; ?>" style="height:100px;width:100px;" id="output"/>
				<input type="hidden" name="img_exist" value="<?= $img_track; ?>" />
			</div>
			<div class="form-group col-md-12"> <span class="btn btn-file btn btn-default"><?= $_data['upload_image'];?>
				<input type="file" name="uploaded_file" onchange="loadFile(event)" />
				</span> 
			</div>
        </div>
        <div class="box-footer">
          <div class="form-group pull-right">
            <?php if($hdnid=='0') { ?>
            <button type="button" onclick="tenant_email_exist();" name="button" class="btn btn-success"><i class="fa fa-floppy-o"></i> <?= $button_text; ?></button>
            <?php } else { ?>
            <button type="submit" name="button" class="btn btn-success"><i class="fa fa-floppy-o"></i> <?= $button_text; ?></button>
            <?php } ?>
            <a class="btn btn-warning" href="<?= WEB_URL; ?>tenant/tenantlist.php"><i class="fa fa-reply"></i> <?= $_data['back_text'];?></a> </div>
        </div>
      </form>
      <!-- /.box-body -->
    </div>
    <!-- /.box -->
  </div>
</div>
<!-- /.row -->
<script type="text/javascript">
function validateMe(){
	if($("#name").val() == ''){
		alert("<?= $_data['required_1']; ?>");
		$("#name").focus();
		return false;
	}
	else if($("#email").val() == '' || !checkValidEmail('email')){
		alert("<?= $_data['required_2']; ?>");
		$("#email").focus();
		return false;
	}
	else if($("#phone").val() == ''){
		alert("<?= $_data['required_3']; ?>");
		$("#phone").focus();
		return false;
	}
	else if($("#address").val() == ''){
		alert("<?= $_data['required_4']; ?>");
		$("#address").focus();
		return false;
	}
	else if($("#business_name").val() == ''){
		alert("<?= $_data['required_5']; ?>");
		$("#business_name").focus();
		return false;
	}
	else if($("#business_phone").val() == ''){
		alert("<?= $_data['required_6']; ?>");
		$("#business_phone").focus();
		return false;
	}
	else if($("#nature_of_business").val() == ''){
		alert("<?= $_data['required_7']; ?>");
		$("#nature_of_business").focus();
		return false;
	}
	else{
		return true;
	}

	/* var emergency_contact_dialcode = $("#emergency_contact_dialcode").val();
	if(emergency_contact_dialcode != ""){
		var inp = $(".emergency_contact_div .iti__country-list li.iti__preferred[data-dial-code=" + emergency_contact_dialcode.slice(1) + "]");
		var id = inp[0].id.split("-")[2];
		var input = document.querySelector('#emergency_contact_telephone');
		var iti = window.intlTelInputGlobals.getInstance(input);
		iti.setCountry(id);
	} */
}

//check renter email exist or not
function tenant_email_exist(){
   var email = $("#email").val();
   if(email != ''){
	   $.ajax({
		  url: '../ajax/getunit.php',
		  type: 'POST',
		  data: 'email='+email+'&token=tenant_email_exist',
		  dataType: 'json',
		  success: function(data) {
			 if(data != '-99'){
			 	if(data.email_exist){
					alert('<?= $_data['email_exist']; ?>');
					$("#txtREmail").focus();
				} else {
					jQuery("#frm_tenant_entry").submit();
				}
			 }
			 else{
			 	window.location.href = '../index.php';
			 }
		  }
	   });
   } else {
   		$("#frm_tenant_entry").submit();
   }
}
</script>
<?php include_once('../footer.php'); ?>
