<?php

use KupShop\AdminBundle\AdminList\BaseList;

class ParametersList extends BaseList
{
    use AdminListSortable;

    protected $template = 'listSortable.tpl';

    protected $tableName = 'parameters';
    protected $showMassEdit = true;

    protected $pageDivide = 1000;
    protected ?string $tableAlias = 'par';

    protected $tableDef = [
        'id' => 'id',
        'fields' => [
            'parameter' => ['translate' => true, 'field' => 'par.name', 'spec' => 'par.name', 'fieldType' => BaseList::TYPE_STRING, 'class' => 'text-bold'],
            'name_frontend' => ['translate' => true, 'field' => 'par.name_frontend', 'spec' => 'par.name_frontend', 'fieldType' => BaseList::TYPE_STRING],
            'unit' => ['translate' => true, 'field' => 'par.unit', 'spec' => 'par.unit', 'fieldType' => BaseList::TYPE_STRING],
            'value_type' => ['translate' => true, 'field' => 'par.value_type', 'spec' => 'par.value_type', 'render' => 'renderType', 'fieldType' => ParametersList::TYPE_LIST],
            'uses_count' => ['translate' => true, 'field' => 'cnt'],
            'ID' => ['field' => 'id', 'wpjAdmin' => true],
            'description' => ['translate' => true, 'field' => 'par.descr', 'spec' => 'par.descr', 'visible' => 'N', 'fieldType' => BaseList::TYPE_STRING],
            'figure' => ['translate' => true, 'field' => 'par.figure', 'spec' => 'par.figure', 'visible' => 'N', 'fieldType' => BaseList::TYPE_BOOL],
        ],
    ];

    public function customizeTableDef($tableDef)
    {
        $tableDef = parent::customizeTableDef($tableDef);

        if (!getVal('name')) {
            $tableDef['fields'] = array_merge(
                ['Pořadí' => ['field' => 'position', 'render' => 'renderPosition', 'class' => 'overflow-visible', 'size' => '70px']],
                $tableDef['fields']
            );
        }

        if (findModule(\Modules::PARAMETER_GROUPS)) {
            $tableDef['fields']['Skupiny parametrů'] = ['field' => 'parameter_groups', 'visible' => 'N', 'spec' => function (Query\QueryBuilder $qb) {
                $qbParameterGroups = sqlQueryBuilder()
                    ->select('GROUP_CONCAT(DISTINCT pg.name SEPARATOR ", ")')
                    ->from('parameter_groups', 'pg')
                    ->innerJoin('pg', 'parameter_groups_items', 'pgi', 'pg.id=pgi.id_parameter_group')
                    ->innerJoin('pgi', 'parameter_groups_items_parameters', 'pgip', 'pgi.id=pgip.id_parameter_group_item')
                    ->where('pgip.id_parameter =  par.id');

                $qb->addSubselect($qbParameterGroups, 'parameter_groups');
            }];
        }

        $tableDef['fields']['value_type']['fieldOptions'] = [
            'int' => translate('int', 'parameters'),
            'char' => translate('char', 'parameters'),
            'list' => translate('list', 'parameters'),
        ];

        return $tableDef;
    }

    public function renderType($values, $column)
    {
        $value = $this->getListRowValue($values, $column['field']);
        switch ($value) {
            case 'float':
                $value = 'číslo';
                break;
            case 'char':
                $value = 'text';
                break;
            case 'list':
                $value = 'seznam';
                break;
        }

        return "{$value}";
    }

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

        $item = getVal('moved_item');
        if (!empty($item)) {
            $this->saveList($item, 'parameters');
            exit('{}');
        }
    }

    public function getQuery()
    {
        $qb = sqlQueryBuilder()
            ->select('par.*', 'COUNT(pp.id) as cnt')
            ->from('parameters', 'par')
            ->leftJoin('par', 'parameters_products', 'pp', 'pp.id_parameter = par.id')
            ->groupBy('par.id');

        extract($_GET, EXTR_SKIP | EXTR_REFS);

        if (isset($name) && $name) {
            $qb->where('name LIKE :name')->setParameter('name', '%'.$name.'%');
        }

        return $qb;
    }
}
