<?php
include('../config/constant.php');
include('../includes/header.php');
include('../includes/navbar.php');
?>
<!-- Begin Page Content-->
<div class="container-fluid">


  <!-- Button trigger modal -->

  <div class="card shadow mb-4">
    <div class="card-header py-3">
      <h5 class="m-0 font weight-bold text-primary">Product</h5></div>
        <div class="modal-body">
                <form action="../productdes/add_productdes.php" id="addProductdesForm" method="POST" enctype="multipart/form-data">

        <div class="modal fade" id="productdesModal" tabindex="1" role="dialog" aria-labelledby="examplemodalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">product Description</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>

              <div class="modal-body">
                    <div class="form-group">

                      <input type="hidden" name="id" id="id" class="form-control" placeholder="Enter Id">
                    </div>
                    <div class="form-group">
                      <label>Subcategory</label>
                      <select class="form-control" id="subcategory-dropdown" name="subcategory">
                                    <option value="">Select Subcategory</option>
                                    <?php 
                   require_once '../config/db.php';
                   require_once '../config/database.class.php';
                   require_once '../config/functions.php';
                   $d = new database($db_config);?>
                   
                       
                     <?php 
                               $query = "SELECT * FROM subcategory ";
                       $executeQuery = $d->selectQueryInArray($query);
                              
                              if($executeQuery )  {
                                // code...
                             
                            foreach ($executeQuery as $row)
                              {
                              ?>

<option value="<?php echo $row['subcategory'];?>"><?php echo $row["subcategory"];?></option>
<?php
}}
?>
           
                                   
                                </select>
                    </div>
                     
                     <div class="form-group">
                      <label>Product</label>
                      <input type="text" name="product" id="product" class="form-control" placeholder="Enter Product">
                    </div>
                     <div class="form-group">
                      <label>Description</label>
                    <textarea style="resize: none; min-height:250px; min-width:430px;" name="description" id="description"></textarea>
                  </div>
                   
                  <div class="form-group">
                      <label>Choose product</label>
                      <input type="text" name="choose" id="choose" class="form-control" placeholder="Enter Choose Product">
                    </div>
                    <div class="form-group">
                      <label>Delivery Option</label>
                      <input type="text" name="delivery" id="delivery" class="form-control" placeholder="Enter Delivery Option">
                    </div>
                     
                    
                  </div>
                 <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" name="registerbtn" class="registerbtn btn-primary btn" onclick="saveProductdes();" >Save</button>
                  </div>
                </div>
             </form>
              </div>
            </div>
          </div>
          <div class="card-body">
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#productdesModal" style="float: right">
          Add product Description
        </button>
        <br/>
        <br/>
        <br/>

            <div class="table-responsive">
              <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                <thead>
                  <tr>
                    <th>ID</th>
                    <th>Subcategory</th>
                    <th>Product</th>
                    <th>Description</th>
                    <th>Choose Our Product</th>
                    <th>Delivery Option</th>
                    
                    <th>Created_at</th>
                    <th>Updated_at</th>
                    <th>EDIT</th>
                    <th>DELETE</th>
                  </tr>
                </thead>
                <tbody>
                  <!-- append using getAllServices() function -->
                </tbody>
              </table>   
            </div></div>
          </div>
          <!-- End of Main Content -->


          <?php
          include('../includes/scripts.php');
          include('../includes/footer.php');
          ?>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.3.0/jquery.form.min.js" ></script>
          <script type="text/javascript">
            $(document).ready(function(){
              getAllProductdes();
            });

            function saveProductdes(){
              var options = {
                beforeSend: function () {
              //anything before sending request eg loader etc....
            },
            success: function (res) {
              // once receive response from backend
              //parese json
              var json = $.parseJSON(res);
              if(json.status=='success'){
                alert(json.message);
                $('#productdesModal').modal('hide');
                $('#id').val('');
                $('#subcategory').val('');
                $('#product').val('');
                $('#description').val('');
                $('#choose').val(''); 
                $('#delivery').val('');
                getAllProductdes();
              }
              else{
                alert(json.message);
              }
              
            },
            error: function (response) {

            }        
          };
          $('#addProductdesForm').ajaxForm(options);
        }


        function editProduct(id){
// alert(id);
$('#productdesModal').modal('show');
$('#id').val(id);

$.ajax({
  type: "POST",
  data: 'id=' + id,
  url: '../productdes/get_productdes_by_id.php',
  success: function (response) {
    console.log(response)
    var json = $.parseJSON(response);

    $('#id').val(json[0].id);
    $('#subcategory').val(json[0].subcategory);
    $('#product').val(json[0].product);
    $('#description').val(json[0].description);
    $('#choose').val(json[0].choose);
    $('#delivery').val(json[0].delivery);
     
  }
});
}

function deleteProduct(id){
  if(confirm("Are you sure to delete")){


   $.ajax({
    type: "POST",
    data: 'id=' + id,
    url: '../productdes/delete_productdes.php',
    success: function (response) {
      console.log(response)
      var json = $.parseJSON(response);

      if(json.status=='success'){
        alert(json.message);
         getAllProductdes();
      }
      else{
        alert(json.message);
      }

    }
  });
 }
}

function  getAllProductdes(){
  $.ajax({
    type: "POST",
    data: 'id=' + id,
    url: '../productdes/get_all_productdes.php',
    success: function (response) {

      var json = $.parseJSON(response);
      var html = "";
      $('#dataTable tbody').html(html);

      for(i=0;i<json.length;i++){
        html = html+`
        <tr>
        <td>`+(i+1)+`</td>
        <td>`+json[i].subcategory+`</td>
        <td>`+json[i].product+`</td>
        <td>`+json[i].description+`</td>
        <td>`+json[i].choose+`</td>
        <td>`+json[i].delivery+`</td>
        
        <td>`+json[i].created_at+`</td>
        <td>`+json[i].updated_at+`</td>
        <td> 
        <button type="button" class="btn btn-success" onclick="editProduct(`+json[i].id+`);">
        <span class="glyphicon glyphicon-plus"></span> Edit</button>
        </td>
        <td>
        <button type="button" class="btn btn-danger" onclick="deleteProduct(`+json[i].id+`);">
        <span class="glyphicon glyphicon-plus"></span> DELETE</button>
        </td> 
        </div>
        </tr>`;
      }
      $('#dataTable tbody').html(html);
    }
  });
}



</script>
