<?php

namespace KupShop\ElninoBundle\Admin\Tabs;

use KupShop\AdminBundle\Admin\WindowTab;
use KupShop\KupShopBundle\Util\StringUtil;
use Query\Operator;

class AbraTab extends WindowTab
{
    protected $title = 'flapAbra';

    protected $template = 'window/tabs/abraTab.tpl';

    public function getLabel()
    {
        return 'Abra';
    }

    public static function getTypes()
    {
        return [
            'deliveryDelivery' => 99,
            'deliveryPayment' => 99,
        ];
    }

    public function getVars($smarty_tpl_vars)
    {
        $vars = [];

        $type = $smarty_tpl_vars['type'];
        $id = '';
        $edit_type = $type;

        switch ($type) {
            case 'products':
                $isMaster = StringUtil::startsWith($smarty_tpl_vars['body']->value['data']['code'] ?? '', 'master_');
                if ($isMaster) {
                    $id = sqlQueryBuilder()->select('id_abra')
                    ->from('abra_masters')
                    ->where(Operator::equalsNullable(['id_master' => $this->getId()]))->execute()->fetchColumn();
                } else {
                    $id = sqlQueryBuilder()->select('code')
                    ->from('products')
                    ->where(Operator::equals(['id' => $this->getId()]))->execute()->fetchColumn();
                    $edit_type = null;
                }
                break;

            case 'deliveryDelivery':
                $id = sqlQueryBuilder()->select('id_abra')
                    ->from('abra_deliveries')
                    ->where(Operator::equals(['id_delivery' => $this->getId()]))->execute()->fetchColumn();
                break;

            case 'deliveryPayment':
                $id = sqlQueryBuilder()->select('id_abra')
                    ->from('abra_payments')
                    ->where(Operator::equals(['id_payment' => $this->getId()]))->execute()->fetchColumn();
                break;

            case 'sections':
                $id = sqlQueryBuilder()->select('id_abra')
                    ->from('abra_sections')
                    ->where(Operator::equals(['id_section' => $this->getId()]))->execute()->fetchColumn();
                break;
        }

        $vars['abra_id'] = $id;
        $vars['abra_edit'] = $edit_type;

        return $vars;
    }

    public function handleUpdate()
    {
        parent::handleUpdate();

        $data = $this->getData();
        switch ($data['abra_edit'] ?? '') {
            case 'deliveryDelivery':
                sqlQuery('REPLACE INTO abra_deliveries (id_abra, id_delivery) VALUES (:id_abra, :id_delivery)',
                    ['id_abra' => $data['id_abra'], 'id_delivery' => $this->getID()]
                );
                break;

            case 'deliveryPayment':
                sqlQuery('REPLACE INTO abra_payments (id_abra, id_payment) VALUES (:id_abra, :id_payment)',
                    ['id_abra' => $data['id_abra'], 'id_payment' => $this->getID()]
                );
                break;

            case 'products':
                if (!empty($data['id_abra'])) {
                    sqlQuery('REPLACE INTO abra_masters (id_abra, id_master) VALUES (:id_abra, :id_master)',
                        ['id_abra' => $data['id_abra'], 'id_master' => $this->getID()]
                    );
                }
        }
    }
}
