<?php
/**
 * Livewire Component
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @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\Core\LocationType;


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


/**
 * Livewire Component
 *
 * @version     1.0.0 / 2026-05-25
 * @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',
        'is_internal' => 'boolean',
        'updated_at'  => 'datetime',
    ];


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


    /**
     * @var     string
     */
    protected string $_gridLocaleFile = 'globals::location-types';


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


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


    /**
     * @var     array
     */
    protected $listeners = [
        'location-type-saved' => 'render',
    ];


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


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


    /**
     *
     * @return      LengthAwarePaginator<LocationType>
     *
     * @version     1.0.0 / 2026-05-25
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    private function _getData(): LengthAwarePaginator
    {
        return IndexCollectionRepository::getLocationTypes(
            ExtendQuery: $this->_getQuery(),
            orderBy: $this->_getOrderBy()
        );

    } // _getData()


    /**
     *
     * @return      DataGrid
     *
     * @version     1.0.0 / 2026-05-25
     * @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('location-type'))
            ->setActions([
                'edit-location-type',
            ])
            ->setActionSvgIcons([
                'edit-location-type' => svg_icon('globals.svg-icons.action-edit'),
            ])
            ->setActiveFilters($this->_hasActiveFilters())
            ->setColumnCallback(function ($Item, $property, $value) {
                switch ($property) {
                    case 'active':
                        if ($value === true) {
                            $value = svg_icon('globals.svg-icons.status-active', 'data-tippy-content="'.__('globals::global.status.active').'"');
                        } else {
                            $value = svg_icon('globals.svg-icons.status-inactive', 'data-tippy-content="'.__('globals::global.status.inactive').'"');
                        }
                        break;

                    case 'is_internal':
                        if ($value === true) {
                            $value = svg_icon('globals.svg-icons.status-active', 'data-tippy-content="'.__('globals::global.status.active').'"');
                        } else {
                            $value = svg_icon('globals.svg-icons.status-inactive', 'data-tippy-content="'.__('globals::global.status.inactive').'"');
                        }
                        break;
                }
                return $value;
            })
            ->setColumns($this->_gridColumns)
            ->setData($gridData)
            ->setLocaleFile($this->_gridLocaleFile)
            ->setSorting($this->sortColumns, $this->sortColumn, $this->sortDirection)
            ->useCaptions(true)
            ->useDefaultCreateButton(false)
            ->useDefaultSearch(true);

    } // _getGrid()


    /**
     *
     * @version     1.0.0 / 2026-05-25
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function render()
    {
        return view('globals::livewire.core.location-type.index', [
            'grid' => $this->_getGrid(),
        ]);

    } // render()


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


} // class Index extends Component {}
