<?php
/**
 * Livewire Component
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @version     STUB: 4.1.0
 * @generated   2026-04-06 02:17:45
 * @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\Country;


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


/**
 * Livewire Component
 *
 * @version     3.2.0 / 2026-04-10
 * @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 = [];


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


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


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


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


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


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


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


    /**
     *
     * @return      LengthAwarePaginator<Country>
     *
     * @version     1.1.0 / 2026-04-09
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    private function _getData(): LengthAwarePaginator
    {
        return IndexCollectionRepository::getCountries(
            ExtendQuery: $this->_getQuery(),
            orderBy: $this->_getOrderBy(),
            withInactive: true
        );
    } // _getData()


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

        $locale = app()->getLocale();

        return (new DataGrid('country'))
            ->setActiveFilters($this->_hasActiveFilters())
            ->setColumnCallback(function ($Item, $property, $value) use ($locale) {
                switch ($property) {
                    case 'short_names':
                        $value = $Item->short_names->{$locale};
                        break;
                }
                return $value;
            })
            ->setColumns($this->_gridColumns)
            ->setToggleCallback('onToggle', 'active')
            ->setData($gridData)
            ->setLocaleFile($this->_gridLocaleFile)
            ->setSorting($this->sortColumns, $this->sortColumn, $this->sortDirection)
            ->useCaptions(true)
            ->useDefaultSearch(true)
            ->useDefaultCreateButton(false);

    } // _getGrid()


    /**
     *
     * @param       string|integer $id
     *
     * @throws      \Throwable
     *
     * @return      void
     *
     * @version     1.0.0 / 2026-04-06
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function onToggle(string|int $id): void
    {
        try {
            $Country = Country::findOrFail($id);

            $Country->active = !$Country->active;

            $Country->save();

        } catch (\Throwable $th) {
            throw $th;
        }
    } // onToggle()


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


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


} // class Index extends Component {}
