<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (26.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\FirewallOption\Pages;

use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\Servers\ProxmoxVps\App\UI\FirewallOption\Buttons\UpdateButton;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Builder\BaseContainer;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AjaxElementInterface;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\RawDataJsonResponse;
use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;

/**
 * Class FirewallOption
 * @package ModulesGarden\Servers\ProxmoxVps\App\UI\FirewallOption\Pages
 * @todo - move html code to new class - new Label(some text, Label::DANGER)
 */
class FirewallOption extends BaseContainer implements ClientArea, AjaxElementInterface
{
    use ApiService;
    use ProductService;

    protected $id = 'firewallOption';
    protected $title = 'firewallOption';
    protected $vueComponent = true;
    protected $defaultVueComponentName = 'mg-firewallOption';

    public function initContent()
    {
        $this->addButton(new UpdateButton());
    }

    public function returnAjaxData()
    {
        $vars = ['enteries' => []];
        $lang = sl("lang");
        $allowed = ['log_level_in', 'log_level_out', 'policy_in', 'policy_out'];
        foreach ($this->vm()->firewallOptions()->read()->toArray() as $key => $value)
        {
            if(!in_array($key, $allowed) && !$this->acl()->hasFirewallOption($key)){
                continue;
            }
            if (is_numeric($value) && $value == 1)
            {
                $vars['enteries'][$lang->tr($key)] = '<span class="lu-label lu-label--success lu-label--status">' . sl('lang')->tr($value) . '</span>';
            }
            else
            {
                if (is_numeric($value) && $value == 0)
                {
                    $vars['enteries'][$lang->tr($key)] = '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($value) . '</span>';
                }
                else
                {
                    $vars['enteries'][$lang->tr($key)] = $lang->tr($value);
                }
            }
        }
        return (new RawDataJsonResponse(['data' => $vars]));
    }


}