<?php 
if(!isset($_GET['pid'])){
	header("location: upload_product");
} 

include("side_nav.php"); 
include("top_nav.php");

function status($a,$b){
	if($a == $b){
		return 'selected';
	}
}
 
$product_id = $_GET['pid'];
$output = '';
require('class/cproduct.php');
$product = new Product('product');

if(isset($_POST['submit'])){
   
   $other_images = [];

   $dir = "../../uploads/";
   
   $temp = $_FILES['main_image']['tmp_name'];
   $main_image = $_FILES['main_image']['name'];
   $targetFilePath = $dir . $main_image;
   move_uploaded_file($temp, $targetFilePath);

   foreach($_FILES['other_images']['name'] as $key => $val){
	   $temp = $_FILES['other_images']['tmp_name'][$key];
	   $fileName = $_FILES['other_images']['name'][$key];
	   $targetFilePath = $dir . $fileName;
	   array_push($other_images,  $fileName);
	   move_uploaded_file($temp, $targetFilePath);
   }
    
   $product_name = $_POST['product_name'];
   $barcode = $_POST['barcode'];
   $formulation  = $_POST['formulation'];
   $category     = $_POST['category'];
   $sub_category_id     = $_POST['sub_category_id'];
   $company      = $_POST['company'];
   $package      = $_POST['package'];
   $tag          = $_POST['tag'];
   $product_info = $_POST['product_info'];  
   
   $response = $product->updateProduct($main_image,$other_images,$product_name,$barcode,$formulation,$category,$sub_category_id,$company,$package,$tag,$product_info,$user_id,$product_id);
   
    if($response == true){
		$output = '<div class="alert alert-success alert-dismissible fade in" role="alert">
						<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span>
						</button>
						<strong>Success. </strong> Product updated.
					</div>';
	}else{
		$output =   '<div class="alert alert-danger alert-dismissible fade in" role="alert">
						<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span>
						</button>
						<strong>Failed. </strong> Could not update product. Please try again.
				    </div>';
	}
}
?>


        <!-- page content -->
        <div class="right_col" role="main">
          <div class="">
            <div class="clearfix"></div>
            <div class="row">
              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>Update product<small></small></h2>
                    <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                      </li>
                      <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                        <ul class="dropdown-menu" role="menu">
                          <li><a href="#">Settings 1</a>
                          </li>
                          <li><a href="#">Settings 2</a>
                          </li>
                        </ul>
                      </li>
                      <li><a class="close-link"><i class="fa fa-close"></i></a>
                      </li>
                    </ul>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
                    <br />
					<?php
					    $product_id = $_GET['pid'];
						$sql = mysqli_query($con, "SELECT * FROM product WHERE product_id = '$product_id' ");
						$res = mysqli_fetch_array($sql);
					?>
                    <form action="update_product?pid=<?php echo $product_id; ?>" method="POST" enctype="multipart/form-data" class="form-horizontal form-label-left">

						<div class="form-group">
							<div class="col-md-8 col-sm-8 col-xs-12 col-md-offset-2">
									<?php echo $output; ?>
							</div>
						</div>
					
						<div class="form-group ">
						    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name"></label>
						    <div class="col-md-2 ">
								<div class="image view view-first">
									<img style="width: 100%;  display: block;" id="profile-img" 
									   src="
									   <?php 
											if($res['main_image_url'] == " "){
												echo "../../uploads/no_image.jpg";
											}else{
												echo '../../uploads/' . $res['main_image_url']; 
											} 
									   ?>"  alt="image" />
								</div>
							</div>
						    
						</div>
					
					  <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Product Main Image <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input type="file" name="main_image" class="form-control col-md-7 col-xs-12" >
                        </div>
                      </div>
					  
					  <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Other Images (Max of 3) <span class="required"></span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input type="file" name="other_images[]" class="form-control col-md-7 col-xs-12" multiple>
                        </div>
                      </div>

                       <div class="form-group">
					        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name"></label>
					         <?php 
							   $images =  explode(',', $res['other_images_url']); 
							   foreach($images as $img):
						    ?>
							<div class="col-md-2 ">
								  <div class="image view view-first">
									<img style="width: 100%;  display: block;" id="profile-img" 
									   src="../../uploads/<?php if($img == ""){echo "no_image.jpg";}elseif(isset($img)){ echo $img;} ?>"  alt="image" />
								  </div>
							</div>
						  <?php endforeach; ?>
                        </div>					   
					  
                      <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Product Name <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input type="text" name="product_name" required="required" value="<?php echo $res['product_name'];  ?>" class="form-control col-md-7 col-xs-12">
                        </div>
                      </div>
					  <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Barcode <span class="required"></span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input type="text" name="barcode" value="<?php echo $res['barcode'];  ?>" class="form-control col-md-7 col-xs-12">
                        </div>
                      </div>
					  <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">Formulation</label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <select name="formulation"  class="form-control" >
						        <option value="<?php echo $res['formulation_id'] ?> "> <?php echo $product->trans('formulation', 'formulation_name', 'formulation_id', $res['formulation_id']) ;  ?> </option > 
						        <option > <?php
                                    $filter = mysqli_query($con, "SELECT * FROM formulation");
                                    while($row = mysqli_fetch_array($filter))
                                    {
                                    echo"<option value=".$row['formulation_id'].">" . $row['formulation_name'] . "</option>";
                                    }
                                   ?> 
								</option>
                            </select>
                        </div>
                      </div>
					  
                      <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">Category</label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <select name="category" id="category" class="form-control">
						         <option value="<?php echo $res['category_id'] ?>" > <?php echo $product->trans('category', 'category_name', 'category_id', $res['category_id']) ;  ?> </option > 
						         <option > <?php
                                    $filter = mysqli_query($con, "SELECT * FROM category WHERE display_status = 1 ");
                                    while($row = mysqli_fetch_array($filter)){
                                         echo"<option value=".$row['category_id'].">" . $row['category_name'] . "</option>";
                                    }
                                   ?> 
								</option>
                            </select>
                        </div>
                      </div>
					  
					  <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">Subcategory</label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <select name="sub_category_id" id="subcategory" class="form-control">
							    <?php  
								  if(empty($res['category_id'])):
								?>
                            	<option>-------Select category first--------</option>
								<?php endif; ?>
								<option value="<?php echo $res['sub_category_id'] ?>" > <?php echo $product->trans('sub_category', 'sub_category', 'sub_category_id', $res['sub_category_id']) ;  ?> </option >
                            </select>
                        </div>
                      </div>
					  
					  <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">Package</label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <select name="package"  class="form-control">
						        <option value="<?php echo $res['package_id'] ?>" ><?php echo $product->trans('package', 'package_name', 'package_id', $res['package_id']);  ?> </option >
                            	<option > <?php
                                    $filter =mysqli_query($con, "SELECT * FROM package");
                                    while($row = mysqli_fetch_array($filter))
                                    {
                                    echo"<option value=".$row['package_id'].">" . $row['package_name'] . "</option>";
                                    }
                                   ?> 
								</option>
                            </select>
                        </div>
                      </div>
					  
					  <div class="form-group">
                        <label for="middle-name" class="control-label col-md-3 col-sm-3 col-xs-12">Tag</label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input class="form-control col-md-7 col-xs-12" value="<?php echo $res['tag'];  ?>" type="text" name="tag" placeholder="Separate each tag by space">
                        </div>
                      </div>
					  
					  <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">Company</label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <select  name="company"  class="form-control">
						  <option value="<?php echo $res['company_id'] ?>" ><?php echo $product->trans('company', 'company_name', 'company_id', $res['company_id']);  ?> </option >
                            	<option > <?php
                                    $filter =mysqli_query($con, "SELECT * FROM company");
                                    while($row = mysqli_fetch_array($filter))
                                    {
                                    echo"<option value=".$row['company_id'].">" . $row['company_name'] . "</option>";
                                    }
                                   ?> 
								</option>
                            </select>
                        </div>
                      </div>
					  
					  <div class="form-group">
					   <label for="middle-name" class="control-label col-md-3 col-sm-3 col-xs-12">Product Info</label>
					    <div class="col-md-6 col-sm-6 col-xs-12">
					        <textarea name="product_info" required="required" class="form-control col-md-7 col-xs-12" ><?php echo $res['product_info'];  ?></textarea>
					     </div>
					  </div>
					  
					  <div class="ln_solid"></div>
                      <div class="form-group">
                        <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                          <button type="submit" name="submit" class="btn btn-primary">Update</button>
                        </div>
                      </div>

                    </form>
                  </div>
                </div>
              </div>
            </div>
            </div>
			</div>
    <!-- jQuery -->
    <script src="../vendors/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="../vendors/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- FastClick -->
    
    <!-- Custom Theme Scripts -->
    <script src="../build/js/custom.min.js"></script>
	</body>
</html>    
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/froala-editor/2.7.3/js/froala_editor.pkgd.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/froala-editor@3.0.4/js/froala_editor.pkgd.min.js"></script>  

<script> $(function() { $('textarea').froalaEditor() }); </script>
<script>
$(document).ready(function(){
    $('#category').on('change',function(){
        var categoryId = $(this).val();
		
        if(categoryId){
            $.ajax({
                type:'POST',
                url:'process.php',
                data:{
				  category_id : categoryId,	
				},
                success:function(html){
                    $('#subcategory').html(html);
                }
            }); 
        }else{
            $('#subcategory').html('<option value="">Select category first</option>');
        }
    });
});
</script>
		