<?php

namespace App\Http\Livewire\Components\Informacia\Components;

use App\Repositories\Eloquent\Criteria\OrderAscCriteria;
use App\Repositories\Eloquent\Criteria\OrderDescCriteria;
use App\Repositories\Eloquent\Criteria\WithTrashedCriteria;
use App\Repositories\InformaciaRepositoryContract;
use Livewire\Component;
use Livewire\WithPagination;
use Prettus\Repository\Exceptions\RepositoryException;

/**
 * Class Zoznam
 *
 * @package App\Http\Livewire\Components\Informacia\Components
 */
final class Zoznam extends Component
{
    use WithPagination;

    /**
     *
     */
    private const PER_PAGE = 15;

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

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

    /**
     * @var InformaciaRepositoryContract
     */
    protected InformaciaRepositoryContract $informaciaRepository;

    /**
     * @param InformaciaRepositoryContract $informaciaRepository
     * @return void
     */
    public function boot(InformaciaRepositoryContract $informaciaRepository): void
    {
        $this->informaciaRepository = $informaciaRepository;
    }

    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws RepositoryException
     */
    public function render()
    {
        $informacie = $this->informaciaRepository
            ->pushCriteria(WithTrashedCriteria::class)
            ->pushCriteria(($this->sort == 1) ? OrderAscCriteria::class : OrderDescCriteria::class)
            ->paginate(self::PER_PAGE);

        return view('pages.sprava.informacie.livewire.components.zoznam')->withInformacie($informacie);
    }
}
