<?php

namespace App\Http\Livewire\Pages\Parameter;

use Livewire\Component;
use Livewire\WithPagination;
use App\Models\ConfParameter;
use App\Models\TabParameterInfo;
use App\Models\TabSpnpage;

class ParameterIndex extends Component
{
    use WithPagination;
    public $action = 'list';
    public $searchBy, $searchByPage, $editPid, $message, $keyword, $perPage = 20, $searchSelected = 'name', $searchSelectedPage = '';
    public $selectedParameters = [];
    public $deletePid;
    public $totalItems;
    protected $listeners = [
        'deleteItem',
        'deleteSelected',
        'showparameterListForm',
        'loadPage'
    ];

    public function mount()
    {
        $this->searchBy = [
            'name' => 'By Name',
            'value' => 'By Value',
            'description' => 'By Description'
        ];

        $this->searchByPage = TabSpnpage::select('pagecode', 'pagename')->get()->toArray();
        logger($this->searchByPage);
    }

    public function render()
    {
        $query = ConfParameter::select('conf_parameter.PID', 'conf_parameter.name', 'conf_parameter.value', 'conf_parameter.description', 'conf_parameter.active', 'tab_parameter_info.pageCode', 'tab_parameter_info.detail')
            ->leftJoin('tab_parameter_info', 'conf_parameter.name', '=', 'tab_parameter_info.parameterName');

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

        if ($this->searchSelectedPage) {
            $query->where('tab_parameter_info.pageCode', $this->searchSelectedPage);
        }

        $query->orderBy('PID', 'DESC');
        $results = $query->paginate($this->perPage);
        $this->totalItems = $results->total();
        return view('livewire.pages.parameter.parameter-index', compact('results'));
    }
    public function updatedPerPage($value)
    {
        $this->adjustPageForNewPerPage();
    }

    public function adjustPageForNewPerPage()
    {
        $lastPage = ceil($this->totalItems / $this->perPage);
        if ($this->page > $lastPage) {
            $this->setPage($lastPage);
        }
    }
    public function search()
    {
        $this->resetPage();
    }

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

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

    public function showparameterEditForm($pid)
    {
        $this->editPid = $pid;
        $this->action = 'edit';
    }
    

    public function showparameterDeleteForm($pid)
    {
        $this->deletePid = $pid;
        $this->action = 'delete';
    }

    public function deleteParameter($pid)
    {
        $parameter = ConfParameter::where('PID', $pid)->firstOrFail();
        $parameterName = $parameter->name;

        ConfParameter::where('PID', $pid)->delete();
        TabParameterInfo::where('parameterName', $parameterName)->delete();

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

    public function deleteSelectedParameters()
    {
        $parameters = ConfParameter::whereIn('PID', $this->selectedParameters)->get();
        foreach ($parameters as $parameter) {
            $parameterName = $parameter->name;
            ConfParameter::where('PID', $parameter->PID)->delete();
            TabParameterInfo::where('parameterName', $parameterName)->delete();
        }

        session()->flash('message', 'Selected parameters deleted successfully!');
        $this->selectedParameters = [];
        $this->showparameterListForm();
    }
    
    public function deleteItem($pid)
    {
        $this->deleteParameter($pid);
    }

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

    public function loadPage($page)
    {
        $this->action = $page;
        //$this->parameters = ConfParameter::all();
    }
}
