<?php


namespace Benhauer\Salesmanago\Block\Adminhtml\Client;

use \Magento\Backend\Block\Template\Context;
use \Magento\Framework\Registry;
use \Magento\Framework\Data\FormFactory;
use \Magento\Store\Model\System\Store;

use \Magento\Backend\Block\Widget\Form\Generic;

class AccountSettings extends Generic
{
    protected $_systemStore;

    public function __construct(
        Context $context,
        Registry $registry,
        FormFactory $formFactory,
        Store $systemStore,
        array $data = []
    ) {
        $this->_systemStore = $systemStore;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * Init form
     *
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setId('account_settings_form');
        $this->setTitle(__('Account Settings'));
    }

    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()
    {
//        $model = $this->_coreRegistry->registry('mystore_slider');
//
//        if ($model->getId()) {
//            $model->setData('test', 'female');
//        }

        $form = $this->_formFactory->create(
            ['data' =>
                [
                    'id' => 'edit_form',
                    'action' => $this->getData('action'),
                    'method' => 'post', 'enctype' => 'multipart/form-data'
                ]
            ]
        );

        $form->setHtmlIdPrefix('mystore_slider_');

        $fieldset = $form->addFieldset(
            'base_fieldset',
            [
                'legend' => __('Account Settings'),
                'class' => 'fieldset-wide'
            ]
        );

        $fieldset->addField(
            'slider_title',
            'text',
            [
                'name' => 'slider_title',
                'label' => __('Slider Title'),
                'title' => __('Slider Title'),
                'value' => 'test value',
                'required' => true
            ]
        );

        if (/*$model->getSliderId()*/ true) {
            $fieldset->addField(
                'slider_id',
                'hidden',
                [
                    'name' => 'slider_id'
                ]
            );
        }

        $fieldset->addField(
            'slider_title_button',
            'button',
            [
                'name' => 'slider_title_save',
                'label' => __('Save'),
                'class' => 'save primary',
                'data_attribute' => [
                    'mage-init' => ['button' => ['event' => 'save']],
                    'form-role' => 'save',
                ],
                'sort_order' => 90,
            ]
        );

        $fieldset->addField(
            'url',
            'text',
            [
                'name' => 'url',
                'label' => __('Slider URL'),
                'title' => __('Slider URL'),
                'required' => true,
                'class' => 'validate-xml-identifier'
            ]
        );

        $fieldset->addField(
            'image',
            'image',
            [
                'name' => 'image',
                'label' => __('Slider Image'),
                'title' => __('Slider Image'),
                'required' => true
            ]
        );

        $fieldset->addField(
            'test',
            'radios',
            [
                'label' => __('test'),
                'title' => __('test'),
                'name' => 'test',
                'required' => true,
                'values' => array(
                    array('value'=>'male','label'=>'Male'),
                    array('value'=>'female','label'=>'Female'),
                )
            ]
        );

        /**
         * Check is single store mode
         */
        if (!$this->_storeManager->isSingleStoreMode()) {
            $field = $fieldset->addField(
                'store_id',
                'multiselect',
                [
                    'name' => 'stores[]',
                    'label' => __('Store View'),
                    'title' => __('Store View'),
                    'required' => true,
                    'values' => $this->_systemStore->getStoreValuesForForm(false, true)
                ]
            );
            $renderer = $this->getLayout()->createBlock(
                'Magento\Backend\Block\Store\Switcher\Form\Renderer\Fieldset\Element'
            );
            $field->setRenderer($renderer);
        } else {
            $fieldset->addField(
                'store_id',
                'hidden',
                ['name' => 'stores[]', 'value' => $this->_storeManager->getStore(true)->getId()]
            );
            $model->setStoreId($this->_storeManager->getStore(true)->getId());
        }

        $fieldset->addField(
            'is_active',
            'select',
            [
                'label' => __('Status'),
                'title' => __('Status'),
                'name' => 'is_active',
                'required' => true,
                'options' => ['1' => __('Enabled'), '0' => __('Disabled')]
            ]
        );

//        $form->setValues($model->getData());
        $form->setUseContainer(true);
        $this->setForm($form);

        return parent::_prepareForm();
    }
}
