<?php

namespace KupShop\ReclamationsSuppliersBundle\Admin\lists;

use KupShop\AdminBundle\AdminList\BaseList;
use KupShop\AdminBundle\Query\Invert;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\KupShopBundle\Util\HtmlBuilder\HTML;
use KupShop\ReclamationsSuppliersBundle\Util\ReclamationsSuppliersUtil;
use Query\Operator;

class ReclamationsSuppliersList extends BaseList
{
    protected $tableName = 'reclamations_suppliers';
    protected ?string $tableAlias = 'rs';
    protected $tableDef = [
        'id' => 'rs.id',
        'fields' => [
            'code' => ['translate' => true, 'field' => 'rs.code', 'size' => 0.8],
            'supplier' => ['translate' => true, 'field' => 's.name'],
            'pieces' => ['translate' => true, 'field' => 'pieces', 'size' => 0.6],
            'status' => ['translate' => true, 'render' => 'renderStatus', 'field' => 'rs.status', 'size' => 0.8],
            'date_created' => ['translate' => true, 'field' => 'rs.date_created', 'render' => 'renderDateTime'],
            'date_accepted' => ['translate' => true, 'field' => 'rs.date_accepted', 'render' => 'renderDateTime'],
            'date_handle' => ['translate' => true, 'field' => 'rs.date_handle', 'render' => 'renderDateTime'],
        ],
        'class' => 'getRowClass',
    ];

    protected $orderParam = [
        'sort' => 'code',
        'direction' => 'DESC',
    ];

    /** @var ReclamationsSuppliersUtil */
    protected $reclamationsSuppliersUtil;

    public function __construct()
    {
        $this->reclamationsSuppliersUtil = ServiceContainer::getService(ReclamationsSuppliersUtil::class);
    }

    public function customizeTableDef($tableDef)
    {
        if (findModule(\Modules::WAREHOUSE) || findModule(\Modules::CHECK_APP)) {
            $tableDef['fields']['stockInCheck'] = ['translate' => true, 'field' => 'rs.id', 'size' => 1, 'class' => 'right', 'render' => 'renderAutomaticStockIn'];
        }

        return $tableDef;
    }

    public function renderStatus($values, $column)
    {
        return $this->reclamationsSuppliersUtil->getStatuses()[$values['status']];
    }

    public function renderAutomaticStockIn($values)
    {
        if ($values['status'] != 0) {
            return '';
        }

        $output = HTML::create('a');
        $output
            ->attr('href', "javascript:nw('reclamationSupplierCheck', '{$values['id']}')")
            ->tag('span')
            ->class('badge badge-primary')
                    ->tag('span')
                    ->class('glyphicon glyphicon-barcode')
                        ->text(' Kontrola')
                    ->end()
            ->end();

        return $output;
    }

    public function getQuery()
    {
        $qb = sqlQueryBuilder()
            ->select('rs.id, rs.status, rs.code, rs.date_created, rs.date_accepted, rs.date_handle, SUM(rsi.pieces) pieces, s.name')
            ->from($this->getTableName(), 'rs')
            ->leftJoin('rs', 'reclamations_suppliers_items', 'rsi', 'rs.id = rsi.id_reclamation_supplier')
            ->join('rs', 'suppliers', 's', 'rs.id_supplier = s.id')
            ->groupBy('rs.id');

        if ($status = getVal('status')) {
            $qb->andWhere(
                Invert::checkInvert(Operator::inIntArray($status, 'rs.status'), getVal('status_invert'))
            );
        }

        if ($code = getVal('code')) {
            $qb->andWhere(
                Invert::checkInvert(
                    Operator::orX(
                        [
                            Operator::equals(['rs.id' => $code]),
                            Operator::equals(['rs.code' => $code]),
                        ]
                    ),
                    getVal('code_invert')
                )
            );
        }

        if ($productId = getVal('id_product')) {
            $qb->andWhere('rsi.id_product = :id_product')
                ->setParameter('id_product', $productId);

            if ($variationId = getVal('id_variation')) {
                if (intval($variationId) > 0) {
                    $qb->andWhere('rsi.id_variation = :id_variation')
                        ->setParameter('id_variation', $variationId);
                }
            }
        }

        if ($suppliers = getVal('suppliers')) {
            $qb->andWhere(
                Invert::checkInvert(
                    Operator::inIntArray($suppliers, 'rs.id_supplier'),
                    getVal('suppliers_invert')
                )
            );
        }

        if ($name = getVal('surname')) {
            $qb->leftJoin('rsi', 'reclamations', 'r', 'rsi.id_reclamation = r.id');
            $qb->andWhere(Invert::checkInvert(Operator::like(['r.surname' => "%{$name}%", 'r.name' => "%{$name}%"], 'OR'), getVal('surname_invert')));
        }

        if ($reclamationCode = getVal('reclamation_code')) {
            $qb->leftJoin('rsi', 'reclamations', 'r', 'rsi.id_reclamation = r.id');
            $qb->andWhere(Invert::checkInvert(Operator::equals(['r.code' => $reclamationCode]), getVal('reclamation_code_invert')));
        }

        return $qb;
    }
}

return ReclamationsSuppliersList::class;
