<?php
/**
 * modules/categories/list_categories.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 * FROM TBL_CATEGORIES ORDER BY CATEGORY_NAME ASC";
$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-tags mr-2"></i>Coal Categories</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=categories&page=add_categories'); ?>" class="btn btn-primary shadow-sm">
                        <i class="fas fa-plus-circle mr-1"></i> Register Category
                    </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="categoriesTable" 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>Category Name</th>
                                    <th width="15%" class="text-center">Status</th>
                                    <?php if($p['can_view'] == 1 || $p['can_edit'] == 1 || $p['can_del'] == 1): ?>
                                        <th width="15%" 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 text-dark"><?php echo htmlspecialchars($row['CATEGORY_NAME']); ?></td>
                                    <td class="text-center align-middle">
                                        <span class="badge <?php echo ($row['STATUS'] == 'ACTIVE') ? 'badge-success' : 'badge-secondary'; ?> px-3 py-1 shadow-sm">
                                            <?php echo htmlspecialchars($row['STATUS']); ?>
                                        </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=categories&page=view_categories&id='.$row['CATEGORY_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=categories&page=edit_categories&id='.$row['CATEGORY_ID']); ?>" 
                                               class="btn btn-sm btn-white border text-primary" title="Edit Category" 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 Category' : 'Activate Category';
                                                    $confirm   = $is_active ? 'Are you sure you want to deactivate this category?' : 'Restore to active status?';
                                                ?>
                                                <a href="index.php?s=<?php echo encrypt_url('mod=categories&page=delete_categories&id='.$row['CATEGORY_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(); 
        $('#categoriesTable').DataTable({
            "responsive": true, "autoWidth": false, "order": [[1, "asc"]],
            "columnDefs": [{ "orderable": false, "targets": "no-export" }],
            "language": { "search": "<strong>Quick Filter:</strong>", "lengthMenu": "Show _MENU_ entries" }
        });
    }
});
</script>