<?php
include_once 'header.php';
?>

<div class="page-wrapper">
        <!-- ============================================================== -->
        <!-- Bread crumb and right sidebar toggle -->
        <!-- ============================================================== -->
        <div class="page-breadcrumb">
          <div class="row">
            <div class="col-12 d-flex no-block align-items-center">
              <h4 class="page-title">All Inventory List</h4>
              <div class="ms-auto text-end">
                <nav aria-label="breadcrumb">
                  <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="#">Dashboard</a></li>
                    <li class="breadcrumb-item active" aria-current="page">
                     Inventory
                    </li>
                    <li class="breadcrumb-item"><a id="add" href="#">Add Inventory</a></li>
                  </ol>
                </nav>
              </div>
            </div>
          </div>
        </div>
        <!-- ============================================================== -->
        <!-- End Bread crumb and right sidebar toggle -->
    
        <div class="container-fluid row">
            <div class="col-12">  
              <div class="card">
                <div class="card-body">
                  <h5 class="card-title"> </h5>
                  <div class="table-responsive">
                    <table id="zero_config" class="table table-striped table-bordered">
                      <thead>
                        <tr>
                          <th>S.No</th>
                          <th>Particular</th>
                          <th>Description</th>
                          <th>Amount</th>
                          <th>Date</th>
                          <th>Action</th>
                        </tr>
                      </thead>
                      <tbody id="tbody">
                      <?php
                             
                                $obj=new Database();
                                $obj->select('inventory','*',null,null,'id DESC',null);
                                $result=$obj->getResult(); 
                                $c=0;
                                                              
                                foreach($result as list("id"=>$id,"particular"=>$particular,"description"=>$desc,"amount"=>$amount,"date"=>$date,"f_id"=>$f_id))
                                {                                 
                                  $c=$c+1;
                                  echo "<tr><td>$c</td><td>$particular</td><td>$desc</td><td>$amount</td><td>$date</td><td><button class='badge btn-info btn-view' name='edit[]' data-id=$f_id id='edit'><span class='mdi-tooltip-edit'></span></button>&nbsp;&nbsp;<button class='badge btn-danger' name='id[]' data-id=$f_id id='id'>X</button></td></tr>";
                                }                              
                                ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <!-- ============================================================== -->
          <!-- End PAge Content -->
          <!-- ============================================================== -->
          <!-- ============================================================== -->

<!-- The Modal -->
<div class="modal" id="myModal">
  <div class="modal-dialog" style="max-width: 40%;">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Add Inventory</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
        <form id="modelForm">
		<div class="row">                                                    
                                                 <div class="col-md-12">
                                                        <div class="card">
                                                            <form class="form-horizontal" id="form">
                                                                <div class="card-body">
                                                                    <h4 class="card-title"></h4>
																	                                  <div class="form-group row">
                                                                        <label for="particular" class="col-sm-3 text-end control-label col-form-label">Particular</label>
                                                                        <div class="col-sm-9">
                                                                            <select class="form-control" id="particular">
                                                                               <option></option>
																			                                         <option>Salary</option>
                                                                               <option>Foods</option>
                                                                               <option>E Bills/Others</option>
                                                                               <option>Repair/Services</option>
                                                                               <option>Goods Purchase</option>                                                                         
                                                                               <option>Shop/Office Rent</option>
                                                                               <option>Travel Fare</option>
                                                                               <option>Tour Party/Trip</option>
                                          
                                                                            </select>
                                                                        </div>
                                                                    </div>
                                                                    <div class="form-group row">
                                                                        <label for="desc" class="col-sm-3 text-end control-label col-form-label">
                                                                        Description</label>
                                                                        <div class="col-sm-9">
                                                                            <input type="text" class="form-control" id="desc"/>
                                                                        </div>
                                                                    </div>
                                                                    <div class="form-group row">
                                                                        <label for="amount" class="col-sm-3 text-end control-label col-form-label">
                                                                        Amount</label>
                                                                        <div class="col-sm-9">
                                                                        <input type="number" class="form-control" id="amount"/>
                                                                        </div>
                                                                    </div>       
                                               
                                                   
                                                                    
                                                                    <div class="form-group row">
                                                                            <label for="date" class="col-sm-3 text-end control-label col-form-label">Date</label>
                                                                            <div class="col-sm-9">
                                                                                <input type="date" class="form-control date-inputmask" id="date" value="<?php echo date('Y-m-d');?>"/>
                                                                            </div>
                                                                    </div>

                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
							                </div>
      </div>
      <!-- Modal footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" id="save-data">Save Data</button>
      </div>

    </div>
  </div>
</div>
<!-- The Modal -->
<div class="modal" id="myModal1">
  <div class="modal-dialog" style="max-width: 40%;">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Modify Inventory</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
        <form id="modelForm">
		<div class="row">                                                    
                                                 <div class="col-md-12">
                                                        <div class="card">
                                                            <form class="form-horizontal" id="form1">
                                                                <div class="card-body">
                                                                    <h4 class="card-title"></h4>
																	                                  <div class="form-group row">
                                                                        <label for="particular" class="col-sm-3 text-end control-label col-form-label">Particular</label>
                                                                        <div class="col-sm-9">
                                                                            <select class="form-control" id="particular1">
                                                                               <option></option>
																			                                         <option>Salary</option>
                                                                               <option>Foods</option>
                                                                               <option>E Bills/Others</option>
                                                                               <option>Repair/Services</option>
                                                                               <option>Goods Purchase</option>                                                                         
                                                                               <option>Shop/Office Rent</option>
                                                                               <option>Travel Fare</option>
                                                                               <option>Tour Party/Trip</option>
                                          
                                                                            </select>
                                                                        </div>
                                                                    </div>
                                                                    <div class="form-group row">
                                                                        <label for="desc" class="col-sm-3 text-end control-label col-form-label">
                                                                        Description</label>
                                                                        <div class="col-sm-9">
                                                                            <input type="text" class="form-control" id="desc1"/>
                                                                            <input type="hidden" class="form-control" id="id1"/>
                                                                        </div>
                                                                    </div>
                                                                    <div class="form-group row">
                                                                        <label for="amount" class="col-sm-3 text-end control-label col-form-label">
                                                                        Amount</label>
                                                                        <div class="col-sm-9">
                                                                        <input type="number" class="form-control" id="amount1"/>
                                                                        </div>
                                                                    </div>       
                              
                                                                    <div class="form-group row">
                                                                            <label for="date" class="col-sm-3 text-end control-label col-form-label">Date</label>
                                                                            <div class="col-sm-9">
                                                                                <input type="text" class="form-control date-inputmask" id="date1" value="<?php echo date('Y-m-d');?>"/>
                                                                            </div>
                                                                    </div>

                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
							                </div>
      </div>
      <!-- Modal footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" id="edit-data">Save Data</button>
      </div>

    </div>
  </div>
</div>


          <footer class="footer text-center">
          All Rights Reserved by ECI. Designed and Developed by
          <a href="https://www.ntlogix.in">NTLogix</a>.
        </footer>
       
   <!-- All Jquery -->
    <!-- ============================================================== -->
    <script src="../assets/libs/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap tether Core JavaScript -->
    <script src="../assets/libs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <!-- slimscrollbar scrollbar JavaScript -->
    <script src="../assets/libs/perfect-scrollbar/dist/perfect-scrollbar.jquery.min.js"></script>
    <script src="../assets/extra-libs/sparkline/sparkline.js"></script>
    <!--Wave Effects -->
    <script src="../dist/js/waves.js"></script>
    <!--Menu sidebar -->
    <script src="../dist/js/sidebarmenu.js"></script>
    <!--Custom JavaScript -->
    <script src="../dist/js/custom.min.js"></script>
    <!-- this page js -->
    <script src="../assets/extra-libs/multicheck/datatable-checkbox-init.js"></script>
    <script src="../assets/extra-libs/multicheck/jquery.multicheck.js"></script>
    <script src="../assets/extra-libs/DataTables/datatables.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
   
    <script>
      var msg;
      var icon;
      
      function showAlert(msg,icon) {
          Swal.fire({
              title: msg,
              icon: icon,
              confirmButtonText: 'Close'
          });
      }
      
      $(document).ready(function(){
              $("#zero_config").DataTable();
              $('#add').click(function(e){ 
                    e.preventDefault();
                    $("#modelForm").get(0).reset();                  
                    setTimeout(function(){$("#myModal").modal('show');}, 100);
                });
              $('#save-data').click(function(){
                   var particular = $("#particular").val();
                   var desc = $("#desc").val();
                   var amount = $("#amount").val();
				           var date = $("#date").val();             
                    if((particular.length===0)||(desc===0)||(amount.length===0))
                    {
                      showAlert('some field is empty!','warning');
                      return 0;
                    }                  
                         $.ajax({
                             url:"core/inventory.php",
                             method:"post",
                             data: {
                                particular:particular,
                                desc:desc,
                                amount:amount,
                                date:date, 
                                state:'add',                        
                             },
                             success: function(data){                         
                               if(data==1)
                               {
                                $("#myModal").modal('toggle');
                                showAlert('Inventory Successfully Added!','success');
                                setTimeout(function(){window.location='./inventory.php';}, 2000); 
                               
                               }
                             }
                         });                
               });
               $('#edit-data').click(function(){
                   var particular = $("#particular1").val();
                   var desc = $("#desc1").val();
                   var amount = $("#amount1").val();
				           var date = $("#date1").val(); 
                   var id = $("#id1").val();             
                    if((particular.length===0)||(desc===0)||(amount.length===0))
                    {
                      showAlert('some field is empty!','warning');
                      return 0;
                    }                  
                         $.ajax({
                             url:"core/inventory.php",
                             method:"post",
                             data: {
                                id:id,
                                state:'edit',
                                particular:particular,
                                desc:desc,
                                amount:amount,
                                date:date,                         
                             },
                             success: function(data){                         
                               if(data==1)
                               {
                                $("#myModal").modal('toggle');
                                showAlert('Inventory Successfully Updated!','success');
                                setTimeout(function(){window.location='./inventory.php';}, 2000); 
                               
                               }
                             }
                         });                
               });
               $("#zero_config").on("click", "#edit", function() {              
                var id = $(this).data("id"); 
                $.ajax({
                                url:"core/fetch-student.php",
                                method:"post",
                                data: {
                                    id:id,
                                    state:'inventory_edit',                                
                                },
                                success: function(data){ 
                                    $.each(data,function(key,value){  
                                          $('#id1').val(id);                           
                                          $('#particular1').val(value['particular']);
                                          $('#desc1').val(value['description']);
                                          $('#amount1').val(value['amount']);                                             
                                          $('#date1').val(value['date']);  
                                                                                   
                                        })
                                }
                            });
                $("#myModal1").modal('toggle');

               });
               $("#zero_config").on("click", "#id", function() {              
                var id = $(this).data("id"); 
                if (confirm('Do you really want to Delete Attendence!',"")) {                      
                      $.ajax({
                                  url:"core/delete.php",
                                  method:"post",
                                  data: {
                                    id:id,
                                    state:'invt',                          
                                  },
                                  success: function(data){                        
                                    if(data.length===1)
                                    {
                          
                                      showAlert('Inventory Successfully Deleted!','success');
                                      setTimeout(function(){window.location='./inventory.php';}, 2000); 
                                      
                                    }
                                  }
                              });    
                        } 
              });
         
        });
    
</script>

  </body>
</html>