172 lines
5.8 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Filament\Resources\UserResource\Pages;
use App\Filament\Resources\UserResource\RelationManagers;
use App\Models\User;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Facades\Hash;
class UserResource extends Resource
{
protected static ?string $model = User::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
protected static ?string $navigationGroup = 'Data Master';
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->label('Nama Pengguna')
->required()
->maxLength(255),
Forms\Components\TextInput::make('email')
->label('Email')
->email()
->required()
->maxLength(255),
Forms\Components\TextInput::make('password')
->password()
->required(fn (string $context): bool => $context === 'create')
->dehydrateStateUsing(fn ($state) => Hash::make($state))
->dehydrated(fn ($state) => filled($state))
->maxLength(255),
Forms\Components\Select::make('role')
->label('Role')
->relationship('roles', 'name')
->preload()
->searchable()
->required(),
Forms\Components\TextInput::make('nip')
->label('NIP')
->maxLength(255),
Forms\Components\Select::make('gender')
->label('Jenis Kelamin')
->options([
'L' => 'L',
'P' => 'P',
]),
Forms\Components\DatePicker::make('birth_date')
->label('Tanggal Lahir'),
Forms\Components\TextInput::make('phone')
->label('Nomor Telepon')
->tel()
->maxLength(255),
Forms\Components\Textarea::make('address')
->label('Alamat')
->columnSpanFull(),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->label('Nama')
->searchable(),
Tables\Columns\TextColumn::make('email')
->label('Email')
->searchable(),
Tables\Columns\TextColumn::make('nip')
->label('NIP')
->searchable(),
Tables\Columns\TextColumn::make('gender')
->label('Jenis Kelamin')
->formatStateUsing(fn (string $state): string => ucfirst($state)),
Tables\Columns\TextColumn::make('birth_date')
->label('Tanggal Lahir')
->date(),
Tables\Columns\TextColumn::make('phone')
->label('Nomor Telepon')
->searchable(),
Tables\Columns\TextColumn::make('roles.name')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('deleted_at') // Add this column to show deleted timestamp
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
Tables\Filters\TrashedFilter::make()->label('Tampilkan Data yang Dihapus'),
])
->actions([
Tables\Actions\EditAction::make()->visible(fn () => auth()->user()->hasRole('admin')),
Tables\Actions\DeleteAction::make()->visible(fn () => auth()->user()->hasRole('admin')),
Tables\Actions\RestoreAction::make()->visible(fn (User $record) => auth()->user()->hasRole('admin') && $record->trashed()),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make()->visible(fn () => auth()->user()->hasRole('admin')),
]),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListUsers::route('/'),
'create' => Pages\CreateUser::route('/create'),
'edit' => Pages\EditUser::route('/{record}/edit'),
];
}
public static function getNavigationLabel(): string
{
return 'Pengguna';
}
public static function getBreadcrumb(): string
{
return 'Pengguna';
}
public static function getPluralModelLabel(): string
{
return 'Pengguna';
}
// This method is required to apply the SoftDeletingScope to the query
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
}