<?php

namespace KupShop\PohodaBundle\Admin\Actions;

use KupShop\AdminBundle\Admin\Actions\AbstractAction;
use KupShop\AdminBundle\Admin\Actions\ActionResult;
use KupShop\AdminBundle\Admin\Actions\IAction;
use KupShop\KupShopBundle\Context\LanguageContext;
use Query\Operator;
use Symfony\Component\Routing\Router;

class PrintPohodaInvoice extends AbstractAction implements IAction
{
    /**
     * @var LanguageContext
     */
    protected $languageContext;

    public function getTypes(): array
    {
        return [
            'orders',
        ];
    }

    public function getName(): string
    {
        return 'Stáhnout fakturu z Pohody';
    }

    public function execute(&$data, array $config, string $type): ActionResult
    {
        return false;
    }

    public function getVars(): array
    {
        $vars = parent::getVars();

        $idPohodaInvoice = sqlQueryBuilder()
            ->select('id_pohoda_invoice')
            ->from('orders_pohoda_invoices')
            ->where(Operator::equals(['id_order' => $this->getId()]))->execute()->fetchColumn();

        if (!$idPohodaInvoice) {
            $vars['error'] = 'Objednavka nema naparovanou zadnou fakturu.';

            return $vars;
        }

        $order = new \Order($this->getId());

        $vars['pohoda_invoice_url'] = path('kupshop_orderingbundle_pdfinvoice', [
            'id_order' => $this->getId(),
            'cf' => $order->getSecurityCode(),
        ], Router::ABSOLUTE_URL);

        return $vars;
    }

    public function isVisible()
    {
        return in_array($this->dialogData['status'], getStatuses('handled'));
    }

    public function isEnabled()
    {
        return findModule(\Modules::POHODA, \Modules::SUB_INVOICE);
    }

    public function getIcon()
    {
        return 'glyphicon glyphicon-save-file';
    }

    public function getDialogHrefAttributes()
    {
        return 'data-modal-class="modal-translations" data-modal-hide-buttons="true"';
    }
}
