<?php

loadcontroller('base/module');

class ControllerModuleBoughtAlso extends ControllerBaseModule {

    protected $referencePathFile = 'module/boughtalso';

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

    protected function validate()
    {
        $this->loadLanguage();
        $targetIsDuplicatedObject = '';

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

        $boughtalsoModule =  isset($this->request->post['boughtalso_module'])? $this->request->post['boughtalso_module'] : [];
        $errors = [];
        $boughtalsoModuleHabilitado = [] ;

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

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

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

        return $errors;
    }

    protected function saveSetting(){
        if (isset($this->request->post['boughtalso_module'])) {
            foreach ($this->request->post['boughtalso_module'] as $key => $value) {
                //Forçando salvar o layoutid apenas para a tela de produtos
                $this->request->post['boughtalso_module'][$key]['layout_id'] = Type_IdLayout::Tp_IdProduct;
            }
        }
        parent::saveSetting();
    }

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

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


}
