<?php


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

use Magento\Backend\Block\Template;

/**
 * Class PreviewDiscount
 * @package Magetop\Advancepreview\Block\Adminhtml\Product\Edit
 */
class PreviewDiscount extends Template
{
    /**
     * @var \Magetop\Advancepreview\Helper\Data
     */
    protected $previewHelper;
    /**
     * @var \Magetop\Advancepreview\Model\ResourceModel\PreviewDiscount\CollectionFactory
     */
    protected $colPreviewDiscountFactory;

    /**
     * PreviewDiscount constructor.
     * @param Template\Context $context
     * @param \Magetop\Advancepreview\Helper\Data $previewHelper
     * @param \Magetop\Advancepreview\Model\ResourceModel\PreviewDiscount\CollectionFactory $colPreviewDiscountFactory
     * @param array $data
     */
    public function __construct(
        Template\Context $context,
        \Magetop\Advancepreview\Helper\Data $previewHelper,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewDiscount\CollectionFactory $colPreviewDiscountFactory,
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->previewHelper = $previewHelper;
        $this->colPreviewDiscountFactory = $colPreviewDiscountFactory;
    }

    /**
     * @param $discountId
     * @param $productId
     * @return array
     */
    public function getDiscountById($discountId, $productId) {
        $collection = $this->colPreviewDiscountFactory->create()
            ->addFieldToSelect(array('*'))
            ->addFieldToFilter('discount_id',$discountId);
//        echo (string)$collection->getSelect();
        $discountData = [
            'discount_id'=>0,
            'product_id'=>$productId,
            'discount_title'=>'',
            'discount_price'=>0,
            'discount_amount'=>0,
            'created_at'=>date('Y-m-d H:i:s')
        ];
        if($collection && $collection->getSize() > 0) {
            $discountData = $collection->getFirstItem()->getData();
        }
        return $discountData;
    }

    /**
     * @param int $productId
     * @return mixed
     */
    public function getDiscountProductId($productId = 0) {
        $collection = $this->colPreviewDiscountFactory->create()
            ->addFieldToSelect('*')
            ->addFieldToFilter('product_id',$productId)
            ->setOrder('discount_id','ASC');
//        echo (string)$collection->getSelect();
        return $collection;
    }

    /**
     * @return array
     */
    public function getAjaxUrl() {
        $saveUrl = $this->previewHelper->getOpdAdminUrl('advancepreview/previewDiscount/save');
        $itemsUrl = $this->previewHelper->getOpdAdminUrl('advancepreview/previewDiscount/items');
        $editUrl = $this->previewHelper->getOpdAdminUrl('advancepreview/previewDiscount/edit');
        $dellUrl = $this->previewHelper->getOpdAdminUrl('advancepreview/previewDiscount/dellItem');
        return [
            'save_url'=>$saveUrl,
            'item_url'=>$itemsUrl,
            'edit_url'=>$editUrl,
            'dell_url'=>$dellUrl,
        ];
    }
}
