<?php
/**
 * Livewire Component
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @version     STUB: 4.2.0
 * @generated   2026-04-06 19:35:49
 * @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\User;


use BplanBase\Globals\DataGrid\DataGrid;
use BplanBase\Globals\Enums\UserAccessLevel;
use BplanBase\Globals\Models\User;
use BplanBase\Globals\Repositories\IndexCollectionRepository;
use BplanBase\Globals\Traits\WithDataGrid;
use Illuminate\Pagination\LengthAwarePaginator;
use Livewire\Component;
use Livewire\WithPagination;


/**
 * Livewire Component
 *
 * @version     4.4.0 / 2026-05-26
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class Index extends Component
{


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


    use WithDataGrid;
    use WithPagination;


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


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


    /**
     * @var     array
     */
    protected array $_formatCasts = [
        'active'            => 'boolean',
        'created_at'        => 'datetime',
        'deleted_at'        => 'datetime',
        'email_verified_at' => 'datetime',
        'internal'          => 'boolean',
        'restricted'        => 'boolean',
        'updated_at'        => 'datetime',
    ];


    /**
     * Specifies the columns to use and their order
     *
     * @var     array
     */
    protected array $_gridColumns = [
        'id',
        'name',
        'email',
    ];


    /**
     * @var     string
     */
    protected string $_gridLocaleFile = 'globals::users';


    /**
     * @var     string
     */
    protected string $_modelClass = User::class;


    /**
     * @var     array
     */
    protected array $_searchFields = [
        'email',
        'name',
    ];


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


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


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


    /**
     *
     * @return      LengthAwarePaginator<User>
     *
     * @version     1.2.0 / 2026-05-26
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    private function _getData(): LengthAwarePaginator
    {
        return IndexCollectionRepository::getUsers(
            ExtendQuery: $this->_getQuery(),
            orderBy:     $this->_getOrderBy(),
            paginate:    ['pageName' => 'users-page'],
            withInternal: \Registry::get('core.user')->hasMinAccessLevel(UserAccessLevel::Maintainer)
        );
    } // _getData()


    /**
     *
     * @return      DataGrid
     *
     * @version     1.0.0 / 2026-04-06
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    private function _getGrid(): DataGrid
    {
        $gridData = $this->_getData();

        return (new DataGrid('user'))
            ->setActions([
                'edit-user',
            ])
            ->setActionSvgIcons([
                'edit-user' => svg_icon('globals.svg-icons.action-edit'),
            ])
            ->setActiveFilters($this->_hasActiveFilters())
            ->setColumnCallback(function ($Item, $property, $value) {
                switch ($property) {
                    case 'email':
                        // ...
                        break;

                    case 'id':
                        // ...
                        break;

                    case 'name':
                        // ...
                        break;
                }
                return $value;
            })
            ->setColumns($this->_gridColumns)
            ->setData($gridData)
            ->setLocaleFile($this->_gridLocaleFile)
            ->setSorting($this->sortColumns, $this->sortColumn, $this->sortDirection)
            ->useCaptions(true)
            ->useDefaultCreateButton(true)
            ->useDefaultSearch(true);

    } // _getGrid()


    /**
     *
     * @return 	    Builder|null
     *
     * @version     1.0.0 / 2026-04-06
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    protected function _getQuery()
    {
        $ExtendQuery = null;

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

        if (!empty($this->search)) {
            /*
            **  Query mit Filter-Parametern zusammenstellen. */
            $ExtendQuery = User::where(function ($Q) {
                $Q->where('email', 'like', '%'.$this->search.'%')
                    ->orWhere('name', 'like', '%'.$this->search.'%');
            });
        }
        return $ExtendQuery;

    } // _getQuery()


    /**
     *
     * @version     1.2.0 / 2026-04-06
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    #[On('user-created'), On('user-edited'), On('user-deleted')]
    public function render()
    {
        return view('globals::livewire.user.index', [
            'grid' => $this->_getGrid()
        ]);
    } // render()


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


} // class Index extends Component {}
