<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Terminy\Http\Controllers\Backend\MoznostiUdalosti\Livewire\Components;

use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Seka\Portal\Modul\Terminy\Entities\MoznostiUdalosti\MoznostUdalosti;

/**
 * Class Zoznam
 *
 * @package Seka\Portal\Modul\Terminy\Http\Controllers\Backend\MoznostiUdalosti\Livewire\Components
 */
final class Zoznam extends Component
{
    use WithPagination;

    /**
     * @var int
     */
    protected const PER_PAGE = 15;

    /**
     * @var int
     */
    public int $sort = 0;

    /**
     * @var string
     */
    protected string $paginationTheme = 'bootstrap';

    /**
     * @var string|null
     */
    public ?string $state = null;

    /**
     * @var string|null
     */
    public ?string $type = null;

    /**
     * @var \Illuminate\Database\Eloquent\Collection|\Illuminate\Contracts\Pagination\Paginator
     */
    protected $moznostiUdalosti;

    /**
     * @var string
     */
    protected string $createPath = 'modul.terminy.backend.moznostiUdalosti.moznost.create';

    /**
     * @var string
     */
    protected string $editPath = 'modul.terminy.backend.moznostiUdalosti.moznost.edit';

    /**
     *
     */
    public function mount(): void
    {
        $this->moznostiUdalosti = MoznostUdalosti::withTrashed()->get();
    }

    /**
     * @return \Illuminate\Contracts\View\View
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function render(): View
    {
        $this->moznostiUdalosti = $this->getQuery()->withTrashed()->paginate(self::PER_PAGE);

        return view('Terminy::pages.moznostiUdalosti.backend.livewire.components.zoznam')
            ->withMoznostiUdalosti($this->moznostiUdalosti)
            ->withCreatePath($this->createPath)
            ->withEditPath($this->editPath);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected function getQuery(): Builder
    {
        $query = MoznostUdalosti::query()->with('vytvoril');

        return $query->orderBy('updated_at', $this->sort ? 'asc' : 'desc');
    }
}
