<?php

namespace ThurData\Servers\KerioEmail\App\Traits;


use ThurData\Servers\KerioEmail\App\UI\Admin\Custom\Sections\RowSection;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\BaseForm;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Sections\HalfPageSection;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 08.11.19
 * Time: 14:20
 * Class FormExtendedTrait
 */
trait FormExtendedTrait
{
    protected $sectionCounter = 0;

    /**
     * auto generate double section helper
     *
     * @param array $fields
     * @return $this
     * @throws \Exception
     */
    protected function generateDoubleSection($fields = [])
    {
        if(!method_exists($this, 'addSection'))
        {
            throw new \Exception('This trait can not be used in class: '.self::class);
        }

        $sections = [];
        $fieldCounter = 0;
        foreach($fields as $field)
        {
            $halfPageSection = new HalfPageSection('generated_'.$this->sectionCounter.'_'.$fieldCounter);
            $halfPageSection->addField($field);
            $sections[] = $halfPageSection;

            $fieldCounter++;
        }

        $rawSection = new RowSection('generated_row_section_'.$this->sectionCounter);

        foreach($sections as $section)
        {
            $rawSection->addSection($section);
        }

        $this->addSection($rawSection);
        $this->sectionCounter++;
        return $this;
    }

}