<?php
  require_once("../../session_check.php");
  require_once("../../../classes/main_classes.php");

  //OBJECTS
  $retrieveObj=new RETRIEVE();
  $rowcountObj=new ROWCOUNT();

  $null_p_id=0;

  //get last inserted item
  $get_product=$retrieveObj->retriever("products","*","products.user_id",$user_id,"products.p_id","DESC","fetch");

  if($get_product!="1020"){
    //check if there if a product that has no image
    $check_products=$retrieveObj->retriever("product_images","*","product_images.p_id='".$get_product->p_id."' and p_image_type","primary_image","fetch");

    if($check_products=="1020"){
      $null_p_id=$get_product->p_id;
    }else{
       $null_p_id=0;
    }
  }
    
?>

<h4 class="page-title">
  <span class="la la-cubes"></span>
  Products
</h4>
<p><a href="#" class="bold" onclick="products_view()">Products</a> <span class="fas fa-angle-right"></span> Add product</p>
<div class="content-wrapper ">
    <div class="row">
        <div class="col-md-12">
          <div class="mx-auto card p-2">
            <p class="text-center lead">Add product : <i>Information</i></p>
            <form id="add_product_form" class="row">
              <div class="col-md-4">
                <div class="my-2">
                  <input class="form-control" type="text" name="product_name" placeholder="Product name">
                </div>
              </div>
              <div class="col-md-4">
                <div class="my-2">
                  <select class="form-control" name="product_category">
                    <option selected disabled>Product category</option>
                    <option value="food_product">Food product</option>
                    <option value="chemical_product">Chemical product</option>
                    <option value="tech_product">Tech product</option>
                  </select>
                </div>
              </div>
              <div class="col-md-4">
                <div class="my-2">
                  <input class="form-control" type="text" name="price" placeholder="Price">
                </div>
              </div>

              <div class="col-md-12 pt-3">
                <label for="product_description">Product Description :</label>
                <textarea class="form-control" name="product_description" id="product_description"></textarea>
              </div>
              <div class="col-md-12">
                <div class="my-2" id="add_product_response">
                  
                </div>

                <div class="my-2">
                  <input type="hidden" name="add_product_note">
                  <button type="submit" class="btn btn-primary btn-block enter-btn" onclick="add_product_basic_info()">Next</button>
                </div>
              </div>
            </form>
          </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var editor = new Simditor({
      textarea: $('#product_description')
      //optional options
    });

    
    var null_p_id=<?php echo $null_p_id ?>

    $(document).ready(function() {
      if(null_p_id==0){
        
      }else{
        $("#main_section").load("includes_/products/add_product_main_image.php",{
          p_id:null_p_id
        });
      }
      
    })
</script>
