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

class ControllerModuleaviseme extends ControllerBaseModule
{

    protected $referencePathFile = 'module/avise_me';

    protected function loadData()
    {
        $this->load->type('tipocomum');

        $this->dataVue['Type_Status'] = Type_Status::getEnumList();

        $this->dataVue['configurations'] = $this->model_setting_setting->getSetting('avise_me');

        if ($this->config->get('config_stock_checkout')) {
            $this->dataVue['stock_checkout_message'] = $this->language->get('warning_stock_checkout');
        }

        $this->dataVue['help_link'] = 'https://manual.futurasistemas.com.br/manual-do-sistema/como-funciona-o-avisa-me-quando-chegar-fec07/';

        $this->setDefaultValues();
    }

    private function setDefaultValues()
    {
        $arrayDefault = array('avise_me_status');

        foreach ($arrayDefault as $value) {
            if (!in_array($value, array_keys($this->dataVue['configurations']))) {
                $this->dataVue['configurations'][$value] = "0";
            }
        }
    }

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

        $response = [];

        $this->load->model('setting/setting');
        $this->model_setting_setting->editSetting('avise_me', $this->request->post);
        $response['success'] = $this->language->get('text_success');

        return $this->response->setOutputJson($response);
    }

    protected function validate()
    {
        $this->loadLanguage();
        $this->load->type('tipocomum');

        extract($this->request->post);

        $errors = [];

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

        return $errors;
    }

    public function install()
    {
        $this->load->model('setting/setting');
        $this->load->model('setting/extension');

        $aviseme_array = [
            'avise_me_status' => Type_Status::Tp_Desabilitado,
        ];

        $this->model_setting_setting->editSetting('avise_me', $aviseme_array);
    }

    public function uninstall() {
            $this->load->model('setting/setting');
            $this->model_setting_setting->deleteSetting('avise_me');
    }

}
