<?php

namespace KupShop\ReclamationsBundle\Admin\Tabs;

use KupShop\AdminBundle\Admin\WindowTab;
use KupShop\KupShopBundle\Util\Compat\SymfonyBridge;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class ReclamationsWindowTab extends WindowTab
{
    protected $title = 'flapReclamationsSettings';
    protected $template = 'window/settings.reclamations.tpl';

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

    public function getVars($smarty_tpl_vars)
    {
        $vars = parent::getVars($smarty_tpl_vars);

        $vars['tabView'] = $this;
        $vars['deliveries'] = \Delivery::getAll();

        if (findModule(\Modules::RETURNS, \Modules::SUB_CP_AUTH_CODES)) {
            $vars['reclamations_auth_codes'] = sqlFetch($this->selectSQL('returns_auth_codes', [], ['COUNT(*) as total', 'SUM(used="Y") as used']));
        }

        return $vars;
    }

    public function handleImportAuthCodes()
    {
        if ($path = $this->getFile()) {
            try {
                $csv = new \SplFileObject($path);
                $csv->setFlags(\SplFileObject::READ_CSV);
                $csv->setCsvControl(';', "'");
                if ($csv->valid()) {
                    $columns = ['ID', 'Note', 'Date', 'Date2', 'Date3', 'PackageID'];
                    $codes = [];
                    while ($csv->valid()) {
                        $row_data = $csv->current();
                        if (count($row_data) == count($columns)) {
                            $row_data = array_combine($columns, $row_data);
                            $code = trim($row_data['ID']);
                            $date = trim($row_data['Date2']);
                            $packageID = trim($row_data['PackageID'], '"');
                            if (empty($packageID) && empty($date)) {
                                $codes[] = ['code' => $code];
                                sqlQuery("INSERT IGNORE INTO `returns_auth_codes` (`code`) VALUES ('{$code}')");
                            }
                        }
                        $csv->next();
                    }
                }
            } catch (Error $e) {
                $error = 'Chyba: '.$e->getMessage();

                $this->window->returnError($error);
            }
        }

        $this->window->returnOK();
    }

    public function getPlaceholders()
    {
        $placeholders = [
            'KOD_REKLAMACE' => [
                'text' => 'Kód vytvořené reklamace',
            ],
            'ODKAZ_PDF' => [
                'text' => 'Odkaz na reklamační protokol',
            ],
            'ODKAZ_VRATKA' => [
                'text' => 'Odkaz na vratku',
            ],
            'KOD_VRATKY' => [
                'text' => 'Kód vratky',
            ],
        ];

        if (findModule(\Modules::RETURNS, \Modules::SUB_CP_AUTH_CODES)) {
            $placeholders['PACKAGE_LABEL'] = [
                'text' => 'Autorizační kód Odvozu zboží',
            ];
        }

        return $placeholders;
    }

    public function getFile()
    {
        $request = SymfonyBridge::getCurrentRequest();

        /** @var $file UploadedFile */
        $file = $request->files->get('file');
        if (!$file) {
            return '';
        }

        return $file->getRealPath();
    }
}
