129 lines
4.5 KiB
PHP
129 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Resources\MetricResource\Pages;
|
|
use App\Filament\Resources\MetricResource\RelationManagers;
|
|
use App\Models\Metric;
|
|
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 Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Components\Section;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
|
|
class MetricResource extends Resource
|
|
{
|
|
protected static ?string $model = Metric::class;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
TextInput::make('name')
|
|
->required()
|
|
->maxLength(255),
|
|
TextInput::make('help')
|
|
->maxLength(255),
|
|
|
|
Section::make('Node ID')
|
|
->description('节点设置必须唯一')
|
|
->columns([
|
|
'default' => 3
|
|
])
|
|
->schema([
|
|
// 1 = Numeric, 2 = String, 3 = GUID
|
|
Select::make('identifier_type')
|
|
->options([
|
|
1 => 'Numeric',
|
|
2 => 'String',
|
|
3 => 'GUID',
|
|
])
|
|
->default(1)
|
|
->label('Identifier Type')
|
|
->required()
|
|
->reactive(), // 确保当选择的值变化时触发其他字段的重新渲染
|
|
|
|
TextInput::make('namespace_index')
|
|
->label('Namespace')
|
|
->required(),
|
|
|
|
// 数值型标识符字段
|
|
TextInput::make('numeric_id')
|
|
->label('Numeric ID')
|
|
->required()
|
|
->visible(fn($get) => $get('identifier_type') == Metric::TYPE_NUMERIC),
|
|
|
|
// 字符串型标识符字段
|
|
TextInput::make('string_id')
|
|
->label('String ID')
|
|
->required()
|
|
->visible(fn($get) => $get('identifier_type') == Metric::TYPE_STRING),
|
|
|
|
// GUID 型标识符字段
|
|
TextInput::make('guid_id')
|
|
->label('GUID ID')
|
|
->required()
|
|
->visible(fn($get) => $get('identifier_type') == Metric::TYPE_GUID),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
TextColumn::make('name'),
|
|
TextColumn::make('nodeid')
|
|
->label('Node ID')
|
|
->state(function ($record) {
|
|
switch ($record->identifier_type) {
|
|
case 1: // Numeric
|
|
return 'ns=' . $record->namespace_index . ';i=' . $record->numeric_id;
|
|
case 2: // String
|
|
return 'ns=' . $record->namespace_index . ';s=' . $record->string_id;
|
|
case 3: // GUID
|
|
return 'ns=' . $record->namespace_index . ';g=' . $record->guid_id;
|
|
default:
|
|
return 'Unknown Type';
|
|
}
|
|
}),
|
|
TextColumn::make('help')
|
|
])
|
|
->filters([
|
|
//
|
|
])
|
|
->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\ListMetrics::route('/'),
|
|
'create' => Pages\CreateMetric::route('/create'),
|
|
'edit' => Pages\EditMetric::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|