<?php

/* * ********************************************************************
 * ProxmoxAddon product developed. (Sep 10, 2018)
 * *
 *
 *  CREATED BY MODULESGARDEN       ->       http://modulesgarden.com
 *  CONTACT                        ->       contact@modulesgarden.com
 *
 *
 * This software is furnished under a license and may be used and copied
 * only  in  accordance  with  the  terms  of such  license and with the
 * inclusion of the above copyright notice.  This software  or any other
 * copies thereof may not be provided or otherwise made available to any
 * other person.  No title to and  ownership of the  software is  hereby
 * transferred.
 *
 *
 * ******************************************************************** */

namespace ModulesGarden\ProxmoxAddon\App\UI\IpManagement\Fields;

use MGProvision\Proxmox\v2 as proxmox;
use ModulesGarden\ProxmoxAddon as main;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\AjaxFields\Select;
use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;

/**
 * Description of NodeSelect
 *
 * @author Pawel Kopec <pawelk@modulesgardne.com>
 */
class NodeSelect extends Select implements AdminArea
{
    private $serverId;

    public function prepareAjaxData()
    {
        $this->serverId          = $this->getRequestValue('sid', null);
        $this->availableValues[] = ['key' => 0, 'value' => sl('lang')->absoluteT('Any')];
        if ($this->serverId > 0)
        {
            $servers = main\Core\Models\Whmcs\Server::where('id', $this->serverId);
        }
        else
        {
            $servers = main\Core\Models\Whmcs\Server::whereIn("type", ["proxmoxVPS", "ProxmoxCloudVps"])->where('disabled', '0');
        }
        $nodeRepository = new proxmox\repository\NodeRepository();
        $this->value    = [0];
        if ($this->getRequestValue('id'))
        {
            $this->value = [main\App\Models\IpAddress::where('id', $this->getRequestValue('id'))->first()->node];
        }
        foreach ($servers->get() as $server)
        {
            /* @var $server main\Core\Models\Whmcs\Server */
            try
            {
                $host = $server->ipaddress ? $server->ipaddress : $server->hostname;
                if(is_numeric($server->port)){
                    $host .=":".$server->port;
                }
                $password = html_entity_decode(decrypt($server->password), ENT_QUOTES);
                $api  = new proxmox\Api($host, $server->username, $server->accesshash, $password);
                $nodeRepository->setApi($api);
                foreach ($nodeRepository->fetch() as $node)
                {
                    $this->availableValues[] = [
                        'key'   => $node->getNode(),
                        'value' => $node->getNode()
                    ];

                }
            }
            catch (proxmox\ProxmoxApiException $ex)
            {//login to proxmox host failed
                if ($ex->getCode() != 401)
                {
                    throw $ex;
                }
            }
        }
    }
}
