<link rel="stylesheet" href="<?php echo base_url(); ?>assets/vendor/datatables/media/css/jquery.dataTables.css">
<div class="main-content">
    <div class="page-title">
        <div class="title">Payments History</div>
        <!-- <div class="sub-title">UI datatables</div> -->
    </div>
    <div class="card bg-white">
        <div class="card-header">
            View Payments History
        </div>
        <div class="card-block">
            <?php
            $attributes = array('name' => 'formAdd', 'id' => 'formAdd', 'class' => 'form-validation');
            echo form_open_multipart('', $attributes);
            ?>
            <div class="form-group">
                <label>Module</label>
                <select name="module" value="expense" class="form-control" style="width: 100%;">
                    <option value=''>Select Module</option>
                    <option value="booking">Booking</option>
                    <option value="other_services">Other Services</option>
                    <option value="supplier">Supplier</option>
                    <option value="butcher">Butcher</option>
                    <option value="expense">Expense</option>
                    <option value="other_income">Other Income</option>
                </select>
            </div>
            <div class="form-group">
                <label>Users</label>
                <select name="user_id" class="form-control" style="width: 100%;">
                    <option value=''>Select User</option>
                    <?php foreach($users as $row) : ?>
                        <option value="<?php echo $row->id ?>"><?php echo $row->name ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label>Start Date</label>
                <input type="date" id="start_date" name="start_date" class="form-control">
            </div>
            <div class="form-group">
                <label>End Date</label>
                <input type="date" id="end_date" name="end_date" class="form-control">
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-primary m-r">Submit</button>
                <button type="reset" class="btn btn-default">Reset</button>
            </div>
            <?php echo form_close(); ?>
        </div>
        <hr>

        <?php if(isset($payments) && !empty($payments)) : ?>
        <div class="card-block">
            <table class="table table-bordered table-striped datatable editable-datatable responsive align-middle bordered">
                <thead>
                    <tr>
                        <th>Payment Module</th>
                        <th>Module ID</th>
                        <th>Debit</th>
                        <th>Credit</th>
                        <th>User</th>
                        <th>Created At</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $total_debit = 0; ?>
                    <?php $total_credit = 0; ?>
                    <?php foreach($payments as $row) : ?>
                        <?php $total_credit += $row->credit; ?> 
                        <?php $total_debit += $row->debit; ?>
                        <tr>
                            <td><?php echo $row->payment_module; ?></td>
                            <td><?php echo $row->module_id; ?></td>
                            <td><?php echo $row->debit; ?></td>
                            <td><?php echo $row->credit; ?></td>
                            <td>
                                <?php echo ($row->user_id != 0 && $row->user_id != null) ? $this->user->getNameBy($row->user_id) : 'N/A'; ?>
                            </td>
                            <td><?php echo $row->created_at; ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
                <tfoot>
                    <tr>
                        <th>Totals</th>
                        <th></th>
                        <th><?php echo $total_debit; ?></th>
                        <th><?php echo $total_credit; ?></th>
                        <th><?php echo $total_debit - $total_credit; ?></th>
                        <th></th>
                    </tr>
                </tfoot>
            </table>
        </div>
        <?php else: ?>
            <div class="card-block">
                <p>No payment records available!</p>
            </div>
        <?php endif; ?>
    </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<!-- page scripts -->
<script src="<?php echo base_url(); ?>assets/vendor/datatables/media/js/jquery.dataTables.js"></script>
<!-- end page scripts -->
<!-- initialize page scripts -->
<script src="<?php echo base_url(); ?>assets/scripts/helpers/bootstrap-datatables.js"></script>
<script src="<?php echo base_url(); ?>assets/scripts/tables/table-edit.js"></script>
<!-- end initialize page scripts -->
