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

            <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">Customer Rating</li>
                    </ol>

                    <div class="page-header">
                        <h1>Customer Rating Report – <?php echo date('d M Y'); ?></h1>
                        <p>Ratings based on arrears of customer’s last loan.</p>
                    </div>
                </div>
            </div>

            <!-- Printable Title -->
            <div id="printableArea" style="display:none;">
                <h4>Customer Rating Report</h4>
                <p>Generated: <?php echo date('d M Y H:i'); ?></p>
                <p>Total Customers: <?php echo count($ratings); ?></p>
            </div>

            <!-- Table -->
            <table class="table table-bordered table-hover table-condensed dataTable" id="rating_table">
                <thead>
                <tr>
                    <th>#</th>
                    <th>Customer Name</th>
                    <th>Mobile</th>
                    <th>Loan Ref</th>
                    <th class="text-center">Arrears Count</th>
                    <th class="text-center">Rating</th>
                    <th>Label</th>
                </tr>
                </thead>

                <tbody>
                <?php if ($ratings): ?>
                    <?php $i = 1; foreach ($ratings as $r): ?>
                        <tr>
                            <td><?php echo $i++; ?></td>
                            <td><?php echo htmlspecialchars($r['customer_name']); ?></td>
                            <td><?php echo htmlspecialchars($r['mobile_no']); ?></td>
                            <td><?php echo htmlspecialchars($r['loan_ref']); ?></td>

                            <td class="text-center">
                                <strong><?php echo $r['arrears']; ?></strong>
                            </td>

                            <td class="text-center">
                                <?php echo $r['rating']; ?>
                            </td>

                            <td>
                                <span class="label label-<?php 
                                    echo ($r['rating'] == 1 ? 'success' : ($r['rating'] == 2 ? 'primary' : ($r['rating'] == 3 ? 'warning' : 'danger')));
                                ?>">
                                    <?php echo $r['rating_label']; ?>
                                </span>
                            </td>

                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr><td colspan="7" class="text-center text-muted">No rated customers available.</td></tr>
                <?php endif; ?>
                </tbody>
            </table>
        </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>
jQuery(document).ready(function() {
    $('#rating_table').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'print',
            text: 'Print Report',
            title: '',
            messageTop: function() { return $('#printableArea').html(); },
            exportOptions: { columns: [0,1,2,3,4,5,6] }
        }],
        pageLength: 25,
        order: [[5, 'asc'], [1, 'asc']], // Sort by rating then name
    });
});
</script>

<style>
.label-success { background: #5cb85c; }
.label-primary { background: #337ab7; }
.label-warning { background: #f0ad4e; }
.label-danger  { background: #d9534f; }
</style>
