HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //home/posscale/www/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'),
        ];
    }
}