<?php

class ControllerPaymentPagamentoDinamico extends Controller
{
    private $error = array();

    protected $fieldsToRemoveMask = [];

    public function index()
    {

        $language = $this->load->language('payment/pagamentodinamico');
        $this->data = array_merge($this->data, $language);

        $this->document->setTitle($this->language->get('heading_title'));
        $this->document->addScript('view/public/js/payment/pagamentodinamico.js');

        $this->load->model('setting/setting');
        $this->load->dmodel('tool/image');
        $this->load->type('tipocomum');

        $this->data['ftr_geral'] = new ftr_geral();
        $this->data['Type_DisplayModuleOption'] = Type_DisplayModuleOption::getEnumList();
        $this->data['Type_Status'] = Type_Status::getEnumList();

        $token = $this->session->data['token'];

        $this->data['breadcrumbs'] = [
            [
                'text' => $this->language->get('text_home'),
                'href' => $this->url->link('common/home', 'token=' . $token, 'SSL'),
                'separator' => false
            ],
            [
                'text' => $this->language->get('text_payment'),
                'href' => $this->url->linkAnchor('extension/module', 'token=' . $token, 'tab-info=' . $this->request->get['type'], 'SSL'),
                'separator' => ' :: '
            ],
            [
                'text' => $this->language->get('heading_title'),
                'href' => $this->url->link('payment/pagamentodinamico', 'token=' . $token . '&type=' . $this->request->get['type'], 'SSL'),
                'separator' => ' :: '
            ]
        ];

        $urlSave = $this->url->link('payment/pagamentodinamico/save', 'token=' . $token . '&type=' . $this->request->get['type'], 'SSL');
        $urlCancel = $this->url->linkAnchor('extension/module', 'token=' . $token, 'tab-info=' . $this->request->get['type'], 'SSL');

        if ($this->config->get('pagamentodinamico_modules')) {

            $modules = $this->config->get('pagamentodinamico_modules');

            $modules = array_map(function ($array) {
                if (file_exists(DIR_IMAGE . $array['pagamentodinamico_image_success']) && is_file(DIR_IMAGE . $array['pagamentodinamico_image_success'])) {
                    $array['pagamentodinamico_image_src'] = $this->dmodel_tool_image->resize($array['pagamentodinamico_image_success'], 100, 100);
                } else {
                    $array['pagamentodinamico_image_src'] = $this->dmodel_tool_image->resize('no_image.jpg', 100, 100);
                }
                return $array;

            }, $modules);

        } elseif ($this->config->get('pagamentodinamico_opcao')) {
            //codigo feito para retroconpatibilidade
            $modules[] = array(
                'pagamentodinamico_opcao' => $this->config->get('pagamentodinamico_opcao'),
                'pagamentodinamico_total' => $this->config->get('pagamentodinamico_total'),
                'pagamentodinamico_message' => $this->config->get('pagamentodinamico_message'),
                'pagamentodinamico_information' => $this->config->get('pagamentodinamico_information'),
                'pagamentodinamico_image_success' => $this->config->get('pagamentodinamico_image_success')
            );
        } else {
            $modules = array();
        }

        $this->data['data'] = [
            'modules' => $modules,
            'configurations' => [
                'pagamentodinamico_status' => $this->config->get('pagamentodinamico_status'),
                'pagamentodinamico_display' => $this->config->get('pagamentodinamico_display'),
                'pagamentodinamico_sort_order' => $this->config->get('pagamentodinamico_sort_order'),
                'pagamentodinamico_status_iniciado' => $this->config->get('pagamentodinamico_status_iniciado'),
                'pagamentodinamico_status_pago' => $this->config->get('pagamentodinamico_status_pago'),
                'pagamentodinamico_status_baixa' => $this->config->get('pagamentodinamico_status_baixa')
            ],
            'save' => str_replace('&amp;', '&', $urlSave),
            'cancel' => str_replace('&amp;', '&', $urlCancel)
        ];

        $this->load->model('localisation/order_status');
        $this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();

        $this->data['image_success'] = $this->dmodel_tool_image->resize('no_image.jpg', 100, 100);

        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render('payment/pagamentodinamico'));
    }

    public function save()
    {
        $language = $this->load->language('payment/pagamentodinamico');
        $this->data = array_merge($this->data, $language);

        $response = [];

        if (!$this->user->hasPermission('modify', 'payment/pagamentodinamico')) {
            $response['warning'] = $this->language->get('error_permission');
            return $this->response->setOutputJson($response);
        }

        $validate = $this->validateConfig();
        if ($validate) {
            $response['errors'] = $validate;
            return $this->response->setOutputJson($response);
        }

        //remover mask do valor total pq quando fizeram acharam que num ia da nada
        foreach($this->request->post['pagamentodinamico_modules'] as $key => $value){
            if(isset($value['pagamentodinamico_total'])){
                $this->request->post['pagamentodinamico_modules'][$key]['pagamentodinamico_total'] = ftr_geral::removeMaskToDouble($value['pagamentodinamico_total']);
            }
        }

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

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

    private function validateConfig()
    {
        return false;
    }


}
