<?php

namespace KupShop\ReturnsBundle\Admin\Tabs;

use KupShop\AdminBundle\Admin\WindowTab;
use Query\Operator;

class ReturnsOrdersTab extends WindowTab
{
    protected $title = 'flapReturnsOrders';
    protected $template = 'window/returnOrders.tpl';

    public static function getTypes()
    {
        return [
            'orders' => 1,
        ];
    }

    public function getVars($smarty_tpl_vars)
    {
        $returnInfo = sqlQueryBuilder()
            ->select('*')
            ->from('order_returns_info')
            ->where(Operator::equals(['id_order' => $this->getID()]))
            ->execute()
            ->fetchAssociative();

        return [
            'return' => $returnInfo,
        ];
    }

    public function handleUpdate()
    {
        $getData = $this->getData();

        if (!empty($getData['date_return_to'])) {
            $data['date_return'] = $this->prepareDateTime($getData['date_return_to']);
        }

        if ($data ?? false) {
            sqlQueryBuilder()
                ->insert('order_returns_info')
                ->directValues([
                    'id_order' => $this->getID(),
                    'date_return_to' => ':date_return',
                ])
                ->onDuplicateKeyUpdate(['date_return_to' => ':date_return'])
                ->setParameter('date_return', $data['date_return'])
                ->execute();
        }
    }

    public function getLabel()
    {
        return translate('flapReturnsSettings', 'returnsSettingsTab');
    }
}
