<?php

declare(strict_types=1);

namespace External\PompoBundle\Admin\lists;

use Query\Operator;
use Query\QueryBuilder;

class UsersList extends \Admin\Lists\UsersList
{
    public function customizeTableDef($tableDef)
    {
        $tableDef = parent::customizeTableDef($tableDef);

        $tableDef['fields']['Číslo zákazníka'] = [
            'field' => 'id_drs',
            'spec' => function (QueryBuilder $qb) {
                $qb->addSelect('du.id_drs')
                    ->leftJoin('u', 'drs_users', 'du', 'du.id_user = u.id');
            },
            'visible' => 'Y',
        ];

        return $tableDef;
    }

    public function getQuery()
    {
        $qb = parent::getQuery();

        if ($id = getVal('IDu')) {
            $qb->leftJoin('u', 'drs_users', 'du', 'du.id_user = u.id')
                ->orWhere(
                    Operator::equals(['du.id_drs' => $id])
                );
        }

        return $qb;
    }
}

return UsersList::class;
