<?php

namespace KupShop\ProductsSerialNumbersBundle\Admin\lists;

use KupShop\AdminBundle\AdminList\BaseList;
use KupShop\AdminBundle\Query\Invert;
use KupShop\CatalogBundle\Query\Search;
use KupShop\DevelopmentBundle\Query\QueryBuilder;
use KupShop\KupShopBundle\Util\HtmlBuilder\HTML;
use Query\Operator;

class ProductsSerialNumbersList extends BaseList
{
    protected $tableDef = [
        'id' => 'psn_id',
        'fields' => [
            'ID' => ['field' => 'psn_id', 'size' => 0.3],
            'Sériové číslo' => ['field' => 'psn_serial_number', 'size' => 0.8],
            'Tisk' => ['render' => 'renderPrint', 'size' => 0.3],
            'Produkt' => ['field' => 'p_title', 'render' => 'renderName', 'size' => 2, 'type' => 'product', 'type_id' => 'p_id'],
            'Dodavatel' => ['field' => 'sup_name', 'size' => 1, 'type' => 'suppliers', 'type_id' => 'sup_id'],
            'Naskladnění' => ['field' => 'psn_id_stock_in', 'size' => 0.5, 'type' => 'stockIn', 'type_id' => 'psn_id_stock_in'],
            'Objednávka' => ['field' => 'o_order_no', 'size' => 0.5, 'type' => 'order', 'type_id' => 'o_id'],
        ],
    ];

    public function renderName($values, $column)
    {
        $name = [$values['p_title'], $values['pv_title']];

        return join(' - ', array_filter($name));
    }

    public function getQuery()
    {
        /** @var QueryBuilder $qb */
        $qb = sqlQueryBuilder()->select(
            'psn.id psn_id',
            'psn.serial_number psn_serial_number',
            'psn.id_stock_in psn_id_stock_in',
            'p.title p_title',
            'p.id p_id',
            'pv.title pv_title',
            'pv.id pv_id',
            'oi.id si_id',
            'o.id o_id',
            'o.order_no o_order_no',
            'sup.name sup_name',
            'sup.id sup_id',
        )->from('products_serial_numbers', 'psn')
            ->innerJoin('psn', 'products', 'p', 'p.id = psn.id_product')
            ->leftJoin('psn', 'products_variations', 'pv', 'pv.id = psn.id_variation')
            ->leftJoin('psn', 'order_items', 'oi', 'oi.id = psn.id_order_item')
            ->leftJoin('oi', 'orders', 'o', 'o.id = oi.id_order')
            ->leftJoin('psn', 'stock_in', 'si', 'si.id = psn.id_stock_in')
            ->leftJoin('si', 'suppliers', 'sup', 'si.id_supplier = sup.id');

        if ($serialNumber = getVal('serialNumber')) {
            $qb->andWhere(
                Invert::checkInvert(\Query\Operator::equals(['psn.serial_number' => $serialNumber]), getVal('serialNumber_invert'))
            );
        }

        if ($orderId = getVal('idOrder')) {
            $searchFields = [
                ['field' => 'o.id', 'match' => 'both', 'order' => '1'],
                ['field' => 'o.order_no', 'match' => 'both', 'order' => '1'],
            ];
            $qb->andWhere(Invert::checkInvert(Search::searchFields($orderId, $searchFields), getVal('idOrder_invert')));
        }

        if ($productId = getVal('idProduct')) {
            $qb->andWhere(Operator::equals(['psn.id_product' => $productId]));

            if ($variationId = getVal('idVariation')) {
                if (intval($variationId) > 0) {
                    $qb->andWhere(Operator::equals(['psn.id_variation ' => $variationId]));
                }
            }
        }

        return $qb;
    }

    public function renderPrint($values, $column)
    {
        $href = 'launch.php?s=printCenter.php&type=product&template_type=serialNumber&ID='.$values['p_id'].
            '&IDv='.$values['pv_id'].
            '&sn='.$values['psn_serial_number'];

        return HTML::create('a')
            ->attr('title', 'Vytisknout')
            ->attr('href', $href)
            ->tag('span')
            ->class('badge')
            ->tag('i')
            ->class('glyphicon glyphicon-print')
            ->end()
            ->end();
    }
}

$main_class = ProductsSerialNumbersList::class;
