<?php

namespace App\Http\Livewire\Pages\Exchangerate;

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

class ExchangerateIndex extends Component
{
    use WithPagination;

    public $action = 'list';
    public $searchBy, $editCurrency, $editExdate, $deleteCurrency, $deleteExdate, $message, $keyword, $perPage = 20, $searchSelected = 'center_conf_exchangerate.currency';
    public $selectedExchangerates = [];

    protected $listeners = [
        'deleteItem',
        'deleteSelected',
        'showexchangerateListForm',
        'loadPage'
    ];

    public function mount()
    {
        $this->searchBy = [
            'center_conf_exchangerate.currency' => 'By Currency',
            'center_conf_exchangerate.exdate' => 'By Expired date',
            'center_conf_exchangerate.finishdate' => 'By Finish date'
        ];
        $this->showexchangerateListForm();
        $this->message = null;  
    }

    public function render()
    {
        $query = CenterConfExchangerate::select(
            'center_conf_exchangerate.currency', 
            'center_conf_exchangerate.exdate', 
            'center_conf_exchangerate.finishdate', 
            'center_conf_exchangerate.rate', 
            'center_conf_exchangerate.baht', 
            'center_conf_exchangerate.amenddate', 
            'center_conf_exchangerate_export.baht as exbaht'
        )
        ->join('center_conf_exchangerate_export', function($join) {
            $join->on('center_conf_exchangerate.currency', '=', 'center_conf_exchangerate_export.currency')
                 ->on('center_conf_exchangerate.exdate', '=', 'center_conf_exchangerate_export.exdate')
                 ->on('center_conf_exchangerate.finishdate', '=', 'center_conf_exchangerate_export.finishdate');
        });

        if ($this->searchSelected && $this->keyword) {
            $query->where($this->searchSelected, 'LIKE', '%' . $this->keyword . '%');
        }

        $query->orderBy('center_conf_exchangerate.exdate', 'DESC')
              ->orderBy('center_conf_exchangerate_export.exdate', 'DESC')
              ->orderBy('center_conf_exchangerate.currency', 'DESC')
              ->orderBy('center_conf_exchangerate_export.currency', 'DESC');

        $results = $query->paginate($this->perPage);

        return view('livewire.pages.exchangerate.exchangerate-index', compact('results'));
    }

    public function search()
    {
        $this->resetPage();
    }

    public function showexchangerateListForm()
    {
        $this->action = 'list';
    }

    public function showexchangerateAddForm()
    {
        $this->action = 'add';
    }

    public function showexchangerateEditForm($currency, $exdate)
    {
        $this->editCurrency = $currency;
        $this->editExdate = $exdate;
        $this->action = 'edit';
    }

    public function deleteExchangerate($currency, $exdate)
    {
        CenterConfExchangerate::where('currency', $currency)
                              ->where('exdate', $exdate)
                              ->delete();
        CenterConfExchangerateExport::where('currency', $currency)
                                    ->where('exdate', $exdate)
                                    ->delete();

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

    public function deleteSelectedExchangerates()
    {
        foreach ($this->selectedExchangerates as $exchangerate) {
            $parts = explode(',', $exchangerate);
            CenterConfExchangerate::where('currency', $parts[0])
                                  ->where('exdate', $parts[1])
                                  ->delete();
            CenterConfExchangerateExport::where('currency', $parts[0])
                                        ->where('exdate', $parts[1])
                                        ->delete();
        }

        session()->flash('message', 'Selected Exchangerates deleted successfully!');
        $this->selectedExchangerates = [];
        $this->showexchangerateListForm();
    }

    public function deleteItem($currency, $exdate)
    {
        $this->deleteExchangerate($currency, $exdate);
    }

    public function deleteSelected()
    {
        $this->deleteSelectedExchangerates();
    }

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