Files
data-collection-terminal/management-panel/app/Filament/Resources/MetricResource.php
2024-09-09 14:40:59 +08:00

155 lines
5.3 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Filament\Resources\MetricResource\Pages;
use App\Livewire\MetricWidgetChart;
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;
use LaraZeus\InlineChart\Tables\Columns\InlineChart;
class MetricResource extends Resource
{
protected static ?string $model = Metric::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
protected static ?string $navigationLabel = '测点配置管理'; // 设置侧边栏的中文名称
protected static ?string $label = '测点';
protected static ?string $pluralLabel = '测点';
public static function getBreadcrumb(): string
{
return '测点配置管理'; // 修改最外层面包屑标题为 "测点配置管理"
}
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->label('名称')
->required()
->maxLength(255),
TextInput::make('help')
->label('帮助')
->maxLength(255),
Section::make('节点 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('标识符类型')
->required()
->reactive(), // 确保当选择的值变化时触发其他字段的重新渲染
TextInput::make('namespace_index')
->label('命名空间')
->required(),
// 数值型标识符字段
TextInput::make('numeric_id')
->label('数值 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')
->label('名称'),
TextColumn::make('nodeid')
->label('节点 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';
}
}),
InlineChart::make('data')
->label('数据')
->chart(MetricWidgetChart::class)
->maxWidth(350)
->maxHeight(90)
->description('')
->toggleable(),
TextColumn::make('help')
->label('帮助'),
])
->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'),
];
}
}