 <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">All Expense</h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="<?php echo base_url() ?>admin/admin/dashboard">Dashboard</a></li>
              <li class="breadcrumb-item"><a href="#">Expense Management</a></li>
              <li class="breadcrumb-item active">All Expense</li>

            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="card card-default">
          <div class="card-header">
            <h3 class="card-title">View All Expense</h3>
            <div class="card-tools">
              <a href="javascript:void(0)" data-toggle="modal" data-target="#exampleModal" class="btn btn-success btn-sm"><i class="fa fa-plus" aria-hidden="true"></i> Add New Expense</a>
            </div>
          </div>
            <!-- /.card-header -->
            <div class="card-body">
              <div class="category-filter">
                <select id="categoryFilter" class="form-control form-controls">
                  <option value="">Show All</option>
                  <?php
                    $atp_id = user_id();
                    $this->db->where('acc_name_inst_id', $atp_id);
                    $all_acc_head = $this->db->get('account_head_name')->result();
                    foreach ($all_acc_head as $key => $head):
                  ?>
                    <option value="<?php echo $head->acc_name; ?>"><?php echo $head->acc_name; ?></option>
                  <?php endforeach; ?>
                  <option value="Paid Shipping Charge">Paid Shipping Charge</option>
                  <option value="Marksheet Buy">Marksheet Buy</option>
                  <option value="Certificate Buy">Certificate Buy</option>
                  <option value="Advance Salary">Advance Salary</option>
                  <option value="Fulll Salary">Fulll Salary</option>
                </select>
              </div>
              <table id="filterTable" class="table table-bordered table-striped">
                <thead>
                <tr>
                  <th>ID</th>
                  <th>Expense Name</th>
                  <th>Expense Note</th>
                  <th>Expense Date</th>
                  <th>Expense Amount</th>
                  <!-- <th></th> -->
                </tr>
                </thead>
                <tbody>
                <?php $i=1;
                $ttl_exp = 0; 
                foreach ($all_expense as $expense):

                $this->db->where('acc_name_id', $expense->expense_head_id);
                $expense_head = $this->db->get('account_head_name')->row();
                ?>
                <tr>
                  <td><?php echo $i ?></td>
                  <td>
                    <?php if($expense->expense_head_id > 0): ?>
                      <?php echo $expense_head->acc_name ?>
                    <?php else: ?>
                      <?php echo $expense->expense_category ?>
                    <?php endif; ?>
                  </td>
                  <td><?php echo $expense->expense_note ?></td>
                  <td><?php echo date('d-m-Y', strtotime($expense->expense_date))?></td>
                  <td><b>&#8377; <?php echo $expense->expense_amount ?></b></td>
                </tr>
                <?php $ttl_exp += $expense->expense_amount; ?>
                <?php $i++; ?>
                <?php endforeach; ?>
                <tr>
                  <td></td>
                  <td></td>
                  <td></td>
                  <td><b>Total</b></td>
                  <td><b>&#8377;<?php echo $ttl_exp; ?></b></td>
                </tr>
                </tbody>
              </table>
            </div>
          </div>
      </div>
    </section>
  </div>


<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Add New Expense</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <?php echo form_open(base_url().'admin/expense_management/add_new_expense'); ?>
        <div class="modal-body">
          <div class="form-group">
            <label for="recipient-name" class="col-form-label">Select A Account Head <span class="text-danger">*</span></label>
            <select type="text" class="form-control" name='expense_head_id' required id="recipient-name">
              <option value="">Select</option>
              <?php foreach ($all_account_head as $account_head): ?>
                <option value="<?php echo $account_head->acc_name_id; ?>"><?php echo $account_head->acc_name; ?></option>
              <?php endforeach ?>
            </select>
          </div>
          <div class="form-group">
            <label for="message-sss" class="col-form-label">Amount <span class="text-danger">*</span></label>
            <input class="form-control" id="message-sss" name="expense_amount" required placeholder="Enter Expense Amount"/>
          </div>
          <div class="form-group">
            <label for="message-date" class="col-form-label">Date <span class="text-danger">*</span></label>
            <input class="form-control" id="message-date" type="date" name="expense_date" required placeholder="Enter Expense Amount"/>
          </div>
          <div class="form-group">
            <label for="message-text" class="col-form-label">Note</label>
            <textarea class="form-control" id="message-text" name="expense_note" placeholder="Enter Any Note"></textarea>
          </div>
        </div>
        <div class="modal-footer">
          <button type="submit" class="btn btn-primary">Save</button>
        </div>
      <?php echo form_close(); ?>
    </div>
  </div>
</div>
<script>
  $("document").ready(function () {
    $("#filterTable").dataTable({
      "responsive": true, "lengthChange": true, "autoWidth": false, "searching": true, "ordering": false,
    });
    var table = $('#filterTable').DataTable();
    $("#filterTable_filter.dataTables_filter").append($("#categoryFilter"));
    var categoryIndex = 0;
    $("#filterTable th").each(function (i) {
      if ($($(this)).html() == "Expense Name") {
        categoryIndex = i; return false;
      }
    });
    $.fn.dataTable.ext.search.push(
      function (settings, data, dataIndex) {
        var selectedItem = $('#categoryFilter').val()
        var category = data[categoryIndex];
        if (selectedItem === "" || category.includes(selectedItem)) {
          return true;
        }
        return false;
      }
    );
    $("#categoryFilter").change(function (e) {
      table.draw();
    });
    table.draw();
  });
</script>
<style>
  select.form-controls {
    display: inline;
    width: 200px;
    margin-left: 25px;
  }
</style>