<?php

namespace App\Http\Livewire\Pages\Exchangerate;

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

class ExchangerateEdit extends Component
{
    public $currency, $exdate, $finishdate, $rate, $baht, $exbaht;

    public function mount($currency, $exdate)
    {
        $exchangerate = CenterConfExchangerate::where('currency', $currency)
                                              ->where('exdate', $exdate)
                                              ->first();
        if ($exchangerate) {
            $this->currency = $exchangerate->currency;
            $this->exdate = $exdate;
            $this->finishdate = $exchangerate->finishdate;
            $this->rate = $exchangerate->rate;
            $this->baht = $exchangerate->baht;

            $export = CenterConfExchangerateExport::where('currency', $currency)
                                                  ->where('exdate', $exdate)
                                                  ->first();
            $this->exbaht = $export ? $export->baht : null;
        }
    }

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

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

        $export = CenterConfExchangerateExport::where('currency', $this->currency)
                                              ->where('exdate', $this->exdate)
                                              ->first();
        if ($export) {
            CenterConfExchangerateExport::where('currency', $this->currency)
                                        ->where('exdate', $this->exdate)
                                        ->update([
                                            'finishdate' => $this->finishdate,
                                            'rate' => $this->rate,
                                            'baht' => $this->exbaht,
                                        ]);
        } else {
            CenterConfExchangerateExport::create([
                'currency' => $this->currency,
                'exdate' => $this->exdate,
                'finishdate' => $this->finishdate,
                'rate' => $this->rate,
                'baht' => $this->exbaht,
            ]);
        }

        $this->emit('showexchangerateListForm');
        session()->flash('message', 'Exchangerate updated successfully!');
    }

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

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