<?php

namespace App\Livewire;

use Livewire\Component;
use Livewire\Attributes\Validate;

class Company extends Component
{

    #[Validate('required')] 
    public $name = '';
    public $business_name = '';
    public $logo = '';
    public $phone = '';
    public $email = '';
    public $pec = '';
    public $costitution_date = null;
    public $address = '';
    public $zip = '';
    public $city_id = null;
    public $country_id = null;
    public $operational_headquarters = '';
    public $fiscal_code = '';
    public $vat = ''; 
    public $sdi_code = '';
    public $ateco_code = '';
    public $enabled = true;

    public $current_company = null;

    public $is_edit = false;

    public function render()
    {

        // Get Company
        $this->current_company = \App\Models\Company::first();

        if ($this->current_company != null)
        {

            $this->name = $this->current_company->name;
            $this->business_name = $this->current_company->business_name;
            $this->logo = $this->current_company->logo;
            $this->phone = $this->current_company->phone;
            $this->email = $this->current_company->email;
            $this->pec = $this->current_company->pec;
            $this->costitution_date = $this->current_company->pec;
            $this->address = $this->current_company->address;
            $this->zip = $this->current_company->zip;
            $this->city_id = $this->current_company->city_id;
            $this->country_id = $this->current_company->country_id;
            $this->operational_headquarters = $this->current_company->operational_headquarters;
            $this->fiscal_code = $this->current_company->fiscal_code;
            $this->vat = $this->current_company-> vat;
            $this->sdi_code = $this->current_company->sdi_code;
            $this->ateco_code = $this->current_company->ateco_code;
            $this->enabled = $this->current_company->enabled;

        }

        return view('livewire.company');
    }

    public function update()
    {

        $this->validate();

        try {
            $this->current_company->update([
                'name' => $this->name,
                'business_name' => $this->business_name,
                'logo' => $this->logo,
                'phone' => $this->phone,
                'email' => $this->email,
                'pec' => $this->pec,
                'costitution_date' => $this->pec,
                'address' => $this->address,
                'zip' => $this->zip,
                'city_id' => $this->city_id,
                'country_id' => $this->country_id,
                'operational_headquarters' => $this->operational_headquarters,
                'fiscal_code' => $this->fiscal_code,
                'vat' => $this-> vat,
                'sdi_code' => $this->sdi_code,
                'ateco_code' => $this->ateco_code,
                'enabled' => $this->enabled,
            ]);
            session()->flash('success','Dato aggiornato');
            //$this->resetFields();
            $this->is_edit = false;
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }

    }

}
