<?php

/* * ********************************************************************
 * Wordpress_Manager Product developed. (Dec 11, 2017)
 * *
 *
 *  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\Forms;

use ModulesGarden\ProxmoxAddon as main;
use ModulesGarden\ProxmoxAddon\App\UI\IpManagement\Fields\NodeSelect;
use ModulesGarden\ProxmoxAddon\App\UI\IpManagement\Providers\IpAddressProvider;
use ModulesGarden\ProxmoxAddon\App\UI\Validators\NumberValidator;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\BaseForm;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\Fields;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\Sections\HalfPageSection;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\Sections\RawSection;

/**
 * Description of CreateForm
 *
 * @author Pawel Kopec <pawelk@modulesgarden.com>
 */
class CreateForm extends BaseForm implements AdminArea
{

    public function initContent()
    {
        $this->initIds('ipManagementCreateForm');
        $this->setFormType('create');
        $this->addClass('lu-row');
        $this->setProvider(new IpAddressProvider);
        $this->initFields();
        $this->loadDataToForm();
    }

    protected function initFields()
    {
        //Sections
        $mainSection = new RawSection('mainSection');
        $mainSection->setMainContainer($this->mainContainer);
        $leftSection = new HalfPageSection('leftSection');
        $leftSection->setMainContainer($this->mainContainer);
        $rightSection = new HalfPageSection('rightSection');
        $rightSection->setMainContainer($this->mainContainer);
        //IP Pool
        $field = new Fields\Text('ipPool');
        $field->addValidator(new main\App\UI\Validators\IpAddressValidator(true));
        $field->setPlaceholder('192.168.0.100');
        $mainSection->addField($field);
        //Mask -> cidr
        $field = new Fields\Text('cidr');
        $field->addValidator(new main\App\UI\Validators\CidrValidator(true));
        $field->setPlaceholder('24');
        $mainSection->addField($field);

        $row1 = new main\Core\UI\Widget\Forms\Sections\SectionLuRow('row1');
        //VLAN Trunks
        $field = new Fields\Text('trunks');
        $field->addValidator(new NumberValidator());
        $leftSection->addField($field);
        //Tag
        $field = new Fields\Text('tag');
        $field->addValidator(new NumberValidator(1, 4094));
        $rightSection->addField($field);

        $row1->addSection($leftSection);
        $row1->addSection($rightSection);

        $leftSection = new HalfPageSection('leftSectionr2');
        $leftSection->setMainContainer($this->mainContainer);
        $rightSection = new HalfPageSection('rightSectionr2');
        $rightSection->setMainContainer($this->mainContainer);
        $row2 = new main\Core\UI\Widget\Forms\Sections\SectionLuRow('row2');

        //Subnet Mask
        $field = new Fields\Text('subnet_mask');
        $field->addValidator(new main\App\UI\Validators\IpAddressValidator(false));
        $field->setPlaceholder('255.255.255.0');
        $mainSection->addField($field);
        //Gateway
        $field = new Fields\Text('gateway');
        $field->addValidator(new main\App\UI\Validators\IpAddressValidator(false, false));
        $field->setPlaceholder('192.168.0.1');
        $mainSection->addField($field);
        //Server
        $field  = new Fields\Select('sid');
        $values = ["0" => main\Core\ServiceLocator::call('lang')->absoluteT('Any')];
        $values += (array)main\Core\Models\Whmcs\Server::whereIn("type", ["proxmoxVPS", "ProxmoxCloudVps"])->pluck("name", "id")->toArray();
        $field->setAvailableValues($values);
        $leftSection->addField($field);
        //Node
        $field = new NodeSelect('node');
        $field->addReloadOnChangeField('sid');
        $field->setMainContainer($this->mainContainer);
        $rightSection->addField($field);

        $row2->addSection($leftSection);
        $row2->addSection($rightSection);

        //Virtualization
        $field = new Fields\Select('visualization');
        $field->setAvailableValues(["Auto" => main\Core\ServiceLocator::call('lang')->absoluteT('Auto'), "KVM" => "KVM", "LXC" => "LXC"]);
        $mainSection->addField($field);
        //Private Address
        $field = new Fields\Switcher('private');
        $mainSection->addField($field);
        //bridge
        $field = new Fields\Text('bridge');
        $mainSection->addField($field);
        //Add sections
        $this->addSection($mainSection);
        $this->addSection($row1);
        $this->addSection($row2);
    }

    protected function getDefaultActions()
    {
        return ['create'];
    }
}
