<?php
namespace PortalPeople\DisabledDates\Controller\Adminhtml\Date;

use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
use PortalPeople\DisabledDates\Model\DisabledDateFactory;

class Edit extends Action
{
    protected $resultPageFactory;
    protected $disabledDateFactory;

    public function __construct(
        Action\Context $context,
        PageFactory $resultPageFactory,
        DisabledDateFactory $disabledDateFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->disabledDateFactory = $disabledDateFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $id = $this->getRequest()->getParam('entity_id');
        $model = $this->disabledDateFactory->create();
        if ($id) {
            $model->load($id);
            if (!$model->getId()) {
                $this->messageManager->addErrorMessage(__('This date no longer exists.'));
                return $this->_redirect('*/*/');
            }
        }
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend($id ? __('Edit Disabled Date') : __('New Disabled Date'));
        return $resultPage;
    }
}
