<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (27.03.19)
* *
*
*  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\Servers\ProxmoxVps\App\UI\Firewall\Forms;

use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\BaseForm;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Hidden;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Select;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Switcher;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Text;
use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;

class GroupForm extends BaseForm implements ClientArea
{
    use ApiService;
    use ProductService;


    protected function initFields()
    {
        //enable
        $field = new Switcher('enable');
        $this->addField($field);
        //pos
        if ($this->getFormType() == "update")
        {
            $this->addField(new Hidden("pos"));
        }
        //action
        $field   = new Select('action');
        $options = [];
        foreach ($this->api()->get("/cluster/firewall/groups", []) as $response)
        {
            $options[$response['group']] = sl("lang")->tr($response['group']);
        }
        $field->setAvailableValues($options);
        $this->addField($field);
        //iface
        $field      = new Select('iface');
        $ifaces     = [];
        $interfaces = $this->configuration()->getFirewallInterfaces();
        if (in_array("venet", $interfaces))
        {
            $ifaces["venet"] = sl('lang')->tr("venet");
        }
        if (in_array("eth", $interfaces))
        {
            foreach ($this->vm()->getNetworkDevices() as $nd)
            {
                $ifaces[$nd->getId()] = $nd->getId();
            }
        }
        $field->setAvailableValues($ifaces);
        $this->addField($field);
        //comment
        $field = new Text('comment');
        $this->addField($field);
        //type
        $this->addField((new Hidden("type"))->setDefaultValue('group'));
    }

}