<?php

namespace App\Http\Livewire\Pages\Parameter;

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

class ParameterCreate extends Component
{
    public $name, $message, $value, $description, $active = 'Y', $detail, $pageCode = [];
    public $searchByPage = [];

    public function mount()
    {
        $this->searchByPage = TabSpnpage::select('pagecode', 'pagename')->get()->toArray();
    }

    public function addParameter()
    {
        $this->validate([
            'name' => 'required|string|max:30',
            'value' => 'required|string|max:50',
            'description' => 'required|string|max:100',
            'active' => 'required',
            'detail' => 'required|string',
            'pageCode' => 'required|array|min:1',
        ]);

        ConfParameter::create([
            'name' => $this->name,
            'value' => $this->value,
            'description' => $this->description,
            'active' => $this->active,
        ]);

        $pageCodesString = implode(',', $this->pageCode);

        TabParameterInfo::create([
            'parameterName' => $this->name,
            'pageCode' => $pageCodesString,
            'detail' => $this->detail,
        ]);

        $this->reset(['name', 'value', 'description', 'active', 'pageCode', 'detail']);
        $this->emit('showparameterListForm');
        session()->flash('message', 'Parameter added successfully!');
    }

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


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