<?php
/******************************************************************************
 *   ______                                                                   *
 *  /       \                                                                 *
 * /$$$$$$  | ________  __    __   ______    ______                           *
 * $$ |__$$ |/        |/  |  /  | /      \  /      \                          *
 * $$    $$ |$$$$$$$$/ $$ |  $$ |/$$$$$$  |/$$$$$$  |                         *
 * $$$$$$$$ |  /  $$/  $$ |  $$ |$$ |  $$/ $$    $$ |                         *
 * $$ |  $$ | /$$$$/__ $$ \__$$ |$$ |      $$$$$$$$/                          *
 * $$ |  $$ |/$$      |$$    $$/ $$ |      $$       |                         *
 * $$/   $$/ $$$$$$$$/  $$$$$$/  $$/        $$$$$$$/                          *
 *                                                                            *
 * @PROJECT    : 3D Preview Extension [Magetop.com]                           *
 * @AUTHOR     : Azure - Developer                                            *
 * @COPYRIGHT  : © 2019  Magetop.com                                          *
 * @LINK       : https://Magetop.com                                          *
 * @CREATED    :  10/01/2022                                                  *
 ******************************************************************************/

namespace Magetop\Advancepreview\Ui\Component\Orders\Listing\Column;
use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;

class CustomInvoiceGrandTotal extends Column
{
    protected $_orderRepository;
    protected $_searchCriteria;
    protected $dailyShippingFactory;
    protected $colPreviewOrderItemsFactory;
    protected $priceHelper;
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        OrderRepositoryInterface $orderRepository,
        SearchCriteriaBuilder $criteria,
        \Magento\Framework\Pricing\Helper\Data $priceHelper,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewDailyShipping\CollectionFactory $dailyShippingFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewOrderItems\CollectionFactory $colPreviewOrderItemsFactory,
        array $components = [],
        array $data = []
    ) {
        $this->_orderRepository = $orderRepository;
        $this->_searchCriteria  = $criteria;
        $this->dailyShippingFactory = $dailyShippingFactory;
        $this->colPreviewOrderItemsFactory = $colPreviewOrderItemsFactory;
        $this->priceHelper = $priceHelper;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $orderId = isset($item['order_id']) ? (int)$item['order_id'] : 0;
                if($orderId <= 0) {
                    continue;
                }

                $order  = $this->_orderRepository->get($orderId);
                $priceItem = $this->getCustomProjectNameInOrder($order);
                $item[$this->getData('name')] = $this->priceHelper->currency($priceItem,true,false);
            }
        }

        return $dataSource;
    }

    public function getCustomProjectNameInOrder(
        \Magento\Sales\Model\Order $order
    ) {
        $items = $order->getAllItems();
        $priceItem = 0;
        foreach ($items as $item) {
            $infoRequest = $item->getProductOptionByCode('info_buyRequest');
            if (isset($infoRequest['txt_project_final_price'])) {
                $priceItem += $infoRequest['txt_project_final_price'];
            }

        }
        return $priceItem;
    }
    protected function getDailyShippingById($dailyId = 0) {
        if($dailyId > 0) {
            $collection = $this->dailyShippingFactory->create()
                ->addFieldToFilter('daily_shipping_id',$dailyId);
            return $collection && $collection->getSize() > 0 ? $collection->getFirstItem() : null;
        }
        return  null;
    }
    protected function getCustomPreviewOrderItem($itemId) {
        $collection = $this->colPreviewOrderItemsFactory->create()
            ->addFieldToFilter('item_id',$itemId);
        // echo (string)$collection->getSelect(). '--';
        return $collection && $collection->getSize() > 0 ? $collection->getFirstItem() : null;
    }
    protected function getOrderItemByInvoiceId($invoiceId) {
        $collection = $this->colPreviewOrderItemsFactory->create()
            ->addFieldToFilter('invoice_id',$invoiceId);
        $price = 0;
        $customItem = $collection && $collection->getSize() > 0 ? $collection->getFirstItem() : null;
        if($customItem) {
            $infoRequest = $customItem->getData('info_request');
            if($infoRequest != '') {
                $infoRequest = json_decode($infoRequest);
                if (isset($infoRequest['txt_project_final_price'])) {
                    $price = $infoRequest['txt_project_final_price'];
                }

            }
        }
        return $price;
    }

}
