<?php


namespace Magetop\Advancepreview\Block\Adminhtml\Product\Edit;

use Magento\Backend\Block\Template;
class Form extends Template
{
    protected $previewHelper;
    protected $colPreviewThreeFactory;
    public function __construct(
        Template\Context $context,
        \Magetop\Advancepreview\Helper\Data $previewHelper,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewThree\CollectionFactory $colPreviewThreeFactory,
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->previewHelper = $previewHelper;
        $this->colPreviewThreeFactory = $colPreviewThreeFactory;
    }
    public function getPreviewById($previewId, $productId) {
        $collection = $this->colPreviewThreeFactory->create()
            ->addFieldToSelect(array('*'))
            ->addFieldToFilter('threed_id',$previewId);
        $previewData = [
            'threed_id'=>0,
            'product_id'=>$productId,
            'title'=>'',
            'filename'=>'',
            'created_at'=>date('Y-m-d H:i:s')
        ];
        if($collection && $collection->getSize() > 0) {
            $previewData = $collection->getFirstItem()->getData();
        }
        return $previewData;
    }
    public function getPreviewByProductId($productId = 0) {
        $collection = $this->colPreviewThreeFactory->create()
            ->addFieldToSelect('*')
            ->addFieldToFilter('product_id',$productId)
            ->setOrder('threed_id','DESC');
//        echo (string)$collection->getSelect();
        return $collection;
    }
    public function getAjaxUrl() {
        $uploadUrl = $this->previewHelper->getOpdAdminUrl('advancepreview/productPreview/uploadMedia');
        $itemsUrl = $this->previewHelper->getOpdAdminUrl('advancepreview/productPreview/items');
        $editUrl = $this->previewHelper->getOpdAdminUrl('advancepreview/productPreview/edit');
        return [
            'upload_url'=>$uploadUrl,
            'item_url'=>$itemsUrl,
            'edit_url'=>$editUrl,
        ];
    }
}
