Files
nuhodds_fi4/app/Filament/Pages/Statistic.php
T
2026-06-18 18:58:29 +02:00

317 lines
15 KiB
PHP

<?php
namespace App\Filament\Pages;
use App\Actions\LoadStatisticFixturesLeaguesOptions;
use App\Actions\LoadStatisticFixturesMatchOptions;
use App\Actions\Selections\LoadPlayer;
use App\Actions\Selections\LoadSeasons;
use App\Models\Element;
use App\Models\StatisticFixtures;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Slider;
use Filament\Pages\Dashboard as BaseDashboard;
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Filament\Support\RawJs;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\HtmlString;
class Statistic extends BaseDashboard
{
use HasFiltersForm;
protected static string|BackedEnum|null $navigationIcon = 'phosphor-chart-line-up';
protected string $view = 'filament.pages.statistic';
protected ?string $pollingInterval = null;
protected static ?string $title = 'Statistic';
protected int|string|array $columnSpan = [
'md' => 2,
'xl' => 3,
];
public function getActiveTab()
{
return $this->activeTab;
}
public function getHeading(): string|Htmlable|null
{
return '';
}
public function filtersForm(Schema $schema): Schema
{
return $schema
->components([
Grid::make([
'default' => 1,
'sm' => 3,
'md' => 4,
])
->columnSpanFull()
->extraAttributes([
'class' => 'p-5 bg-blue-50 dark:bg-blue-950 rounded-lg border border-blue-200 dark:border-blue-800',
])
->schema([
Select::make('league_id')
// ->prefixIconColor('primary')
->prefixIconColor(fn(Get $get): string => $get('league_id') ? 'success' : 'danger')
->prefixIcon('phosphor-trophy')
->hiddenLabel()
->options(fn() => LoadStatisticFixturesLeaguesOptions::load(1))
->live()
->placeholder('League')
->afterStateUpdated(function (callable $set, $state) {
$set('match_id', null);
$set('team_id', null);
$set('tm_player_id', null);
$set('season', null);
}),
Select::make('match_id')
->hiddenLabel()
->prefixIconColor(fn(Get $get): string => $get('match_id') ? 'success' : 'danger')
->prefixIcon('phosphor-calendar-check')
->options(function (Get $get) {
return LoadStatisticFixturesMatchOptions::load($get('league_id'));
})
->afterStateUpdated(function (callable $set) {
$set('team_id', null);
$set('tm_player_id', null);
})
->live()
->placeholder('Events'),
Select::make('team_id')
->hiddenLabel()
->prefixIconColor(function (Get $get): string {
if ($get('team_id')) {
return 'success';
} elseif ($get('similar_past_matches') != 'none') {
return 'gray';
} else {
return 'danger';
}
})
->prefixIcon('phosphor-users-three')
->hidden(fn(Get $get) => $get('similar_past_matches') != 'none')
->options(function (Get $get) {
if ($get('match_id')) {
$res = StatisticFixtures::where('id', $get('match_id'))->first();
$arr = [];
$arr[$res->home_id] = Element::query()->where('id', $res->home_id)->value('name');
$arr[$res->away_id] = Element::query()->where('id', $res->away_id)->value('name');
return $arr;
}
})
->afterStateUpdated(function (callable $set) {
$set('tm_player_id', null);
})
->live()
->placeholder('Team'),
Select::make('team_position')
->hiddenLabel()
->placeholder('Team Position')
->prefix('Team Position')
->prefixIcon('phosphor-map-pin')
->hidden(fn(Get $get) => $get('similar_past_matches') != 'none')
->default('all')
->selectablePlaceholder(false)
->options([
'all' => 'All',
'home' => 'Home',
'away' => 'Away',
])
->live(),
Select::make('statistic_type')
->hiddenLabel()
->placeholder('Statistic Type')
->prefixIconColor('success')
->prefixIcon('phosphor-arrows-out-line-vertical')
->default('none')
->selectablePlaceholder(false)
->options([
'gls' => 'Goals',
'crn' => 'Corner',
'fls' => 'Fouls Commited',
'ofs' => 'Offside',
'crd' => 'Cards',
'snt' => 'Shots On Target',
'tsh' => 'Total Shots',
])
->live(),
Select::make('similar_past_matches')
->hiddenLabel()
->placeholder('Similar Past Matches')
->prefixIconColor('primary')
->suffix(fn(Get $get): string|HtmlString => $get('similar_past_matches') != 'none' ? new HtmlString('<div class="text-center w-24 font-bold rounded p-1 bg-blue-600 dark:bg-blue-700 text-white dark:text-gray-300">± ' . $get('accuracy') . '</div>') : '')
// ->columnSpan([
// 'default' => 1,
// 'sm' => 2,
// ])
->prefixAction(
Action::make('iconInfo')
->icon('phosphor-question')
->tooltip(fn() => $this->tooltip_similar_past_matches())
)
->default('none')
->live()
->selectablePlaceholder(false)
->afterStateUpdated(function (callable $set) {
$set('team_id', null);
$set('tm_player_id', null);
$set('team_position', null);
})
->options([
'none' => 'SPM: None',
'league' => 'SPM: League',
'all' => 'SPM: All Database',
]),
Slider::make('accuracy')
// ->label('Accuracy min: 0, max: 0.25, steps: 0.025')
->hiddenLabel()
->hidden(fn(Get $get) => $get('similar_past_matches') == 'none')
->fillTrack()
->minValue(0)
->maxValue(0.25)
->default(0.125)
->step(0.025)
->decimalPlaces(3)
// ->tooltips()
// ->columnSpan([
// 'default' => 1,
// 'xl' => 2,
// ])
->pips()
->pipsFormatter(RawJs::make(<<<'JS'
`${$value.toFixed(2)}`
JS))
->live(),
Select::make('tm_player_id')
->hiddenLabel()
->allowHtml()
->placeholder('Player')
->hidden(fn(Get $get) => $get('similar_past_matches') != 'none')
->prefixIconColor(fn(Get $get): string => $get('similar_past_matches') == 'none' ? 'primary' : '')
->prefixIcon('icon-player')
->suffixAction(
Action::make('copyCostToPrice')
->action(function (callable $set) {
$set('tm_player_id', null);
$set('player_on_field', null);
})
->color('gray')
->icon('phosphor-x'),
)
->preload()
->options(function (Get $get) {
return LoadPlayer::load($get('team_id'));
})
->live(),
Select::make('player_on_field')
->hiddenLabel()
->placeholder('Time on field')
->prefixIcon('phosphor-watch')
->default('all')
->hidden(fn(Get $get) => ! $get('tm_player_id') or $get('similar_past_matches') != 'none')
->prefixIconColor(fn(Get $get): string => ($get('tm_player_id') and $get('similar_past_matches') == 'none') ? 'primary' : '')
->selectablePlaceholder(false)
->options([
'all' => 'All',
'lineup' => 'Lineup',
'substituted' => 'Substituted',
'notplayed' => 'Not Played',
])
->live(),
Select::make('season')
->columns(2)
->hiddenLabel()
->multiple()
->placeholder('Season')
->hidden(fn(Get $get) => $get('season_or_date') == 'date')
->prefixIcon('phosphor-calendar-star')
->prefixIconColor('primary')
->preload()
->options(function (Get $get) {
return LoadSeasons::load($get('league_id'));
})
->afterStateUpdated(function (callable $set) {
$set('team_id', null);
$set('tm_player_id', null);
})
->live(),
DatePicker::make('start_date')
->hiddenLabel()
->hidden(fn(Get $get) => $get('season_or_date') == 'season')
->native(false)
->prefixIconColor('primary')
->prefixIcon('phosphor-calendar')
->live()
->prefix('Start')
->suffixAction(
Action::make('clear_start')
->icon('phosphor-x')
->color('gray')
->action(fn(Set $set) => $set('start_date', null))
),
DatePicker::make('end_date')
->hiddenLabel()
->hidden(fn(Get $get) => $get('season_or_date') == 'season')
->native(false)
->prefixIconColor('primary')
->prefixIcon('phosphor-calendar')
->live()
->prefix('End')
->suffixAction(
Action::make('clear_end')
->icon('phosphor-x')
->color('gray')
->action(fn(Set $set) => $set('end_date', null))
),
Radio::make('season_or_date')
->columnSpan([
'default' => 1,
'sm' => 2,
])
->inline()
->live()
->default('season')
->hiddenLabel()
->options([
'season' => 'Season',
'date' => 'Date',
]),
]),
]);
}
protected function tooltip_similar_past_matches(): HtmlString
{
$str = '<div class="m-3 grid grid-cols-4 gap-2 max-w-lg">';
$str .= '<div class="col-span-4 font-bold">Similar Past Matches (SMP)</div>';
$str .= '<div class="font-bold">None</div>';
$str .= '<div class="col-span-3"><u>Team</u> within selected league</div>';
$str .= '<div class="font-bold">League</div>';
$str .= '<div class="col-span-3"><u>Simmilar events</u> in league in the range of slider.</div>';
$str .= '<div class="font-bold">All Database</div>';
$str .= '<div class="col-span-3"><u>Simmilar events</u> in database in the range of slider.</div>';
$str .= '</div>';
return new HtmlString($str);
}
}