<?php

namespace App\Http\Livewire\Pages\SendPatch;

use App\Models\ConfServerLicense;
use App\Models\ConfServerPendding;
use App\Models\ConfSmartupdate;
use App\Http\Livewire\Pages\SendPatch\SendPatchEdit;
use App\Models\LogSendPath2customer;
use Livewire\Component;

class DeleteMultiPatchBox extends Component
{

    protected $listeners = ['updateServerkey' , 'updateDeletedPatches'];
    public  $isLoading = false;
        public $selectedPatches = [];
    public $selectedPatchName = [];
    public $serverkey = '', $serverId,  $showSearch = false, $searchInProgress, $reponseMessages = [];
    public $showProgressModal = false;
    public $results = [];

    public function mount($selectedPatches)
    {
        $this->selectedPatches = $selectedPatches;
    }
    public function updateServerkey($serverkey)
    {

        $serverLicense = ConfServerLicense::where("SNKEY", $serverkey)->first();
        if (isset($serverLicense)) {
            $this->serverId = $serverLicense->ID;
            $this->serverkey = $serverkey;
        }
    }
    public function updateDeletedPatches($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 removePatch($key)
    {
        $keyIndex = array_search($key, $this->selectedPatches);

        if ($keyIndex !== false) {
            unset($this->selectedPatches[$keyIndex]);
            unset($this->selectedPatchName[$keyIndex]);

            $this->selectedPatches = array_values($this->selectedPatches);
            $this->selectedPatchName = array_values($this->selectedPatchName);
        }

        $this->emit('removePatchSelected', $key);
    }


    public function deleteSelectedPatches()
    {

        $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)) {
                    ConfServerPendding::where("PatchID", $pId)->where('ServerID', $serverLicense->ID)->delete();
                    static::logSendPatch($pId, $serverLicense->ID, "Delete Patch");
                    $this->reponseMessages[] = "[Delete 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;
        $this->selectedPatchName = [];
        $this->selectedPatches = [];
        $this->emit('updateSelectPatchAfterDeleted');
    }
    public static function logSendPatch($patchId, $serverId, $logDesc)
    {
        $log = new LogSendPath2customer;
        $log->UID =  auth()->user()->uid;
        $log->PATCHID =  $patchId;
        $log->SERVERKEYID =  $serverId;
        $log->ACTDATETIME =  date("Y-m-d H:i:s");
        $log->LOGDESC =  $logDesc;
        $log->IPACTIVE =  request()->ip();
        $log->save();
    }
    public function render()
    {
        return view('livewire.pages.send-patch.delete-multi-patch-box');
    }
}
