<?php

loadcontroller('base/module');

class ControllerModuleSpecialAction extends ControllerBaseModule
{
    protected $referencePathFile = 'module/special_action';

    protected $arrayJson = ['special_action_module', 'special_action_promotions_selected'];

    protected function loadModel()
    {
        parent::loadModel();
    }


    protected function loadData()
    {
        $this->load->dmodel('design/layout');
        $this->load->model('catalog/special_action');
        $this->dataVue['special_action_options'] = $this->autocomplete();
        parent::loadData();

        $this->dataVue['TypesForJs'] = array(
            Type_DsimDnao::getTypeForJs(),
            Type_ModulePosition::getTypeForJs(),
            Type_DefaultValues::getTypeForJs(),
            Type_Status::getTypeForJs(),
            Type_AutoOrManual::getTypeForJs(),
            Type_SpecialActionStyles::getTypeForJs()
        );

        $this->dataVue['special_action_styles'] = [
            [
                'type' => '0',
                'image' => file_get_contents('../catalog/view/theme/vista/image/special-action/bg-0.svg'),
            ],
            [
                'type' => '1',
                'image' => file_get_contents('../catalog/view/theme/vista/image/special-action/bg-1.svg'),
            ],
             [
                'type' => '2',
                'image' => file_get_contents('../catalog/view/theme/vista/image/special-action/bg-2.svg'),
            ],
            [
                'type' => '3',
                'image' => file_get_contents('../catalog/view/theme/vista/image/special-action/bg-3.svg'),
            ],
            [
                'type' => '4',
                'image' => file_get_contents('../catalog/view/theme/vista/image/special-action/bg-4.svg'),
            ],
            [
                'type' => '5',
                'image' => file_get_contents('../catalog/view/theme/vista/image/special-action/bg-5.svg'),
            ],
            [
                'type' => '6',
                'image' => file_get_contents('../catalog/view/theme/vista/image/special-action/bg-6.svg'),
            ],
            [
                'type' => '7',
                'image' => file_get_contents('../catalog/view/theme/vista/image/special-action/bg-7.svg'),
            ],
        ];


        // Remove as opções de layout que não são usadas nos módulos de lista de produtos
        $data = array(
            'notarray' => Type_NotListaProdutos::getEnumList()
        );

        if (isset($this->request->post['special_action_item'])) {
            $special_actions = explode(',', $this->request->post['special_action_item']);
        } else {
            $special_actions = explode(',', $this->config->get('special_action_item'));
        }

        $this->dataVue['special_actions'] = array();


        foreach ($special_actions as $special_action) {
            if ($special_action) {
                $special_info = $this->model_catalog_special_action->getSpecialAction($special_action);

                if ($special_info) {
                    $this->dataVue['special_actions'][] = array(
                        'order_promotion_id' => $special_info['order_promotion_id'],
                        'description'        => $special_info['description']
                    );
                }
            }
        }

        $this->dataVue['layouts'] = $this->dmodel_design_layout->getLayouts($data);
    }

    protected function actionUrls()
    {
        parent::actionUrls();

        $this->dataVue['special_actions_url'] =  $this->url->linkAnchor(
            'module/special_action/autocomplete',
            'token=' . $this->token,
            'filter_special=',
            'SSL'
        );
    }

    public function autocomplete()
    {
        $json = array();

        $this->load->helper('ftr_geral');
        $this->load->type('tipocomum');
        $this->load->model('catalog/special_action');

        $results = $this->model_catalog_special_action->getSpecialActions();

        foreach ($results as $result) {
            $json[] = array(
                'id' => $result['order_promotion_id'],
                'name'     => strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'))
            );
        }

        return $json;
    }

    public function validate()
    {
        $this->loadLanguage();

        $errors = [];
        $specialActionModule =  isset($this->request->post['special_action_module']) ? $this->request->post['special_action_module'] : [];

        if (isset($specialActionModule) && !empty($specialActionModule)) {
            foreach ($specialActionModule as $key => $value) {
                $module_errors = $this->validate->execute([
                    'special_action_module[' . $key . '][limit]' => [
                        'value' => $value['limit'],
                        'validates' => ['isEmpty'],
                    ],
                ]);

                $errors = array_merge($errors, $module_errors);
            }
        }


        return $errors;
    }

    public function save()
    {
        parent::save();
        $this->session->data['success'] = null;
    }

    public function install()
    {
        $this->load->model('setting/extension');
        $this->model_setting_extension->install('module', 'special_action');
    }

    public function uninstall()
    {
        $this->load->model('setting/extension');
        $this->model_setting_extension->uninstallSetting('module', 'special_action');
    }
}