<?php

declare(strict_types=1);

namespace KupShop\PreordersBundle\Admin\lists;

use Doctrine\DBAL\ParameterType;
use Query;

global $cfg;

require_once $cfg['Path']['shared_version'].'admin/lists/OrdersList.php';

class PreordersOrdersCreatedList extends \OrdersList
{
    public function customizeTableDef($tableDef): array
    {
        $tableDef = parent::customizeTableDef($tableDef);

        foreach ($tableDef['fields'] as &$field) {
            if ($field['translation_section'] ?? false) {
                continue;
            }

            $field['translation_section'] = 'orders';
        }

        return $tableDef;
    }

    public function getFilterQuery(): Query\QueryBuilder
    {
        $qb = parent::getFilterQuery();
        $qb->andWhere("JSON_VALUE(o.note_admin, '$.preorders.id_preorder_date') IS NOT NULL");

        if ($id = getVal('id_preorder_date')) {
            $qb->andWhere("JSON_VALUE(o.note_admin, '$.preorders.id_preorder_date') = :id_preorder_date")
                ->addParameters(['id_preorder_date' => $id], ['id_preorder_date' => ParameterType::INTEGER]);
        }

        return $qb;
    }
}

return PreordersOrdersCreatedList::class;
