<?php
/**
 * Quote Delete from buyer end.
 *
 * @category  Magetop Extensions
 * @package   Magetop_Quotesystem
 * @author    Magetop Developer
 * @copyright Copyright (c) Magetop (https://www.magetop.com)
 * @license   https://www.magetop.com/license.html
 */

namespace Magetop\Quotesystem\Controller\Buyerquote;

use Magento\Framework\App\Action\Action;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\Context;
use Magetop\Quotesystem\Api\QuoteRepositoryInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Message\ManagerInterface;

class RemoveQuoteItem extends \Magento\Customer\Controller\AbstractAccount
{
    /**
     * @var Magetop\Quotesystem\Api\QuoteRepositoryInterface
     */
    protected $quoteRepository;
     
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    /**
     * @var ResultFactory
     */
    protected $resultFactory;

    /**
     * @var ManagerInterface
     */
    protected $messageManager;

    /**
     * @param Context                  $context
     * @param QuoteRepositoryInterface $quoteRepository
     * @param \Psr\Log\LoggerInterface $logger
     * @param ResultFactory            $resultFactory
     * @param ManagerInterface         $messageManager
     */
    public function __construct(
        Context $context,
        QuoteRepositoryInterface $quoteRepository,
        \Psr\Log\LoggerInterface $logger,
        ResultFactory $resultFactory,
        ManagerInterface $messageManager
    ) {
        $this->_quoteRepository = $quoteRepository;
        $this->logger = $logger;
        $this->resultFactory = $resultFactory;
        $this->_messageManager = $messageManager;
        parent::__construct(
            $context
        );
    }

    /**
     * Delete pending quote from model.
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        $itemId = (int)$this->getRequest()->getParam('id');
        try {
            $this->_quoteRepository->deleteById($itemId);
            $this->messageManager->addSuccess(__("Pending Quote successfully deleted."));
            $viewQuotePath = 'quotesystem/buyerquote/viewquotes/';
            return $this->resultRedirectFactory->create()->setPath($viewQuotePath, ['_current' => true]);
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->logger->critical($e);
            throw json_encode($e->getMessage());
        } catch (\Exception $e) {
            $this->logger->critical($e);
            throw json_encode($e->getMessage());
        }
    }
}
