<?php

use KupShop\AdminBundle\AdminList\BaseList;
use KupShop\I18nBundle\Admin\Util\ListTranslationsFigureBadges;
use KupShop\I18nBundle\Translations\InfoPanelTranslation;

class InfoPanelList extends BaseList
{
    use ListTranslationsFigureBadges;

    protected $tableName = 'info_panels';
    protected ?string $tableAlias = 'ip';

    protected $tableDef = [
        'id' => 'ip.id',
        'fields' => [
            'ID' => ['field' => 'id', 'translate' => true, 'size' => 0.5],
            'name' => ['field' => 'name', 'translate' => true, 'size' => 1],
            'type' => ['field' => 'type', 'translate' => true, 'size' => 0.5],
            'info_panel_date_from' => ['field' => 'date_from', 'translate' => true, 'size' => 1, 'render' => 'renderDateTime'],
            'info_panel_date_to' => ['field' => 'date_to', 'translate' => true, 'size' => 1, 'render' => 'renderDateTime'],
            'active' => ['field' => 'active', 'translate' => true, 'render' => 'renderBoolean', 'size' => 0.8],
        ],
    ];

    protected $orderParam = [
        'sort' => 'ID',
        'direction' => 'ASC',
    ];

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

        if (findModule(\Modules::TRANSLATIONS)) {
            $tableDef['fields']['translationsFigure'] = $this->getTranslationsFigureField(
                InfoPanelTranslation::class,
                figureField: 'active',
                column: ['translation_section' => 'translations', 'visible' => 'Y'],
            );
        }

        return $tableDef;
    }

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

        if (!empty(getVal('dateFrom'))) {
            $qb->andWhere('date_from >= :filterDateFrom')
                ->setParameter('filterDateFrom', $this->prepareDate(getVal('dateFrom')));
        }

        if (!empty(getVal('dateTo'))) {
            $qb->andWhere('date_to <= :filterDateTo')
                ->setParameter('filterDateTo', $this->prepareDate(getVal('dateTo')));
        }

        return $qb;
    }
}
