<?php
/**
 * Livewire Component
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @generated   2025-04-13 18:30:42
 * @package     bplan-base/globals
 * @subpackage  LivewireComponents
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 * @copyright   Copyright (C) 2025, 2026 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
 */

namespace BplanBase\Globals\Livewire\Location;


use BplanBase\Globals\Enums\UserAccessLevel;
use BplanBase\Globals\Models\Location;
use BplanBase\Globals\Repositories\IndexCollectionRepository;
use BplanBase\Globals\Repositories\LocationRepository;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;


/**
 * Livewire Component
 *
 * @version     3.0.0 / 2025-03-16
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class Index extends Component
{


/* +++ TRAITS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */


    use WithPagination;


/* +++ CLASS CONSTANTS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT MEMBERS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     *
     * @var array
     */
    #[Url(keep: false)]
    public $filterLocations = [];


    /**
     *
     * @var array
     */
    protected $listeners = [
        'location-created' => 'render',
        'location-edited'  => 'render',
        'location-deleted' => 'render',
    ];


    /**
     *
     * @var string
     */
    public string $search = '';


/* +++ CLASS MEMBERS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT METHODS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     *
     * @version     1.0.0 / 2026-03-12
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    private function _getLocations()
    {
        $ExtendQuery = null;

        $this->search = trim($this->search);

        if (!empty($this->search)) {
            /*
            **  Query mit Filter-Parametern zusammenstellen. */
            $ExtendQuery = Location::whereRaw('LOWER(labels) LIKE ?', ['%'.strtolower($this->search).'%']);
        }
        /*
            @todo   Das muss noch angepasst werden.
                    hasMinAccessLevel() prüft aktuell den generellen AccessLevel des Users und nicht den
                    Level im Tenant-Kontext.
        */
        return IndexCollectionRepository::getLocations(
            ExtendQuery: $ExtendQuery,
            paginate: ['pageName' => 'locations-page'],
            withInternal: \Registry::get('core.user')->hasMinAccessLevel(UserAccessLevel::Maintainer)
        );

    } // _getLocations()


    /**
     *
     * @return      void
     *
     * @version     1.0.0 / 2026-03-16
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function mount()
    {
        \Registry::put('env.page-title', __('globals::locations.page.title'));

    } // mount()


    /**
     *
     * @version     1.1.0 / 2026-03-12
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function render()
    {
        return view('globals::livewire.location.index', [
            'locations' => $this->_getLocations(),
        ]);
    } // render()


/* +++ CLASS METHODS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


} // class Index extends Component {}
