<!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"; ?>

  <div class="content-wrapper">
    <section class="content-header">
      <h1>
        <?= $this->lang->line('expenses_list'); ?>
        <small>View/Search Expenses</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"><?= $this->lang->line('expenses_list'); ?></li>
      </ol>
    </section>

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

    <section class="content">
      <div class="row">

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

        <div class="col-xs-12">
          <div class="box box-primary">

            <div class="box-header">
              <h3 class="box-title">&nbsp;</h3>
              <?php if($CI->permissions('expense_add')) { ?>
              <div class="box-tools">
                <a class="btn btn-block btn-info" href="<?php echo $base_url; ?>expense/add">
                  <i class="fa fa-plus"></i> <?= $this->lang->line('new_expense'); ?>
                </a>
              </div>
              <?php } ?>
            </div>

            <div class="box-body">
              <table id="example2" class="table table-bordered" width="100%">
                <thead class="bg-gray">
                  <tr>
                    <th class="text-center"><input type="checkbox" class="group_check checkbox"></th>
                    <th><?= $this->lang->line('date'); ?></th>
                    <th><?= $this->lang->line('category'); ?></th>
                    <th><?= $this->lang->line('reference_no'); ?></th>
                    <th><?= $this->lang->line('expense_for'); ?></th>
                    <th><?= $this->lang->line('amount'); ?></th>
                    <th><?= $this->lang->line('account'); ?></th>
                    <th><?= $this->lang->line('note'); ?></th>
                    <th><?= $this->lang->line('created_by'); ?></th>
                    <th><?= $this->lang->line('action'); ?></th>
                  </tr>
                </thead>

                <tbody></tbody>

                <tfoot>
                  <tr class="bg-gray">
                      <th></th>
                      <th></th>
                      <th></th>
                      <th></th>
                      <th style="text-align:right">Total</th>
                      <th></th>
                      <th></th>
                      <th></th>
                      <th></th>
                      <th></th>
                  </tr>
                </tfoot>

              </table>
            </div>

          </div>
        </div>

      </div>
    </section>

    <?= form_close();?>
  </div>

  <?php include"footer.php"; ?>
  <div class="control-sidebar-bg"></div>
</div>

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

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

   var table = $('#example2').DataTable({ 
        "aLengthMenu": [[10,25,50,100,500],[10,25,50,100,500]],

        dom:'<"row margin-bottom-12"<"col-sm-12"<"pull-left"l><"pull-right"fr><"pull-right margin-left-10 "B>>>tip',

        buttons: {
            buttons: [
                {
                    className: 'btn bg-red btn-flat hidden delete_btn pull-left',
                    text: 'Delete',
                    action: function () { multi_delete(); }
                },
                { extend: 'copy', className: 'btn bg-teal btn-flat', footer:true, exportOptions:{columns:[1,2,3,4,5,6,7]} },
                { extend: 'excel', className: 'btn bg-teal btn-flat', footer:true, exportOptions:{columns:[1,2,3,4,5,6,7]} },
                { extend: 'pdf', className: 'btn bg-teal btn-flat', footer:true, exportOptions:{columns:[1,2,3,4,5,6,7]} },
                { extend: 'print', className: 'btn bg-teal btn-flat', footer:true, exportOptions:{columns:[1,2,3,4,5,6,7]} },
                { extend: 'csv', className: 'btn bg-teal btn-flat', footer:true, exportOptions:{columns:[1,2,3,4,5,6,7]} },
                { extend: 'colvis', className: 'btn bg-teal btn-flat', text:'Columns' }
            ]
        },

        "processing": true,
        "serverSide": true,
        "order": [],
        "responsive": true,

        language: {
            processing: '<div class="text-primary bg-primary">Processing...</div>'
        },

        "ajax": {
            "url": "<?php echo site_url('expense/ajax_list')?>",
            "type": "POST",
            complete: function () {
                $('.column_checkbox').iCheck({
                    checkboxClass: 'icheckbox_square-orange',
                    radioClass: 'iradio_square-orange',
                    increaseArea: '10%'
                });
                call_code();
            }
        },

        "columnDefs": [
            { "targets": [0,9], "orderable": false },
            { "targets": [0], "className": "text-center" }
        ],

        /* ✅ FIXED TOTAL */
        "footerCallback": function (row, data, start, end, display) {
            var api = this.api();

            var intVal = function (i) {
                if (typeof i === 'string') {
                    return parseFloat(
                        i.replace(/<[^>]*>/g,'')   // remove HTML
                         .replace(/[^0-9.\-]/g,'') // remove currency symbols
                    ) || 0;
                }
                return typeof i === 'number' ? i : 0;
            };

            var pageTotal = api
                .column(5, { page: 'current' })
                .data()
                .reduce(function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0);

            $(api.column(5).footer()).html(to_Fixed(pageTotal));
        }

    });

    new $.fn.dataTable.FixedHeader(table);
});
</script>

<script src="<?php echo $theme_link; ?>js/expense.js"></script>

<script>
$(".<?php echo basename(__FILE__,'.php');?>-active-li").addClass("active");
</script>

</body>
</html>