<?php


namespace Magetop\Advancepreview\Controller\Adminhtml\PreviewDailyShipping;


use Magento\Backend\App\Action;

/**
 * Class UploadMedia
 * @package Magetop\Advancepreview\Controller\Adminhtml\ProductPreview
 */
class Save extends \Magetop\Advancepreview\Controller\Adminhtml\PreviewDailyShipping
{
    /**
     *
     */
    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 $previewDailyShippingFactory;
    protected $resPreviewDailyShippingFactory;
    protected $colPreviewDailyShippingFactory;

    public function __construct(
        Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
        \Magento\Framework\Filesystem\Driver\File $file,
        \Magetop\Advancepreview\Model\AdvancePreviewFileFactory $advancePreviewFileFactory,
        \Magetop\Advancepreview\Model\AdvancePreviewUploadFactory $advancePreviewUploadFactory,
        \Magetop\Advancepreview\Model\PreviewDailyShippingFactory $previewDailyShippingFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewDailyShippingFactory $resPreviewDailyShippingFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewDailyShipping\CollectionFactory $colPreviewDailyShippingFactory
    )
    {
        parent::__construct($context, $jsonFactory);
        $this->file = $file;
        $this->advancePreviewFileFactory = $advancePreviewFileFactory;
        $this->advancePreviewUploadFactory = $advancePreviewUploadFactory;
        $this->previewDailyShippingFactory = $previewDailyShippingFactory;
        $this->resPreviewDailyShippingFactory = $resPreviewDailyShippingFactory;
        $this->colPreviewDailyShippingFactory = $colPreviewDailyShippingFactory;

    }

    /**
     * @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['daily_shipping_id']) ? (int)$params['daily_shipping_id'] : 0;
        $productId =  isset($params['product_id']) ? (int)$params['product_id'] : 0;
        $amount = isset($params['amount']) ? $params['amount'] : 0;
        $dataSave = array(
            'product_id'=>$productId,
            'amount'=>$amount,
            'status'=>1,
            'delivery_type'=>$params['delivery_type'],
            'delivery_days'=>$params['delivery_days'],
            'created_at'=>date('Y-m-d H:i:s')
        );
        if($id > 0) {
            $dataSave['daily_shipping_id'] = $id;
            unset($dataSave['created_at']);
        }
        $isSave = $this->saveCustomItem($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\PreviewDailyShipping')->setTemplate('Magetop_Advancepreview::product/daily_shipping/items.phtml')->toHtml();

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