<?php


namespace Magetop\Advancepreview\Controller\Adminhtml\PreviewAdditionalTab;


use Magento\Backend\App\Action;

/**
 * Class UploadMedia
 * @package Magetop\Advancepreview\Controller\Adminhtml\ProductPreview
 */
class Save extends \Magetop\Advancepreview\Controller\Adminhtml\PreviewAdditionalTab
{
    /**
     *
     */
    const CUSTOM_PREVIEWS = 'custom_previews';
    /**
     * @var \Magento\Framework\Filesystem\Driver\File
     */
    protected $file;
    /**
     * @var \Magetop\Advancepreview\Model\AdvancePreviewFileFactory
     */
    protected $advancePreviewFileFactory;
    /**
     * @var \Magetop\Advancepreview\Model\AdvancePreviewUploadFactory
     */
    protected $advancePreviewUploadFactory;
    protected $previewAdditionalFactory;
    protected $resPreviewAdditionalFactory;
    protected $colPreviewAdditionalFactory;

    public function __construct(
        Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
        \Magento\Framework\Filesystem\Driver\File $file,
        \Magetop\Advancepreview\Model\AdvancePreviewUploadFactory $advancePreviewUploadFactory,
        \Magetop\Advancepreview\Model\PreviewAdditionalFactory $previewAdditionalFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewAdditionalFactory $resPreviewAdditionalFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewAdditional\CollectionFactory $colPreviewAdditionalFactory
    )
    {
        parent::__construct($context, $jsonFactory);
        $this->file = $file;
        $this->advancePreviewUploadFactory = $advancePreviewUploadFactory;
        $this->previewAdditionalFactory = $previewAdditionalFactory;
        $this->resPreviewAdditionalFactory = $resPreviewAdditionalFactory;
        $this->colPreviewAdditionalFactory = $colPreviewAdditionalFactory;

    }

    /**
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\Result\Json|\Magento\Framework\Controller\ResultInterface|void
     * @throws \Magento\Framework\Exception\FileSystemException
     */
    function execute()
    {
        $response = [
            'status'=>'error',
            'message'=>__('Something wrong when saving data'),
            'html'=>''
        ];
        $params = $this->getRequest()->getParams();
        $id = isset($params['addition_id']) ? (int)$params['addition_id'] : 0;
        $dataSave = array(
            'product_id'=>$params['product_id'],
            'status'=>1,
            'title'=>$params['title'],
            'description'=>$params['description'],
            'price'=>$params['price'],
            'created_at'=>date('Y-m-d H:i:s')
        );
        if($id > 0) {
            $dataSave['addition_id'] = $id;
            unset($dataSave['created_at']);
        }
        $isSave = $this->saveAdditionalItem($dataSave);
        if($isSave) {
            $response['status'] = 'success';
            $response['message'] = __('you have saved data success');
            $response['html'] = $this->_view->getLayout()->createBlock('Magetop\Advancepreview\Block\Adminhtml\Product\Edit\PreviewAdditionalTab')->setTemplate('Magetop_Advancepreview::product/addition_tab/items.phtml')->toHtml();

        }
        return $this->getAjaxResponse($response);
    }
    protected function saveAdditionalItem($dataSave = []) {
        $model = $this->previewAdditionalFactory->create();
        $model->setData($dataSave);
        try {
            $this->resPreviewAdditionalFactory->create()->save($model);
            return true;
        }
        catch (\Exception $exception) {
        }
        return false;
    }
}
