<div class="col-lg-12">
 <div class="card">
    <div class="card-body">
        <h4 class="card-title">Assign Permissions to Role</h4>

        <!-- Step 1: Role Selector -->
        <?= form_open('admin/settings/index', ['method' => 'get']) ?>
            <div class="form-group">
                <label for="role_id">Select Role:</label>
                <select name="role_id" id="role_id" class="form-control" onchange="this.form.submit()">
                    <option value="">-- Select Role --</option>
                    <?php foreach ($roles as $role): ?>
                        <option value="<?= $role->id ?>" <?= ($selected_role_id == $role->id) ? 'selected' : '' ?>>
                            <?= ucfirst($role->label) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
        <?= form_close(); ?>

        <!-- Step 2: Assign Permissions -->
        <?php if (!empty($selected_role_id)): ?>
            <?= form_open('admin/RolePermissions/save') ?>
            <input type="hidden" name="role_id" value="<?= $selected_role_id ?>">

            <div class="table-responsive">
                <table id="" class="table display table-bordered table-striped">
                    <thead class="text-center">
                        <tr>
                            <th class="text-left">Controller</th>
                            <th>View</th>
                            <th>Add</th>
                            <th>Edit</th>
                            <th>Delete</th>
                            
                        </tr>
                    </thead>
                    <tbody>
                        <!-- <pre><?php print_r($assigned_permissions); ?></pre> -->

                        <?php
                        $methods = ['view', 'add', 'edit', 'delete'];
                        $grouped = [];
                        foreach ($permissions as $perm) {
                            $grouped[$perm->controller][] = $perm;
                        }
                        ?>

                        <?php foreach ($grouped as $controller => $perms): ?>
                            <tr>
                                <td><strong><?= ucfirst($controller) ?></strong></td>
                                <?php foreach ($methods as $method): ?>
                                    <?php
                                    $permFound = false;
                                    foreach ($perms as $perm) {
                                        if ($perm->method == $method) {
                                            $permFound = true;
                                            echo '<td class="text-center"><input type="checkbox" name="permissions[]" value="' . $perm->id . '" ' . (in_array($perm->id, $assigned_permissions) ? 'checked' : '') . '></td>';
                                            break;
                                        }
                                    }
                                    if (!$permFound) echo '<td></td>';
                                    ?>
                                <?php endforeach; ?>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <button type="submit" class="btn btn-success mt-3">Save Permissions</button>
            <?= form_close(); ?>
        <?php endif; ?>
    </div>
</div>
</div>