<?php

loadcontroller('base/module');

class ControllerModuleBuyLater extends ControllerBaseModule {

    protected $referencePathFile = 'module/buy_later';

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

        $this->dataVue['positions'] = Type_ModulePosition::getEnumList(); // Posições para o Módulo Carrinho de Compras
        $this->dataVue['Type_DsimDnao'] = Type_DsimDnao::getEnumList();

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

    protected function validate()
    {

        $this->loadLanguage();

        $this->load->helper('ftr_geral');
        $this->load->helper('ftr_validacao');

        extract($this->request->post);
        $errors = array();

        if (!isset($buy_later_module)) {
            $buy_later_module = array();
        }

        $allRegisteredIdLayouts = array_map(function ($module) {
            if ($module['status'] == Type_Status::Tp_Habilitado && $module['position'] != Type_ModulePosition::Tp_AppMobile) {
                return $module['layout_id'];
            }
        }, $buy_later_module);

        $errors = $this->validate->execute([
            'buy_later_title' => [
                'value' => trim($buy_later_title),
                'validates' => ['isEmpty'],
            ],
            'buy_later_status' => [
                'value' => $buy_later_status,
                'validates' => ['isEmpty', 'isEnabledOrDisabled'],
            ],
        ]);

        foreach ($buy_later_module as $key => $module){
            $module_errors = $this->validate->execute([
                'buy_later_module['.$key.'][limit]' => [
                    'value' => $module['limit'],
                    'validates' => [
                        'isEmpty',
                        'isIntegerNumber',
                        [
                            'method' => 'isMinMaxNumber',
                            'min' => 1,
                            'max' => 24
                        ]
                    ],
                    'condition' => 'boughtaslo_module['.$key.'][status]'
                ],
                'buy_later_module['.$key.'][position]' => [
                    'value' => $module['position'],
                    'validates' => ['isEmpty'],
                ],
                'buy_later_module['.$key.'][entry_type]' => [
                    'value' => $module['entry_type'],
                    'validates' => [
                        'isEmpty',
                        [
                            'method' => 'isYesOrNo',
                            'type' => 'Type_DsimDnao',
                        ]
                    ],
                ],
                'buy_later_module['.$key.'][layout_id]' => [
                    'value' => $module['layout_id'],
                    'validates' => ['isEmpty'],
                ],
                'buy_later_module['.$key.'][status]' => [
                    'value' => $module['status'],
                    'validates' => ['isEmpty', 'isEnabledOrDisabled'],
                ],
                'buy_later_module['.$key.'][sort_order]' => [
                    'value' => $module['sort_order'],
                    'validates' => ['isEmpty'],
                ],
            ]);

            $findKeyRegisteredIdLayout = [];
            foreach ($allRegisteredIdLayouts as $registeredIdLayoutKey => $registeredIdLayoutValue) {
                if ($registeredIdLayoutValue == $module['layout_id']) {
                    $findKeyRegisteredIdLayout[] = $registeredIdLayoutValue;
                }
            }

            if ($module['status'] == Type_Status::Tp_Habilitado && count($findKeyRegisteredIdLayout) > 1) {
                $errors['buy_later_module['. $key .'][layout_id]'][] =
                    'Você não pode habilitar o módulo mais de uma vez no mesmo layout!';
            }

            $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', 'buy_later');
    }

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


}
