<?php
class ControllerPaymentCod extends Controller
{
    private $error = array();

    public function index()
    {
        $this->language->load('payment/cod');

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

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

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

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('setting/setting');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            $this->removeMasks();
            $this->model_setting_setting->editSetting('cod', $this->request->post, 0, ['cod_shipping_methods']);

            $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->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_enabled'] = $this->language->get('text_enabled');
        $this->data['text_disabled'] = $this->language->get('text_disabled');
        $this->data['text_all_zones'] = $this->language->get('text_all_zones');
        $this->data['text_shipping'] = $this->language->get('text_shipping');

        $this->data['entry_order_status'] = $this->language->get('entry_order_status');
        $this->data['entry_title'] = $this->language->get('entry_title');
        $this->data['entry_total'] = $this->language->get('entry_total');
        $this->data['entry_total_obs'] = $this->language->get('entry_total_obs');
        $this->data['entry_geo_zone'] = $this->language->get('entry_geo_zone');
        $this->data['entry_status'] = $this->language->get('entry_status');
        $this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
        $this->data['entry_display'] = $this->language->get('entry_display');


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

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

        $this->data['breadcrumbs'] = array();

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

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

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

        $this->data['action'] = $this->url->link('payment/cod', '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->load->model('setting/extension');
        $results = $this->model_setting_extension->getExtensions('shipping');
        if ($results) {
            $results = array_filter($results, function($result) {
                return $this->config->get($result['code'] . '_status');
            });

            $shipping_methods = $this->config->get('cod_shipping_methods') ?? [];

            $results = array_map(function($result) use ($shipping_methods) {
                $result['title'] = $this->config->get($result['code'] . '_titlefutura');
                $result['checked'] = in_array($result['code'], $shipping_methods);
                return $result;
            }, $results);
        }

        $this->data['shipping_methods'] = $results ?? [];

        if (isset($this->request->post['cod_title'])) {
            $this->data['cod_title'] = $this->request->post['cod_title'];
        } else {
            $this->data['cod_title'] = $this->config->get('cod_title');
        }

        if (isset($this->request->post['cod_total'])) {
            $this->data['cod_total'] = $this->request->post['cod_total'];
        } else {
            $this->data['cod_total'] = $this->config->get('cod_total');
        }

        if (isset($this->request->post['cod_order_status_id'])) {
            $this->data['cod_order_status_id'] = $this->request->post['cod_order_status_id'];
        } else {
            $this->data['cod_order_status_id'] = $this->config->get('cod_order_status_id');
        }

        $this->load->model('localisation/order_status');

        $this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();

        if (isset($this->request->post['cod_geo_zone_id'])) {
            $this->data['cod_geo_zone_id'] = $this->request->post['cod_geo_zone_id'];
        } else {
            $this->data['cod_geo_zone_id'] = $this->config->get('cod_geo_zone_id');
        }

        $this->load->model('localisation/geo_zone');

        $this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();

        if (isset($this->request->post['cod_display'])) {
            $this->data['cod_display'] = $this->request->post['cod_display'];
        } else {
            $this->data['cod_display'] = $this->config->get('cod_display');
        }

        if (isset($this->request->post['cod_status'])) {
            $this->data['cod_status'] = $this->request->post['cod_status'];
        } else {
            $this->data['cod_status'] = $this->config->get('cod_status');
        }

        if (isset($this->request->post['cod_sort_order'])) {
            $this->data['cod_sort_order'] = $this->request->post['cod_sort_order'];
        } else {
            $this->data['cod_sort_order'] = $this->config->get('cod_sort_order');
        }

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

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

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

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

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