<?php

namespace App\Http\Livewire\Pages\SendPatch;

use App\Models\ConfServerLicense;
use App\Models\ConfServerPendding;
use App\Models\ConfSmartupdate;
use Livewire\Component;
use Livewire\WithPagination;

class DeleteMultiPatchList extends Component
{

    use WithPagination;
    public $serverkey = '';
    public $searchBy, $serverId, $editPid, $message, $keyword, $perPage = 10, $searchSelected = 'PID';
    public $selectedPatches = [];
    
    protected $listeners = ['updateServerkey'];
    public function mount()
    {

        $this->searchBy = [
            'PID' => 'PID',
            'PATCHNAME' => 'Patch Name',
            'PDESC' => 'Description',
            'MAJOR_VERSION' => 'Major Vaersion',
            'Remark' => 'Remark'

        ];
        // $this->selectedPatches = [];
    }
    public function search()
    {
        $this->resetPage();
    }

    public function updateServerkey($serverkey)
    {
    
        $serverLicense = ConfServerLicense::where("SNKEY", $serverkey)->first();
        if (isset($serverLicense)) {
            $this->serverId = $serverLicense->ID;
        }
        $this->render();
    }

    public function render()
    {
    
        $query = ConfServerPendding::select('PID', 'PATCHNAME', 'PDESC', 'PDATE', 'PLEVEL', 'Remark', 'MAJOR_VERSION')
            ->join('conf_smartupdate', 'conf_smartupdate.PID', '=', 'conf_server_pendding.PatchID')
            ->where('conf_server_pendding.ServerID', $this->serverId);

        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.delete-multi-patch-list', [
            'results' => $results
        ]);
    }
}
