<?php


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

use Magento\Backend\Block\Template;

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


    public function __construct(
        Template\Context $context,
        \Magetop\Advancepreview\Helper\Data $previewHelper,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewDailyShipping\CollectionFactory $collectionFactory,
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->previewHelper = $previewHelper;
        $this->collectionFactory = $collectionFactory;
    }

    public function getDailyShippingItem($dailyShippingId, $productId) {
        $collection = $this->collectionFactory->create()
            ->addFieldToSelect(array('*'))
            ->addFieldToFilter('daily_shipping_id',$dailyShippingId);
//          echo (string)$collection->getSelect();
        $discountData = [
            'daily_shipping_id'=>0,
            'product_id'=>$productId,
            'amount'=>'',
            'status'=>0,
            'delivery_type'=>'Standard',
            'delivery_days'=>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 getDailyShippingProductId($productId = 0) {
        $collection = $this->collectionFactory->create()
            ->addFieldToSelect('*')
            ->addFieldToFilter('product_id',$productId);
//        echo (string)$collection->getSelect();
        return $collection;
    }

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