<?php

namespace ThurData\Servers\KerioEmail\App\UI\Admin\ProductConfiguration\Pages\Sections;


use ThurData\Servers\KerioEmail\App\Enums\Size;
use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository;
use ThurData\Servers\KerioEmail\App\Traits\LangHandler;
use ThurData\Servers\KerioEmail\App\Traits\ServerParams;
use ThurData\Servers\KerioEmail\App\Traits\KerioApiHandler;
use ThurData\Servers\KerioEmail\App\UI\Admin\Custom\Fields\ExtendedInputField;
use ThurData\Servers\KerioEmail\App\UI\Admin\Custom\Sections\BoxSectionExtended;
use ThurData\Servers\KerioEmail\App\UI\Admin\Custom\Sections\HalfPageCustomCosSection;
use ThurData\Servers\KerioEmail\Core\UI\Interfaces\AdminArea;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Sections\BoxSection;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Sections\HalfPageSection;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Fields\Text;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\ClassOfService;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 29.08.19
 * Time: 08:35
 * Class ClassOfServiceFeatures
 */
class ClassOfServiceFeatures extends BoxSectionExtended implements AdminArea
{
    use LangHandler;

    protected $id       = 'classOfServiceFeatures';
    protected $name     = 'classOfServiceFeatures';
    protected $title    = 'classOfServiceFeatures';

    public function initContent()
    {
        $this->loadCos();
    }

    /**
     *
     */
    public function loadCos()
    {
        $lang = $this->getLang();

        $left  = new HalfPageCustomCosSection('left');
        $right = new HalfPageCustomCosSection('right');

        /**
         * api manager
         */
        $manager = new KerioManager();
        /**
         * repository
         */
        $repository = $manager->getApiByProduct($this->getRequestValue('id'))->soap->repository();
        /**
         * cos
         */
        $cos = $repository->cos->all();
        $this->cos = $cos;

        $pointer = 1;
        foreach($cos as $key => $cosModel)
        {
            /* @var $cosModel ClassOfService */
            $id = 'cos['.$cosModel->getId().']';
            $quete = $cosModel->getMbMailQuote() ;

            $field = new ExtendedInputField($cosModel->getId());
            $field->setFieldType(ExtendedInputField::TYPE_NUMBER);
            $field->addHtmlAttribute('min', Size::UNLIMITED);
            $field->setName($id);
            $field->setRawTitle("{$cosModel->getName()} ({$quete} MB)");
            $field->setRawDescription(sprintf($lang->absoluteT('Enter to limit an accounts number of %s with quota %s MB or set -1 to unlimited'),ucfirst($cosModel->getName()), $quete) );
            $field->setDefaultValue(Size::DEFAULT_NULL_VALUE);

            if($pointer %2 == 0)
            {
                $right->addField($field);
            }else{
                $left->addField($field);
            }
            $pointer++;
        }
        $this
            ->addSection($left)
            ->addSection($right);
    }

}