<?php

declare(strict_types=1);

namespace External\PompoBundle\Admin\lists;

use KupShop\KupShopBundle\Util\HtmlBuilder\HTML;
use Query\QueryBuilder;

class OrderDiscountsGeneratedCouponsList extends \KupShop\OrderDiscountBundle\Admin\lists\OrderDiscountsGeneratedCouponsList
{
    public function customizeTableDef($tableDef)
    {
        $tableDef = parent::customizeTableDef($tableDef);

        $tableDef['fields']['Uplatněno na prodejně'] = [
            'field' => 'sellerUsed',
            'spec' => function (QueryBuilder $qb) {
                $qb->addSelect('(SELECT IF(JSON_VALUE(s.data, "$.branchId") IS NOT NULL, CONCAT("Pompo", " - ", JSON_VALUE(s.data, "$.branchId"), " ", s.title), "") FROM sellers s WHERE s.id = JSON_VALUE(dc.data, "$.sellerId")) as sellerUsed')
                    ->addSelect('JSON_VALUE(dc.data, "$.sellerId") as sellerId');
            },
            'render' => 'renderSellerUsed',
            'visible' => 'Y',
        ];

        return $tableDef;
    }

    public function renderSellerUsed($values, $column): HTML
    {
        return HTML::create('a')
                ->attr('href', 'javascript:nw(\'sellers\', \''.$values['sellerId'].'\', \'\')')
                ->text($values['sellerUsed'])
                ->end();
    }
}

return OrderDiscountsGeneratedCouponsList::class;
