<?php

loadController('base/module');

class ControllerSafetySafety extends ControllerBaseModule
{
    protected $referencePathFile = 'safety/safety';

    protected function breadcrumbs()
    {
        $this->dataVue['breadcrumbs'] = [
            [
                'href' => $this->url->link('common/home', 'token=' . $this->token, 'SSL'),
                'text' => $this->language->get('text_home'),
            ],
            [
                'href' => $this->url->link($this->referencePathFile, 'token=' . $this->token, 'SSL'),
                'text' => $this->language->get('heading_title'),
            ]
        ];
    }

    protected function actionUrls()
    {
        $this->dataVue['action'] = $this->url->link(
            "$this->referencePathFile/save",
            'token=' . $this->token,
            'SSL'
        );

        $this->dataVue['back'] = $this->url->link(
            'common/home',
            'token=' . $this->token,
            'SSL'
        );
    }

    public function loadModel()
    {
        parent::loadModel();

    }

    protected function saveSetting()
    {
        $this->load->model('setting/setting');
        $this->load->model('setting/emailvalidate');

        $listSettings = [$this->fileName.'_limiter' => []];

        $listSettings = array_merge($listSettings,[$this->fileName.'_recaptcha' => []], [$this->fileName.'_email' => []]);

        $listSettings[$this->fileName.'_limiter'] = array_merge($listSettings[$this->fileName.'_limiter'],array_filter($this->request->post, function($k) {
             return strrpos($k,$this->fileName.'_limiter') !== false;
         }, ARRAY_FILTER_USE_KEY));

        $listSettings[$this->fileName.'_recaptcha'] = array_merge($listSettings[$this->fileName.'_recaptcha'],array_filter($this->request->post, function($k) {
            return strrpos($k,'google_recaptcha') !== false;
        }, ARRAY_FILTER_USE_KEY));

        $listSettings[$this->fileName.'_email'] = array_merge($listSettings[$this->fileName.'_email'],array_filter($this->request->post, function($k) {
            return strrpos($k,'email_validation') !== false;
        }, ARRAY_FILTER_USE_KEY));


        if($listSettings['safety_email']['email_validation_status'] > 0) {
            $this->model_setting_emailvalidate->enable();
        }else{
            $this->model_setting_emailvalidate->disable();
        }

        foreach($listSettings as $key => $list){
            $this->model_setting_setting->editSetting($key, $list, 0, $this->arrayJson);
        }
    }

    public function loadData()
    {
        parent::loadData();

        $this->dataVue['TypesForJs'] = array(
            Type_Status::getTypeForJs(),
            Type_DsimDnao::getTypeForJs(),
            Type_SearchBarForm::getTypeForJs(),
            Type_SearchBarButtonBackground::getTypeForJs(),
            Type_StatusEn::getTypeForJs(),
            Type_ProductBoxPromotionStyle::getTypeForJs(),
            Type_Tamanho::getTypeForJs(),
            Type_EfeitoImagem::getTypeForJs(),
            Type_CartIcon::getTypeForJs(),
            Type_ProductListTitleStyle::getTypeForJs(),
            Type_HeaderLogoPosition::getTypeForJs(),
        );

        $configs = ['safety_limiter_status',
            'safety_limiter_time_break',
            'safety_limiter_transaction',
            'safety_limiter_transaction_time',
            'safety_limiter_client',
            'safety_limiter_ip',
            'safety_limiter_mail',
            'wg24_google_recaptcha_status',
            'google_recaptcha_site_key',
            'google_recaptcha_secret_key',
            'email_validation_status',
        ];

        foreach ($configs as $value){
            $this->dataVue['configurations'][$value] = $this->config->get($value);

            if($value == 'safety_limiter_client' OR $value == 'safety_limiter_ip')
            if($this->dataVue['configurations'][$value] == null){
                $this->dataVue['configurations'][$value] = 0;
            }
        }

    }
    protected function validate()
    {
        $errors = [];
        if($this->request->post['safety_limiter_status']){
            $errors = $this->validate->execute([
                'safety_limiter_time_break' => [
                    'value' => $this->request->post['safety_limiter_time_break'],
                    'validates' => ['isEmpty'],
                ],
                'safety_limiter_transaction' => [
                    'value' => $this->request->post['safety_limiter_transaction'],
                    'validates' => [
                        'isEmpty',
                        [
                            'method' => 'isMinMaxNumber',
                            'min' => '1',
                            'max' => '99999'
                        ],
                    ]
                ],
                'safety_limiter_transaction_time' => [
                    'value' => $this->request->post['safety_limiter_transaction_time'],
                    'validates' => [
                        'isEmpty',
                        [
                            'method' => 'isMinMaxNumber',
                            'min' => '1',
                            'max' => '99999'
                        ],
                    ]
                ],
                'safety_limiter_mail' => [
                    'value' => $this->request->post['safety_limiter_mail'],
                    'validates' => [
                        'isEmpty',
                        [
                            'method' => 'isEmail',
                            'message' => $this->language->get('error_email'),
                        ],
                    ]
                ],
            ]);

            if ((!isset($this->request->post['safety_limiter_ip']) || $this->request->post['safety_limiter_ip'] != 1)
                && (!isset($this->request->post['safety_limiter_client']) || $this->request->post['safety_limiter_client'] != 1)) {
                $errors['message'] = $this->language->get('error_method');
            }
        }

        return $errors;
    }
}