<?php

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

class PrintCenterWarehousePositions extends Base
{
    use DatabaseCommunication;
    private $pageVars;

    public function __construct($pageVars)
    {
        $this->pageVars = $pageVars;
    }

    public function get_vars()
    {
        $vars = parent::get_vars();
        $vars = array_merge($this->pageVars, $vars);

        $printLabels = ServiceContainer::getService(\KupShop\AdminBundle\AdminRegister\WarehousePositionsPrintLabelsLocator::class);
        $generic = [];
        foreach ($printLabels->getPrintClasses() as $class) {
            $generic[$class->getLabel()] = $class->getDimensions();
        }

        if (!($printClass = $printLabels->getPrintClass($vars['template_type']))) {
            $printClasses = $printLabels->getPrintClasses();
            $printClass = reset($printClasses);
        }

        $this->setTemplate($printClass->getTemplate());
        $vars['count_rows'] = $generic[$vars['template_type']][1];
        $vars['count_columns'] = $generic[$vars['template_type']][0];

        $qb = sqlQueryBuilder()->select('*')
            ->from('warehouse_positions', 'wp');

        $positions = getVal('positions');
        if ($positions) {
            $qb->andWhere(\Query\Operator::inIntArray($positions, 'id'));
        }

        $location = getVal('location');
        if ($location) {
            $qb->andWhere(\Query\Operator::equals(['id_location' => $location]));
        }

        $vars['positions'] = $qb->execute();
        $vars['positionsArrow'] = getVal('arrows');
        $vars['positionsBarcode'] = getVal('barcode_position');

        return $vars;
    }
}
