Files
nuhodds_fi4/app/Filament/Pages/Statistic.php
T
2026-08-11 18:26:38 +02:00

359 lines
17 KiB
PHP

<?php
namespace App\Filament\Pages;
use App\Actions\LoadStatisticFixturesLeaguesOptions;
use App\Actions\LoadStatisticFixturesMatchOptions;
use App\Filament\Pages\Selections\LoadPlayer;
use App\Filament\Pages\Selections\LoadSeasons;
use App\Models\SofaScoreRosa;
use App\Models\StatisticFixtures;
use BackedEnum;
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
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\Contracts\View\View;
use Illuminate\Support\HtmlString;
class Statistic extends BaseDashboard
{
use HasFiltersForm, HasPageShield;
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()
->schema([
Select::make('league_id')
->live()
->searchable()
->prefixIconColor(fn(Get $get): string => $get('league_id') ? 'success' : 'danger')
->prefixIcon('phosphor-trophy-fill')
->hiddenLabel()
->options(fn() => LoadStatisticFixturesLeaguesOptions::load(1))
->placeholder('League')
->afterStateUpdated(function (callable $set, $state) {
$set('match_id', null);
$set('team_id', null);
$set('ss_player_id', null);
$set('season', null);
}),
Select::make('match_id')
->live()
->hiddenLabel()
->prefixIconColor(fn(Get $get): string => $get('match_id') ? 'success' : 'danger')
->prefixIcon('phosphor-calendar-check-fill')
->options(function (Get $get) {
return LoadStatisticFixturesMatchOptions::load($get('league_id'));
})
->afterStateUpdated(function (callable $set) {
$set('team_id', null);
$set('ss_player_id', null);
})
->placeholder('Events'),
Select::make('team_id')
->live()
->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-fill')
->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 = [];
if ($res) {
$arr[$res->home_id] = SofaScoreRosa::query()->where('ss_team_id', $res->home_id)->value('ss_team_name');
$arr[$res->away_id] = SofaScoreRosa::query()->where('ss_team_id', $res->away_id)->value('ss_team_name');
}
return $arr;
}
})
->afterStateUpdated(function (callable $set) {
$set('ss_player_id', null);
})
->placeholder('Team'),
Select::make('team_position')
->live()
->hiddenLabel()
->placeholder('Team Position')
->prefix('TP')
->prefixAction(
Action::make('iconInfo')
->color('success')
->icon('phosphor-question-fill')
->tooltip(fn() => 'Team Position')
)
->hidden(fn(Get $get) => $get('similar_past_matches') != 'none')
->default('all')
->selectablePlaceholder(false)
->options([
'all' => 'All',
'home' => 'Home',
'away' => 'Away',
])->afterStateUpdated(function (callable $set) {
// $set('team_id', null);
$set('ss_player_id', null);
}),
]),
Grid::make([
'default' => 1,
'sm' => 3,
'md' => 4,
])
->columnSpanFull()
->schema([
Select::make('statistic_type')
->live()
->hiddenLabel()
->allowHtml()
->placeholder('Statistic Type')
->prefixIconColor('success')
->prefixIcon(function (Get $get) {
return match ($get('statistic_type')) {
'gls' => 'phosphor-soccer-ball-fill',
'crn' => 'phosphor-angle',
'fls' => 'phosphor-first-aid-kit',
'ofs' => 'phosphor-arrow-fat-lines-right-fill',
'crd' => 'phosphor-cards-fill',
'snt' => 'phosphor-target-fill',
'tsh' => 'phosphor-chart-polar-fill',
default => 'phosphor-soccer-ball-fill',
};
})
->default('gls')
->selectablePlaceholder(false)
->options([
'gls' => 'Goals',
'crn' => 'Corner',
'fls' => 'Fouls Commited',
'ofs' => 'Offside',
'crd' => 'Cards',
'snt' => 'Shots On Target',
'tsh' => 'Total Shots',
]),
Select::make('similar_past_matches')
->live()
->hiddenLabel()
->prefix('SPM')
->placeholder('Similar Past Matches')
->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>') : '')
->prefixAction(
Action::make('iconInfo')
->icon('phosphor-question-fill')
// ->color(fn(Get $get): string => $get('similar_past_matches') != 'none' ? 'success' : 'primary')
->color('success')
->tooltip(fn() => $this->tooltip_similar_past_matches())
)
->default('none')
->selectablePlaceholder(false)
->afterStateUpdated(function (callable $set) {
// $set('team_id', null);
$set('ss_player_id', null);
$set('team_position', null);
})
->options([
'none' => 'None',
'league' => 'League',
'all' => 'All Database',
]),
Slider::make('accuracy')
->live()
->hiddenLabel()
->hidden(fn(Get $get) => $get('similar_past_matches') == 'none')
->fillTrack()
->minValue(0)
->maxValue(0.25)
->default(0.125)
->step(0.025)
->decimalPlaces(3)
->pips()
->pipsFormatter(RawJs::make(<<<'JS'
`${$value.toFixed(2)}`
JS)),
Select::make('ss_player_id')
->live()
->hiddenLabel()
->allowHtml()
->placeholder('Player')
->hidden(fn(Get $get) => $get('similar_past_matches') != 'none')
->prefixIconColor(fn(Get $get): string => $get('similar_past_matches') == 'none' ? 'success' : '')
->prefixIcon('icon-player')
->preload()
->searchable()
->extraAttributes(fn() => ['id' => 'player_selector'])
->suffixAction(
Action::make('iconInfo')
->icon('phosphor-question-fill')
->color('success')
->tooltip(fn() => $this->tooltip_player_info())
)
->options(function (Get $get) {
return LoadPlayer::load($get);
}),
Select::make('player_on_field')
->live()
->hiddenLabel()
->placeholder('Time on field')
->prefixIcon('phosphor-watch-fill')
->default('all')
->hidden(fn(Get $get) => ! $get('ss_player_id'))
->prefixIconColor('success')
->selectablePlaceholder(false)
->options([
'all' => 'All',
'lineup' => 'Lineup',
'substituted' => 'Substituted',
'notplayed' => 'Not Played',
]),
]),
Grid::make([
'default' => 4,
'sm' => 4,
])
->schema([
Radio::make('season_or_date')
->live()
->extraAttributes([
'class' => 'radio-statistic-group',
])
->columnSpan([
'default' => 4,
'sm' => 4,
])
->inline()
->default('season')
->hiddenLabel()
->options([
'season' => 'Season',
'date' => 'Date',
]),
]),
Select::make('season')
->live()
->columns(2)
->hiddenLabel()
->multiple()
->placeholder('All Seasons')
->prefixIconColor('success')
// ->prefixIconColor(fn(Get $get): string => $get('season') ? 'success' : 'primary')
->hidden(fn(Get $get) => $get('season_or_date') == 'date')
->prefixIcon('phosphor-calendar-star-fill')
->preload()
->options(function (Get $get) {
return LoadSeasons::load($get('league_id'));
}),
DatePicker::make('start_date')
->live()
->hiddenLabel()
->hidden(fn(Get $get) => $get('season_or_date') == 'season')
->native(false)
->prefixIconColor(fn(Get $get): string => $get('start_date') ? 'success' : 'primary')
->prefixIcon('phosphor-calendar-fill')
->prefix('Start')
->suffixAction(
Action::make('clear_start')
->icon('phosphor-x')
->color('gray')
->action(fn(Set $set) => $set('start_date', null))
),
DatePicker::make('end_date')
->live()
->hiddenLabel()
->hidden(fn(Get $get) => $get('season_or_date') == 'season')
->native(false)
->prefixIconColor(fn(Get $get): string => $get('end_date') ? 'success' : 'primary')
->prefixIcon('phosphor-calendar-fill')
->prefix('End')
->suffixAction(
Action::make('clear_end')
->icon('phosphor-x')
->color('gray')
->action(fn(Set $set) => $set('end_date', null))
),
]);
}
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);
}
protected function tooltip_player_info(): HtmlString
{
$str = '<div class="m-3 grid grid-cols-2 gap-2 max-w-lg">';
$str .= '<div class="col-span-2 font-bold">Player Information</div>';
$str .= '<div class="font-bold">Events played:</div>';
$str .= '<div>sum</div>';
$str .= '<div class="font-bold">Stronger foot:</div>';
$str .= '<div>left | right | both</div>';
$str .= '<div class="font-bold">Average on field:</div>';
$str .= '<div>minutes</div>';
$str .= '</div>';
return new HtmlString($str);
}
}