<?php


namespace Magetop\Advancepreview\Controller\Adminhtml\PreviewDiscount;


use Magento\Backend\App\Action;

/**
 * Class UploadMedia
 * @package Magetop\Advancepreview\Controller\Adminhtml\ProductPreview
 */
class DellItem 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;
        $isDell = $this->deleteDiscount($discountId);
        if($isDell) {
            $response['status'] = 'success';
            $response['message'] = __('You have deleted item 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 deleteDiscount($discountId) {
        $model = $this->previewDiscountFactory->create();
        $model->setId($discountId);
        try {
            $this->resPreviewDiscountFactory->create()->delete($model);
            return true;
        }
        catch (\Exception $exception) {
        }
        return false;
    }
}
