<?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') {
    
	//$store=$_POST['store'];
	
	$cate=$_POST['cate_id'];
	$merchant_title=$_POST['merchant_title'];
	$no_of_flyer=$_POST['no_of_flyer'];	
	$description=$_POST['description'];
	
	
	
	
	
	
	
	if(empty($cate)){
		$error=true;
		$errors['cate']='Please select category name';
	}
	
	
	if($error==false)
	{
		
		
		$createEmployee=mysqli_query($con,"UPDATE products SET cate_id='".$cate."',name='".$merchant_title."',price='".$no_of_flyer."',description='".$description."' WHERE id=".$_GET['id']);
		//echo '<pre>';print_r($_FILES['image']['name']);
		if( !empty ($_FILES['image']['name'][0])){ 
			mysqli_query($con,"DELETE FROM product_img WHERE product_id=".$_GET['id']);
			foreach($_FILES["image"]["tmp_name"] as $key=>$tmp_name) {
				
				$destinationPath= getcwd().'/uploads/product';
				if(!file_exists($destinationPath)){
					mkdir($destinationPath,0777,true);
				}
				$file_name=time().'_'.str_replace(' ', '_', $_FILES['image']['name'][$key]);
				
				$tempName=$_FILES['image']['tmp_name'][$key];
				move_uploaded_file($tempName,$destinationPath.'/'.$file_name);
				$filePath=ROOT.'/uploads/product/'.$file_name;
				
				mysqli_query($con,"INSERT INTO product_img (product_id,image,status) VALUES('".$_GET['id']."','".$filePath."',1)");
				
			}
		}
		if($createEmployee){
			$errors['success']='Product updated successfully.';
		}else{
			$error=true;
			$errors['error']='Something went wrong!!!, Please try again.';
		}
	}
	
	
}




$category=mysqli_query($con,"SELECT * FROM categories ");
$flyer=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM products WHERE id=".$_GET['id']));
?>
<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">Edit 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>';
} 

?>

  <div class="box box-primary">
  
  
   <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 <?php echo ($flyer['cate_id']==$cat['id'])?'selected':''; ?> 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" value="<?php echo $flyer['name']; ?>" >
	<?php if($error==true && isset($errors['name'])){?>
		<p class="error"><?php echo $errors['name'];?></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" value="<?php echo $flyer['price']; ?>" >
	<?php if($error==true && isset($errors['price'])){?>
		<p class="error"><?php echo $errors['price'];?></p>
	<?php } ?>
   </div>
   </div>
   
   </div>
 
   
   <div class="row">
    <div class="col-md-8">
	 <label>Description </label>
   <div class="form-group">
	<textarea name="description" rows="5" class="form-control" ><?php echo $flyer['description']; ?></textarea>
   </div>
   </div>
   <div class="col-md-4">
   <div class="form-group">
	<div class="file-field input-field">
      <div class="btn btn-primary">
        <span>Images</span>
        <input multiple type="file" name="image[]">
      </div>
      
    </div>
   </div>
  
   <?php $imges=mysqli_query($con,"SELECT * FROM product_img WHERE product_id=".$flyer['id']." "); ?>
    <?php if(mysqli_num_rows($imges)>0){
		  while ($img=mysqli_fetch_array($imges)){ 
	?>
	<img src="<?php echo $img['image'];?>" width="100" height="100" alt=""/>
		
	<?php } } ?>
		
   </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'; ?>