<?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">
      <h6 class="m-0 font weight-bold text-primary">

        <div class="modal fade" id="homeproductModal" 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</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
                <form action="../homeproduct/add_homeproduct.php" id="addHomeproductForm" method="POST" enctype="multipart/form-data">
                  <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>Category</label>
                      <select class="form-control" id="category-dropdown" name="catid" onchange="get_subcategory();">
                                    <option value="">Select Category</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 home WHERE parent_id=0 ";
                       $executeQuery = $d->selectQueryInArray($query);
                              
                              if($executeQuery )  {
                                // code...
                             
                            foreach ($executeQuery as $row)
                              {
                                 
                              ?>
 <option value="<?php echo $row['id'];?>"><?php echo $row["category"];?></option>
<?php
}}
?>
           
                                   
                                </select>
                    </div>
                   <div class="form-group">
		  <label>Sub Category</label>
		  <select class="form-control" id="subcategory-dropdown" name="subcategory"action="get_subcat.php">
			 <option value="">Select SubCategory</option>
			  <?php  
			  $catid= $_GET['catid'];
			  $query = "SELECT * FROM subcategory WHERE catid='$catid'";
                       $executeQuery = $d->selectQueryInArray($query);
                              
                              if($executeQuery )  {
                                // code...
                             
                            foreach ($executeQuery as $row)
                              {
                                 
                              ?>
 <option value="<?php echo $row['id'];?>"><?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>Code</label>
                      <input type="text" name="code" id="code" class="form-control" placeholder="Enter Code">
                    </div>
                    <div class="form-group">
                      <label>Finish</label>
                      <input type="text" name="finish" id="finish" class="form-control" placeholder="Enter Finish">
                    </div>
                    <div class="form-group">
                      <label>Size</label>
                      <input type="text" name="size" id="size" class="form-control" placeholder="Enter Size">
                    </div>
                    <div class="form-group">
                      <label>Length</label>
                      <input type="text" name="length" id="length" class="form-control" placeholder="Enter Size">
                    </div>
                    <div class="form-group">
                      <label>Width</label>
                      <input type="text" name="width" id="width" class="form-control" placeholder="Enter Width">
                    </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="saveHomeproduct();">Save</button>
                  </div>
                </div></form>
                
              </div>
            </div>
          </div>
          <div class="card-body">
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#homeproductModal" style="float: right">
          Add new in Iron Product</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>Category</th>
                    <th>Subcategory</th>
                    <th>Product</th>
                    <th>Code</th>
                    <th>Finish</th>
                    <th>Size</th>
                    <th>Length</th>
                    <th>Width</th>
                    <th>Created_at</th>
                    <th>Updated_at</th>
                    <th>EDIT</th>
                    <th>DELETE</th>
                  </tr>
                </thead>
                <tbody>
                  <!-- append using getAllHome() function -->
                </tbody>
              </table>   
            </div></div>
          </div></div>
          <!-- End of Main Content -->






<?php
          include('../includes/scripts.php');
          include('../includes/footer.php');
          ?>
          <script src ="https:/code.jquery.com/jquery-3.5.1.min.js" crossorigin="anonymous"></script>
            <script>
            
            $(document).ready(function(){
                $('#category-dropdown').on('change',function(){
                    var catid = this.value;
                    $.ajax({
                        url: "get_subcat.php?category='.preg_replace('#[-]+#','-',trim($row['catid'])).'"> '.$row['catid'].'",
                        type: "POST",
                        data:{
                            catid:catid
                        },
                        cache:false,
                        success:function(result){
                            $("#subcategory-dropdown").html(result);
                        }
                    });
                });
            }

</script>
          <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(){
              getAllHomeproduct();
            });

            function saveHomeproduct(){
              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);
                $('#homeproductModal').modal('hide');
                $('#id').val('');
                $('#category').val('');
                $('#subcategory').val('');
                $('#product').val('');
                $('#code').val('');
                $('#finish').val('');
                $('#size').val('');
                $('#length').val('');
                $('#width').val('');
                getAllHomeproduct();
              }
              else{
                alert(json.message);
              }
              
            },
            error: function (response) {

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


        function editHomeproduct(id){
// alert(id);
$('#homeproductModal').modal('show');
$('#id').val(id);

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

    $('#id').val(json[0].id);
    $('#category').val(json[0].catid);
    $('#subcategory').val(json[0].subcategory);
    $('#product').val(json[0].product);
    $('#code').val(json[0].code);
    $('#finish').val(json[0].finish);
    $('#size').val(json[0].size);
    $('#length').val(json[0].length);
    $('#width').val(json[0].width);
  }
});
}

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


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

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

    }
  });
 }
}

function getAllHomeproduct(){
  $.ajax({
    type: "POST",
    data: 'id=' + id,
    url: '../homeproduct/get_all_homeproduct.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].category+`</td>
        <td>`+json[i].subcategory+`</td>
        <td>`+json[i].product+`</td>
        <td>`+json[i].code+`</td>
        <td>`+json[i].finish+`</td>
        <td>`+json[i].size+`</td>
        <td>`+json[i].length+`</td>
        <td>`+json[i].width+`</td>
        <td>`+json[i].created_at+`</td>
        <td>`+json[i].updated_at+`</td>
        <td> 
        <button type="button" class="btn btn-success" onclick="editHomeproduct(`+json[i].id+`);">
        <span class="glyphicon glyphicon-plus"></span> Edit</button>
        </td>
        <td>
        <button type="button" class="btn btn-danger" onclick="deleteHomeproduct(`+json[i].id+`);">
        <span class="glyphicon glyphicon-plus"></span> DELETE</button>
        </td> 
        </div>
        </tr>`;
      }
      $('#dataTable tbody').html(html);
    }
  });
}



</script>


          