<?php

namespace App\Http\Livewire\Pages\SendPatch;

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

class SendMultiPatchBox extends Component
{
    protected $listeners = ['updateSelectedPatches'];

    public $selectedPatches = [];
    public $selectedPatchName = [];
    public $serverkey = '', $showSearch = false, $searchInProgress, $reponseMessages = [];
    public $showProgressModal = false;
    public $results = [];

    public function updatedServerkey()
    {
        $this->showSearch = true;
        if ($this->searchInProgress) {
            $this->reset('results');
        }

        $this->searchInProgress = true;
        $this->results = ConfServerLicense::where('SNKEY', 'LIKE', '%' . $this->serverkey . '%')->take(50)->get();
        $this->searchInProgress = false;
    }

    public function selectResult($key)
    {
        $this->serverkey = $key;
        $this->results = [];
        $this->showSearch = false;
    }

    public function mount($selectedPatches)
    {
        $this->selectedPatches = $selectedPatches;
    }
    public function updateSelectedPatches($patchId, $allSelectPatchName)
    {
        if (is_array($patchId)) {
            $this->selectedPatches = $patchId;
            $this->selectedPatchName = $allSelectPatchName;
        } else {
            if (in_array($patchId, $this->selectedPatches)) {
                $key = array_search($patchId, $this->selectedPatches);
                if ($key !== false) {
                    unset($this->selectedPatches[$key]);
                    unset($this->allSelectPatchName[$key]);
                }
            } else {
                $this->selectedPatches[] = $patchId;
                $this->selectedPatchName[] = $allSelectPatchName;
            }
        }
    }

    public function sendSelectedPatches()
    {
      
        $this->reponseMessages = [];
        if (count($this->selectedPatches) > 0 &&  !empty($this->serverkey)) {
            foreach ($this->selectedPatches as $pId) {
                $serverLicense = ConfServerLicense::where('SNKEY', $this->serverkey)->first();
                if (isset($serverLicense)) {
                    $serverPhpVersion = $serverLicense->PHP_VERSION_ID;

                    $patch = ConfSmartupdate::where('PID', $pId)->first();
                    $patchPhpVersion = $patch->PHP_VERSION_ID;

                    if ($serverPhpVersion != 0 && $patchPhpVersion != 0 && $serverPhpVersion != $patchPhpVersion) {
                        $this->reponseMessages[] = '<span class="text-error">Serverkey : ' . $this->serverkey . " and Patch : " . $pId . " PHP version conflict</span>";
                        continue;
                    }

                    $checkPendding = ConfServerPendding::where("PatchID", $pId)->where("ServerID", $serverLicense->ID)->first();
            
                    if (isset($checkPendding)) {
                        ConfServerPendding::where("PatchID", $pId)->where("ServerID", $serverLicense->ID)->update([
                            "TaskStatus" => "0",
                            "TaskFinish" => '0000_00_00 00:00:00'
                        ]);
                        SendPatchEdit::logSendPatch($patch, $serverLicense->ID, "Reload Patch");
                        $this->reponseMessages[] = "[Reload Patch] Send Success Serverkey : " . $this->serverkey . " and Patch : " . $pId ;
                      
                    } else {
                        $serverPedding  = new ConfServerPendding;
                        $serverPedding->ServerID = $serverLicense->ID;
                        $serverPedding->PatchID = $pId;
                        $serverPedding->TaskDate = date("Y-m-d H:i:s");
                        $serverPedding->TaskType = "";
                        $serverPedding->TaskStatus = "0";
                        $serverPedding->TaskRunner = auth()->user()->username;
                        $serverPedding->save();

                        SendPatchEdit::logSendPatch($patch, $serverLicense->ID, "Add Patch");
                        $this->reponseMessages[] = "[Add Patch] Send Success Serverkey : " . $this->serverkey . " and Patch : " . $pId ;
               
                    }
                  
                } else {

                    $this->reponseMessages[] = '<span class="text-error">Serverkey : ' . $this->serverkey . ' not found</span>';
               
                }
            }
        }
        $this->showProgressModal = true;
    }
    public function render()
    {
        return view('livewire.pages.send-patch.send-multi-patch-box');
    }
}
