<?php

namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualInterface\Fields;

use ModulesGarden\ProxmoxAddon\App\Models\VirtualNetwork;
use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
use ModulesGarden\ProxmoxAddon\App\Services\Ip\Ipv4Range;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\AjaxFields\Select;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

class NetworkSelect extends Select implements ClientArea
{
    protected $id   = 'networkSelect';
    protected $name = 'network';

    public function prepareAjaxData()
    {
        session_write_close();
        $this->availableValues[]    = [
            'key' => 'public',
            'value' => sl('lang')->abtr('Public')
        ];
        foreach (VirtualNetwork::ofHostingId($this->getWhmcsParamByKey('serviceid'))->select('id', 'name')->get() as $vn)
        {
            $this->availableValues[]    = [
                'key' => $vn->id,
                'value' => $vn->name
            ];
        }

        if($this->availableValues)
        {
            $this->value    = $this->availableValues[0]['key'];
        }
    }
}