<?php

namespace PortalPeople\DisabledDates\Block\Adminhtml\Date;

use Magento\Backend\Block\Widget\Form\Container;
use Magento\Backend\Block\Widget\Context;
use Magento\Framework\Registry;

class Edit extends Container
{
    protected $_coreRegistry = null;

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

    protected function _construct()
    {
        $this->_objectId = 'entity_id';
        $this->_blockGroup = 'PortalPeople_DisabledDates';
        $this->_controller = 'adminhtml_date';

        parent::_construct();

        $this->buttonList->update('save', 'label', __('Save Disabled Date'));
        $this->buttonList->update('delete', 'label', __('Delete Disabled Date'));

        if ($this->_isAllowedAction('PortalPeople_DisabledDates::disabled_dates')) {
            $this->buttonList->update('save', 'class', 'save primary');
            $this->buttonList->add(
                'saveandcontinue',
                [
                    'label' => __('Save and Continue Edit'),
                    'class' => 'save',
                    'data_attribute' => [
                        'mage-init' => [
                            'button' => ['event' => 'saveAndContinueEdit', 'target' => '#edit_form'],
                        ],
                    ]
                ],
                -100
            );
        } else {
            $this->buttonList->remove('save');
        }

        if ($this->_coreRegistry->registry('disableddates_date') && $this->_coreRegistry->registry('disableddates_date')->getId()) {
            $this->buttonList->remove('delete');
        }
    }

    public function getHeaderText()
    {
        $model = $this->_coreRegistry->registry('disableddates_date');
        if ($model && $model->getId()) {
            return __("Edit Disabled Date '%1'", $this->escapeHtml($model->getDisabledDate()));
        } else {
            return __('New Disabled Date');
        }
    }

    protected function _isAllowedAction($resourceId)
    {
        return $this->_authorization->isAllowed($resourceId);
    }

    public function getFormActionUrl()
    {
        return $this->getUrl('*/*/save');
    }
}
