<div class="row g-4">
    <div class="col-xl-4 col-lg-5">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">Roles</h5>
            </div>
            <div class="card-body p-0">
                <div class="roles-list">
                    @forelse ($roles as $role)
                        <a href="{{ route('staff.index', ['tab' => 'roles', 'role' => $role->id]) }}"
                           class="roles-item text-decoration-none {{ ($selectedRole?->id) === $role->id ? 'active' : '' }}">
                            <div class="roles-item-icon" style="background: color-mix(in srgb, var(--accent-color) 15%, transparent); color: var(--accent-color);">
                                <i class="bi bi-shield-fill"></i>
                            </div>
                            <div class="roles-item-info">
                                <span class="roles-item-name">{{ $role->name }}</span>
                                <span class="roles-item-count">{{ $role->users_count }} users</span>
                            </div>
                        </a>
                    @empty
                        <div class="p-4 text-center text-muted">No roles defined</div>
                    @endforelse
                </div>
            </div>
        </div>
    </div>

    <div class="col-xl-8 col-lg-7">
        @if ($selectedRole)
            @can('update', $selectedRole)
                <form id="rolePermissionsForm" action="{{ route('roles.update', $selectedRole) }}" method="POST">
                    @csrf
                    @method('PUT')
            @endcan

            <div class="card">
                <div class="card-header d-flex flex-column flex-sm-row align-items-sm-center justify-content-sm-between gap-2">
                    <div>
                        <h5 class="card-title mb-0">Permissions — {{ $selectedRole->name }}</h5>
                        <span class="text-muted small">Spatie Laravel Permission (cached, module-based)</span>
                    </div>
                    <div class="d-flex flex-wrap gap-2">
                        @can('delete', $selectedRole)
                            @if (! $selectedRole->isSuperAdmin())
                                <form action="{{ route('roles.destroy', $selectedRole) }}" method="POST"
                                      onsubmit="return confirm('Delete the role &quot;{{ $selectedRole->name }}&quot;?');">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn btn-outline-danger btn-sm"
                                            @disabled($selectedRole->users_count > 0)
                                            title="{{ $selectedRole->users_count > 0 ? 'Reassign or remove staff before deleting this role' : 'Delete role' }}">
                                        <i class="bi bi-trash me-1"></i> Delete Role
                                    </button>
                                </form>
                            @endif
                        @endcan
                        @can('update', $selectedRole)
                            <button type="submit" class="btn btn-primary btn-sm" form="rolePermissionsForm" @disabled($selectedRole->isSuperAdmin())>
                                <i class="bi bi-check-lg me-1"></i> Save Changes
                            </button>
                        @endcan
                    </div>
                </div>
                <div class="card-body p-0">
                    @if ($selectedRole->users_count > 0)
                        <div class="px-3 py-2 border-bottom bg-light small text-muted">
                            {{ $selectedRole->users_count }} staff member(s) use this role. Reassign them before deleting.
                        </div>
                    @endif
                    <div class="table-responsive">
                        <table class="table roles-perm-table mb-0">
                            <thead>
                                <tr>
                                    <th>Module</th>
                                    <th>Permission</th>
                                    <th class="text-center" style="width: 100px;">Granted</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($permissionGroups as $group => $permissions)
                                    @foreach ($permissions as $index => $permission)
                                        <tr>
                                            @if ($index === 0)
                                                <td rowspan="{{ count($permissions) }}" class="align-middle fw-semibold bg-light">
                                                    {{ $group }}
                                                </td>
                                            @endif
                                            <td>{{ $permission['label'] }}</td>
                                            <td class="text-center">
                                                <input type="checkbox" class="form-check-input"
                                                       name="permissions[]"
                                                       value="{{ $permission['name'] }}"
                                                       @checked($permission['granted']['all'])
                                                       @disabled($selectedRole->isSuperAdmin() || auth()->user()->cannot('update', $selectedRole))>
                                            </td>
                                        </tr>
                                    @endforeach
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            @can('update', $selectedRole)
                </form>
            @endcan
        @else
            <div class="card">
                <div class="card-body text-center text-muted py-5">
                    Create a role to manage permissions.
                </div>
            </div>
        @endif
    </div>
</div>

@can('create', \App\Models\Role::class)
    <div class="modal fade" id="addRoleModal" tabindex="-1" aria-labelledby="addRoleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form action="{{ route('roles.store') }}" method="POST">
                    @csrf
                    <div class="modal-header">
                        <h5 class="modal-title" id="addRoleModalLabel">Add Role</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="role_name" class="form-label">Role Name</label>
                            <input type="text" name="name" id="role_name" class="form-control @error('name') is-invalid @enderror"
                                   value="{{ old('name') }}" required>
                            @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary">Create Role</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endcan
