@php
    /**
     * @var string $mode create|edit|filter
     * @var iterable $permissoesDisponiveis
     * @var array<string, array<string, int>>|null $selectedPermissions
     * @var \Illuminate\Support\Collection|null $permissoesUsuario
     */
    $mode = $mode ?? 'create';
    $selectedPermissions = $selectedPermissions ?? [];
    $permissoesUsuario = $permissoesUsuario ?? collect();
    $searchInputId = $searchInputId ?? 'permissions-search';
    $wrapperId = $wrapperId ?? 'checkboxCategoriasNoticias';
    $selectAllTarget = $selectAllTarget ?? $wrapperId;
    $permissoesOrdenadas = collect($permissoesDisponiveis)->sortBy('value');
@endphp

<div class="mb-3">
    <label for="{{ $searchInputId }}" class="form-label fw-semibold">{{ __('Buscar permissão') }}</label>
    <div class="input-group">
        <span class="input-group-text bg-light" aria-hidden="true">
            <i class="fa-solid fa-magnifying-glass text-muted"></i>
        </span>
        <input
            type="search"
            class="form-control js-permissions-search"
            id="{{ $searchInputId }}"
            data-permissions-wrapper="{{ $wrapperId }}"
            placeholder="{{ __('Digite para filtrar as permissões...') }}"
            autocomplete="off"
        >
    </div>
</div>

<div id="{{ $wrapperId }}" data-permissions-wrapper="{{ $wrapperId }}">
    <div class="table-responsive">
        <table class="table table-hover table-striped align-middle">
            <thead class="table-light">
                <tr>
                    <th>{{ __('Permissão') }}</th>
                    <th>{{ __('Visualizar') }}</th>
                    <th>{{ __('Adicionar') }}</th>
                    <th>{{ __('Editar') }}</th>
                    <th>{{ __('Excluir') }}</th>
                </tr>
            </thead>
            <tbody>
                @foreach($permissoesOrdenadas as $status)
                    @php
                        $permissaoUsuario = $permissoesUsuario->firstWhere('permissao', $status->name);
                        $selectedRow = $selectedPermissions[$status->name] ?? null;
                        $isMainChecked = match ($mode) {
                            'edit' => (bool) $permissaoUsuario,
                            'filter' => $selectedRow !== null,
                            default => (bool) old("permission.{$status->name}", false),
                        };
                        $actionChecked = function (string $action) use ($mode, $permissaoUsuario, $selectedRow, $status) {
                            if ($mode === 'edit') {
                                return $permissaoUsuario && $permissaoUsuario->{$action};
                            }
                            if ($mode === 'filter' && $selectedRow !== null) {
                                return !empty($selectedRow[$action]);
                            }
                            return (bool) old("{$action}.{$status->name}", false);
                        };
                    @endphp
                    <tr data-permission-row="{{ $status->name }}">
                        <td>
                            <div class="form-check">
                                @if($mode === 'edit')
                                    <input
                                        class="form-check-input js-permission-main"
                                        type="checkbox"
                                        name="permission[]"
                                        value="{{ $status->name }}"
                                        @checked($isMainChecked)
                                        onclick="javascript:toggleCheckboxesInRow(this)"
                                    >
                                @else
                                    <input type="hidden" name="permission[{{ $status->name }}]" value="0">
                                    <input
                                        class="form-check-input js-permission-main"
                                        type="checkbox"
                                        name="permission[{{ $status->name }}]"
                                        value="1"
                                        @checked($isMainChecked)
                                        onclick="javascript:toggleCheckboxesInRow(this)"
                                    >
                                @endif
                                <label class="form-check-label">{{ __($status->value) }}</label>
                            </div>
                        </td>
                        @foreach(['visualizar', 'adicionar', 'editar', 'excluir'] as $action)
                            <td>
                                <div class="form-check">
                                    <input type="hidden" name="{{ $action }}[{{ $status->name }}]" value="0">
                                    <input
                                        class="form-check-input js-permission-action"
                                        type="checkbox"
                                        name="{{ $action }}[{{ $status->name }}]"
                                        value="1"
                                        @checked($actionChecked($action))
                                    >
                                    <label class="form-check-label">
                                        @switch($action)
                                            @case('visualizar') {{ __('Visualizar') }} @break
                                            @case('adicionar') {{ __('Adicionar') }} @break
                                            @case('editar') {{ __('Editar') }} @break
                                            @default {{ __('Excluir') }}
                                        @endswitch
                                    </label>
                                </div>
                            </td>
                        @endforeach
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>
