<?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\ProxmoxCloudVps\App\UI\FirewallOption\Forms;

use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\FirewallOption\Providers\FirewallOptionProvider;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\BaseForm;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Select;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Switcher;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;


class UpdateForm extends BaseForm implements ClientArea
{
    use ProductService;

    public function initContent()
    {
        $this->initIds('updateForm');
        $this->setFormType('update');
        $this->setProvider(new FirewallOptionProvider());
        $this->initFields();
        $this->loadDataToForm();
    }

    public function getAllowedActions()
    {
        return ['update'];
    }

    private function initFields()
    {
        //logLevel
        $logLevels = [
            "nolog"   => sl("lang")->tr("nolog"),
            "info"    => sl("lang")->tr("info"),
            "err"     => sl("lang")->tr("err"),
            "warning" => sl("lang")->tr("warning"),
            "crit"    => sl("lang")->tr("crit"),
            "alert"   => sl("lang")->tr("alert"),
            "emerg"   => sl("lang")->tr("emerg"),
            "debug"   => sl("lang")->tr("debug"),
        ];
        //enable
        if($this->acl()->hasFirewallOption("enable")){
            $field = new Switcher("enable");
            $this->addField($field);
        }
        //dhcp
        if($this->acl()->hasFirewallOption("dhcp")){
            $field = new Switcher("dhcp");
            $this->addField($field);
        }
        //ndp
        if($this->acl()->hasFirewallOption("ndp")){
            $field = new Switcher("ndp");
            $field->setDefaultValue("on");
            $this->addField($field);
        }
        //radv
        if($this->acl()->hasFirewallOption("radv")){
            $field = new Switcher("radv");
            $this->addField($field);
        }
        //macfilter
        if($this->acl()->hasFirewallOption("macfilter")){
            $field = new Switcher("macfilter");
            $field->setDefaultValue("on");
            $this->addField($field);
        }
        //ipfilter
        if($this->acl()->hasFirewallOption("ipfilter")){
            $field = new Switcher("ipfilter");
            $this->addField($field);
        }
        //log_level_in
        $field = new Select('log_level_in');
        $field->setAvailableValues($logLevels);
        $this->addField($field);
        //log_level_out
        $field = new Select('log_level_out');
        $field->setAvailableValues($logLevels);
        $this->addField($field);
        //policy_in
        $field = new Select('policy_in');
        $field->setAvailableValues([
            "DROP"   => sl("lang")->tr("DROP"),
            "ACCEPT" => sl("lang")->tr("ACCEPT"),
            "REJECT" => sl("lang")->tr("REJECT"),
        ]);
        $field->setDefaultValue("DROP");
        $this->addField($field);
        //policy_out
        $field = new Select('policy_out');
        $field->setAvailableValues([
            "DROP"   => sl("lang")->tr("DROP"),
            "ACCEPT" => sl("lang")->tr("ACCEPT"),
            "REJECT" => sl("lang")->tr("REJECT"),
        ]);
        $field->setDefaultValue("ACCEPT");
        $this->addField($field);
    }
}