<?php
loadcontroller('base/module');

class ControllerModuleSpecial extends ControllerBaseModule{

    protected $referencePathFile = 'module/special';

    // protected $arrayJson = ['special_module'];
    
    protected function loadModel(){
        parent::loadModel();
        $this->load->dmodel('design/layout');
		$this->load->model('catalog/special_action');
    }

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

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

        // Remove as opções de layout que não são usadas nos módulos de lista de produtos
        $this->dataVue['layouts'] = $this->dmodel_design_layout->getLayouts([
            'notarray' => Type_NotListaProdutos::getEnumList()
        ]);

        // Layouts disponíveis para o Aplicativo do Consumidor
        $this->dataVue['layoutsApp'] = Type_LayoutApp::getEnumList();

		$this->dataVue['special_action'] = $this->model_catalog_special_action->getSpecialActions();

    }

    public function validate(){

        $this->loadLanguage();

        if (!$this->user->hasPermission('modify', 'module/special')) {
            $this->errors['warning'] = $this->language->get('error_permission');
        }

        $specialModule =  isset($this->request->post['special_module'])? $this->request->post['special_module'] : [];
        $errors = [];
        $specialHabilitado = [] ;


        foreach ($specialModule as $key => $value){

            if($value['status'] == Type_Status::Tp_Habilitado){
                array_push($specialHabilitado , $value);
            }

            $module_errors = $this->validate->execute([
                'specialModule['.$key.'][limit]' => [
                    'value' => $value['limit'],
                    'validates' => [
                        'isEmpty',
                        'isIntegerNumber',
                        [
                            'method' => 'isMinMaxNumber',
                            'min' => 1,
                            'max' => 99
                        ]
                    ],
                ],
            ]);
          $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');
    }

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

}
