<?php
session_start();
require_once 'includes/header.php';
require_once 'includes/config.php';
$error=false;
$errors=array();
// avoid user arriving to this page without logging in
if(!isset($_SESSION['user_logged_in']) && $_SESSION['user_logged_in']!=TRUE )
{
	header('Location:login.php');

}
$jobs=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM jobs WHERE id=".$_GET['id']));
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
	
	
	$merchant_email=$_POST['merchant_email'];
	$title=$_POST['title'];
	$location=$_POST['location'];
	$job_type=$_POST['job_type'];
	$company_name=$_POST['company_name'];
	$phone=$_POST['phone'];
	$last_date=$_POST['last_date'];
	$description=$_POST['description'];
	$salary=$_POST['salary'];
	
	
	
	$front_picture=$_FILES['image']['name'];
	
	
	
	if($front_picture!=''){
			$destinationPath= getcwd().'/uploads/job';
			if(!file_exists($destinationPath)){
				mkdir($destinationPath,0777,true);
			}
			$file_name=time().'_'.$_FILES['image']['name'];
			
			$tempName=$_FILES['image']['tmp_name'];
			move_uploaded_file($tempName,$destinationPath.'/'.$file_name);
			$filePath=ROOT.'/uploads/job/'.$file_name;
		}else{
			$filePath=$jobs['image'];
		}
	
	if($error==false)
	{
		$createCustomer=mysqli_query($con,"UPDATE jobs SET merchant_email='".$merchant_email."',title='".$title."',location='".$location."',type='".$job_type."',company_name='".$company_name."',contact_no='".$phone."',last_date='".$last_date."',salary='".$salary."',image='".$filePath."',description='".$description."' WHERE id=".$_GET['id']);
		//$createCustomer=mysqli_query($con,"INSERT INTO jobs (merchant_email,title,location,type,company_name,contact_no,last_date,salary,description,image,status) VALUES('".$merchant_email."','".$title."','".$location."','".$job_type."','".$company_name."','".$phone."','".$last_date."','".$salary."','".$description."','".$filePath."',1)");
		
		if($createCustomer){
			$errors['success']='Job updated successfully.';
		}else{
			$error=true;
			$errors['error']='Something went wrong!!!, Please try again.';
		}
	}
	
	
}

?>
<style>
.modal{
	z-index: 9999;
	max-height: 55%;
	
}
.modal-content{
	border:none;
}
</style>
 <div class="content">
        <div class="container-fluid">
          <div class="row">
            <div class="col-md-12">
              <div class="card">
                <div class="card-header card-header-primary">
                  <h4 class="card-title">Add Job</h4>
                  <p class="card-category"></p>
                </div>
                <div class="card-body">

<?php

if (isset($error) && $error==false && isset($errors['success'])) {
	echo '<div class="alert alert-success" role="alert" id="success_message">Success <i class="glyphicon glyphicon-thumbs-up"></i> '.$errors['success'].'</div>';
} 

if (isset($error) && $error==true && isset($errors['error'])) {
	echo '<div class="alert alert-danger" role="alert" id="success_message">Success <i class="glyphicon glyphicon-thumbs-up"></i> '.$errors['error'].'</div>';
} 

?>

  <form role="form" method="post" enctype="multipart/form-data">
   <div class="box-body">
   

   
   <div class="row">
    <div class="col-md-6">
	 <div class="form-group">
	<label>Title</label>
	<input type="text" name="title" value="<?php echo $jobs['title']; ?>">
    </div>
	
   </div>
  
   <div class="col-md-6">
	 <div class="form-group">
	<label>Location</label>
	<input type="text" name="location" value="<?php echo $jobs['location']; ?>">
    </div>
	
   </div>

   </div>
   
    <div class="row">
    <div class="col-md-6">
	<label>Job Type</label>
	 <div class="form-group">	
	<select class="form-control" name="job_type">
	<option value="">Select Job Type</option>
	<option value="part_time" <?php echo ($jobs['type']=='part_time')?'selected':''; ?>>Part Time</option>
	<option value="full_time"<?php echo ($jobs['type']=='full_time')?'selected':''; ?>>Full Time</option>
	<option value="freelance" <?php echo ($jobs['type']=='freelance')?'selected':''; ?>>Freelance</option>
	</select>
    </div>
	
   </div>
  
   <div class="col-md-6">
	 <div class="form-group">
	<label>Contact No</label>
	<input type="text" name="phone" value="<?php echo $jobs['contact_no']; ?>">
    </div>
	
   </div>

   </div>
   
    <div class="row">
	 <div class="col-md-6">
	 <div class="form-group">
	<label>Merchant Email</label>
	<input type="text" name="merchant_email" value="<?php echo $jobs['merchant_email']; ?>">
    </div>
	
   </div>
       <div class="col-md-6">
		 <div class="form-group">
	<label>Last Date</label>
	 <input name="last_date" data-date-format="mm/dd/yyyy" placeholder="End Date" type="text" id="last-date" class="form-control datepicker" value="<?php echo $jobs['last_date']; ?>">
    </div>
	
   </div>
   </div>
     <div class="row">
	 <div class="col-md-6">
	  <div class="form-group">
	<div class="file-field input-field">
      <div class="btn btn-primary">
        <span>Image</span>
        <input type="file" name="image">
      </div>
      <img src="<?php echo $jobs['image'];?>" width="100" height="100" alt=""/>
    </div>
    </div>

	
   </div>
    <div class="col-md-6">
	 <div class="form-group">
	<label>Organization Name</label>
	<input type="text" name="company_name" value="<?php echo $jobs['company_name']; ?>">
    </div>
	
   </div>
   </div>
   <div class="row">
   <div class="col-md-6">
	 <div class="form-group">
	<label>Salary</label>
	<input type="text" name="salary" value="<?php echo $jobs['salary']; ?>">
    </div>
	
   </div>
   </div>
   
   <div class="row">
    <div class="col-md-12">
   
   <div class="form-group">
   <label>Description</label><br>
	<textarea type="text" name="description" class="form-control"  ><?php echo $jobs['description']; ?></textarea>
	
   </div>
   </div>
   
   </div>
   
   
   
   <div class="box-footer">
		<button type="submit" class="btn btn-primary">Save</button>
   </div>
			  
   </form>
  </div>
  </div>
  </div>
  </div>
  </div>
  </div>
<script>   CKEDITOR.replace( 'description' );   </script>
<?php include_once 'includes/footer.php'; ?>
<script type="text/javascript">
$(document).ready(function(){
  $('#last-date').datepicker();
    //$('#last-date').datepicker({ dateFormat: 'yy-mm-dd',onSelect: function(dateText, inst) {$('#last-date').val(dateText);}});
   // $('#last-date').datepicker({ onSelect: function(dateText, inst) {alert(dateText );}});
  });
  </script>