<?php

loadcontroller('base/module');

class ControllerModuleLpbc extends ControllerBaseModule
{
    protected $referencePathFile = 'module/lpbc';

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

    protected function loadData()
    {
        $this->load->dmodel('design/layout');
        $this->load->model('catalog/category');
        parent::loadData();

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

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

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

        $results = $this->model_catalog_category->getCategories(0);

        foreach ($results as $result) {
            if ($result['status']) {
                $this->dataVue['categories'][] = array(
                    'category_id' => $result['category_id'],
                    'name'        => $result['name']
                );
            }
        }
    }

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

        $errors = [];
        $lpbcModuleHabilitado = [];

        $isShowOnlyLogged = $this->request->post['lpbc_module_interests_status'] == '1';
        $lpbcModule =  isset($this->request->post['lpbc_module']) ? $this->request->post['lpbc_module'] : [];
        $lpbcModuleInterests =  isset($this->request->post['lpbc_module_interests']) ? $this->request->post['lpbc_module_interests'] : [];

        if(empty($lpbcModule)) {
            return $errors = [
                'message' => $this->language->get('errorModule')
            ];
        }

        if ($isShowOnlyLogged && isset($lpbcModuleInterests) && !empty($lpbcModuleInterests)) {
            foreach ($lpbcModuleInterests as $key => $value) {
                if ($value['status'] == Type_Status::Tp_Habilitado) {
                    array_push($lpbcModuleHabilitado, $value);
                }

                $module_errors = $this->validate->execute([
                    'lpbc_module_interests[' . $key . '][limit]' => [
                        'value' => $value['limit'],
                        'validates' => ['isEmpty'],
                    ],
                ]);

                $errors = array_merge($errors, $module_errors);
            }
        } else if (!$isShowOnlyLogged && isset($lpbcModule) && !empty($lpbcModule)) {
            foreach ($lpbcModule as $key => $value) {
                if ($value['status'] == Type_Status::Tp_Habilitado) {
                    array_push($lpbcModuleHabilitado, $value);
                }

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

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