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

class ControllerModuleProductViewedAll extends ControllerBaseModule{

    protected $referencePathFile = 'module/product_viewed_all';

    protected $arrayJson = ['product_viewed_all_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/product_viewed_all')) {
            $this->errors['warning'] = $this->language->get('error_permission');
        }

        $productViewedAllModule =  isset($this->request->post['product_viewed_all_module'])? $this->request->post['product_viewed_all_module'] : [];
        $errors = [];
        $productViewedHabilitado = [] ;


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

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

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

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

}
