<?php

namespace App\Http\Livewire\Pages\Company;

use Livewire\Component;
use App\Models\Company;

class CompanyEdit extends Component
{
    public $editCompanyId;
    public $companyTypes;
    public $company;
  

    protected $rules = [
        'company.company_type_id' => 'required|exists:company_types,id',
        'company.name' => 'required|string|max:255',
        'company.tax' => 'required|integer|max:9999999999999',
        'company.branch' => 'required|integer|max:999999',
        'company.name_en' => 'required|string|max:255',
        'company.address' => 'required|string|max:255',
        'company.district' => 'required|string|max:255',
        'company.sub_province' => 'required|string|max:255',
        'company.province' => 'required|string|max:255',
        'company.postcode' => 'required|string|max:10',
        'company.phone' => 'required|string|max:15',
        'company.fax' => 'nullable|string|max:15',
        'company.tax_incentive' => 'nullable|string|max:255',
    ];

    protected $messages = [
        'company.company_type_id.required' => 'The company type field is required.',
        'company.company_type_id.exists' => 'The selected company type is invalid.',
        'company.name.required' => 'The name field is required.',
        'company.name.string' => 'The name must be a string.',
        'company.name.max' => 'The name may not be greater than 255 characters.',
        'company.tax.required' => 'The tax field is required.',
        'company.tax.integer' => 'The tax must be an integer.',
        'company.tax.max' => 'The tax field must not exceed 13 characters.',
        'company.branch.required' => 'The branch field is required.',
        'company.branch.integer' => 'The branch must be an integer.',
        'company.branch.max' => 'The branch field must not exceed 6 characters.',
        'company.name_en.required' => 'The English name field is required.',
        'company.name_en.string' => 'The English name must be a string.',
        'company.name_en.max' => 'The English name may not be greater than 255 characters.',
        'company.address.required' => 'The address field is required.',
        'company.address.string' => 'The address must be a string.',
        'company.address.max' => 'The address may not be greater than 255 characters.',
        'company.district.required' => 'The district field is required.',
        'company.district.string' => 'The district must be a string.',
        'company.district.max' => 'The district may not be greater than 255 characters.',
        'company.sub_province.required' => 'The sub-province field is required.',
        'company.sub_province.string' => 'The sub-province must be a string.',
        'company.sub_province.max' => 'The sub-province may not be greater than 255 characters.',
        'company.province.required' => 'The province field is required.',
        'company.province.string' => 'The province must be a string.',
        'company.province.max' => 'The province may not be greater than 255 characters.',
        'company.postcode.required' => 'The postcode field is required.',
        'company.postcode.string' => 'The postcode must be a string.',
        'company.postcode.max' => 'The postcode may not be greater than 10 characters.',
        'company.phone.required' => 'The phone field is required.',
        'company.phone.string' => 'The phone must be a string.',
        'company.phone.max' => 'The phone may not be greater than 15 characters.',
        'company.fax.string' => 'The fax must be a string.',
        'company.fax.max' => 'The fax may not be greater than 15 characters.',
        'company.tax_incentive.string' => 'The tax incentive must be a string.',
        'company.tax_incentive.max' => 'The tax incentive may not be greater than 255 characters.',
    ];
    public function mount($editCompanyId, $companyTypes)
    {
        $this->editCompanyId = $editCompanyId;
        $this->companyTypes = $companyTypes;
        $this->company = Company::find($editCompanyId);
    }
    public function render()
    {
        return view('livewire.pages.company.company-edit');
    }

    public function submitEditForm()
    {
        $this->validate();

        $this->company->save();

        $this->emit('showCompanyList', 'Company successfully updated.');
    }
    public function goBack()
    {
        $this->emit('showCompanyList', '');
    }
}
