<!DOCTYPE html>
<html>
<head>
<!-- TABLES CSS CODE -->
<?php include"comman/code_css.php"; ?>
</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 box-primary">
            <div class="box-header with-border">
              <h3 class="box-title">Batch Tracking</h3>
            </div>
            <div class="box-body" style="padding-bottom: 0;">
              <div class="row batch-filters">
                <!-- Store -->
                <div class="col-md-3 col-sm-6">
                  <div class="form-group">
                    <label for="store_id" class="control-label">Store</label>
                    <?php if(store_module() && is_admin()) {
                      $this->load->view('store/store_code', array('show_store_select_box'=>true, 'store_id'=>get_current_store_id(), 'div_length'=>'col-md-12', 'show_all'=>'true', 'form_group_remove'=>true, 'no_label'=>true));
                    } else {
                      echo "<input type='hidden' name='store_id' id='store_id' value='".get_current_store_id()."'>";
                      echo "<p class='form-control-static text-muted'>Current store</p>";
                    } ?>
                  </div>
                </div>
                <!-- Warehouse -->
                <div class="col-md-3 col-sm-6">
                  <div class="form-group">
                    <label for="warehouse_id" class="control-label">Warehouse</label>
                    <?php if(warehouse_module()) {
                      $this->load->view('warehouse/warehouse_code', array('show_warehouse_select_box'=>true, 'div_length'=>'col-md-12', 'show_all'=>'true', 'form_group_remove'=>true, 'show_all_option'=>true, 'no_label'=>true));
                    } else {
                      echo "<input type='hidden' name='warehouse_id' id='warehouse_id' value=''>";
                      echo "<p class='form-control-static text-muted'>—</p>";
                    } ?>
                  </div>
                </div>
                <!-- Item (expiry-enabled only, with search) -->
                <div class="col-md-3 col-sm-6">
                  <div class="form-group">
                    <label for="item_id" class="control-label">Item</label>
                    <select class="form-control select2-batch-item" id="item_id" name="item_id" data-placeholder="Search item...">
                      <?= function_exists('get_items_expiry_select_list') ? get_items_expiry_select_list() : get_items_select_list(); ?>
                    </select>
                  </div>
                </div>
                <!-- Batch Number -->
                <div class="col-md-3 col-sm-6">
                  <div class="form-group">
                    <label for="batch_number" class="control-label">Batch Number</label>
                    <input type="text" class="form-control" id="batch_number" name="batch_number" placeholder="Search batch number">
                  </div>
                </div>
              </div>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <table id="example2" class="table table-bordered table-striped" width="100%">
                <thead class="bg-gray">
                <tr>
                  <th>#</th>
                  <th>Item Code</th>
                  <th>Item Name</th>
                  <th>Batch Number</th>
                  <th>Manufacturing Date</th>
                  <th>Expiry Date</th>
                  <th class="text-right">Initial Qty</th>
                  <th class="text-right">Sold Qty</th>
                  <th class="text-right">Remaining Qty</th>
                  <th>Warehouse</th>
                  <th>Status</th>
                  <th>Created Date</th>
                </tr>
                </thead>
                <tbody id="batches_tbody">
                  <tr>
                    <td colspan="12" class="text-center">Loading...</td>
                  </tr>
                </tbody>
              </table>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>
        <!-- /.col -->
      </div>
      <!-- /.row -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
  
  <?php include"footer.php"; ?>
</div>
<!-- ./wrapper -->

<?php include"comman/code_js.php"; ?>

<script>
$(document).ready(function() {
  // Item dropdown: quick search always visible, placeholder
  $('#item_id').select2({
    placeholder: 'Search item...',
    allowClear: true,
    width: '100%',
    minimumResultsForSearch: 0
  });

  load_batches_list();

  // Reload on filter change
  $('#store_id, #warehouse_id, #item_id, #batch_number').on('change keyup', function() {
    load_batches_list();
  });

  function load_batches_list() {
    var form_data = {
      store_id: $('#store_id').val(),
      warehouse_id: $('#warehouse_id').val(),
      item_id: $('#item_id').val(),
      batch_number: $('#batch_number').val(),
      <?php echo $this->security->get_csrf_token_name(); ?>: '<?php echo $this->security->get_csrf_hash(); ?>'
    };
    
    $.ajax({
      url: base_url + 'batches/get_batches_list',
      type: 'POST',
      data: form_data,
      dataType: 'html',
      success: function(response) {
        $('#batches_tbody').html(response);
      },
      error: function() {
        $('#batches_tbody').html('<tr><td colspan="12" class="text-center text-danger">Error loading batches</td></tr>');
      }
    });
  }
});
</script>

</body>
</html>
