<?php

loadcontroller('base/module');

class ControllerModuleBrandProducts extends ControllerBaseModule{

    protected $referencePathFile = 'module/brandproducts';

    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()
        );
    }

    public function validate(){

        $this->loadLanguage();
        $targetIsDuplicatedObject = '';

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

        $brandModule =  isset($this->request->post['brandproducts_module'])? $this->request->post['brandproducts_module'] : [];
        $errors = [];
        $brandModuleHabilitado = [] ;

        foreach ($brandModule as $key => $value){
          if($value['status'] == Type_Status::Tp_Habilitado){
              array_push($brandModuleHabilitado , $value);
          }

          $module_errors = $this->validate->execute([
            'brandModule['.$key.'][limit]' => [
                'value' => $value['limit'],
                'validates' => [
                    'isEmpty',
                    'isIntegerNumber',
                    [
                        'method' => 'isMinMaxNumber',
                        'min' => 1,
                        'max' => 24
                    ]
                ],
            ],
          ]);

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

        $targetIsDuplicatedObject = $this->validate->targetIsDuplicatedObject(
            $brandModuleHabilitado,
            $this->language->get('error_duplicate'),
            ["position"]
        );


        if($targetIsDuplicatedObject){
            $errors['message'] = $targetIsDuplicatedObject;
        }

        return $errors;
    }

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

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

    }
}