<?php

namespace App\Http\Livewire\Pages\Exchangerate;

use Livewire\Component;
use App\Models\CenterConfExchangerate;
use App\Models\CenterConfExchangerateExport;
use Carbon\Carbon;

class ExchangerateCreate extends Component
{
    public $currency, $exdate, $finishdate, $rate, $baht, $exbaht, $pageCode = [];
    public $searchByPage = [];

    public function mount()
    {
        // Initial setup if necessary
    }

    public function addExchangerate()
    {
        $this->validate([
            'currency' => 'required',
            'exdate' => 'required',
            'finishdate' => 'nullable',
            'rate' => 'nullable|numeric',
            'baht' => 'nullable|numeric',
            'exbaht' => 'nullable|numeric',
        ]);

        //$exdate = Carbon::createFromFormat('m/d/Y', $this->exdate)->format('Y-m-d');
        //$finishdate = $this->finishdate ? Carbon::createFromFormat('m/d/Y', $this->finishdate)->format('Y-m-d') : null;

        CenterConfExchangerate::create([
            'currency' => $this->currency,
            'exdate' => $this->exdate,
            'finishdate' => $this->finishdate,
            'rate' => $this->rate,
            'baht' => $this->baht,
        ]);

        CenterConfExchangerateExport::create([
            'currency' => $this->currency,
            'exdate' => $this->exdate,
            'finishdate' => $this->finishdate,
            'rate' => $this->rate,
            'baht' => $this->exbaht,
        ]);

        $this->reset(['currency', 'exdate', 'finishdate', 'rate', 'baht', 'exbaht']);
        $this->emit('showexchangerateListForm');
        session()->flash('message', 'Exchangerate added successfully!');
    }

    public function render()
    {
        return view('livewire.pages.exchangerate.exchangerate-create');
    }

    public function loadPage($page)
    {
        $this->action = $page;
    }
}

