<?php
/**
 * modules/menue/list_menue.php - WIMAS Corporate Standard
 */

// 1. PERMISSIONS
$p = ['V' => 0, 'A' => 0, 'E' => 0, 'D' => 0];
if (in_array(1, $_SESSION['all_role_ids'] ?? [])) {
    $p = ['V' => 1, 'A' => 1, 'E' => 1, 'D' => 1];
}

// 2. FETCH DATA (LEFT JOIN ensures empty Parent Categories still appear)
$sql = "SELECT m.MAIN_MENU_ID, m.MENU_NAME as PARENT, 
               c.CHILD_MENU_ID, c.MENU_NAME as CHILD, 
               c.MENU_URL, COALESCE(c.SORT_ORDER, m.SORT_ORDER) as SORT_ORDER, 
               COALESCE(c.STATUS, m.STATUS) as STATUS 
        FROM TBL_MAIN_MENU m
        LEFT JOIN TBL_CHILD_MENU c ON m.MAIN_MENU_ID = c.MAIN_MENU_ID
        ORDER BY m.SORT_ORDER ASC, c.SORT_ORDER ASC";
$result = $db->query($sql);
?>

<style>
    .dataTables_filter input {
        border-radius: 20px;
        border: 1px solid #ced4da;
        padding: 5px 12px;
        width: 250px !important;
    }
    .btn-group .btn i { font-size: 14px; }
</style>

<section class="content-header">
    <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-sitemap mr-2"></i>Menu Architecture</h1>
            </div>
            <div class="col-sm-6 text-right">
                <?php if($p['A'] == 1): ?>
                <a href="index.php?s=<?php echo encrypt_url('mod=menue&page=add_menue'); ?>" class="btn btn-primary shadow-sm">
                    <i class="fas fa-plus-circle mr-1"></i> Register New Page
                </a>
                <?php endif; ?>
            </div>
        </div>
    </div>
</section>

<section class="content">
    <div class="container-fluid">
        <div class="card card-outline card-primary shadow-sm">
            <div class="card-body">
                <table id="menuTable" class="table table-bordered table-striped table-hover w-100">
                    <thead>
                        <tr>
                            <th>Parent Category</th>
                            <th>Child Page</th>
                            <th>Internal Route</th>
                            <th class="text-center">Order</th>
                            <th class="text-center">Status</th>
                            <th class="text-center">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while($row = mysqli_fetch_assoc($result)): ?>
                        <tr>
                            <td class="align-middle"><span class="badge badge-info shadow-sm px-2 py-1"><?php echo $row['PARENT']; ?></span></td>
                            
                            <td class="font-weight-bold align-middle">
                                <?php echo $row['CHILD'] ? $row['CHILD'] : '<span class="text-muted"><i>-- Empty Category --</i></span>'; ?>
                            </td>
                            
                            <td class="align-middle">
                                <code><?php echo $row['MENU_URL'] ? $row['MENU_URL'] : 'N/A'; ?></code>
                            </td>
                            
                            <td class="text-center align-middle"><?php echo $row['SORT_ORDER']; ?></td>
                            
                            <td class="text-center align-middle">
                                <span class="badge <?php echo ($row['STATUS'] == 'ACTIVE') ? 'badge-success' : 'badge-danger'; ?> px-2 py-1">
                                    <?php echo ($row['STATUS'] == 'ACTIVE') ? 'Active' : 'Hidden'; ?>
                                </span>
                            </td>
                            
                            <td class="text-center align-middle">
                                <div class="btn-group">
                                    <?php if($row['CHILD_MENU_ID'] && $p['E'] == 1): ?>
                                    <a href="index.php?s=<?php echo encrypt_url('mod=menue&page=edit_menue&id='.$row['CHILD_MENU_ID']); ?>" 
                                       class="btn btn-xs btn-outline-primary mr-1" title="Edit Child">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <?php endif; ?>

                                    <?php if($row['CHILD_MENU_ID'] && $p['D'] == 1): ?>
                                    <button type="button" class="btn btn-xs btn-outline-danger" title="Delete Child" 
                                            onclick="if(confirm('Delete this menu item?')) location.href='index.php?s=<?php echo encrypt_url('mod=menue&page=delete_menue&id='.$row['CHILD_MENU_ID']); ?>';">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                    <?php endif; ?>
                                </div>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</section>

<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>
$(function () {
    if ($.fn.DataTable.isDataTable('#menuTable')) {
        $('#menuTable').DataTable().destroy();
    }

    $("#menuTable").DataTable({
      "responsive": true, "lengthChange": true, "autoWidth": false, "pageLength": 10,
      "order": [[0, "asc"], [3, "asc"]]
    });
});
</script>