<?php

namespace KupShop\ProductsBatchesBundle\Admin;

use Query\Operator;

class productsBatches extends \Window
{
    protected $template = 'window/productsBatches.tpl';

    protected $tableName = 'products_batches';

    protected $nameField = 'code';

    public function get_vars()
    {
        $vars = parent::get_vars();
        $pageVars = getVal('body', $vars);

        $data = &$pageVars['data'];
        if (!empty($data['id_product'])) {
            $item = sqlQueryBuilder()->select('p.title product_title', 'pv.title variation_title')
                ->from('products', 'p')
                ->leftJoin('p', 'products_variations', 'pv', 'p.id = pv.id_product')
                ->where(Operator::equalsNullable(['p.id' => $data['id_product'], 'pv.id' => $data['id_variation']]))
                ->execute()->fetch();
            $data['product_title'] = $item['product_title'];
            $data['variation_title'] = $item['variation_title'];
        }

        $vars['body'] = $pageVars;

        return $vars;
    }

    public function getData()
    {
        $data = parent::getData();

        if (getVal('Submit')) {
            $data['date_expiry'] = $this->prepareDate($data['date_expiry']);
        }

        return $data;
    }
}

return productsBatches::class;
