<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</div>
    </div>
<div class="badge bg-warning text-dark">
  Cash in Hand: Rs.<?php echo number_format($this->session->userdata('cash_in_hand')); ?>
</div>
    <?php
    // Helper function to render dashboard widget
    if (!function_exists('dashboard_widget')) {
        function dashboard_widget($id, $icon, $title, $subtitle, $amount, $color = "primary") { ?>
            <div class="col-lg-3 col-md-6 mb-3" id="<?php echo $id; ?>">
                <div class="card shadow-sm border-0 h-100">
                    <div class="card-body d-flex align-items-center">
                        <div class="rounded-circle bg-<?php echo $color ?> text-white d-flex align-items-center justify-content-center me-3" style="width:60px;height:60px;">
                            <i class="<?php echo $icon ?> fa-2x"></i>
                        </div>
                        <div>
                            <h6 class="text-muted mb-1"><?php echo $title ?></h6>
                            <h4 class="mb-0"><span class="amount">Rs.<?php echo number_format($amount) ?></span></h4>
                            <small class="text-muted"><?php echo $subtitle ?></small>
                        </div>
                    </div>
                </div>
            </div>
        <?php }
    }
    ?>

    <!-- Today's Cash Widget -->
    <div class="row">
        <?php dashboard_widget("todays-cash", "fa fa-calendar-day", "Today's Cash", "Cash collected Today (00:00 to now)", $todays_cash, "primary"); ?>
    </div>

    <!-- User-wise Cash Widgets -->
    <div class="row mt-4">
        <h5 class="mb-3">User-wise Cash Collected Today</h5>
        <?php foreach($users as $user):
            $user_cash = $user_cash_totals[$user->id] ?? 0;
            if($user_cash > 0) {
                dashboard_widget(
                    "user-".$user->id,
                    "fa fa-user",
                    $user->name,
                    "Collected Today",
                    $user_cash,
                    "info"
                );
            }
        endforeach; ?>
    </div>

    <!-- Shift-wise Cash Widgets -->
    <?php if(!empty($shifts)): ?>
    <div class="row mt-4">
        <h5 class="mb-3">Shift-wise Cash Collected Today</h5>
        <?php foreach($shifts as $shift):
            $shift_cash = $shift_cash_totals[$shift->id] ?? 0;
            if($shift_cash > 0) {
                dashboard_widget(
                    "shift-".$shift->id,
                    "fa fa-clock",
                    $shift->name,
                    "Collected Today",
                    $shift_cash,
                    "warning"
                );
            }
        endforeach; ?>
    </div>
    <?php endif; ?>

    <!-- Payments Table -->
    <div class="card-block mt-4">
        <table class="table table-bordered table-striped 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 foreach($payments as $row) : ?>

<!-- <pre><?php print_r($payments) ?></pre>                    <tr> -->
                        <td><?php echo $row->payment_module ?></td>
                        <td>
                            <?php if ($row->payment_module == 'Token'): ?>
                                <a href="<?php echo base_url('admin/tokens/print/'.$row->module_id); ?>" target="_blank">
                                    Slip #<?php echo $row->module_id ?>
                                </a>
                            <?php else: ?>
                                <?php echo $row->module_id ?>
                            <?php endif; ?>
                        </td>
                        <td><?php echo $row->debit ?></td>
                        <td><?php echo $row->credit ?></td>
                        <td><?php echo ($row->user_id ? $this->user->getNameBy($row->user_id) : '-') ?></td>
                        <td><?php echo $row->created_at ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- Initialize DataTable -->
<script>
$(document).ready(function() {
    $('.datatable').DataTable({
        "order": [[ 5, "desc" ]],
        "pageLength": 25,
        "responsive": true
    });
});
</script>
