<?php
include('db_con.php');
include('function.php');
if (isset($_SESSION['user_id'])) {
    $user_type = $_SESSION['user_type'];
    $user_id = $_SESSION['user_id'];
    $user_name = $_SESSION['user_name'];
    $branch_id = $_SESSION['branch_id'];
    $branch_name = $_SESSION['branch_name'];
    $store_id = $_SESSION['store_id'];
    $store_name = $_SESSION['store_name'];
}else{
  header('Location: login');
  die();
}
stock_update($connect);
$sql = "SELECT IFNULL(doc_status,'OFF') FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'cost'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $cost = $statement->fetchColumn();
            
?>
<!DOCTYPE html>
<html>
<head>
<title><?php  if(basename(__FILE__, '.php'=='inventory_detail.php')){echo 'Item Inquiry';}?></title>
  <?php include('include/head.php'); ?>
  
</head>
<body>
  <?php include('include/header.php'); ?>
  <?php include('include/sidebar.php'); ?>
<div class="main-container">
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark"></h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Inventory</a></li>
              <li class="breadcrumb-item active">Item Inquiry </li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <!-- Small boxes (Stat box) -->
     <span id="alert_action"></span>
    <div class="row">
        <div class="col-12">
          <div class="row">
          <div class="col-6">
             <select name="type_id" id="type_id" class="form-control input-sm type_id select2" />
                           <?php echo fill_itemtype($connect);?>
                        </select>
                        <br />
                        <br />
                      
                        <select name="item_id" id="item_id" class="form-control input-sm item_id"  />
                          
                        </select>
          </div>
          <div class="col-6">
             <select name="cust_id" id="cust_id" class="form-control input-sm cust_id select2" />
                           <?php echo fill_c_name($connect);?>
                        </select>
                       
          </div>
          </div>
         
                          
                        <br />
          <div class="card">
            <div class="card-header">
              <div class="row">
                          <div class="col-lg-10 col-md-10 col-sm-8 col-xs-6">
                              <h4 class="panel-title">Inventory Item Detail</h4>
                            </div>
                            
                </div>
            </div>
            <!-- /.card-header -->
            <div class="card-body">
              
                        <table width="100%" id="_dataTable" class="table table-bordered table-striped table-hover">
                          <thead>
                  <tr>
                    
                                    
                    <th width="2%">ID</th>                 
                    <th width="15%">Date</th>
                    
                    <th width="20%">From | To</th>
                    <th width="12%">Item</th>
                    <th width="10%">Description</th>
                  
                    
                    <th width="10%">Cost</th>
                    <th width="10%">Rate</th>                    
                    <th width="10%">Qty In</th>    
                     <th width="10%">Qty Out</th> 
                      <th width="10%">Stock</th>    
                                      
                    
                    
                    
                  </tr>
                </thead>
                
                        </table>
                         </div>
              </div>
          </div>
      </div>
             
        
        
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
 
  <!-- /.content-wrapper -->
 <?php include('include/footer.php'); ?>
    </div>
    </div>
  <?php include('include/script.php'); ?>
  
</body>
</html>
        <div id="dataModal" class="modal fade">
          <div class="modal-dialog">
            <form method="post" id="data_form">
              <div class="modal-content">
              <div class="modal-header">
                
            <h4 class="modal-title"><i class="fa fa-plus"></i> Stock Detail</h4>
              </div>
              <div class="modal-body">
              <div class="form-group">
              <label>Item Name :</label>
              <span id="item_name"></span>
              <span id="span_item_detail"></span>
              <hr />
              <span id="total_stock"></span>
            </div>
                
                
              </div>
              <div class="modal-footer">
                <input type="hidden" name="item_no" id="item_no" />
                <input type="hidden" name="btn_action" id="btn_action" />
                <input type="hidden" name="op_stock" id="op_stock" value="1" />
                
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              </div>
            </div>
            </form>
          </div>
        </div>
<script>
   $(document).ready(function(){
          $(document).on('select2:select','.select2', function(e){
            
            var type_id = $(this).val();           
            $.ajax({
              url:"fill_item.php",
              method:"POST",
              data:{type_id:type_id},
              dataType:"text",
              success:function(data)
              {
                $('#item_id').html(data);
                
              }
            });
         });
      });
    </script>        
<script>
$(document).ready(function(){
  $(document).on('change', '#type_id', function(){
  var type_id = $(this).val();
  $('#_dataTable').DataTable().destroy();
  if(type_id != '')
  {
   load_data();
  }
  else
  {
   load_data();
  }
 });
  $(document).on('change', '#item_id', function(){
  var item_id = $(this).val();
  $('#_dataTable').DataTable().destroy();
  
   load_data();
  
 });
  $(document).on('change', '#cust_id', function(){
  var cust_id = $(this).val();
  $('#_dataTable').DataTable().destroy();
  if(cust_id != '')
  {
   load_data();
  }
  else
  {
   load_data();
  }
 });
  function load_data()
 {
  var item_id = $('#item_id').val();
  var type_id = $('#type_id').val();
  var cust_id = $('#cust_id').val();
  var _dataTable = $('#_dataTable').DataTable({
   "processing":true,
   "serverSide":true,
   "order":[],
   "ajax":{
    url:"inventory_detail_fetch.php",
    type:"POST",
    data:{type_id:type_id, item_id:item_id,cust_id:cust_id}
   },
   "columnDefs":[
    {
     "targets":[1,2,4,5],
     "orderable":false,
    },
   ],
   "language": {
          "info": "_START_-_END_ of _TOTAL_ entries",
          searchPlaceholder: "Search"
        },
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        
        "pageLength": 25
  });
 }
$('.select2').select2();  
});
    
</script>