<?php
/**
 * Block for Quote list at customer 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\Block;

use Magetop\Quotesystem\Model\ResourceModel\Quotes\CollectionFactory;
use Magento\Framework\Pricing\Helper\Data;
use Magetop\Quotesystem\Model\Quotes;
use Magetop\Quotesystem\Helper\Data as Helper;

class ViewQuotes extends \Magento\Framework\View\Element\Template
{
    /**
     * @var customerSession
     */
    protected $_customerSession;
    /**
     * @var quoteCollectionFacory
     */
    protected $_quoteCollectionFactory;
    /**
     * @var quoteCollection
     */
    protected $_quoteCollection;
    /**
     * @var pricingHelper
     */
    protected $_pricingHelper;
    /**
     * @var Magetop\Quoetsystem\Model\Quotes
     */
    protected $_quotesModel;

    /**
     * @var $nameClass
     */
    public $nameClass = "fa fa-sort";

    /**
     * @var $quantityClass
     */
    public $quantityClass = "fa fa-sort";

    /**
     * @var $priceClass
     */
    public $priceClass = "fa fa-sort";

    /**
     * @var Helper
     */
    protected $helper;

    /**
     * @var Context
     */
    protected $storeManager;

    /**
     * @var \Magento\Framework\App\Response\RedirectInterface
     */
    protected $redirect;

    /**
     * @var \Magento\Framework\App\Response\Http
     */
    protected $response;

    /**
     * Construct
     *
     * @param MagentoCustomerModelSession                       $customerSession
     * @param MagentoCatalogBlockProductContext                 $context
     * @param CollectionFactory                                 $quotesCollectionFactory
     * @param Data                                              $pricingHelper
     * @param Quotes                                            $quotesModel
     * @param Helper                                            $helper
     * @param \Magento\Framework\Json\Helper\Data               $jsonHelper
     * @param \Magento\Framework\App\Response\RedirectInterface $redirect
     * @param \Magento\Framework\App\Response\Http              $response
     * @param [type]                                            $data
     */
    public function __construct(
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Catalog\Block\Product\Context $context,
        CollectionFactory $quotesCollectionFactory,
        Data $pricingHelper,
        Quotes $quotesModel,
        Helper $helper,
        \Magento\Framework\Json\Helper\Data $jsonHelper,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Framework\App\Response\Http $response,
        array $data = []
    ) {
        $this->_customerSession = $customerSession;
        $this->_quoteCollectionFactory = $quotesCollectionFactory;
        $this->_pricingHelper = $pricingHelper;
        $this->_quotesModel = $quotesModel;
        $this->_storeManager =  $context->getStoreManager();
        $this->helper = $helper;
        $this->jsonHelper = $jsonHelper;
        $this->redirect = $redirect;
        $this->response = $response;
        parent::__construct($context, $data);
    }
    
    /**
     * Customer Id by customer session
     *
     * @return int
     */
    public function getCustomerId()
    {
        return $this->_customerSession->getCustomerId();
    }
    /**
     * Get Collection of quotes
     *
     * @return collection
     */
    public function getQuotesCollection()
    {
        $page = 1;
        $limit = 10;
        if ($proName = $this->getRequest()->getParam('limit')) {
            $limit = $proName != "" ? $proName : 10;
        }
        if ($proName = $this->getRequest()->getParam('p')) {
            $page = $proName != "" ? $proName : 1;
        }
        if (!$this->_quoteCollection) {
            $collection = $this->_quoteCollectionFactory
                ->create()
                ->addFieldToSelect('entity_id')
                ->addFieldToSelect('product_id')
                ->addFieldToSelect('product_name')
                ->addFieldToSelect('product_price')
                ->addFieldToSelect('quote_price')
                ->addFieldToSelect('quote_qty')
                ->addFieldToSelect('quote_currency')
                ->addFieldToSelect('status')
                ->addFieldToSelect('created_at')
                ->addFieldToFilter(
                    'main_table.customer_id',
                    $this->getCustomerId()
                )->setPageSize($limit) // only get 10 products
                ->setCurPage($page);
            $collection->getSelect()
                ->join(
                    ["wqd" => $collection->getTable("mt_quote_details")],
                    "wqd.entity_id = main_table.quote_id AND wqd.quote_generate = 0",
                    ['en_id' => 'entity_id']
                );
        }
        return $collection;
    }
     
    /**
     * Get formatted price by currency
     *
     * @param string $price
     * @return format price string
     */
    public function getFormattedPrice($price)
    {
        $fromCurrency = $this->helper->getCurrentCurrencyCode();
        $toCurrency = $this->helper->getBaseCurrencyCode();
        $amount = $this->helper->getmtconvertCurrency($fromCurrency, $toCurrency, $price);
        return $this->_pricingHelper
            ->currency($amount, true, false);
    }
  
    /**
     * Use to get current url.
     */
    public function getCurrentUrl()
    {
        // Give the current url of recently viewed page
        return $this->_urlBuilder->getCurrentUrl();
    }

    /**
     * Check whether a quote is sold or not?
     *
     * @param  int $QStatus
     * @return boolean
     */
    public function quoteStatusIsNotSold($QStatus)
    {
        if ($QStatus!=\Magetop\Quotesystem\Model\Quotes::STATUS_SOLD) {
            return true;
        }
        return false;
    }

    /**
     * GetIsSecure
     */
    public function getIsSecure()
    {
        return $this->getRequest()->isSecure();
    }

    /**
     * Get Configuration Value
     *
     * @return $values
     */
    public function getConfigValue()
    {
        return $this->helper->getConfigValues();
    }

    /**
     * Get Base URL
     */
    public function getBaseUrl()
    {
        return $this->getBaseUrl();
    }

    /**
     * Get Helper
     *
     * @return Magetop\QuoteSystem\Helper\Data
     */
    public function getHelper()
    {
        return $this->helper;
    }

    /**
     * Get Json Helper
     *
     * @return Magento\Framework\Json\Helper\Data
     */
    public function getJsonHelper()
    {
        return $this->jsonHelper;
    }

    /**
     * Get QuoteId
     */
    public function getQuoteId()
    {
        return $this->_customerSession->getSelectedQuoteId();
    }

    /**
     * Prepare layout
     *
     * @return $this
     */
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        if ($this->getQuotesCollection()->getData() || $this->getRequest()->getParams('status')) {
            $pager = $this->getLayout()->createBlock(
                \Magento\Theme\Block\Html\Pager ::class,
                'quotesystem.pager'
            )
                ->setCollection(
                    $this->getQuotesCollection()
                );
            $this->setChild('pager', $pager);
            $this->getQuotesCollection()->load();
        }
        return $this;
    }

    /**
     * Get pager Html
     *
     * @return string
     */
    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }
}
