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

$output = '';

if(isset($_POST['submit'])){
   require('class/cproduct.php');
   $product = new Product('product');
   $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'];
   $product_id = substr($product_name, 0, 3) . rand(100000000,10000000000000) . date('d');
   $barcode  = $_POST['barcode'];
   $formulation  = $_POST['formulation'];
   $category     = $_POST['category'];
   $sub_category_id     = $_POST['sub_category_id'];
   $company      = $_POST['company'];
   $package      = $_POST['package'];
   $cost        = $_POST['cost'];
   $price        = $_POST['price'];
   $tag          = $_POST['tag'];
   $quantity     = $_POST['quantity'];
   $product_description = $_POST['product_description'];
   if($product->isUploadedProduct($product_name) == true){
	   $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> Product uploaded already. To continue Rename image and product.
				    </div>';
   }else{
	    $response = $product->uploadProduct($product_id,$barcode,$main_image,$other_images,$product_name,$cost,$price,$quantity,$formulation,$category,$sub_category_id,$company,$package,$tag,$product_description,$user_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 uploaded.
					</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 upload 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>Add new 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 />
                    <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" 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">Product Main Image <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input type="file" required="required" 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" required="required" 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">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" 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" 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="Price">Cost price <span class="required"></span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input type="number" name="cost"  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="Price">Selling price <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input type="number" name="price" required="required" 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="quantity">Quantity <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input type="text" name="quantity" placeholder="starting stock" 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 > <?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 > <?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">
                            	<option>-------Select category first--------</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 > 
								<?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" 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 > <?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 Description</label>
					    <div class="col-md-6 col-sm-6 col-xs-12">
					        <textarea name="product_description"  class="form-control col-md-7 col-xs-12" ></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">Submit</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>
$(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>
<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>