<?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', $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',
            'pageCode' => 'required',
        ]);

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

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

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

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