<!DOCTYPE html>
<html>
<head>
<!-- TABLES CSS CODE -->
<?php include"comman/code_css_form.php"; ?>
<link rel="stylesheet" href="<?php echo $theme_link; ?>plugins/iCheck/square/blue.css">
<!-- </copy> -->  

</head>
<body class="hold-transition skin-blue sidebar-mini">


<div class="wrapper">
 
 <?php include"sidebar.php"; ?>
    
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <?=$page_title;?>
        <small></small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="<?php echo $base_url; ?>dashboard"><i class="fa fa-dashboard"></i>Home</a></li>
        <li class="active"><?=$page_title;?></li>
      </ol>
    </section>

    <!-- Main content -->
    <?= form_open('#', array('class' => '', 'id' => 'table_form')); ?>
    <input type="hidden" id='base_url' value="<?=$base_url;?>">

    <section class="content">
      <div class="row">
        <!-- ********** ALERT MESSAGE START******* -->
        <?php include"comman/code_flashdata.php"; ?>
        <!-- ********** ALERT MESSAGE END******* -->
        <div class="col-xs-12">
          <div class="box">
            <div class="box-header with-border">
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <div class="row">
                <div class="col-md-12">
                    <div class="col-md-3">
                          <select class="form-control select2" id="category_id" name="category_id"  style="width: 100%;">
                            <?php
                               $query1="select * from db_category where status=1";
                               $q1=$this->db->query($query1);
                               if($q1->num_rows($q1)>0)
                                {  echo '<option value="0">-Select Category-</option>'; 
                                    foreach($q1->result() as $res1)
                                  { 
                                    echo "<option value='".$res1->id."'>".$res1->category_name."</option>";
                                  }
                                }
                                else
                                {
                                   ?>
                            <option value="">No Records Found</option>
                            <?php
                               }
                               ?>
                          </select>
                    </div>
                    <div class="col-md-3">
                          <select class="form-control select2" id="qty_filter" name="qty_filter"  style="width: 100%;">
                            <option value="0" selected>Drop Zero Quantity</option>
                            <option value="1">With Zero Quantity</option>
                          </select>
                    </div>
                </div>
              </div>
              <br>
              <table id="example2" class="table table-bordered table-striped" width="100%">
                <thead class="bg-primary ">
                <tr>
                  <th class="text-center">#</th>
                  <th>GRN Code</th>
                  <th>SKU</th>
                  <th>Item Name</th>
                  <!-- <th>Color</th>
                  <th>Size</th> -->
                  <th>GRN Date</th>
                  <th>GRN Qty</th>
                  <th>Current <br> Qty</th>
                  <th>Sold <br> Qty</th>                  
                  <th>Days in <br> Stock</th>
                </tr>
                </thead>
                <tbody>
				
                </tbody>
                <tfoot>
                  <tr class="bg-gray">
                      <th colspan="5" style="text-align:right">Total</th>
                      <th><b> <label id="lbl_sum_p_qty"></label> </b></th>
                      <th><b> <label id="lbl_sum_s_qty"></label> </b></th>
                      <th><b> <label id="lbl_sum_sold_qty"></label> </b></th>
                      <th></th><!-- 7 -->
                  </tr>
              </tfoot>
     
              </table>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>
        <!-- /.col -->
      </div>
      <!-- /.row -->
    </section>
    <!-- /.content -->
     <?= form_close();?>
  
  </div>
  <!-- /.content-wrapper -->
  
 <?php include"footer.php"; ?>

 
  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->
<!-- SOUND CODE -->
<?php include"comman/code_js_sound.php"; ?>
<!-- TABLES CODE -->
<?php include"comman/code_js_form.php"; ?>
<?php include"comman/code_js_datatable.php"; ?>
<script src="<?php echo $theme_link; ?>plugins/iCheck/icheck.min.js"></script>

<!-- <script src="<?php echo $theme_link; ?>js/sheetjs.js" type="text/javascript"></script> -->

<script>
  function fill_tbData(){

      var filter_none_qty = $("#qty_filter").val();
      var category_id =  $("#category_id").val();
      $.ajax({
        url:"<?php echo site_url('reports/stock_details_get')?>",
        type: 'POST',
        data : {category_id:category_id,filter_none_qty:filter_none_qty},
        dataType: 'json',
        success: function (data) {

          var table;
          table = $('#example2').DataTable();
          table.clear();
          var indexNo = 1;

          var sum_p_qty = 0;
          var sum_stock_qty = 0;
          var sum_sold_qty = 0;

          $.each(data.st_info, function(){
            var p_qty = parseInt(this['grn_qty']);
            var st_qty = parseInt(this['stock_qty']);
            var sold_qty = p_qty-st_qty;

            table.row.add([indexNo,this['purchase_code'],this['sku'],this['item_name'], this['purchase_date'], p_qty, st_qty, sold_qty, this['StockinDays']]);
            indexNo++;
            sum_p_qty  += p_qty;
            sum_stock_qty += st_qty;
            sum_sold_qty  += sold_qty;
          });
          table.draw();
          
          $('#lbl_sum_p_qty').empty();
          $('#lbl_sum_p_qty').append(sum_p_qty);
          $('#lbl_sum_s_qty').empty();
          $('#lbl_sum_s_qty').append(sum_stock_qty);
          $('#lbl_sum_sold_qty').empty();
          $('#lbl_sum_sold_qty').append(sum_sold_qty);
          
        },
        error: function (xhr, ajaxOptions, thrownError) {
          console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
      });
  }
</script>

<script>
    $(document).ready(function(){
      $('#example2').DataTable();
      fill_tbData();
    });
</script>
<script>
  $("#category_id").change(function(){
      fill_tbData();
  });
</script>
<script>
  $("#qty_filter").change(function(){
      fill_tbData();
  });
</script>

<!-- Make sidebar menu hughlighter/selector -->
<script>$(".<?php echo basename(__FILE__,'.php');?>-active-li").addClass("active");</script>
    
    
</body>
</html>
