<?php
/**
 * modules/rates/list_rates.php
 */

$p = $GLOBALS['my_privs'] ?? ['can_view' => 0, 'can_add' => 0, 'can_edit' => 0, 'can_del' => 0];
if (in_array(1, $_SESSION['all_role_ids'] ?? [])) {
    $p = ['can_view' => 1, 'can_add' => 1, 'can_edit' => 1, 'can_del' => 1];
}

$sql = "SELECT r.*, c.CATEGORY_NAME, t.TYPE_NAME 
        FROM TBL_RATES r
        JOIN TBL_CATEGORIES c ON r.CATEGORY_ID = c.CATEGORY_ID
        JOIN TBL_COAL_TYPES t ON r.TYPE_ID = t.TYPE_ID
        ORDER BY c.CATEGORY_NAME ASC, t.TYPE_NAME ASC, r.EFFECTIVE_FROM DESC";
$result = $db->query($sql);
?>

<style>
    .thead-wimas { background-color: #F4F7FA; color: #343a40; border-bottom: 2px solid #007bff; }
    .btn-white { background-color: #ffffff; }
    .dataTables_filter input { border-radius: 20px; border: 1px solid #ced4da; padding: 5px 12px; width: 250px !important; }
</style>

<div class="p-3">
    <section class="content-header px-0">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="font-weight-bold text-primary"><i class="fas fa-hand-holding-usd mr-2"></i>Financial Rates Matrix</h1>
                </div>
                <div class="col-sm-6 text-right">
                    <?php if($p['can_add'] == 1): ?>
                    <a href="index.php?s=<?php echo encrypt_url('mod=rates&page=add_rates'); ?>" class="btn btn-primary shadow-sm">
                        <i class="fas fa-plus-circle mr-1"></i> Register New Rate
                    </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </section>

    <section class="content px-0">
        <div class="container-fluid">
            <div class="card card-outline card-primary shadow-sm" style="border-top: 4px solid #007bff;">
                <div class="card-body p-0">
                    <div class="table-responsive p-3">
                        <table id="ratesTable" class="table table-hover table-striped mb-0" style="font-size: 0.9rem;">
                            <thead class="thead-wimas">
                                <tr>
                                    <th width="5%" class="pl-3">#</th>
                                    <th>Coal Category</th>
                                    <th>Coal Type</th>
                                    <th class="text-right">Rate / Ton (Rs)</th>
                                    <th class="text-center">Effective Timeline</th>
                                    <th width="10%" class="text-center">Status</th>
                                    <?php if($p['can_view'] == 1 || $p['can_edit'] == 1 || $p['can_del'] == 1): ?>
                                        <th width="12%" class="text-center no-export">Management</th>
                                    <?php endif; ?>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $i=1; while($row = mysqli_fetch_assoc($result)): ?>
                                <tr>
                                    <td class="pl-3 align-middle text-muted"><?php echo $i++; ?></td>
                                    <td class="align-middle font-weight-bold"><?php echo htmlspecialchars($row['CATEGORY_NAME']); ?></td>
                                    <td class="align-middle"><span class="badge badge-info shadow-sm px-2 py-1"><?php echo htmlspecialchars($row['TYPE_NAME']); ?></span></td>
                                    <td class="align-middle text-right font-weight-bold text-success" style="font-size: 1.1rem;">
                                        <?php echo number_format($row['RATE_PER_TON'], 2); ?>
                                    </td>
                                    <td class="align-middle text-center text-muted" style="font-size: 0.85rem;">
                                        <?php echo date('d-M-Y', strtotime($row['EFFECTIVE_FROM'])); ?> 
                                        <i class="fas fa-arrow-right mx-1" style="font-size:10px;"></i> 
                                        <?php echo $row['EFFECTIVE_TO'] ? date('d-M-Y', strtotime($row['EFFECTIVE_TO'])) : '<span class="text-primary font-weight-bold">Present</span>'; ?>
                                    </td>
                                    <td class="text-center align-middle">
                                        <?php 
                                            if ($row['STATUS'] == 'ACTIVE') {
                                                echo '<span class="badge badge-success px-2 py-1 shadow-sm">CURRENT</span>';
                                            } elseif ($row['STATUS'] == 'HISTORICAL') {
                                                echo '<span class="badge badge-warning px-2 py-1 shadow-sm">HISTORICAL</span>';
                                            } else {
                                                echo '<span class="badge badge-secondary px-2 py-1 shadow-sm">INACTIVE</span>';
                                            }
                                        ?>
                                    </td>
                                    
                                    <?php if($p['can_view'] == 1 || $p['can_edit'] == 1 || $p['can_del'] == 1): ?>
                                    <td class="text-center align-middle no-export">
                                        <div class="btn-group shadow-sm">
                                            <?php if($p['can_view'] == 1): ?>
                                            <a href="index.php?s=<?php echo encrypt_url('mod=rates&page=view_rates&id='.$row['RATE_ID']); ?>" 
                                               class="btn btn-sm btn-white border text-info" title="View Details" data-toggle="tooltip">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <?php endif; ?>

                                            <?php if($p['can_edit'] == 1): ?>
                                            <a href="index.php?s=<?php echo encrypt_url('mod=rates&page=edit_rates&id='.$row['RATE_ID']); ?>" 
                                               class="btn btn-sm btn-white border text-primary" title="Edit Rate Typo" data-toggle="tooltip">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <?php endif; ?>

                                            <?php if($p['can_del'] == 1): ?>
                                                <?php
                                                    $is_active = ($row['STATUS'] == 'ACTIVE');
                                                    $btn_class = $is_active ? 'text-danger' : 'text-success';
                                                    $icon      = $is_active ? 'fa-ban' : 'fa-check-circle';
                                                    $tooltip   = $is_active ? 'Deactivate Rate' : 'Activate Rate';
                                                    $confirm   = $is_active ? 'Are you sure you want to deactivate this rate?' : 'Restore this rate?';
                                                ?>
                                                <a href="index.php?s=<?php echo encrypt_url('mod=rates&page=delete_rates&id='.$row['RATE_ID'].'&action='.($is_active ? 'deactivate' : 'activate')); ?>"
                                                   class="btn btn-sm btn-white border <?php echo $btn_class; ?>"
                                                   onclick="return confirm('<?php echo $confirm; ?>');"
                                                   title="<?php echo $tooltip; ?>" data-toggle="tooltip">
                                                    <i class="fas <?php echo $icon; ?>"></i>
                                                </a>
                                            <?php endif; ?>
                                        </div>
                                    </td>
                                    <?php endif; ?>
                                </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

<script src="assets/plugins/datatables/jquery.dataTables.min.js"></script>
<script src="assets/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="assets/plugins/datatables-bs4/css/dataTables.bootstrap4.min.js">

<script>
window.addEventListener('load', function() {
    if (window.jQuery && $.fn.DataTable) {
        $('[data-toggle="tooltip"]').tooltip(); 
        $('#ratesTable').DataTable({
            "responsive": true, "autoWidth": false, "order": [[1, "asc"], [2, "asc"], [4, "desc"]],
            "columnDefs": [{ "orderable": false, "targets": "no-export" }],
            "language": { "search": "<strong>Quick Filter:</strong>", "lengthMenu": "Show _MENU_ entries" }
        });
    }
});
</script>