<?php

class ControllerPaymentCieloIntegradoFutura extends Controller
{

    private $error = array();
    public $opcoes = array();

    public function index()
    {

        $this->load->language("payment/cielointegradofutura");

        $this->document->setTitle("Checkout Cielo");

        $this->load->helper('ftr_geral');
        $this->load->type('tipocomum');

        $this->data['ftr_geral'] = new ftr_geral();

        $this->data['Type_DisplayModuleOption'] = $this->load->typeForTwig('Type_DisplayModuleOption');

        $this->load->model("setting/setting");
        if (($this->request->server["REQUEST_METHOD"] == "POST") && $this->validate()) {
            $this->removeMasks();
            $this->model_setting_setting->editSetting("cielointegradofutura", $this->request->post);
            $this->session->data["success"] = $this->language->get("text_success");
            $this->redirect($this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'));
        }

        $this->opcoes["heading_title"] = $this->language->get("heading_title");
        $this->opcoes["text_enabled"] = $this->language->get("text_enabled");
        $this->opcoes["text_disabled"] = $this->language->get("text_disabled");
        $this->opcoes["button_save"] = $this->language->get("button_save");
        $this->opcoes["button_cancel"] = $this->language->get("button_cancel");
        $this->opcoes["text_client"] = $this->language->get("text_client");
        $this->opcoes["text_client_obs"] = $this->language->get("text_client_obs");
        $this->opcoes["text_ambiente"] = $this->language->get("text_ambiente");
        $this->opcoes["text_ambiente_obs"] = $this->language->get("text_ambiente_obs");

        $this->opcoes["text_cnpj"] = $this->language->get("text_cnpj");
        $this->opcoes["text_titulo"] = $this->language->get("text_titulo");
        $this->opcoes["text_status"] = $this->language->get("text_status");
        $this->opcoes["text_zona"] = $this->language->get("text_zona");
        $this->opcoes["text_ordem"] = $this->language->get("text_ordem");
        $this->opcoes["text_total_minimo"] = $this->language->get("text_total_minimo");
        $this->opcoes["text_chave_mid"] = $this->language->get("text_chave_mid");
        $this->opcoes["text_antifraude"] = $this->language->get("text_antifraude");
        $this->opcoes["text_total_minimo_antifraude"] = $this->language->get("text_total_minimo_antifraude");
        $this->opcoes["text_soft_descripto"] = $this->language->get("text_soft_descripto");
        $this->opcoes["text_aguardando_pagamento"] = $this->language->get("text_aguardando_pagamento");
        $this->opcoes["text_pago"] = $this->language->get("text_pago");
        $this->opcoes["text_pendente"] = $this->language->get("text_pendente");
        $this->opcoes["text_negado"] = $this->language->get("text_negado");
        $this->opcoes["text_autorizado"] = $this->language->get("text_autorizado");
        $this->opcoes["text_cancelado"] = $this->language->get("text_cancelado");
        $this->opcoes["text_url_retorno"] = $this->language->get("text_url_retorno");
        $this->opcoes["text_url_notificacao"] = $this->language->get("text_url_notificacao");
        $this->opcoes["text_url_status"] = $this->language->get("text_url_status");

        $this->data['entry_display'] = $this->language->get('entry_display');

        if (isset($this->error["warning"])) {
            $this->data["error_warning"] = $this->error["warning"];
        } else {
            $this->data["error_warning"] = "";
        }

        if (isset($this->error["email"])) {
            $this->data["error_email"] = $this->error["email"];
        } else {
            $this->data["error_email"] = "";
        }

        if (isset($this->error['error_cielointegradofutura_titulo'])) {
            $this->data['error_cielointegradofutura_titulo'] = $this->error['error_cielointegradofutura_titulo'];
        } else {
            $this->data['error_cielointegradofutura_titulo'] = '';
        }

        if (isset($this->error['error_cielointegradofutura_sort_order'])) {
            $this->data['error_cielointegradofutura_sort_order'] = $this->error['error_cielointegradofutura_sort_order'];
        } else {
            $this->data['error_cielointegradofutura_sort_order'] = '';
        }

        if (isset($this->error['error_cielointegradofutura_id'])) {
            $this->data['error_cielointegradofutura_id'] = $this->error['error_cielointegradofutura_id'];
        } else {
            $this->data['error_cielointegradofutura_id'] = '';
        }

        $this->data["breadcrumbs"] = array();
        $this->data["breadcrumbs"][] = array(
            "text" => $this->language->get("text_home"),
            "separator" => false,
            "href" => $this->url->link("common/dashboard", "token=" . $this->session->data["token"], "SSL")
        );

        $this->data["breadcrumbs"][] = array(
            "text" => $this->language->get("text_payment"),
            "separator" => " :: ",
            "href" => $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL')
        );

        $this->data["breadcrumbs"][] = array(
            "text" => $this->language->get("text_payment"),
            "separator" => " :: ",
            "href" => $this->url->link("payment/cielointegradofutura", "token=" . $this->session->data["token"] . '&type=' . $this->request->get['type'], "SSL")
        );

        $this->data["action"] = $this->url->link("payment/cielointegradofutura", "token=" . $this->session->data["token"] . '&type=' . $this->request->get['type'], "SSL");
        $this->data["cancel"] = $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL');
        $this->data["ret"] = str_replace("/admin/", "/", $this->url->link("payment/cielointegradofutura/retorno", "", "SSL"));
        $this->data["notification"] = str_replace("/admin/", "/", $this->url->link("payment/cielointegradofutura/notificacao", "", "SSL"));
        $this->data["sta"] = str_replace("/admin/", "/", $this->url->link("payment/cielointegradofutura/status", "", "SSL"));

        $this->data["idPagamento"] = 1;
        $this->data["mod_ativado"] = $this->data["idPagamento"];

        if (($this->data["idPagamento"] == 1)) {
            $this->get("cielointegradofutura_client_teste");
            $this->get("cielointegradofutura_ambiente_teste");
            $this->get("cielointegradofutura_fraude");
            $this->get("cielointegradofutura_status");
            $this->get("cielointegradofutura_titulo");
            $this->get("cielointegradofutura_geo_zone_id");
            $this->get("cielointegradofutura_sort_order");
            $this->get("cielointegradofutura_total");
            $this->get("cielointegradofutura_total_fraude");
            $this->get("cielointegradofutura_id");
            $this->get("cielointegradofutura_soft");
            $this->get("cielointegradofutura_in");
            $this->get("cielointegradofutura_pg");
            $this->get("cielointegradofutura_cnpj");
            $this->get("cielointegradofutura_pe");
            $this->get("cielointegradofutura_ca");
            $this->get("cielointegradofutura_ne");
            $this->get("cielointegradofutura_au");
            $this->get("cielointegradofutura_display");
        }

        if(!isset($this->opcoes["cielointegradofutura_cnpj"])){
            $this->model_setting_setting->insertSettingValue('cielointegradofutura', 'cielointegradofutura_cnpj', '01.027.058/0001-91');
            $this->opcoes["cielointegradofutura_cnpj"] = '01.027.058/0001-91';
        }

        foreach ($this->opcoes as $key => $value) {
            $this->data[$key] = $value;
        }

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

        $this->load->model("localisation/geo_zone");
        $this->data["geo_zones"] = $this->model_localisation_geo_zone->getGeoZones();

        $this->data["tema"] = ($this->data["idPagamento"] == 1) ? "payment/cielointegradofutura" : "payment/cielointegradofutura_ativar";
        $this->template = $this->data["tema"];
        $this->children = array(
            "common/header",
            "common/footer"
        );
        $this->response->setOutput($this->render($this->data["tema"]));
    }

    public function removeMasks(){
        if(isset($this->request->post['cielointegradofutura_total']) && $this->request->post['cielointegradofutura_total']){
            $this->request->post['cielointegradofutura_total'] = ftr_geral::removeMaskToDouble($this->request->post['cielointegradofutura_total']);
        }

        if(isset($this->request->post['cielointegradofutura_total_fraude']) && $this->request->post['cielointegradofutura_total_fraude']){
            $this->request->post['cielointegradofutura_total_fraude'] = ftr_geral::removeMaskToDouble($this->request->post['cielointegradofutura_total_fraude']);
        }
    }

    public function get($campo)
    {
        if (isset($this->request->post[$campo])) {
            $this->opcoes[$campo] = $this->request->post[$campo];
        } else {
            $this->opcoes[$campo] = $this->config->get($campo);
        }
    }

    protected function validate()
    {
        if (!$this->user->hasPermission('modify', 'module/fields_register_brazil')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }


        if ($this->request->post['cielointegradofutura_titulo'] == '') {
            $this->error['error_cielointegradofutura_titulo'] = $this->language->get('cielointegradofutura_titulo');
        }

        if ($this->request->post['cielointegradofutura_sort_order'] == '') {
            $this->error['error_cielointegradofutura_sort_order'] = $this->language->get('cielointegradofutura_sort_order');
        }

        if ($this->request->post['cielointegradofutura_id'] == '') {
            $this->error['error_cielointegradofutura_id'] = $this->language->get('cielointegradofutura_id');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }
}
