<?php

namespace PortalPeople\DisabledDates\Block\Adminhtml\Date\Edit;

use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Registry;
use Magento\Framework\Data\FormFactory;

class Form extends Generic
{
    public function __construct(
        Context $context,
        Registry $registry,
        FormFactory $formFactory,
        array $data = []
    ) {
        parent::__construct($context, $registry, $formFactory, $data);
    }

    protected function _prepareForm()
    {
        $model = $this->_coreRegistry->registry('disableddates_date');
        
        // Create empty model if none exists in registry
        if (!$model) {
            $model = $this->_objectManager->create(\PortalPeople\DisabledDates\Model\DisabledDate::class);
        }

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

        $form->setHtmlIdPrefix('disableddates_');

        $fieldset = $form->addFieldset(
            'base_fieldset',
            ['legend' => __('Disabled Date Information'), 'class' => 'fieldset-wide']
        );

        if ($model->getId()) {
            $fieldset->addField('entity_id', 'hidden', ['name' => 'entity_id']);
        }

        $fieldset->addField(
            'disabled_date',
            'date',
            [
                'name' => 'disabled_date',
                'label' => __('Disabled Date'),
                'title' => __('Disabled Date'),
                'required' => true,
                'date_format' => 'yyyy-MM-dd',
                'class' => 'validate-date'
            ]
        );

        $fieldset->addField(
            'reason',
            'text',
            [
                'name' => 'reason',
                'label' => __('Reason'),
                'title' => __('Reason'),
                'maxlength' => 255,
                'note' => __('Optional reason for disabling this date')
            ]
        );

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

        return parent::_prepareForm();
    }
}
