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

class ControllerModuleRandom extends ControllerBaseModule{

    protected $referencePathFile = 'module/random';

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

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

        $this->dataVue['TypesForJs'] = array(
            Type_Status::getTypeForJs(),
            Type_ModulePosition::getTypeForJs(),
            Type_IdLayout::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();
    }

    public function validate(){

        $this->loadLanguage();

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

        $random =  isset($this->request->post['random_module'])? $this->request->post['random_module'] : [];
        $errors = [];
        $randomHabilitado = [] ;


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

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

            $module_errors = $this->validate->execute([
                'randomModule['.$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', 'random');
    }

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

}
