<?php
/**
 * Livewire Component
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @generated   2025-04-05 02:54: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\TenantRecord\Modals;


use BplanBase\Globals\Http\Requests\UpdateTenantRecordRequest;
use BplanBase\Globals\Models\TenantRecord;
use BplanBase\Globals\Repositories\CountryRepository;
use BplanBase\Globals\Services\TenantRecordService;
use BplanBase\Globals\Traits\InteractsWithBanner;
use Livewire\Attributes\On;
use Livewire\Component;


/**
 * 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 Edit extends Component
{


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


    use InteractsWithBanner;


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


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


    /**
     * @var     string|null
     */
    public ?string $city = null;


    /**
     * @var     string|null
     */
    public ?string $company = null;


    /**
     * @var     array<int, string>
     */
    public array $countries = [];


    /**
     * @var     int|null
     */
    public ?int $country_id = null;


    /**
     * @var     string|null
     */
    public ?string $description = null;


    /**
     * @var     string|null
     */
    public ?string $email = null;


    /**
     * @var     string|null
     */
    public ?string $home_page = null;


    /**
     * @var     string|null
     */
    public ?string $house_number = null;


    /**
     * @var     string|null
     */
    public ?string $mobile_phone_number = null;


    /**
     * @var     string|null
     */
    public ?string $phone_number = null;


    /**
     * @var     string|null
     */
    public ?string $short_name = null;


    /**
     * @var     string|null
     */
    public ?string $street = null;


    /**
     * @var     TenantRecord|null
     */
    public ?TenantRecord $_TenantRecord = null;


    /**
     * @var     TenantRecordService
     */
    protected TenantRecordService $_Service;


    /**
     * @var     string|null
     */
    public ?string $zip_code = null;


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


    /**
     * @var     string
     */
    private static string $_logPrefix = 'TenantRecord/Modals/Edit';


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


    /**
     *
     * @param       TenantRecordService $Service
     *
     * @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 boot(TenantRecordService $Service): void
    {
        $this->_Service = $Service;

    } // boot()


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

        try {
            $this->_Service->update($this->_TenantRecord, [
                'city'                 => $this->city,
                'company'              => $this->company,
                'country_id'           => $this->country_id,
                'description'          => $this->description,
                'email'                => $this->email,
                'home_page'            => $this->home_page,
                'house_number'         => $this->house_number,
                'mobile_phone_number'  => $this->mobile_phone_number,
                'phone_number'         => $this->phone_number,
                'short_name'           => $this->short_name,
                'street'               => $this->street,
                'zip_code'             => $this->zip_code,
            ]);

            $User = auth()->user();

            \AppLog::info('TenantRecord ['.$this->_TenantRecord->id.'] updated by ['.$User->name.' ('.$User->email.')]', self::$_logPrefix);

            $this->dispatch('init-tippyjs');
            $this->dispatch('tenant-record-edited');

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

            $this->dangerBanner(__('globals::tenant-records.error.update-error'));
        }

    } // editTenantRecord()


    /**
     *
     * @param       TenantRecord $tenantRecord
     *
     * @version     1.0.0 / 2026-05-25
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    #[On('show-edit-tenant-record-modal')]
    public function initTenantRecord(TenantRecord $tenantRecord): void
    {
        $this->_TenantRecord = $tenantRecord;

        $this->city                 = $tenantRecord->city;
        $this->company              = $tenantRecord->company;
        $this->country_id           = $tenantRecord->country_id;
        $this->description          = $tenantRecord->description;
        $this->email                = $tenantRecord->email;
        $this->home_page            = $tenantRecord->home_page;
        $this->house_number         = $tenantRecord->house_number;
        $this->mobile_phone_number  = $tenantRecord->mobile_phone_number_country_code.$tenantRecord->mobile_phone_number;
        $this->phone_number         = $tenantRecord->phone_number_country_code.$tenantRecord->phone_number;
        $this->short_name           = $tenantRecord->short_name;
        $this->street               = $tenantRecord->street;
        $this->zip_code             = $tenantRecord->zip_code;

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

    } // initTenantRecord()


    /**
     *
     * @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 mount(): void
    {
        $this->countries = CountryRepository::getLocalizedList();

    } // mount()


    /**
     *
     * @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.tenant-record.modals.edit');

    } // render()


    /**
     *
     * @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 resetModal(): void
    {
        $this->city                = null;
        $this->company             = null;
        $this->country_id          = null;
        $this->description         = null;
        $this->email               = null;
        $this->home_page           = null;
        $this->house_number        = null;
        $this->mobile_phone_number = null;
        $this->phone_number        = null;
        $this->short_name          = null;
        $this->street              = null;
        $this->zip_code            = null;
        $this->_TenantRecord       = null;

        $this->resetValidation();

    } // resetModal()


    /**
     *
     * @return      array
     *
     * @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 rules(): array
    {
        if ($this->_TenantRecord === null) {
            return [];
        }

        $rules = UpdateTenantRecordRequest::getRules($this->_TenantRecord->id);

        unset($rules['id']);
        unset($rules['mobile_phone_number_country_code']);
        unset($rules['phone_number_country_code']);

        return $rules;

    } // rules()


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


} // class Edit extends Component {}
