<?php

namespace App\Http\Livewire\Pages\ServerLicense;

use App\Models\ConfServerLicense;
use Livewire\Component;
use Livewire\WithPagination;
use App\Models\ConfSmartUpdate;
use PDO;
use App\Exports\LicenseExport;
use App\Exports\ServerLicenseExport;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\DB;


class ServerLicenseIndex extends Component
{
    use WithPagination;
    public $action  = 'list';
    public $searchBy, $editId, $message, $keyword, $perPage = 10, $searchSelected = 'COMPANY', $selectedPatch = [], $totalItems;
    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 == 'PHP_VERSION_ID') {
            if ($this->keyword == '32') {
                if ($this->searchSelected && $this->keyword) {
                    $query->whereIn('PHP_VERSION_ID', [0, 1]);
                }
            } elseif ($this->keyword == '64') {
                if ($this->searchSelected && $this->keyword) {
                    $query->whereIn('PHP_VERSION_ID', [0, 2]);
                }
            } else {
                $query->whereIn('PHP_VERSION_ID', []);
            }
        } else {
            if ($this->searchSelected && $this->keyword) {
                $query->where($this->searchSelected, 'LIKE', '%' . $this->keyword . '%');
            }
        }


        $query->orderBy('ID', 'DESC');
        $results = $query->paginate($this->perPage);
        $this->totalItems = $results->total();
        return view('livewire.pages.server-license.server-license-index', compact('results'));
    }
    public function updatedPerPage($value)
    {
        $this->adjustPageForNewPerPage();
    }

    public function adjustPageForNewPerPage()
    {
        $lastPage = ceil($this->totalItems / $this->perPage);
        if ($this->page > $lastPage) {
            $this->setPage($lastPage);
        }
    }
    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;
    }

    public function exportLicense()
    {
        return Excel::download(new ServerLicenseExport(), 'conf_server_license.xlsx');
    }
}
