|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\App\Resources; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Filament\Tables; |
| 7 | +use Filament\Forms\Form; |
| 8 | +use Filament\Tables\Table; |
| 9 | +use Filament\Facades\Filament; |
| 10 | +use App\Models\WhatsappInstance; |
| 11 | +use Filament\Resources\Resource; |
| 12 | +use Illuminate\Support\HtmlString; |
| 13 | +use Filament\Tables\Actions\Action; |
| 14 | +use Filament\Forms\Components\Section; |
| 15 | +use Filament\Tables\Actions\EditAction; |
| 16 | +use Filament\Tables\Actions\ViewAction; |
| 17 | +use Filament\Forms\Components\TextInput; |
| 18 | +use Filament\Notifications\Notification; |
| 19 | +use Filament\Tables\Actions\ActionGroup; |
| 20 | +use Filament\Tables\Actions\DeleteAction; |
| 21 | +use Illuminate\Database\Eloquent\Builder; |
| 22 | +use Filament\Forms\Components\ToggleButtons; |
| 23 | +use Leandrocfe\FilamentPtbrFormFields\PhoneNumber; |
| 24 | +use Illuminate\Database\Eloquent\SoftDeletingScope; |
| 25 | +use App\Filament\App\Resources\WhatsappInstanceResource\Pages; |
| 26 | +use App\Services\Evolution\Instance\DeleteEvolutionInstanceService; |
| 27 | +use App\Services\Evolution\Instance\LogOutEvolutionInstanceService; |
| 28 | +use App\Services\Evolution\Instance\ConnectEvolutionInstanceService; |
| 29 | +use App\Services\Evolution\Instance\RestartEvolutionInstanceService; |
| 30 | +use App\Filament\App\Resources\WhatsappInstanceResource\RelationManagers; |
| 31 | + |
| 32 | + |
| 33 | +class WhatsappInstanceResource extends Resource |
| 34 | +{ |
| 35 | + protected static ?string $model = WhatsappInstance::class; |
| 36 | + |
| 37 | + protected static ?string $navigationIcon = 'fab-whatsapp'; |
| 38 | + |
| 39 | + protected static ?string $navigationGroup = 'Administração'; |
| 40 | + |
| 41 | + protected static ?string $navigationLabel = 'Instâncias WhatsApp'; |
| 42 | + |
| 43 | + protected static ?string $modelLabel = 'Instâncias WhatsApp'; |
| 44 | + |
| 45 | + protected static ?string $modelLabelPlural = "Instâncias WhatsApp"; |
| 46 | + |
| 47 | + protected static ?int $navigationSort = 3; |
| 48 | + |
| 49 | + protected static bool $isScopedToTenant = true; |
| 50 | + |
| 51 | + public static function form(Form $form): Form |
| 52 | + { |
| 53 | + return $form |
| 54 | + ->schema([ |
| 55 | + Section::make('Dados da Instância') |
| 56 | + ->schema([ |
| 57 | + |
| 58 | + TextInput::make('name') |
| 59 | + ->label('Nome da Instância') |
| 60 | + ->unique(WhatsappInstance::class, 'name', ignoreRecord: true) |
| 61 | + ->default(fn () => Filament::getTenant()?->slug ?? '') |
| 62 | + ->required() |
| 63 | + ->prefixIcon('fas-id-card') |
| 64 | + ->validationMessages([ |
| 65 | + 'unique' => 'Nome da instância já cadastrada.', |
| 66 | + ]) |
| 67 | + ->maxLength(20), |
| 68 | + |
| 69 | + PhoneNumber::make('number') |
| 70 | + ->label('Número WhatsApp') |
| 71 | + ->unique(WhatsappInstance::class, 'number', ignoreRecord: true) |
| 72 | + ->mask('+55 (99) 99999-9999') |
| 73 | + ->placeholder('+55 (99) 99999-9999') |
| 74 | + ->required() |
| 75 | + ->prefixIcon('fas-phone') |
| 76 | + ->validationMessages([ |
| 77 | + 'unique' => 'Número já cadastrado.', |
| 78 | + ]), |
| 79 | + |
| 80 | + ])->columns(2), |
| 81 | + |
| 82 | + Section::make('Dados da Instância') |
| 83 | + ->schema([ |
| 84 | + ToggleButtons::make('groups_ignore') |
| 85 | + ->label('Ignorar Grupos') |
| 86 | + ->inline() |
| 87 | + ->boolean() |
| 88 | + ->required(), |
| 89 | + |
| 90 | + ToggleButtons::make('always_online') |
| 91 | + ->label('Status Sempre Online') |
| 92 | + ->inline() |
| 93 | + ->boolean() |
| 94 | + ->required(), |
| 95 | + |
| 96 | + ToggleButtons::make('read_messages') |
| 97 | + ->label('Marcar Mensagens como Lidas') |
| 98 | + ->inline() |
| 99 | + ->boolean() |
| 100 | + ->required(), |
| 101 | + |
| 102 | + ToggleButtons::make('read_status') |
| 103 | + ->label('Marcar Status como Lido') |
| 104 | + ->inline() |
| 105 | + ->boolean() |
| 106 | + ->required(), |
| 107 | + |
| 108 | + ToggleButtons::make('sync_full_history') |
| 109 | + ->label('Sincronizar Histórico') |
| 110 | + ->inline() |
| 111 | + ->boolean() |
| 112 | + ->required(), |
| 113 | + |
| 114 | + ToggleButtons::make('reject_call') |
| 115 | + ->label('Rejeitar Chamadas') |
| 116 | + ->inline() |
| 117 | + ->boolean() |
| 118 | + ->live() |
| 119 | + ->reactive() |
| 120 | + ->required(), |
| 121 | + |
| 122 | + TextInput::make('msg_call') |
| 123 | + ->label('Mensagem para Chamadas Rejeitadas') |
| 124 | + ->required() |
| 125 | + ->hidden(fn ($get) => $get('reject_call') == false) |
| 126 | + ->maxLength(255), |
| 127 | + |
| 128 | + ])->columns(4), |
| 129 | + ]); |
| 130 | + } |
| 131 | + |
| 132 | + public static function table(Table $table): Table |
| 133 | + { |
| 134 | + return $table |
| 135 | + ->columns([ |
| 136 | + Tables\Columns\TextColumn::make('status') |
| 137 | + ->label('Status') |
| 138 | + ->alignCenter() |
| 139 | + ->badge() |
| 140 | + ->searchable(), |
| 141 | + Tables\Columns\TextColumn::make('name') |
| 142 | + ->label('Nome da Instância') |
| 143 | + ->searchable(), |
| 144 | + Tables\Columns\TextColumn::make('number') |
| 145 | + ->label('Número') |
| 146 | + ->searchable(), |
| 147 | + Tables\Columns\TextColumn::make('instance_id') |
| 148 | + ->label('ID da Instância') |
| 149 | + ->searchable(), |
| 150 | + Tables\Columns\TextColumn::make('created_at') |
| 151 | + ->dateTime() |
| 152 | + ->sortable() |
| 153 | + ->toggleable(isToggledHiddenByDefault: true), |
| 154 | + Tables\Columns\TextColumn::make('updated_at') |
| 155 | + ->dateTime() |
| 156 | + ->sortable() |
| 157 | + ->toggleable(isToggledHiddenByDefault: true), |
| 158 | + ]) |
| 159 | + ->filters([ |
| 160 | + // |
| 161 | + ]) |
| 162 | + ->actions([ |
| 163 | + Action::make('showQr') |
| 164 | + ->label('QR Code') |
| 165 | + ->icon('heroicon-o-qr-code') |
| 166 | + ->color('success') |
| 167 | + ->modalHeading('Qr Code WhatsApp') |
| 168 | + ->modalSubmitAction(false) |
| 169 | + ->modalCancelAction( |
| 170 | + \Filament\Actions\Action::make('close') |
| 171 | + ->label('FECHAR') |
| 172 | + ->color('danger') // Cores: primary, secondary, success, danger, warning, gray |
| 173 | + ->extraAttributes(['class' => 'w-full']) // Largura total |
| 174 | + ->close() |
| 175 | + ) |
| 176 | + ->modalWidth('md') // ou sm, lg, xl, 2xl, 3xl, 4xl, 5xl, 6xl, 7xl |
| 177 | + ->modalContent(fn ($record) => view('evolution.qr-code-modal', [ |
| 178 | + 'qrCode' => str_replace('\/', '/', $record->getRawOriginal('qr_code')) |
| 179 | + ])), |
| 180 | + |
| 181 | + ActionGroup::make([ |
| 182 | + Action::make('RestartInstance') |
| 183 | + ->label('Reconectar Instância') |
| 184 | + ->icon('fas-sign-in-alt') |
| 185 | + ->color('info') |
| 186 | + ->action(function ($record) { |
| 187 | + $service = new ConnectEvolutionInstanceService(); |
| 188 | + $response = $service->connectInstance($record->name); |
| 189 | + |
| 190 | + if (isset($response['error'])) { |
| 191 | + Notification::make() |
| 192 | + ->title('Erro ao reconectar') |
| 193 | + ->danger() |
| 194 | + ->send(); |
| 195 | + } else { |
| 196 | + Notification::make() |
| 197 | + ->title('Instância reconectada') |
| 198 | + ->success() |
| 199 | + ->send(); |
| 200 | + } |
| 201 | + }), |
| 202 | + |
| 203 | + ViewAction::make() |
| 204 | + ->color('primary'), |
| 205 | + EditAction::make() |
| 206 | + ->color('secondary'), |
| 207 | + DeleteAction::make() |
| 208 | + ->action(function ($record) { |
| 209 | + |
| 210 | + $service = new DeleteEvolutionInstanceService(); |
| 211 | + $response = $service->deleteInstance($record->name); |
| 212 | + |
| 213 | + // Deleta o registro local após sucesso na API |
| 214 | + $record->delete(); |
| 215 | + |
| 216 | + }), |
| 217 | + ]) |
| 218 | + ->icon('fas-sliders') |
| 219 | + ->color('warning'), |
| 220 | + ]) |
| 221 | + ->bulkActions([ |
| 222 | + |
| 223 | + ]); |
| 224 | + } |
| 225 | + |
| 226 | + public static function getRelations(): array |
| 227 | + { |
| 228 | + return [ |
| 229 | + // |
| 230 | + ]; |
| 231 | + } |
| 232 | + |
| 233 | + public static function getPages(): array |
| 234 | + { |
| 235 | + return [ |
| 236 | + 'index' => Pages\ListWhatsappInstances::route('/'), |
| 237 | + 'create' => Pages\CreateWhatsappInstance::route('/create'), |
| 238 | + 'view' => Pages\ViewWhatsappInstance::route('/{record}'), |
| 239 | + 'edit' => Pages\EditWhatsappInstance::route('/{record}/edit'), |
| 240 | + ]; |
| 241 | + } |
| 242 | +} |
0 commit comments