<?php

declare(strict_types=1);

namespace KupShop\BonusProgramBundle\Admin\lists;

use KupShop\AdminBundle\AdminList\BaseList;

class BonusProgramExchangeList extends BaseList
{
    use \AdminListSortable;

    protected $template = 'listSortable.tpl';

    protected $tableName = 'bonus_points_exchange';

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

        $tableDef['id'] = 'id';
        $tableDef['fields'] = [
            'position' => ['field' => 'position', 'render' => 'renderPosition', 'size' => '70px', 'translate' => true],
            'ID' => ['field' => 'id', 'size' => 0.3, 'visible' => 'N'],
            'name' => ['field' => 'name', 'translate' => true],
            'requiredPoints' => ['field' => 'points', 'translate' => true],
            'active' => ['field' => 'active', 'translate' => true, 'render' => 'renderBoolean'],
            'dateCreated' => ['field' => 'date_created', 'translate' => true, 'render' => 'renderDate'],
        ];

        return $tableDef;
    }

    public function getQuery()
    {
        return sqlQueryBuilder()
            ->select('*')
            ->from($this->getTableName())
            ->orderBy('position');
    }

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

        $item = getVal('moved_item');

        if (!empty($item)) {
            $this->saveList($item, $this->getTableName());
            exit('{}');
        }
    }
}

return BonusProgramExchangeList::class;
