Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
4.5 kB
4
Indexable
Never
<?php

namespace App\Filament\Resources\CompanyResource\RelationManagers;

use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use App\Models\Contact;
use App\Filament\Resources\ContactResource;
use App\Models\CompanyContact;
use Filament\Tables\Actions\AttachAction;

class ContactsRelationManager extends RelationManager
{
    protected static string $relationship = 'contacts';

    public function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Toggle::make('is_point_of_contact')
                    ->inline(false)
                    ->required(),
                Forms\Components\CheckboxList::make('roles')
                    ->options([
                        'Director' => 'Director',
                        'Shareholder' => 'Shareholder',
                        'Beneficial Owner' => 'Beneficial Owner',
                        'Partner' => 'Partner',
                        'Power of Attorney' => 'Power of Attorney',
                        'Other' => 'Other',
                    ])
                    ->afterStateHydrated(function ($component, string $state) {
                        $component->state(json_decode($state));
                    }),
            ])
            ->columns(2);
    }

    public function table(Table $table): Table
    {
        $currentlyAttachedCompanyContactIds = CompanyContact::where('company_id', '=', 6)->pluck('contact_id');

        return $table
            ->recordTitleAttribute('name_and_contact_details')
            ->columns([
                Tables\Columns\TextColumn::make('fullname')
                    ->label('Full name'),
                Tables\Columns\TextColumn::make('email')
                    ->copyable(),
//                Tables\Columns\TextColumn::make('companyContacts.roles.name'),
                Tables\Columns\IconColumn::make('is_point_of_contact')
                    ->icon(fn(string $state): string => match ($state) {
                        '1' => 'heroicon-o-check-circle',
                        '0' => 'heroicon-o-x-circle'
                    })
                    ->color(fn(string $state): string => match ($state) {
                        '1' => 'success',
                        '0' => 'gray'
                    }),
                Tables\Columns\TextColumn::make('roles')
                    ->listWithLineBreaks()
            ])
            ->filters([
                //
            ])
            ->headerActions([
                Tables\Actions\AttachAction::make()
                    ->Label('Add')
                    ->recordSelectSearchColumns(['firstname', 'middlename', 'lastname', 'email', 'secondary_email', 'phone', 'other_contact_details'])
                    ->preloadRecordSelect()
                    ->form(
                        fn(AttachAction $action): array => [
                            $action->getRecordSelect()
                                ->searchable(),
                            Forms\Components\Toggle::make('is_point_of_contact')
                                ->required(),
                            Forms\Components\CheckboxList::make('roles')
                                ->options([
                                    'Director' => 'Director',
                                    'Shareholder' => 'Shareholder',
                                    'Beneficial Owner' => 'Beneficial Owner',
                                    'Partner' => 'Partner',
                                    'Power of Attorney' => 'Power of Attorney',
                                    'Other' => 'Other',
                                ])
                        ]
                    )
            ])
            ->actions([
                Tables\Actions\Action::make('view')
                    ->url(fn(Contact $record): string => ContactResource::getUrl('view', ['record' => $record]))
                    ->icon('heroicon-s-eye'),
                Tables\Actions\EditAction::make('edit')
//                    ->url(fn (Contact $record): string => ContactResource::getUrl('edit', ['record' => $record]))
                    ->icon('heroicon-s-pencil-square'),
                Tables\Actions\DetachAction::make('detach'),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                ]),
            ]);
    }

}
Leave a Comment