<?php

use KupShop\AdminBundle\AdminList\BaseList;

class ProductsChargesList extends BaseList
{
    protected $showMassEdit = true;
    protected $tableName = 'charges';
    protected ?string $tableAlias = 'ch';

    protected $tableDef = [
        'id' => 'id',
        'fields' => [
            'name' => ['field' => 'ch.admin_title', 'size' => 1, 'translate' => true, 'spec' => 'ch.admin_title', 'fieldType' => BaseList::TYPE_STRING],
            'display_name' => ['field' => 'ch.title', 'size' => 2, 'translate' => true, 'spec' => 'ch.title', 'fieldType' => BaseList::TYPE_STRING],
            'type' => ['field' => 'ch.type', 'size' => 1, 'translate' => true, 'render' => 'renderType', 'fieldType' => ProductsChargesList::TYPE_LIST],
            'descr' => ['field' => 'ch.descr', 'visible' => 'N', 'size' => 3, 'translate' => true, 'spec' => 'ch.descr', 'fieldType' => BaseList::TYPE_STRING],
            'price' => ['field' => 'ch.price', 'render' => 'renderPriceVat', 'size' => 0.7, 'translate' => true, 'spec' => 'ch.price', 'fieldType' => BaseList::TYPE_PRICE],
            'vat' => ['field' => 'ch.vat', 'render' => 'renderVat', 'size' => 0.7, 'translate' => true, 'spec' => 'ch.vat', 'fieldType' => ProductsChargesList::TYPE_LIST],
            'charge_required' => ['field' => 'ch.required', 'render' => 'renderBoolean', 'size' => 0.7, 'translate' => true, 'spec' => 'ch.required', 'fieldType' => BaseList::TYPE_BOOL],
            'charge_included' => ['field' => 'ch.included', 'render' => 'renderBoolean', 'size' => 0.7, 'translate' => true, 'spec' => 'ch.included', 'fieldType' => BaseList::TYPE_BOOL, 'visible' => 'N'],
            'charge_checked' => ['field' => 'ch.checked', 'render' => 'renderBoolean', 'size' => 0.7, 'translate' => true, 'spec' => 'ch.checked', 'fieldType' => BaseList::TYPE_BOOL, 'visible' => 'N'],
        ],
    ];

    protected function getVats()
    {
        return sqlQueryBuilder()
            ->select('id', 'vat')
            ->from('vats')
            ->execute()
            ->fetchAll();
    }

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

        $tableDef['fields']['type']['fieldOptions'] = [
            'product' => translate('type_product'),
            'order' => translate('type_order'),
        ];

        $vats = $this->getVats();

        foreach ($vats as $vat) {
            $tableDef['fields']['vat']['fieldOptions'][(int) $vat['id']] = $vat['vat'].'%';
        }

        return $tableDef;
    }

    public function renderVat($values, $column)
    {
        $value = $this->getListRowValue($values, $column['field']);
        if ($value) {
            return getVat($value).'%';
        }

        return translate('byProducts', 'vats');
    }

    public function renderType($values, $column)
    {
        $type = $values['type'] ?? '';

        return translate("type_{$type}", 'productsCharges', true);
    }

    public function getQuery()
    {
        $qb = sqlQueryBuilder()->select('*')->from('charges', 'ch');

        return $qb;
    }
}
