<?php

namespace App\Http\Livewire\Pages\ServerLicense;

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

class ServerLicenseIndex extends Component
{
    use WithPagination;
    public $action  = 'list';
    public $searchBy, $editId, $message, $keyword, $perPage = 10, $searchSelected = 'COMPANY' , $selectedPatch = [] ;
    protected $listeners = [ 'deleteItem', 'deleteSelected' ,'showpatchListForm'];
    public function mount()
    {
        $this->searchBy = [
            'COMPANY' => 'Company',
            'SNKEY' => 'Serverkey',
            'CUR_VERSION' => 'Current Version',
            'STATUS' => 'Status',
            'DATABASETYPE' => 'Database',
            'PHP_VERSION_ID' => 'PHP Version'

        ];
    }
    public function render()
    {

        $query = ConfServerLicense::select('ID', 'SNKEY', 'COMPANY', 'STATUS', 'CUR_VERSION', 'DATABASETYPE', 'LICENSEDATE');

        if ($this->searchSelected && $this->keyword) {
            $query->where($this->searchSelected, 'LIKE', '%' . $this->keyword . '%');
        }
        $query->orderBy('ID', 'DESC');
        $results = $query->paginate($this->perPage);

        return view('livewire.pages.server-license.server-license-index', compact('results'));
    }
    public function search()
    {
        $this->resetPage();
    }
    public function showServerLicenseListForm()
    {
        $this->action = 'list';
    }

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

    public function showServerLicenseEditForm($id)
    {
        $this->editId = $id;
        $this->action = 'edit';
        // $this->emit('showpatchEditForm');
    }

    public function showDeleteModal($id)
    {
        $this->emit('showDeleteModal' ,$id); 
    }

    public function deleteItem($id) {
    
        $serverlicense = ConfServerLicense::where("ID", $id)->first();
        $serverlicense->status = 'N';
        $serverlicense->save();
        $message = "Serverlicense status updated successfully";
        $this->message = $message;
    }
 
}
