<?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');

}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
		$cate=$_POST['cate_id'];
	$merchant_title=$_POST['merchant_title'];
	$no_of_flyer=$_POST['no_of_flyer'];
	$merchant_email=$_POST['merchant_email'];
	$description=$_POST['description'];
	
	
	/* if(empty($store)){
		$error=true;
		$errors['store']='Please select store name';
	} */
	
	/* if(empty($city)){
		$error=true;
		$errors['city']='Please select city';
	} */
	
	if(empty($cate)){
		$error=true;
		$errors['cate']='Please select category name';
	}
	$front_picture=$_FILES['front']['name'];
	//$back_picture=$_FILES['back']['name'];
	
	if($error==false)
	{
		if($front_picture!=''){
			$destinationPath= getcwd().'/uploads/flyer';
			if(!file_exists($destinationPath)){
				mkdir($destinationPath,0777,true);
			}
			$file_name=time().'_'.$_FILES['front']['name'];
			
			$tempName=$_FILES['front']['tmp_name'];
			move_uploaded_file($tempName,$destinationPath.'/'.$file_name);
			$filePath=ROOT.'/uploads/flyer/'.$file_name;
		}else{
			$filePath='';
		}
		/* if($back_picture!=''){
			$destinationPathback= getcwd().'/uploads/flyer';
			if(!file_exists($destinationPathback)){
				mkdir($destinationPathback,0777,true);
			}
			$file_nameback=time().'_'.$_FILES['back']['name'];
			
			$tempNameback=$_FILES['back']['tmp_name'];
			move_uploaded_file($tempNameback,$destinationPathback.'/'.$file_nameback);
			$filePathback=ROOT.'/uploads/flyer/'.$file_nameback;
		}else{
			$filePathback='';
		} */
		
		if(!empty($_SESSION['user'])){
			$user_id=$_SESSION['user'];
		}else{
			$user_id=1;
		}
		
		$createEmployee=mysqli_query($con,"INSERT INTO flyers (cate_id,merchant_title,no_of_flyer,description,front_flyer,status,created_by) VALUES('".$cate."','".$merchant_title."','".$no_of_flyer."','".$description."','".$filePath."',1,'".$user_id."')");
		
		if($createEmployee){
			$errors['success']='Product created successfully.';
		}else{
			$error=true;
			$errors['error']='Something went wrong!!!, Please try again.';
		}
	}
	
	
}

	




$cities=mysqli_query($con,"SELECT * FROM cities  ORDER BY id ASC ");
$category=mysqli_query($con,"SELECT * FROM categories ");
?>
<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 Bid Product</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">
	<select name="cate_id" class="form-control" required>
	<option value="">Select Category</option>
	<?php if(mysqli_num_rows($category)>0){
		  while ($cat=mysqli_fetch_array($category)){
	?>
		<option value="<?php echo $cat['id'];?>"><?php echo $cat['name'];?></option>
	<?php } } ?>
	</select>
	<?php if($error==true && isset($errors['cate_id'])){?>
		<p class="error"><?php echo $errors['cate_id'];?></p>
	<?php } ?>
   </div>
   </div>
   
   
   <div class="col-md-6">
   
   <div class="form-group">
	<input type="text" name="merchant_title" class="form-control" placeholder="Product Name" >
	<?php if($error==true && isset($errors['merchant_title'])){?>
		<p class="error"><?php echo $errors['merchant_title'];?></p>
	<?php } ?>
   </div>
   </div>
   </div>
   
   <div class="row">
   <div class="col-md-6">
  
   <div class="form-group">
	<input type="text" name="no_of_flyer" class="form-control" placeholder="Price" >
	<?php if($error==true && isset($errors['no_of_flyer'])){?>
		<p class="error"><?php echo $errors['no_of_flyer'];?></p>
	<?php } ?>
   </div>
   </div>
   
   </div>
 
   
   <div class="row">
    <div class="col-md-6">
	 <label>Description </label>
   <div class="form-group">
	<textarea name="description" rows="5" class="form-control" ></textarea>
   </div>
   </div>
   <div class="col-md-6">
	 <label>Product Image </label>
   <div class="form-group">
	<input type="file" name="front"  >
   </div>
   </div>
   
   </div>
   <div class="box-footer">
		<button type="submit" class="btn btn-primary">Save</button>
   </div>
			  
   </form>
  </div>
  </div>
  </div>
  </div>
  </div>
  </div>
<?php include_once 'includes/footer.php'; ?>