<?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 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
 */

namespace BplanBase\Globals\Livewire\Location\Modals;


use BplanBase\Globals\Enums\LogLevel;
use BplanBase\Globals\Services\LogService;
use BplanBase\Globals\Repositories\LocationRepository;
use BplanBase\Globals\Repositories\LocationTypeRepository;
use BplanBase\Globals\Services\LocationService;
use Illuminate\Database\Eloquent\Collection;
use Laravel\Jetstream\InteractsWithBanner;
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     1.0.0 / 2025-04-13
 * @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 array|null
     */
    #[Validate('required')]
    public ?array $labelsJson;


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


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


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


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


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


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


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


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


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


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


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


    /**
     *
     * @var LogService
     */
    protected LogService $logService;


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


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


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


    /**
     *
     * @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 boot(LogService $logService, LocationService $locationService)
    {
        $this->logService = $logService;
        $this->locationService = $locationService;

        $this->locations = LocationRepository::getAll();
        $this->locationTypes = LocationTypeRepository::getAll();

    } // boot()


    /**
     *
     * @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 createLocation()
    {
        $this->validate();

        try {
            $this->locationService->create([
                'active' => $this->active,
                'city' => $this->city,
                'email' => $this->email,
                'house_number' => $this->housenumber,
                'labels' => $this->labelsJson,
                'location_type_id' => $this->location_type_id != 0 ? $this->location_type_id : null ,
                'master_id' => $this->master_location_id != 0 ? $this->master_location_id : null ,
                'phone_number' => $this->phonenumber,
                'street' => $this->street,
                'zip_code' => $this->postcode,
            ]);

            $this->logService->log(LogLevel::Info, "Location {$this->labelsJson['en']} added by " . auth()->user()->name . ' (' . auth()->user()->email . ')');

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

        } catch (\Throwable $th) {
            $this->logService->log(LogLevel::Error, 'Locations/Modals/CreateLocation error createLoaction: ' . $th->getMessage() . ', file:' . $th->getFile() . ', line:' .  $th->getLine());
            $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.0.0 / 2025-04-13
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function resetModal()
    {
        $this->active = true;
        $this->city = null;
        $this->email = null;
        $this->housenumber = null;
        $this->labelsJson = null;
        $this->phonenumber = null;
        $this->postcode = null;
        $this->street = null;

    } // resetModal()


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


} // class Create extends Component {}
