<?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\Block\Adminhtml\Order;

use Magento\Backend\Block\Template;
use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Framework\Json\Helper\Data as JsonHelper;

/**
 * Class PreviewOrderTracking
 * @package Magetop\Advancepreview\Block\Adminhtml\Order
 */
class PreviewOrderTracking extends Template
{
    /**
     * @var \Magetop\Advancepreview\Model\ResourceModel\PreviewDiscount\PreviewOrderTrackingFactory
     */
    protected $colPreviewOrderTrackingFactory;


    public function __construct(
        Template\Context $context,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewOrderTracking\CollectionFactory $colPreviewOrderTrackingFactory,
        array $data = [],
        JsonHelper $jsonHelper = null,
        DirectoryHelper $directoryHelper = null
    )
    {
        parent::__construct($context, $data, $jsonHelper, $directoryHelper);
        $this->colPreviewOrderTrackingFactory = $colPreviewOrderTrackingFactory;
    }

    /**
     * @param int $itemId
     * @return mixed
     */
    public function getOrderTrackingItems($itemId = 0) {
        $collection = $this->colPreviewOrderTrackingFactory->create()
            ->addFieldToFilter('item_id',$itemId);
//        echo (string)$collection->getSelect();
        return $collection;
    }

    /**
     * @param int $id
     * @param int $orderId
     * @param int $itemId
     * @param int $productId
     * @return array
     */
    public function getOrderTrackingItem($id = 0, $orderId = 0, $itemId = 0, $productId = 0) {
        $collection = $this->colPreviewOrderTrackingFactory->create()
            ->addFieldToFilter('preview_tracking_id',$id);
        $tracking = [
            'preview_tracking_id'=>0,
            'order_id'=>$orderId,
            'product_id'=>$productId,
            'item_id'=>$itemId,
            'status'=>1,
            'tracking_number'=>'',
            'tracking_link'=>'',
            'courier_company'=>'',
            'created_at'=>date('Y-m-d H:i:s'),
            'updated_at'=>date('Y-m-d H:i:s'),
        ];
        if($collection && $collection->getSize()) {
            $tracking = $collection->getFirstItem->getData();
        }
        return $tracking;
    }
}
