<?php
/**
 * MB "Vienas bitas" (Magetrend.com)
 *
 * @category MageTrend
 * @package  Magetend/PdfTemplates
 * @author   Edvinas Stulpinas <edwin@magetrend.com>
 * @license  http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @link     https://www.magetrend.com/magento-2-pdf-invoice-pro
 */

namespace Magetrend\PdfTemplates\Controller\Adminhtml\Mteditor;

use Magento\Framework\App\Filesystem\DirectoryList;

/**
 * Export action controller class
 *
 * @category MageTrend
 * @package  Magetend/PdfTemplates
 * @author   Edvinas Stulpinas <edwin@magetrend.com>
 * @license  http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @link     https://www.magetrend.com/magento-2-pdf-invoice-pro
 */
class Export extends \Magetrend\PdfTemplates\Controller\Adminhtml\Mteditor
{
    /**
     * Dispatch request
     *
     * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
     * @throws \Magento\Framework\Exception\NotFoundException
     */
    public function execute()
    {
        try {
            $templateId = $this->getRequest()->getParam('id');
            $templateName = $this->getRequest()->getParam('template_name');
            $template = $this->mtEditorManager->initTemplate($templateId);
            $fileName = $template->export($templateName);

            return $this->_jsonResponse([
                'success' => 1,
                'download_link' => $this->getUrl('pdftemplates/mteditor/downloadExport', ['file' => $fileName])
            ]);
        } catch (\Exception $e) {
            return $this->_error($e->getMessage());
        }
    }
}
