<?php $this->load->view("common/header"); ?>

<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button">
                    <span class="clip-list-2"></span>
                </button>
                <?php $this->load->view("common/logo"); ?>
            </div>
            <div class="navbar-tools">
                <?php $this->load->view("common/notifications"); ?>
            </div>
        </div>
    </div>

    <div class="main-container">
        <div class="navbar-content">
            <?php $this->load->view("common/navigation"); ?>
        </div>

        <div class="main-content">
            <div class="container">

                <!-- Page Header -->
                <div class="row">
                    <div class="col-sm-12">
                        <ol class="breadcrumb">
                            <li><a href="<?php echo base_url('dashboard'); ?>">Dashboard</a></li>
                            <li><a href="#">Reports</a></li>
                            <li class="active">Income Report</li>
                        </ol>
                        <div class="page-header">
                            <h1>Income Report</h1>
                            
                        </div>
                    </div>
                </div>

                <!-- Month Filter -->
                <div class="row">
                    <div class="col-md-12">
                        <div class="panel panel-default">
                            <div class="panel-body">
                                <form method="post" action="<?php echo base_url('reports_loan/income_report'); ?>" class="form-inline">
                                    <div class="form-group">
                                        <label>Select Month</label>
                                        <input type="text" name="selected_month" class="form-control month-picker"
                                            value="<?php echo $selected_month; ?>" placeholder="YYYY-MM">
                                    </div>
                                    <button type="submit" class="btn btn-primary">Show Report</button>
                                    <button type="button" class="btn btn-default"
                                        onclick="window.location='<?php echo base_url('reports_loan/income_report'); ?>'">
                                        Current Month
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Printable Header -->
                <div id="printableArea" style="display:none;">
                    <h4>Marketing Report – <?php echo date('F Y', strtotime($selected_month . '-01')); ?></h4>
                    <p><strong>Generated:</strong> <?php echo date('d M Y H:i'); ?> |
                        <strong>Total Records:</strong> <?php echo count($report_data); ?>
                    </p>
                </div>

                <!-- Table -->
                <table class="table table-bordered table-hover dataTable" id="marketing_table">
                    <thead>
                        <tr>
                            <th width="5%">#</th>
                            <th>Date</th>
                            <th>Customer Name</th>
                            <th class="text-right">Income</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $total_interest = 0;
                        $total_commission = 0;
                        $total_income = 0;
                        ?>
                        <?php if (!empty($marketing_loans)): ?>
                            <?php $i = 1;
                            foreach ($marketing_loans as $r): ?>
                                <?php
                                $total_interest += $r['interest_income'];
                                $total_commission += $r['commission'];
                                $total_income += $r['total_income'];
                                ?>
                                <tr>
                                    <td><?php echo $i++; ?></td>
                                    <td><?php echo date('d M Y', strtotime($r['loan_date'])); ?></td>
                                    <td><?php echo htmlspecialchars($r['customer_name']); ?></td>
                                    <td class="text-right"><?php echo number_format($r['interest_income'], 2); ?></td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="4" class="text-center text-muted">No records found for the selected month.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <th colspan="3" class="text-right">Total:</th>
                            <th class="text-right"><?php echo number_format($total_interest, 2); ?></th>
                        </tr>
                    </tfoot>
                </table>


            </div>
        </div>

        <!-- Footer -->
        <div class="footer clearfix">
            <div class="footer-inner"><?php echo date("Y"); ?> &copy; Loan Management System</div>
            <div class="footer-items"><span class="go-top"><i class="clip-chevron-up"></i></span></div>
        </div>
    </div>

    <?php $this->load->view("common/footer"); ?>

    <!-- Scripts -->
    <script src="<?php echo asset_url(); ?>js/jquery.dataTables.min.js"></script>
    <script src="<?php echo asset_url(); ?>js/dataTables.bootstrap.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js"></script>
    <script src="<?php echo asset_url(); ?>js/bootstrap-datetimepicker.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>

    <script>
        $(document).ready(function() {
            // Month Picker
            $('.month-picker').datetimepicker({
                format: 'YYYY-MM',
                viewMode: 'months',
                defaultDate: moment('<?php echo $selected_month; ?>', 'YYYY-MM')
            });

            // DataTable
            $('#marketing_table').DataTable({
                dom: 'Bfrtip',
                buttons: [{
                    extend: 'print',
                    text: 'Print',
                    title: '',
                    messageTop: function() {
                        return $('#printableArea').html();
                    },
                    exportOptions: {
                        columns: [0, 1, 2, 3, 4, 5]
                    },
                    customize: function(win) {
                        $(win.document.body).css('font-size', '12pt');
                    }
                }],
                pageLength: 25,
                order: [
                    [1, 'asc']
                ],
                language: {
                    emptyTable: "No records found for the selected month."
                }
            });
        });
    </script>

    <style>
        @media print {
            #printableArea {
                display: block !important;
            }

            .dataTables_wrapper .dt-buttons {
                display: none;
            }
        }

        .table td,
        .table th {
            vertical-align: middle !important;
        }

        .text-success {
            color: #3c763d;
        }

        .text-danger {
            color: #a94442;
        }
    </style>

</body>

</html>