<?php

loadController('base/module');

class ControllerModuleprecodinamico extends ControllerBaseModule
{
    protected $referencePathFile = 'module/preco_dinamico';

    protected $fieldsToRemoveMask = [
        'discount_value' => 'removeMaskToDouble'
    ];

    private $post = [];

    private $error = array();

    protected function loadModel()
    {
        $this->load->model('setting/setting');
        $this->load->dmodel('sale/customer_group');
        $this->load->dmodel('tool/image');

        parent::loadModel();
    }

    protected function loadData()
    {
        $this->dataVue['Type_Status'] = Type_Status::getEnumList();

        $this->dataVue['TypesForJs'] = array(
            Type_Desconto::getTypeForJs(),
        );

        $this->dataVue['futura_user'] = $this->user->getUserName() == 'futura';

        $this->dataVue['configurations'] = $this->model_setting_setting->getSetting('preco_dinamico');
        $this->dataVue['preco_dinamico_status'] = $this->config->get('preco_dinamico_status');
        $this->dataVue['discount_type'] = $this->config->get('discount_type');
        $this->dataVue['configurations']['preco_dinamico_personalized'] = $this->config->get('preco_dinamico_personalized');
        $this->dataVue['configurations']['parcel_number'] = $this->config->get('parcel_number') ? $this->config->get('parcel_number') : 3;
        $this->dataVue['configurations']['discount_value'] = $this->config->get('discount_value') ? $this->config->get('discount_value') : 10;
        $this->dataVue['configurations']['preco_dinamico_tabelapreco'] = $this->config->get('preco_dinamico_tabelapreco') ?? [];
        $this->dataVue['configurations']['preco_dinamico_tabelapreco_apelido'] = $this->config->get('preco_dinamico_tabelapreco_apelido') ?? [];
        $this->dataVue['configurations']['model'] = $this->config->get('model') ? $this->config->get('model') : 'complete';

        $this->dataVue['themeoptionpanel'] = $this->model_setting_setting->getSetting('wg24themeoptionpanel');
        $this->dataVue['price_color'] = property_exists((Object) $this->dataVue['themeoptionpanel'], 'wg24_product_box_price_col')
            ? $this->dataVue['themeoptionpanel']['wg24_product_box_price_col'] : false;

        if ($this->config->get('advanced_config') !== null) {
            $this->dataVue['configurations']['advanced_config'] = $this->config->get('advanced_config');
        } else {
            $this->dataVue['configurations']['advanced_config'] = !$this->config->get('futura_preco_dinamico_produto') ? 0 : 1;
        }

        $customerGroups = $this->dmodel_sale_customer_group->getCustomerGroups();

        $tabelaPreco = array(
            'selected' => $this->config->get('preco_dinamico_tabelapreco'),
            'apelido' => $this->config->get('preco_dinamico_tabelapreco_apelido')
        );

        $this->dataVue['customerGroups'] = array();
        foreach ($customerGroups as $key => $customerGroup) {
            if ($customerGroup['customer_group_id'] != $this->config->get('config_customer_group_id')) {
                $this->dataVue['customerGroups'][] = array(
                    'selected' => isset($tabelaPreco['selected']) && in_array($customerGroup['customer_group_id'], $tabelaPreco['selected']),
                    'customer_group_id' => $customerGroup['customer_group_id'],
                    'name' => $customerGroup['name'],
                    'apelido' => (isset($tabelaPreco['apelido'][$customerGroup['customer_group_id']]) && ($tabelaPreco['apelido'][$customerGroup['customer_group_id']] != '')) ? $tabelaPreco['apelido'][$customerGroup['customer_group_id']] : ''
                );
            }
        }

        $this->dataVue['no_image'] = $this->dmodel_tool_image->resize(
            'no_image.jpg',
            Type_MiniatureSmall::Tp_width,
            Type_MiniatureSmall::Tp_height
        );


        if ($this->config->get('futura_preco_dinamico_produto')) {
            $this->dataVue['configurations']['futura_preco_dinamico_produto'] = htmlspecialchars_decode($this->config->get('futura_preco_dinamico_produto'), ENT_COMPAT);
        } else {
            $this->dataVue['configurations']['futura_preco_dinamico_produto'] = '$preco = number_format($pr, 2,\',\',\'.\');
                $preco_parcelado = number_format(($pr/3), 2,\',\',\'.\');
                $preco_promo = $special;
                $preco_discount = number_format($pr - ($pr * 10)/100, 2,\',\',\'.\');

                $valor = $special;
                $replace_valor = (double) trim(str_replace([\'R$\', \'.\', \',\'], [\'\', \'\', \'.\'], $valor));
                $preco_parcelado_promo = number_format($replace_valor/3 , 2,\',\',\'.\');
                $preco_promo_discount = number_format($replace_valor - ($replace_valor * 10)/100, 2,\',\',\'.\');

                if ($preco_promo) {
                echo html_entity_decode(nl2br(\'<span style="color: #737373;display:block;font-size: 1rem;line-height: 1.25rem;"><s>R$ \' . $preco .
                \'</s></span><span style="color: #0d0d0d; display:block;font-weight: 700;font-size: 2rem;line-height: 2.25rem;">\' . $preco_promo .
                \' </span><span style="color: #737373; display:block;font-size: 1rem;line-height: 1.25rem;">R$ \' . $preco_promo_discount .
                \' boleto à vista ou até 3x sem juros de R$ \' . $preco_parcelado_promo . \'</span>\'));
                } else {
                echo html_entity_decode(nl2br(\'<span style="color: #0d0d0d;display:block;font-weight: 700;font-size: 2rem;line-height: 2.25rem;">R$ \' . $preco .
                \'</span><span style="color: #737373; display:block;font-size: 1rem;line-height: 1.25rem;">R$ \' . $preco_discount .
                \' boleto à vista ou até 3x sem juros de R$ \' . $preco_parcelado . \'</span>\'));
                }';
        }

        if ($this->config->get('futura_preco_dinamico_exibicoes')) {
            $this->dataVue['configurations']['futura_preco_dinamico_exibicoes'] = htmlspecialchars_decode($this->config->get('futura_preco_dinamico_exibicoes'), ENT_COMPAT);
        } else {
            $this->dataVue['configurations']['futura_preco_dinamico_exibicoes'] = '$preco = number_format($product[\'price_pr\'], 2,\',\',\'.\');
$preco_parcelado = number_format(($product[\'price_pr\']/3), 2,\',\',\'.\');
$preco_promo = $product[\'special\'];
$preco_discount = number_format($product[\'price_pr\'] - ($product[\'price_pr\'] * 10)/100, 2,\',\',\'.\');

$valor = $product[\'special\'];
$replace_valor = (double) trim(str_replace([\'R$\', \'.\', \',\'], [\'\', \'\', \'.\'], $valor));
$preco_parcelado_promo = number_format($replace_valor/3 , 2,\',\',\'.\');
$preco_promo_discount = number_format($replace_valor - ($replace_valor * 10)/100, 2,\',\',\'.\');

if ($preco_promo) {
echo html_entity_decode(nl2br(\'<span style="color: #737373;display:block;font-size: 14;"><s>R$ \' . $preco .
\'</s></span><span style="color: #0d0d0d; display:block;font-size: 16;font-weight: 700;">\' . $preco_promo .
\' </span><span style="color: #737373; display:block;font-size: 1rem;line-height: 1.25rem;">R$ \' . $preco_promo_discount .
\' boleto à vista ou até 3x sem juros de R$ \' . $preco_parcelado_promo . \'</span>\'));
} else {
echo html_entity_decode(nl2br(\'<span style="color: #0d0d0d; display:block;font-size: 14;">R$ \' . $preco .
\'</span><span style="color: #737373; display:block;font-size: 16;font-weight: 400;">R$ \' . $preco_discount .
\' boleto à vista ou até 3x sem juros de R$ \' . $preco_parcelado . \'</span>\'));
}';
        }
    }

    protected function validate()
    {
        $this->load->type('tipocomum');
        extract($this->request->post);

        $errors = $this->validate->execute([
            'futura_preco_dinamico_produto' => [
                'value' => $futura_preco_dinamico_produto ?? '',
                'validates' => ['isEmpty'],
            ],
            'futura_preco_dinamico_exibicoes' => [
                'value' => $futura_preco_dinamico_exibicoes ?? '',
                'validates' => ['isEmpty'],
            ],
            'discount_value' => [
                'value' => $discount_value ?? '',
                'validates' => ['isEmpty'],
            ],
        ]);

        if (!empty($preco_dinamico_tabelapreco)) {
            foreach ($preco_dinamico_tabelapreco as $value) {
                $errors = array_merge($errors, $this->validate->execute([
                    "preco_dinamico_tabelapreco_apelido[$value]" => [
                        'value' => $preco_dinamico_tabelapreco_apelido[$value],
                        'validates' => ['isEmpty'],
                    ],
                ]));
            }
        }
        return $errors;
    }
}
