Files
nuhodds_fi4/app/Livewire/ComparingBookmakerMenu.php
T
2026-01-26 01:52:47 +01:00

197 lines
8.4 KiB
PHP

<?php
namespace App\Livewire;
use App\Filament\Tables\Columns\BookmakerIcon;
use App\Models\ComparingBookmaker;
use App\Models\User;
use Filament\Actions\Action;
use Filament\Actions\BulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Concerns\InteractsWithSchemas;
use Filament\Schemas\Contracts\HasSchemas;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\TextInputColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Enums\FiltersLayout;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Livewire\Component;
class ComparingBookmakerMenu extends Component implements HasActions, HasSchemas, HasTable
{
use InteractsWithActions;
use InteractsWithSchemas;
use InteractsWithTable;
public $user;
protected $listeners = [
'refresh' => '$refresh',
];
public function table(Table $table): Table
{
$this->user = User::getUser();
return $table
->description('')
->description('Bookmakers will be compared from top to bottom. First one matches criteria is used for comparition.')
->reorderable('sorting')
->reorderRecordsTriggerAction(function (Action $action) {
return $action->button();
})
->paginated(false)
->striped()
->query(ComparingBookmaker::query()
->with('bookmaker')
->where('sport_id', $this->user->sport_id)
->where('client_bookmaker_id', $this->user->bookmaker_id)
->orderBy('sorting')
)
->columns([
TextColumn::make('Position')
->alignCenter()
->badge()
->rowIndex(),
TextColumn::make('bookmaker_id')->label('Id'),
BookmakerIcon::make('bookmaker.icon')->label('Icon')->size('lg'),
TextInputColumn::make('time_lastcheck')
->label('Time Passed')
->prefix('Seconds:')
->rules(['required', 'integer', 'between:60,1200']),
TextInputColumn::make('margin_threshold')
->label('Maximum Margin')
->prefix('Threshold:')
->rules(['required', 'integer', 'between:100,200']),
// ToggleColumn::make('is_active'),
])
->searchable(['bookmaker.name'])
// ->filters([
// TernaryFilter::make('is_active')
// ->columnSpan(2)
// ->default(1)
// ->label('Show active'),
// ], layout: FiltersLayout::AboveContent)
->deferFilters(false)
->toolbarActions([
BulkActionGroup::make([
BulkAction::make('time_lastcheck')
->label('Max time since last check in seconds')
->icon('phosphor-timer')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
ComparingBookmaker::where('id', '=', $record->id)
->update(
['time_lastcheck' => $data['time_lastcheck']]
);
}
})
->schema([
TextInput::make('time_lastcheck')
->numeric()
->hiddenLabel()
->prefix('Seconds'),
]),
BulkAction::make('margin_threshold')
->label('Maximum height of marging')
->icon('phosphor-timer')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
ComparingBookmaker::where('id', '=', $record->id)
->update(
['margin_threshold' => $data['margin_threshold']]
);
}
})
->schema([
TextInput::make('margin_threshold')
->hiddenLabel()
->numeric()
->minValue(100)
->maxValue(250)
->prefix('Threshold'),
]),
BulkAction::make('Switch On')
->icon('phosphor-play')
->action(function (Collection $records): void {
foreach ($records as $record) {
ComparingBookmaker::where('id', '=', $record->id)
->update(
['is_active' => 1]
);
}
}),
BulkAction::make('Switch Off')
->icon('phosphor-stop')
->action(function (Collection $records): void {
foreach ($records as $record) {
ComparingBookmaker::where('id', '=', $record->id)
->update(
['is_active' => 0]
);
}
}),
]),
])->recordActions([
Action::make('end')
->iconButton()
->icon('phosphor-arrow-fat-line-down')
->tooltip('Move to the end')
->action(function ($record) {
$res = ComparingBookmaker::where('method_id', $record->method_id)
->where('sport_id', $record->sport_id)
->where('client_bookmaker_id', $record->client_bookmaker_id)
->orderBy('sorting')
->pluck('bookmaker_id', 'id');
$sorting = 1;
foreach ($res as $id => $bookmaker_id) {
if ($bookmaker_id == $record->bookmaker_id) {
ComparingBookmaker::where('id', $id)
->update(['sorting' => \count($res)]);
} else {
ComparingBookmaker::where('id', $id)
->update(['sorting' => $sorting]);
$sorting++;
}
}
}),
Action::make('start')
->iconButton()
->icon('phosphor-arrow-fat-line-up')
->tooltip('Move to begining')
->action(function ($record) {
$res = ComparingBookmaker::where('method_id', $record->method_id)
->where('sport_id', $record->sport_id)
->where('client_bookmaker_id', $record->client_bookmaker_id)
->orderBy('sorting')
->pluck('bookmaker_id', 'id');
$sorting = 2;
foreach ($res as $id => $bookmaker_id) {
if ($bookmaker_id == $record->bookmaker_id) {
ComparingBookmaker::where('id', $id)
->update(['sorting' => 1]);
} else {
ComparingBookmaker::where('id', $id)
->update(['sorting' => $sorting]);
$sorting++;
}
}
}),
]);
}
public function render(): View
{
return view('livewire.comparing-bookmaker-menu');
}
}