<?php


namespace Magetop\Advancepreview\Controller\Adminhtml\PreviewDailyShipping;


use Magento\Backend\App\Action;

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