<?php

/* * ********************************************************************
 * Proxmox 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\RecoveryVms\Forms;

use ModulesGarden\ProxmoxAddon\App\Models\RecoveryVm;
use ModulesGarden\ProxmoxAddon\App\UI\RecoveryVms\Sections\TabContent;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\BaseTabsForm;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\FormConstants;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\Sections\TabSection;
use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;

class DetailForm extends BaseTabsForm implements AdminArea
{

    private $recoveryVm;

    public function initContent()
    {

        $this->initIds('detailForm');
        $this->setFormType(FormConstants::READ);
        if (!$this->getRequestValue('actionElementId'))
        {
            return;
        }
        $this->recoveryVm = RecoveryVm::findOrFail($this->getRequestValue('actionElementId'));
        $this->tabConfig();
        $this->tabStatus();
        $this->tabDns();

    }

    private function tabConfig()
    {
        $section = new TabSection();
        $section->initIds(__FUNCTION__);
        $section->enableGroupBySectionName();
        $section->setMainContainer($this->mainContainer);
        $section->setName(sl('lang')->T(__FUNCTION__));
        $this->addSection($section);
        $content = new TabContent();
        $section->addSection($content);
        $vars = $this->recoveryVm->getConfig();
        foreach ($vars as $k => $v)
        {
            if (is_array($v))
            {
                unset($vars[$k]);
            }
        }
        $content->setCustomTplVars($vars);

    }

    private function tabStatus()
    {
        $section = new TabSection();
        $section->initIds(__FUNCTION__);
        $section->enableGroupBySectionName();
        $section->setMainContainer($this->mainContainer);
        $section->setName(sl('lang')->T(__FUNCTION__));
        $this->addSection($section);
        $content = new TabContent();
        $section->addSection($content);
        $vars = $this->recoveryVm->getStatus();
        unset($vars['pid']);
        unset($vars['template']);
        foreach ($vars as $k => $v)
        {
            if (is_array($v))
            {
                unset($vars[$k]);
            }
        }
        $content->setCustomTplVars($vars);

    }

    private function tabDns()
    {
        $section = new TabSection();
        $section->initIds(__FUNCTION__);
        $section->enableGroupBySectionName();
        $section->setMainContainer($this->mainContainer);
        $section->setName(sl('lang')->T(__FUNCTION__));
        $this->addSection($section);
        $content = new TabContent();
        $section->addSection($content);
        $vars = $this->recoveryVm->getDns();
        foreach ($vars as $k => $v)
        {
            if (is_array($v))
            {
                unset($vars[$k]);
            }
        }
        $content->setCustomTplVars($vars);

    }

}