<?php

namespace App\Http\Livewire\Pages\Company;

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

class CompanyCreate extends Component
{
    public $companyTypes ;
    public $companyType = '1';
    public $name;
    public $tax;
    public $branch;
    public $name_en;
    public $address;
    public $district;
    public $sub_province;
    public $province;
    public $postcode;
    public $phone;
    public $fax;
    public $tax_incentive;
    public $action ;
 
    protected $rules = [
        'companyType' => 'required|exists:company_types,id',
        'name' => 'required|string|max:255',
        'tax' => 'required|integer|max:9999999999999', 
        'branch' => 'required|integer|max:999999', 
        'name_en' => 'required|string|max:255',
        'address' => 'required|string|max:255',
        'district' => 'required|string|max:255',
        'sub_province' => 'required|string|max:255',
        'province' => 'required|string|max:255',
        'postcode' => 'required|string|max:10',
        'phone' => 'required|string|max:15',
        'fax' => 'nullable|string|max:15',
        'tax_incentive' => 'nullable|string|max:255',
    ];

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

    public function submitForm()
    {
        $validatedData = $this->validate();
   
        // Create a new company instance
        $newCompany = new Company();
        $newCompany->company_type_id = $this->companyType;
        $newCompany->name = $this->name;
        $newCompany->tax = $this->tax;
        $newCompany->branch = $this->branch;
        $newCompany->name_en = $this->name_en;
        $newCompany->address = $this->address;
        $newCompany->district = $this->district;
        $newCompany->sub_province = $this->sub_province;
        $newCompany->province = $this->province;
        $newCompany->postcode = $this->postcode;
        $newCompany->phone = $this->phone;
        $newCompany->fax = $this->fax;
        $newCompany->tax_incentive = $this->tax_incentive;

        // Save the new company to the database
        $newCompany->save();

        // Reset form fields
        $this->reset([
            'companyType', 'name', 'tax', 'branch', 'name_en', 'address', 'district', 
            'sub_province', 'province', 'postcode', 'phone', 'fax', 'tax_incentive'
        ]);
        $this->emit('showCompanyList' , 'Company successfully created.');
    }
    public function goBack()
    {
        $this->emit('showCompanyList', '');
    }

}
