Files
nuhodds_fi4/app/Livewire/MapperLeft.php
T
2026-02-04 12:47:48 +01:00

303 lines
13 KiB
PHP

<?php
namespace App\Livewire;
use App\Filament\Tables\Columns\MapperLeftColumn;
use App\Livewire\Mapper\LoadEvents;
use App\Livewire\Mapper\StoreMapped;
use App\Models\CompetitionGroup;
use App\Models\Country;
use App\Models\Event;
use App\Models\Mapper;
use Carbon\Carbon;
use CodeWithKyrian\FilamentDateRange\Forms\Components\DateRangePicker;
use Filament\Actions\Action;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Forms\Components\Select;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Concerns\InteractsWithSchemas;
use Filament\Schemas\Contracts\HasSchemas;
use Filament\Support\Enums\Alignment;
use Filament\Support\Enums\FontWeight;
use Filament\Support\Enums\Size;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Enums\FiltersLayout;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\Indicator;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Livewire\Attributes\Lazy;
use Livewire\Attributes\On;
use Livewire\Component;
#[Lazy]
class MapperLeft extends Component implements HasActions, HasSchemas, HasTable
{
use InteractsWithActions;
use InteractsWithSchemas;
use InteractsWithTable;
public $elements_swopped = false;
public $search_left = false;
protected $listeners = [
'refreshMapperLeft' => '$refresh',
];
public function removeTableFilter(string $filterName, ?string $field = null, bool $shouldTriggerUpdatedFiltersHook = true): void
{
$searchValues = preg_split('/\s+/', $this->search_left, -1, PREG_SPLIT_NO_EMPTY);
if (($key = array_search($field, $searchValues)) !== false) {
unset($searchValues[$key]);
}
if ($searchValues) {
$this->search_left = implode(' ', $searchValues);
} else {
$this->search_left = '';
}
$this->dispatch('refreshMapperLeft');
}
#[Lazy]
public function table(Table $table): Table
{
return $table
->query(LoadEvents::load($this->search_left, auth()->user()->sport_id))
->columns([
TextColumn::make('starttime')
->sortable()
->time('H:i')
->alignCenter()
->description(fn ($record): string => Carbon::parse($record->starttime)->format('d. M. y')),
IconColumn::make('country.icon')->icon(fn (string $state): string => $state)->label(''),
TextColumn::make('competition_group.name')->label('Group')
->wrap()
->tooltip('Click to filter by this group')
->description(fn ($record): string => $record->country->name)
->action(function ($record) {
if (! in_array($record->competition_group_id, $this->tableFilters['events']['competition_group_id'])) {
$this->tableFilters['events']['competition_group_id'][] = (string) $record->competition_group_id;
$this->dispatch('refreshMapperRight');
}
}),
TextColumn::make('home.name')
->label('Home')
->color('home')
->weight(FontWeight::Bold)
->alignRight(),
IconColumn::make('id')->icon('phosphor-minus')->label('')->color('black'),
TextColumn::make('away.name')
->color('away')
->label('Away')
->weight(FontWeight::Bold),
MapperLeftColumn::make('left')->label(''),
])->recordActions([
Action::make('search_home')
->iconButton()
->icon('phosphor-magnifying-glass')
->color('home')
->action(function ($record) {
$this->dispatch('search_right', $record->home->name);
}),
Action::make('search_away')
->icon('phosphor-magnifying-glass')
->iconButton()
->color('away')
->action(function ($record) {
$this->dispatch('search_right', $record->away->name);
}),
// Action::make('drag_left')
// ->icon('phosphor-arrows-out')
// ->iconButton()
// ->extraAttributes([
// 'x-data' => '{ moveoverleft: false }',
// 'x-on' => '{
// dragstart = {
// self: dragging = true;
// event.dataTransfer.effectAllowed = "move;
// event.dataTransfer.setData("text/plain", event.target.id)
// },
// dragover = {
// prevent: "moveoverleft = true"
// },
// dragleave = {
// prevent: "moveoverleft = false"
// }
// }',
// ])
// ->action(function ($record) {
// $this->dispatch('search_right', $record->away->name);
// }),
])
->defaultSort('starttime')
->searchable(['home.name', 'away.name'])
->defaultPaginationPageOption(10)
->filters([
Filter::make('events')
->schema([
Select::make('country_id')
->placeholder('Country')
->hiddenLabel()
// ->multiple()
->searchable()
->preload()
->options(function () {
$res = Event::query()->where('sport_id', auth()->user()->sport_id)->where('betradar_id', 0)->where('starttime', '>', Carbon::now())->groupBy('country_id')->pluck('country_id')->toArray();
return Country::query()->whereIn('id', $res)->orderBy('name')->pluck('name', 'id');
})
->live()
->reactive()
->afterStateUpdated(function (callable $set) {
$set('competition_group_id', null);
}),
Select::make('competition_group_id')
->placeholder('League')
->hiddenLabel()
->label('')
->multiple()
->searchable()
->options(function (Get $get) {
return CompetitionGroup::where('sport_id', auth()->user()->sport_id)
->whereHas('event', function ($query) {
$query->where('starttime', '>', now())
->where('betradar_id', 0);
})
->orderBy('name')
->pluck('name', 'id');
})
->live(),
DateRangePicker::make('starttime_period')
->columnSpan(2)
->displayFormat('d M Y')
->minDate(now())
->hiddenLabel()
->label('')
->live(),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['country_id'],
fn (Builder $query, $country_id) => $query->where('country_id', $country_id)
)->when(
$data['competition_group_id'],
function (Builder $query, $competition_group_id) {
$this->setPage(1);
return $query->whereIn('competition_group_id', $competition_group_id);
}
)->when(
$data['starttime_period'],
function (Builder $query, $starttime_period) {
// dd($starttime_period);
if (isset($starttime_period['start']) && isset($starttime_period['end'])) {
$from = Carbon::createFromDate($starttime_period['start']);
$end = Carbon::createFromDate($starttime_period['end']);
return $query
->whereDate('starttime', '>=', $from->format('Y-m-d 00:00:00'))
->whereDate('starttime', '<=', $end->format('Y-m-d 00:00:00'));
} elseif (isset($starttime_period['start'])) {
$from = Carbon::createFromDate($starttime_period['start']);
return $query
->whereDate('starttime', '>=', $from->format('Y-m-d 00:00:00'));
} elseif (isset($starttime_period['end'])) {
$end = Carbon::createFromDate($starttime_period['end']);
return $query
->whereDate('starttime', '<=', $end->format('Y-m-d 00:00:00'));
}
});
})->indicateUsing(function (): array {
$indicators = [];
if ($this->search_left) {
$searchValues = preg_split('/\s+/', $this->search_left, -1, PREG_SPLIT_NO_EMPTY);
foreach ($searchValues as $value) {
$indicators[] = Indicator::make($value)->removeField($value);
}
}
return $indicators;
})
->columns(4)->columnSpan(4),
], layout: FiltersLayout::AboveContent)
->filtersFormColumns(3)
->persistFiltersInSession()
->deferFilters(false)
->toolbarActions([
Action::make('refreshMapperLeft')
->hiddenLabel()
->color('gray')
->icon('phosphor-x')
->size(Size::Small)
->action(function (Component $livewire) {
$this->search_left = '';
$this->tableSearch = ''; // Clears the main search bar
$livewire->fill(['tableSearch' => '']);
$livewire->resetTableFiltersForm(); // Clears all filters
$this->dispatch('refreshMapperLeft');
}),
]);
}
public function toggle()
{
$this->elements_swopped = ! $this->elements_swopped;
}
#[On('search_left')]
public function searchLeft($str)
{
$this->search_left = $str;
$this->dispatch('refreshMapperLeft');
}
public function leftAction(): Action
{
return Action::make('left')
->modalHeading('Event Mapper')
->modalContent(function (array $arguments) {
$event = Event::where('id', $arguments['id'])->first();
$mapper = Mapper::where('event_id', $arguments['event_id'])->first();
return view('livewire.confirm-mapping', ['event' => $event, 'mapper' => $mapper]);
})
->action(function ($arguments) {
StoreMapped::store($arguments, $this->elements_swopped);
$this->search_right = '';
$this->search_left = '';
$this->dispatch('refreshMapperLeft');
$this->dispatch('refreshMapperRight');
})
->closeModalByEscaping(false)
->modalCloseButton(false)
->closeModalByClickingAway(false)
->modalAlignment(Alignment::Center)
->modalFooterActionsAlignment(Alignment::Center)
->modalWidth('2xl')
->modalSubmitActionLabel('Confirm')
->modalSubmitAction();
}
public function render()
{
return view('livewire.mapper-left');
}
}