<?php


namespace Magetop\Advancepreview\Controller\Adminhtml\OrderTracking;


use Magento\Backend\App\Action;

/**
 * Class UploadMedia
 * @package Magetop\Advancepreview\Controller\Adminhtml\ProductPreview
 */
class DellItem extends \Magetop\Advancepreview\Controller\Adminhtml\OrderTracking
{
    /**
     *
     */
    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 $previewOrderTrackingFactory;
    protected $resPreviewOrderTrackingFactory;
    protected $colPreviewOrderTrackingFactory;

    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\PreviewOrderTrackingFactory $previewOrderTrackingFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewOrderTrackingFactory $resPreviewOrderTrackingFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewOrderTracking\CollectionFactory $colPreviewOrderTrackingFactory
    )
    {
        parent::__construct($context, $jsonFactory);
        $this->file = $file;
        $this->advancePreviewFileFactory = $advancePreviewFileFactory;
        $this->advancePreviewUploadFactory = $advancePreviewUploadFactory;
        $this->previewOrderTrackingFactory = $previewOrderTrackingFactory;
        $this->resPreviewOrderTrackingFactory = $resPreviewOrderTrackingFactory;
        $this->colPreviewOrderTrackingFactory = $colPreviewOrderTrackingFactory;

    }

    /**
     * @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['id']) ? (int)$params['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\Order\PreviewOrderTracking')->setTemplate('Magetop_Advancepreview::order/tracking/tracking_items.phtml')->toHtml();

        }
        return $this->getAjaxResponse($response);
    }


    protected function deleteDiscount($discountId) {
        $model = $this->previewOrderTrackingFactory->create();
        $model->setId($discountId);
        try {
            $this->resPreviewOrderTrackingFactory->create()->delete($model);
            return true;
        }
        catch (\Exception $exception) {
        }
        return false;
    }
}
