<?php


namespace Magetop\Advancepreview\Controller\Adminhtml\PreviewAdditionalTab;


use Magento\Backend\App\Action;

class Items extends \Magetop\Advancepreview\Controller\Adminhtml\PreviewAdditionalTab
{
    protected $previewAdditionalFactory;
    protected $resPreviewAdditionalFactory;
    protected $colPreviewAdditionalFactory;
    public function __construct(
        Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
        \Magetop\Advancepreview\Model\PreviewAdditionalFactory $previewAdditionalFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewAdditionalFactory $resPreviewAdditionalFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewAdditional\CollectionFactory $colPreviewAdditionalFactory
    )
    {
        parent::__construct($context, $jsonFactory);
        $this->previewAdditionalFactory = $previewAdditionalFactory;
        $this->resPreviewAdditionalFactory = $resPreviewAdditionalFactory;
        $this->colPreviewAdditionalFactory = $colPreviewAdditionalFactory;
    }

    public function execute()
    {
        $response = [
            'status'=>'error',
            'html'=>'',
            'message'=>'Can not get data'
        ];
        try {
            $productId = (int)$this->getRequest()->getParam('product_id',0);
            if($productId > 0 && !$this->checkAdditionalProductId($productId)) {
                $this->generateSampleData($productId);
            }
            $response['html'] = $this->_view->getLayout()->createBlock('Magetop\Advancepreview\Block\Adminhtml\Product\Edit\PreviewAdditionalTab')->setTemplate('Magetop_Advancepreview::product/addition_tab/items.phtml')->toHtml();
            $response['status'] = 'success';
            $response['message'] = __('Get Item success');
        }
        catch (\Exception $exception) {
            $response['message'] = $exception->getMessage();
        }
        return $this->getAjaxResponse($response);
    }
    protected function checkAdditionalProductId($productId = 0) {
        $collection = $this->colPreviewAdditionalFactory->create()
            ->addFieldToSelect('addition_id')
            ->addFieldToFilter('product_id',$productId)
            ->setPageSize(1)
            ->setOrder('addition_id','ASC');
        // echo (string)$collection->getSelect();
        return $collection && $collection->getSize() > 0 ? true : false;
    }
    protected function generateSampleData($productId) {
        $createData = date('Y-m-d H:i:s');
        $sampleItems  = [
            [
                'product_id'=>$productId,
                'title'=>'5C transparent',
                'status'=>1,
                'price'=>0,
                'description'=>'5C transparent',
                'created_at'=>$createData,
            ],
            [
                'product_id'=>$productId,
                'title'=>'Quality check',
                'status'=>1,
                'price'=>0,
                'description'=>'Quality check check',
                'created_at'=>$createData,
            ],
            [
                'product_id'=>$productId,
                'title'=>'Design service',
                'status'=>1,
                'price'=>0,
                'description'=>'Design service',
                'created_at'=>$createData,
            ],
        ];
        foreach ($sampleItems as $sampleItem) {
            $this->saveSampleDataItem($sampleItem);
        }
    }
    protected function saveSampleDataItem($dataSave) {
        $model = $this->previewAdditionalFactory->create();
        $model->setData($dataSave);
        try {
            $this->resPreviewAdditionalFactory->create()->save($model);
        }
        catch (\Exception $exception) {

        }
    }
}
