<?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\Modals;


use BplanBase\Globals\Models\LocationType;
use BplanBase\Globals\Repositories\LocationRepository;
use BplanBase\Globals\Repositories\LocationTypeRepository;
use BplanBase\Globals\Services\LocationService;
use BplanBase\Globals\Traits\InteractsWithBanner;
use Illuminate\Database\Eloquent\Collection;
use Livewire\Attributes\On;
use Livewire\Attributes\Validate;
use Livewire\Component;


/**
 * Livewire Component
 *
 * @todo        Beim erneuten Öffnen des Modal-Fensters, zeigt der Zähler für die Sub-Locations
 *              auf dem Tab den Wert der zuletzt geöffneten Location. Erst beim Klick auf den Tab
 *              wird die Zahl aktualisiert.
 *
 * @version     2.0.0 / 2026-05-03
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class Create extends Component
{


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


    use InteractsWithBanner;


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


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


    /**
     * @var     boolean
     */
    // #[Validate('required')]
    public bool $active = true;


    /**
     * @var     string|null
     */
    // #[Validate('required')]
    public ?string $addressAddition = null;


    /**
     * @var     string|null
     */
    // #[Validate('required')]
    public ?string $city = null;


    /**
     * @var     array
     */
    public array $dependencies = [];


    /**
     * @var     string|null
     */
    // #[Validate('required')]
    public ?string $email = null;


    /**
     * @var     string|null
     */
    // #[Validate('required')]
    public ?string $houseNumber = null;


    /**
     * @var     bool
     */
    // #[Validate('required')]
    public bool $internal = false;


    /**
     * @var     boolean
     */
    // #[Validate('required')]
    public bool $isBookable = false;


    /**
     * @var     array|null
     */
    #[Validate('required')]
    public ?array $labelsJson;


    /**
     * @var     string|null
     */
    // #[Validate('required')]
    public string $locale;


    /**
     * @var     string|null
     */
    // #[Validate('nullable|numeric|exists:location_types,id')]
    public ?string $locationTypeId = null;


    /**
     * @var     Collection|null
     */
    public ?Collection $Locations;


    /**
     * @var     LocationService
     */
    protected LocationService $LocationService;


    /**
     * @var     LocationType|null
     */
    public ?LocationType $LocationType;


    /**
     * @var     Collection|null
     */
    public ?Collection $LocationTypes;


    /**
     * @var     string|null
     */
    #[Validate('nullable|numeric|exists:locations,id')]
    public ?string $master_location_id = null;


    /**
     * @var     string|null
     */
    // #[Validate('required')]
    public ?string $phoneNumber = null;


    /**
     * @var     string|null
     */
    // #[Validate('required')]
    public ?string $street = null;


    /**
     * @var     string|null
     */
    // #[Validate('required')]
    public ?string $subType = null;


    /**
     * @var     array
     */
    public array $subTypes = [];


    /**
     * @var     string|null
     */
    // #[Validate('required')]
    public ?string $zipCode = null;


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


    /**
     * @var     string
     */
    private static string $_logPrefix = 'Location/Modals/Create';


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


    /**
     *
     * @version     2.0.0 / 2026-05-03
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function boot(LocationService $LocationService)
    {
        $this->LocationService = $LocationService;

        $this->locale        = app()->getLocale();
        $this->Locations     = LocationRepository::getAll();
        $this->LocationTypes = LocationTypeRepository::getAll();

        $this->dependencies = config('globals.location-sub-type-dependencies.*');

    } // boot()


    /**
     *
     * @version     1.1.0 / 2026-05-03
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function createLocation()
    {
        $this->validate();

        try {
            $this->LocationService->create([
                'active'           => $this->active,
                'address_addition' => $this->addressAddition,
                'city'             => $this->city,
                'email'            => $this->email,
                'house_number'     => $this->houseNumber,
                'internal'         => $this->internal,
                'is_bookable'      => $this->isBookable,
                'labels'           => $this->labelsJson,
                'location_type_id' => $this->locationTypeId != 0 ? $this->locationTypeId : null ,
                'master_id'        => $this->master_location_id != 0 ? $this->master_location_id : null ,
                'phone_number'     => $this->phoneNumber,
                'street'           => $this->street,
                'sub_type'         => $this->subType,
                'zip_code'         => $this->zipCode,
            ]);
            $lc   = app()->getLocale();
            $User = auth()->user();

            \AppLog::info('Location ['.$this->labelsJson[$lc].'] edited by ['.$User->name.' ('.$User->email.')]', self::$_logPrefix);

            $this->dispatch('init-tippyjs');
            $this->dispatch('location-created');

        } catch (\Throwable $th) {
            \AppLog::error($th, self::$_logPrefix);

            $this->dangerBanner('Error when creating location. Check the logs for more information.');
        }
    } // createLocation()


    /**
     *
     * @version     1.0.0 / 2025-04-13
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    #[On('multi-language-json-created')]
    public function onMultiLanguageJsonCreated(?array $json, string $target)
    {
        if ($target == 'create') {
            $this->labelsJson = $json;
        }
    } // onMultiLanguageJsonCreated()


    /**
     *
     * @version     1.0.0 / 2025-04-13
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function render()
    {
        return view('globals::livewire.location.modals.create');

    } // render()


    /**
     *
     * @version     1.1.0 / 2026-05-03
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function resetModal()
    {
        $this->active          = true;
        $this->addressAddition = null;
        $this->city            = null;
        $this->email           = null;
        $this->houseNumber     = null;
        $this->internal        = false;
        $this->isBookable      = false;
        $this->labelsJson      = null;
        $this->phoneNumber     = null;
        $this->street          = null;
        $this->subType         = null;
        $this->zipCode         = null;

        $this->dependencies = config('globals.location-sub-type-dependencies.*');

    } // resetModal()


    /**
     *
     * @version     1.0.0 / 2026-05-03
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function updated($name, $value)
    {
        if ($name === 'locationTypeId') {
            if ($value !== '') {
                $this->LocationType = $this->LocationTypes->where('id', '=', $value)->first();

                $this->subType = null;
                $this->subTypes = $this->LocationType->sub_types;

                sort($this->subTypes);
            }
            $this->dependencies = array_merge(
                config('globals.location-sub-type-dependencies.*'),
                config('globals.location-sub-type-dependencies.'.$this->LocationType->identifier)
            );
        } elseif ($name === 'subType') {
            //
        }
    } // updated()


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


} // class Create extends Component {}
