<?php $currency_symbol = $this->customlib->getSchoolCurrencyFormat(); ?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">

    <section class="content-header">
        <h1>
            <i class="fa fa-object-group"></i> <?php echo $this->lang->line('inventory'); ?></h1>
											 <a href="<?php  echo base_url(); ?>inventoryreport" class="col-md pull-right">Inventory Report</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
								 <a href="<?php  echo base_url(); ?>admin/itemsupplier" class="col-md pull-right">Item Supplier</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>	
						 		 <a href="<?php  echo base_url(); ?>admin/itemcategory" class="col-md pull-right">Item Category</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>											 
								 <a href="<?php  echo base_url(); ?>admin/item" class="col-md pull-right">Add Item</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
								 <a href="<?php  echo base_url(); ?>admin/itemstock" class="col-md pull-right">Add Item Stock</a> 
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
								 <a href="<?php  echo base_url(); ?>admin/issueitem" class="col-md pull-right">Issue Item</a>
    </section>

<!-- Main content -->
<section class="content">
  <div class="row">
    <?php if ($this->rbac->hasPrivilege('item_stock', 'can_add')) { ?> 
      <div class="col-md-4">
        <!-- Receive Stock Form -->
        <div class="box box-primary">
          <div class="box-header with-border">
            <h3 class="box-title"><?php echo $this->lang->line('receive_stock'); ?></h3>
          </div>
          <form id="itemstockform"
                action="<?php echo base_url() ?>admin/itemstock"
                method="post" enctype="multipart/form-data">
            <div class="box-body">
              <?php 
                echo $this->customlib->getCSRF();
                if ($this->session->flashdata('msg')) echo $this->session->flashdata('msg');
                if (isset($error_message)) echo "<div class='alert alert-danger'>{$error_message}</div>";
              ?>

              <!-- Date -->
              <div class="form-group">
                <label for="date"><?php echo $this->lang->line('date'); ?></label><small class="req"> *</small>
                <input
                  autofocus
                  id="date"
                  name="date"
                  type="date"
                  class="form-control"
                  value="<?php echo set_value('date', date('Y-m-d')); ?>"
                />
                <span class="text-danger"><?php echo form_error('date'); ?></span>
              </div>


              <!-- Supplier -->
              <div class="form-group">
                <label for="supplier_id"><?php echo $this->lang->line('supplier'); ?></label>
                <select id="supplier_id" name="supplier_id" class="form-control">
                  <option value=""><?php echo $this->lang->line('select'); ?></option>
                  <?php foreach ($itemsupplier as $sup): ?>
                    <option value="<?php echo $sup['id'] ?>"
                      <?php echo set_value('supplier_id')==$sup['id']?'selected':''?>>
                      <?php echo $sup['item_supplier'] ?>
                    </option>
                  <?php endforeach; ?>
                </select>
                <span class="text-danger"><?php echo form_error('supplier_id'); ?></span>
              </div>

              <!-- Supplier Reference -->
              <div class="form-group">
                <label for="supplier_reference"><?php echo $this->lang->line('supplier_reference'); ?></label>
                <input id="supplier_reference" name="supplier_reference" type="text"
                       class="form-control" value="<?php echo set_value('supplier_reference'); ?>" />
                <span class="text-danger"><?php echo form_error('supplier_reference'); ?></span>
              </div>

              <!-- Attach Document -->
              <div class="form-group">
                <label for="item_photo"><?php echo $this->lang->line('attach_document'); ?></label>
                <input id="item_photo" name="item_photo" type="file"
                       class="filestyle form-control" data-height="40" />
                <span class="text-danger"><?php echo form_error('item_photo'); ?></span>
              </div>

              <!-- Item Category -->
              <div class="form-group">
                <label for="item_category_id"><?php echo $this->lang->line('item_category'); ?></label><small class="req"> *</small>
                <select id="item_category_id" name="item_category_id" class="form-control">
                  <option value=""><?php echo $this->lang->line('select'); ?></option>
                  <?php foreach ($itemcatlist as $cat): ?>
                    <option value="<?php echo $cat['id'] ?>"
                      <?php echo set_value('item_category_id')==$cat['id']?'selected':''?>>
                      <?php echo $cat['item_category'] ?>
                    </option>
                  <?php endforeach; ?>
                </select>
                <span class="text-danger"><?php echo form_error('item_category_id'); ?></span>
              </div>

              <!-- Item -->
              <div class="form-group">
                <label for="item_id"><?php echo $this->lang->line('item'); ?></label><small class="req"> *</small>
                <select id="item_id" name="item_id" class="form-control">
                  <option value=""><?php echo $this->lang->line('select'); ?></option>
                  <!-- AJAX‑populated -->
                </select>
                <span class="text-danger"><?php echo form_error('item_id'); ?></span>
              </div>

              <!-- Description -->
              <div class="form-group">
                <label for="description"><?php echo $this->lang->line('description'); ?></label>
                <textarea id="description" name="description" class="form-control" rows="3"><?php echo set_value('description'); ?></textarea>
              </div>

              <!-- Store -->
              <div class="form-group">
                <label for="store_id"><?php echo $this->lang->line('store'); ?></label>
                <select id="store_id" name="store_id" class="form-control">
                  <option value=""><?php echo $this->lang->line('select'); ?></option>
                  <?php foreach ($itemstore as $store): ?>
                    <option value="<?php echo $store['id'] ?>"
                      <?php echo set_value('store_id')==$store['id']?'selected':''?>>
                      <?php echo $store['item_store'] ?>
                    </option>
                  <?php endforeach; ?>
                </select>
                <span class="text-danger"><?php echo form_error('store_id'); ?></span>
              </div>

              <!-- Quantity -->
              <div class="form-group">
                <label for="quantity"><?php echo $this->lang->line('quantity'); ?></label><small class="req"> *</small>
                <div>
                  <span class="miplus">
                    <select class="form-control" name="symbol">
                      <option value="+">+</option>
                      <option value="-">-</option>
                    </select>
                  </span>
                  <input id="quantity" name="quantity" type="text"
                         class="form-control miplusinput"
                         value="<?php echo set_value('quantity'); ?>" />
                </div>
                <span class="text-danger"><?php echo form_error('quantity'); ?></span>
              </div>

              <!-- Cost -->
              <div class="form-group">
                <label for="cost"><?php echo $this->lang->line('cost'); ?></label><small class="req"> *</small>
                <input id="cost" name="cost" type="text"
                       class="form-control"
                       value="<?php echo set_value('cost'); ?>" />
                <span class="text-danger"><?php echo form_error('cost'); ?></span>
              </div>

              <!-- Amount (calculated) -->
              <div class="form-group">
                <label for="amount"><?php echo $this->lang->line('amount'); ?></label>
                <input id="amount" name="amount" type="text"
                       class="form-control" readonly />
              </div>
            </div><!-- /.box-body -->

            <div class="box-footer">
              <button type="submit" class="btn btn-info pull-right">
                <?php echo $this->lang->line('save'); ?>
              </button>
            </div>
          </form>
        </div>
      </div><!-- /.col-md-4 -->
    <?php } ?>

    <div class="col-md-<?php echo $this->rbac->hasPrivilege('item_stock','can_add') ? '8' : '12'; ?>">
      <!-- Item Stock List -->
      <div class="box box-primary">
        <div class="box-header ptbnull">
          <h3 class="box-title"><?php echo $this->lang->line('item_stock_list'); ?></h3>
        </div>
        <div class="box-body">
          <div class="table-responsive mailbox-messages">
            <table class="table table-hover table-striped table-bordered example">
              <thead>
                <tr>
                  <th><?php echo $this->lang->line('date'); ?></th>
                  <th><?php echo $this->lang->line('supplier'); ?></th>
                  <th><?php echo $this->lang->line('supplier_reference'); ?></th>
                  <th><?php echo $this->lang->line('item_category'); ?></th>
                  <th><?php echo $this->lang->line('item'); ?></th>
                  <th><?php echo $this->lang->line('store'); ?></th>
                  <th><?php echo $this->lang->line('quantity'); ?></th>
                  <th><?php echo $this->lang->line('cost'); ?></th>
                  <th><?php echo $this->lang->line('amount'); ?></th>
                  <th class="text-right"><?php echo $this->lang->line('action'); ?></th>
                </tr>
              </thead>
              <tbody>
                <?php if (empty($itemlist)): ?>
                  <tr>
                    <td colspan="11" class="text-center"><?php echo $this->lang->line('no_record_found'); ?></td>
                  </tr>
                <?php else: foreach ($itemlist as $it): ?>
                  <tr>
                    <td><?php echo date(
                        $this->customlib->getSchoolDateFormat(),
                        $this->customlib->dateyyyymmddTodateformat($it['date'])
                    ); ?></td>
                    <td><?php echo $it['item_supplier']; ?></td>
                    <td><?php echo $it['supplier_reference']; ?></td>
                    <td><?php echo $it['item_category']; ?></td>
                    <td><?php echo $it['name']; ?></td>
                    <td><?php echo $it['item_store']; ?></td>
                    <td><?php echo $it['quantity']; ?></td>
                    <td><?php echo $it['cost']; ?></td>
                    <td><?php echo number_format($it['quantity'] * $it['cost'], 2); ?></td>
                    <td class="pull-right">
                      <?php if ($this->rbac->hasPrivilege('item_stock','can_edit')): ?>
                        <a href="<?php echo base_url()?>admin/itemstock/edit/<?php echo $it['id']?>"
                           class="btn btn-default btn-xs" data-toggle="tooltip" title="<?php echo $this->lang->line('edit'); ?>">
                          <i class="fa fa-pencil"></i>
                        </a>
                      <?php endif; ?>
                      <?php if ($this->rbac->hasPrivilege('item_stock','can_delete')): ?>
                        <a href="<?php echo base_url()?>admin/itemstock/delete/<?php echo $it['id']?>"
                           class="btn btn-default btn-xs" data-toggle="tooltip" title="<?php echo $this->lang->line('delete'); ?>"
                           onclick="return confirm('<?php echo $this->lang->line('delete_confirm')?>');">
                          <i class="fa fa-remove"></i>
                        </a>
                      <?php endif; ?>
                    </td>
                  </tr>
                <?php endforeach; endif; ?>
              </tbody>
            </table>
          </div><!-- /.mail-box-messages -->
        </div><!-- /.box-body -->
      </div><!-- /.box -->
    </div><!-- /.col -->
  </div><!-- /.row -->
</section><!-- /.content -->

<script>
$(function(){
  function updateAmount(){
    var q = parseFloat($('#quantity').val())||0;
    var c = parseFloat($('#cost').val())||0;
    $('#amount').val((q*c).toFixed(2));
  }
  $('#quantity, #cost').on('input', updateAmount);
  updateAmount();
});
</script>



<script type="text/javascript">
    $(document).ready(function () {

        var item_id_post = '<?php echo set_value('item_id') ?>';
        item_id_post = (item_id_post != "") ? item_id_post : 0;
        var item_category_id_post = '<?php echo set_value('item_category_id'); ?>';
        item_category_id_post = (item_category_id_post != "") ? item_category_id_post : 0;
        populateItem(item_id_post, item_category_id_post);

        function populateItem(item_id_post, item_category_id_post) {
            if (item_category_id_post != "") {
                $('#item_id').html("");

                var base_url = '<?php echo base_url() ?>';
                var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
                $.ajax({
                    type: "GET",
                    url: base_url + "admin/itemstock/getItemByCategory",
                    data: {'item_category_id': item_category_id_post},
                    dataType: "json",
                    success: function (data) {
                        $.each(data, function (i, obj)
                        {
                            var select = "";
                            if (item_id_post == obj.id) {
                                var select = "selected=selected";
                            }
                            div_data += "<option value=" + obj.id + " " + select + ">" + obj.name + "</option>";
                        });
                        $('#item_id').append(div_data);
                    }

                });
            }
        }



        var date_format = '<?php echo $result = strtr($this->customlib->getSchoolDateFormat(), ['d' => 'dd', 'm' => 'mm', 'Y' => 'yyyy',]) ?>';

        $('#date').datepicker({
            //  format: "dd-mm-yyyy",
            format: date_format,
            autoclose: true
        });

        $("#btnreset").click(function () {
            $("#form1")[0].reset();
        });


        $('.detail_popover').popover({
            placement: 'right',
            trigger: 'hover',
            container: 'body',
            html: true,
            content: function () {
                return $(this).closest('td').find('.fee_detail_popover').html();
            }
        });

        $(document).on('change', '#item_category_id', function (e) {
            $('#item_id').html("");
            var item_category_id = $(this).val();
            populateItem(0, item_category_id);
        });

    });
</script>