setPrimaryKey('id') ->setDefaultSort('name', 'asc') ->setSortingPillsEnabled(false) ->setAdditionalSelects([ 'users.id as id', 'users.email as email', 'users.email_verified_at as email_verified_at', 'users.status as status', 'users.phone as phone', 'users.company_id as company_id', 'users.created_at as created_at', ]); } public function builder(): Builder { return User::with(['roles', 'company']); } public function columns(): array { return [ Column::make('Usuario', 'name') ->sortable() ->searchable() ->format(function ($value, $row) { $initial = strtoupper(mb_substr($value, 0, 1)); $html = '
'; $html .= '
'.$initial.'
'; $html .= '
'; $html .= '

'.e($value).'

'; $html .= '

'.e($row->email).'

'; $html .= '
'; return $html; }) ->html(), Column::make('Empresa') ->label(fn ($row) => $row->company ? ''.e($row->company->name).'' : '' ) ->html(), Column::make('Rol') ->label(function ($row) { if ($row->roles->isEmpty()) { return 'Sin rol'; } return $row->roles->map(fn ($role) => ''.e($role->name).'' )->implode(' '); }) ->html(), Column::make('Estado', 'status') ->sortable() ->format(function ($value) { $map = [ 'active' => ['badge-success', 'Activo'], 'inactive' => ['badge-ghost', 'Inactivo'], 'suspended' => ['badge-error', 'Suspendido'], ]; [$cls, $label] = $map[$value ?? 'active'] ?? ['badge-ghost', ucfirst($value ?? '')]; return ''.$label.''; }) ->html(), Column::make('Verificado', 'email_verified_at') ->sortable() ->format(fn ($value) => $value ? '' : '' ) ->html(), Column::make('Acciones') ->label(function ($row) { $ver = route('admin.users.show', $row->id); $editar = route('admin.users.edit', $row->id); $name = addslashes($row->name); $isSelf = $row->id === Auth::id(); $html = '
'; $html .= ' '; $html .= ' '; if (! $isSelf) { $html .= ''; } $html .= '
'; return $html; }) ->html(), ]; } public function filters(): array { $roleOptions = [''] + Role::orderBy('name')->pluck('name', 'name')->prepend('Rol: todos', '')->toArray(); return [ SelectFilter::make('Rol') ->options($roleOptions) ->filter(fn (Builder $query, string $value) => $query->whereHas('roles', fn ($q) => $q->where('name', $value)) ), SelectFilter::make('Estado', 'status') ->options([ '' => 'Estado: todos', 'active' => 'Activo', 'inactive' => 'Inactivo', 'suspended' => 'Suspendido', ]) ->filter(fn (Builder $query, string $value) => $query->where('status', $value)), ]; } public function deleteUser(int $id): void { if ($id === Auth::id()) return; User::findOrFail($id)->delete(); } }