<?php

namespace KupShop\AgeVerifyBundle\Admin\Tabs;

use KupShop\AdminBundle\Admin\WindowTab;
use KupShop\AgeVerifyBundle\Utils\AgeVerifyUtil;
use Query\Operator;

class AgeVerifyUsersTab extends WindowTab
{
    protected $title = 'flapAgeVerify';

    protected $template = 'AgeVerifyUsersTab.tpl';

    /** @required */
    public AgeVerifyUtil $ageVerifyUtil;

    public static function getTypes()
    {
        return [
            'users' => 1,
        ];
    }

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

        $data = getVal('age_verification');

        $this->ageVerifyUtil->setVerificationData($data['legal_age'],
            $data['verification_type'],
            null,
            $this->getID());

        // Smazat OAUTH vazbu, když ruším ověření
        if ($data['verification_type'] === '' && findModule(\Modules::USER_OAUTH)) {
            sqlQueryBuilder()->delete('users_provider_ids')
                ->where(Operator::equals(['id_user' => $this->getID(), 'provider' => 'bankid']))
                ->execute();
        }
    }

    public function handleRemoveVerificationOrder()
    {
        sqlQueryBuilder()->update('users_age_verification')
            ->directValues(['id_order' => null])
            ->where(Operator::equals(['id_user' => $this->getID()]))
            ->execute();

        $this->returnOK('Objednávka odpárována.');
    }

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

        $vars['verification_types'] = array_merge([null => '----'], AgeVerifyUtil::$VERIFICATION_TYPES);
        $vars['age_verification'] = $this->selectSQL('users_age_verification', ['id_user' => $this->getID()])->fetchAssociative();

        if (findModule(\Modules::USER_OAUTH)) {
            $vars['oauth'] = $this->selectSQL('users_provider_ids', ['id_user' => $this->getID(), 'provider' => 'bankid'])->fetchAssociative();
        }

        return $vars;
    }

    /**
     * @return string
     */
    public function getLabel()
    {
        return translate($this->title, 'ageVerifyUsersTab');
    }
}
