File: /home/posscale/public_html/printmanager/app/Filament/Resources/AlertThresholdResource.php
<?php
namespace App\Filament\Resources;
use App\Filament\Resources\AlertThresholdResource\Pages;
use App\Filament\Resources\AlertThresholdResource\RelationManagers;
use App\Models\AlertThreshold;
use App\Models\Make;
use App\Models\Tenant;
use App\Models\TonerInk;
use App\Models\TonerLevelMonitoring;
use Filament\Forms;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class AlertThresholdResource extends Resource
{
protected static ?string $model = AlertThreshold::class;
protected static ?string $navigationIcon = 'heroicon-o-bell';
protected static ?string $navigationGroup = "Printer Parts";
protected static ?int $navigationSort = 8;
public static function form(Form $form): Form
{
return $form
->schema([
Select::make('printer_instance_id')
->label('Printer Instance')
->required()
->live()
->searchable()
->options(function () {
return TonerLevelMonitoring::get()
->groupBy('tenant_id') // Group by tenant or any other property
->mapWithKeys(function ($group, $tenantId) {
// Create group label
$tenantName = $tenantId
? \App\Models\Tenant::find($tenantId)->name ?? "Tenant #$tenantId"
: 'Unknown Tenant';
// Map printer names under this tenant
$options = $group->mapWithKeys(fn ($record) => [
$record->printer_instance_id => $record->printer_name_tenant,
])->toArray();
return [$tenantName => $options];
})
->toArray();
}),
TextInput::make('threshold')
->numeric()
->label('Threshold (%)')
->required()
->minValue(0)
->maxValue(100),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('tenant_id'),
// TextColumn::make('printer_instance_id'),
TextColumn::make('printer_name_tenant')
->label('Printer Name'),
TextColumn::make('threshold'),
])
->filters([
SelectFilter::make('tenant_id')
->label('Filter by Customer name')
->options(Tenant::all()->pluck('id', 'id'))
->default(null),
SelectFilter::make('toner_ink_id')
->label('Filter by Toner Ink')
->options(TonerInk::all()->pluck('part_no', 'id'))
->default(null)
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListAlertThresholds::route('/'),
'create' => Pages\CreateAlertThreshold::route('/create'),
'edit' => Pages\EditAlertThreshold::route('/{record}/edit'),
];
}
}