<?php

declare(strict_types=1);

namespace KupShop\AdminBundle\Admin\Actions;

use Query\Operator;

class StockInToggleClosedAction extends AbstractAction implements IAction
{
    public function getTypes(): array
    {
        return ['stockIn'];
    }

    public function getName(): string
    {
        if (!isset($this->dialogData['closed'])) {
            return 'Přpnutí naskladnění';
        }

        return $this->dialogData['closed'] == '1' ? 'Otevřít naskladění' : 'Uzavřít naskladnění';
    }

    public function execute(&$data, array $config, string $type): ActionResult
    {
        $currentStatus = sqlQueryBuilder()->select('closed')->from('stock_in')->where(Operator::equals(['id' => $this->getId()]))->execute()->fetchOne();

        sqlQueryBuilder()->update('stock_in')
            ->directValues(['closed' => $currentStatus == '1' ? 0 : 1])
            ->where(Operator::equals(['id' => $this->getId()]))
            ->execute();

        return new ActionResult(true, $this->dialogData['closed'] == '1' ? 'Naskladnění otevřeno.' : 'Naskladění uzavřeno.');
    }

    public function isEnabled()
    {
        return findModule(\Modules::STOCK_IN);
    }

    public function hasDialog()
    {
        return false;
    }

    public function isVisible()
    {
        return getVal('acn') == 'edit';
    }
}
