<?php

namespace Leadlion\AutogedalWarehouses\Controller\Adminhtml\Product\Condition;

class Edit extends \Leadlion\AutogedalWarehouses\Controller\Adminhtml\Product\Condition
{
    /**
     * Rule edit action
     *
     * @return void
     */
    public function execute()
    {
        $id = $this->getRequest()->getParam('id');
        $model = $this->_objectManager->create(\Leadlion\AutogedalWarehouses\Model\Warehouse::class);

        if ($id) {
            $model->load($id);
            if (!$model->getRuleId()) {
                $this->messageManager->addErrorMessage(__('This rule no longer exists.'));
                $this->_redirect('*/*/');
                return;
            }
        }

        // set entered data if was error when we do save
        $data = $this->_session->getPageData(true);
        if (!empty($data)) {
            $model->addData($data);
        }

        $model->getConditions()->setJsFormObject('rule_conditions_fieldset');

        $this->coreRegistry->register('current_rule', $model);

        $this->_initAction();
        $this->_view->getLayout()
            ->getBlock('warehouse_rule_edit')
            ->setData('action', $this->getUrl('*/*/save'));

        $this->_addBreadcrumb($id ? __('Edit Conditon') : __('New Conditon'), $id ? __('Edit Conditon') : __('New Conditon'));

        $this->_view->getPage()->getConfig()->getTitle()->prepend(
            $model->getRuleId() ? $model->getName() : __('New Conditon')
        );
        $this->_view->renderLayout();
    }
}