<?php

namespace App\Http\Livewire\Pages\SendPatch;

use Livewire\Component;
use Livewire\WithPagination;
use App\Models\ConfSmartUpdate;

class SendMultiPatchList extends Component
{
    use WithPagination;
    public $action  = 'list';
    public $searchBy, $editPid, $message, $keyword, $perPage = 10, $searchSelected = 'PID';
    public $selectedPatches = [];
    public $insertedIds = [];

    protected $listeners = ['deleteItem', 'deleteSelected', 'showMultiPatchList'];

    public function mount()
    {
        $this->searchBy = [
            'PID' => 'PID',
            'PATCHNAME' => 'Patch Name',
            'PDESC' => 'Description',
            'MAJOR_VERSION' => 'Major Vaersion',
            'Remark' => 'Remark'

        ];
        $this->selectedPatches = [];
        // $this->dispatchBrowserEvent('grandchild-component-loaded');
    }

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

    public function showMultiPatchList() {
        $this->render();
    }

    public function loadPage($page)
    {
        $this->emit('menuChanged', $page);
    }
    public function render()
    {
        // dd("dd");
        $query = ConfSmartUpdate::select('PID', 'PATCHNAME', 'PDESC', 'PDATE', 'PLEVEL', 'Remark', 'MAJOR_VERSION');

        if ($this->searchSelected && $this->keyword) {
            $query->where($this->searchSelected, 'LIKE', '%' . $this->keyword . '%');
        }
        $query->orderBy('PID', 'DESC');
        $results = $query->paginate($this->perPage);
    
        return view('livewire.pages.send-patch.send-multi-patch-list', [
            'results' => $results,
            'selectedPatches' => ConfSmartUpdate::whereIn('PID', $this->selectedPatches)->get()
        ]);
    }

 
}
