<?php

class LocationsList extends \KupShop\AdminBundle\AdminList\BaseList
{
    use AdminListSortable;

    protected $pageDivide = 9999;

    protected $template = 'listSortable.tpl';

    protected $tableDef = [
        'id' => 'id',
        'fields' => [
            'Pořadí' => ['field' => 'sort_index', 'render' => 'renderPosition', 'size' => '70px'],
            'Lokace' => ['field' => 'code'],
            'Počet pozic' => ['field' => 'positions'],
        ],
    ];

    public function getQuery()
    {
        $qb = sqlQueryBuilder()
            ->select('wl.id, wl.code', 'COUNT(wpos.id) as positions')
            ->from('warehouse_locations', 'wl')
            ->leftJoin('wl', 'warehouse_positions', 'wpos', 'wl.id = wpos.id_location')
            ->groupBy('wl.id');

        return $qb;
    }

    public function handleDrag()
    {
        $this->saveList(getVal('moved_item'), 'warehouse_locations', ['sort_index', 'code']);
        exit;
    }
}
