<?php

declare(strict_types=1);

namespace Seka\Terminy\Http\Controllers\Backend\Adresar\Livewire\Components;

use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Contracts\View\View;
use Seka\Terminy\Models\Udalost\Udalost;
use Illuminate\Database\Eloquent\Builder;
use Seka\Terminy\Models\MiestoKonania\MiestoKonania;

/**
 * Class Zoznam
 *
 * @package Seka\Terminy\Http\Controllers\Backend\Adresar\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 $miesta;

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

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

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

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

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

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

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