<?php


namespace Magetop\Advancepreview\Controller\Adminhtml\PreviewDiscount;


use Magento\Backend\App\Action;

/**
 * Class UploadMedia
 * @package Magetop\Advancepreview\Controller\Adminhtml\ProductPreview
 */
class Save extends \Magetop\Advancepreview\Controller\Adminhtml\ProductPreview
{
    /**
     *
     */
    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 $previewDiscountFactory;
    protected $resPreviewDiscountFactory;
    protected $colPreviewDiscountFactory;

    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\PreviewDiscountFactory $previewDiscountFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewDiscountFactory $resPreviewDiscountFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewDiscount\CollectionFactory $colPreviewDiscountFactory
    )
    {
        parent::__construct($context, $jsonFactory);
        $this->file = $file;
        $this->advancePreviewFileFactory = $advancePreviewFileFactory;
        $this->advancePreviewUploadFactory = $advancePreviewUploadFactory;
        $this->previewDiscountFactory = $previewDiscountFactory;
        $this->resPreviewDiscountFactory = $resPreviewDiscountFactory;
        $this->colPreviewDiscountFactory = $colPreviewDiscountFactory;

    }

    /**
     * @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();
        $discountId = isset($params['discount_id']) ? (int)$params['discount_id'] : 0;
        $productId =  isset($params['product_id']) ? (int)$params['product_id'] : 0;
        $price =  isset($params['discount_price']) ? $params['discount_price'] : 0;
        $amount = isset($params['discount_amount']) ? $params['discount_amount'] : 0;
        $title = isset($params['discount_title']) ? $params['discount_title'] : '';
        $dataSave = array(
            'product_id'=>$productId,
            'discount_title'=>$title,
            'discount_price'=>$price,
            'discount_amount'=>$amount,
            'created_at'=>date('Y-m-d H:i:s')
        );
        if($discountId > 0) {
            $dataSave['discount_id'] = $discountId;
            unset($dataSave['created_at']);
        }
        $isSave = $this->saveDiscount($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\PreviewDiscount')->setTemplate('Magetop_Advancepreview::product/discount/items.phtml')->toHtml();

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