<?php

class ControllerModuleclearsale extends Controller {

    private $error = array();

    public function index() {
        $language = $this->load->language('module/clearsale');
        $this->data = array_merge($this->data, $language);

        $this->document->setTitle(strip_tags($this->language->get('heading_title')));
        $this->load->model('setting/setting');
        $this->load->helper('ftr_geral');
        $this->load->type('tipocomum');
        $this->load->type('tipoclearsale');

        $this->document->addScript(array(
            'view/public/js/module/clearsale.js',
        ));

        $this->document->addTypesForJs(array(
            Type_ClearSaleIntegracao::getTypeForJs(),
        ));

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {

    		$this->model_setting_setting->editSetting('clearsale', $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->data['token'] = $this->session->data['token'];
        $this->data['cancel'] = $this->url->link('index', 'token=' . $this->session->data['token'], 'SSL');

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

        $this->data['entry_codintegracao']  		 		 = $this->language->get('entry_codintegracao');
        $this->data['entry_status']  		 		         = $this->language->get('entry_status');
        $this->data['entry_homologacao']  		 		   = $this->language->get('entry_homologacao');

        $this->data['ftr_geral'] = new ftr_geral();
        $this->data['Type_Status'] = $this->load->typeForTwig('Type_Status');
        $this->data['Type_ClearSaleIntegracao'] = $this->load->typeForTwig('Type_ClearSaleIntegracao');

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

        if (isset($this->error['codintegracao'])) {
            $this->data['error_codintegracao'] = $this->error['codintegracao'];
        } else {
            $this->data['error_codintegracao'] = '';
        }
        if (isset($this->error['login'])) {
            $this->data['error_login'] = $this->error['login'];
        } else {
            $this->data['error_login'] = '';
        }
        if (isset($this->error['senha'])) {
            $this->data['error_senha'] = $this->error['senha'];
        } else {
            $this->data['error_senha'] = '';
        }
        if (isset($this->error['fingerprint'])) {
            $this->data['error_fingerprint'] = $this->error['fingerprint'];
        } else {
            $this->data['error_fingerprint'] = '';
        }

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];
            unset($this->session->data['success']);
      	} else {
      	    $this->data['success'] = '';
          }

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

        /*infos para o Clearsale start*/
      	if (isset($this->request->post['clearsale_codintegracao'])) {
            $this->data['clearsale_codintegracao'] = $this->request->post['clearsale_codintegracao'];
      	} else{
            $this->data['clearsale_codintegracao'] = $this->config->get('clearsale_codintegracao');
        }
        /*infos para o Clearsale start*/

        /*infos para o Clearsale total*/
        if (isset($this->request->post['clearsale_login'])) {
            $this->data['clearsale_login'] = $this->request->post['clearsale_login'];
      	} else{
            $this->data['clearsale_login'] = $this->config->get('clearsale_login');
        }

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

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

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

        if (isset($this->request->post['clearsale_captura_automatica'])) {
            $this->data['clearsale_captura_automatica'] = $this->request->post['clearsale_captura_automatica'];
      	} else {
            $this->data['clearsale_captura_automatica'] = $this->config->get('clearsale_captura_automatica');
        }
        /*infos para o Clearsale total*/

      	if (isset($this->request->post['clearsale_status'])) {
                  $this->data['clearsale_status'] = $this->request->post['clearsale_status'];
        }elseif($this->config->get('clearsale_status')) {
                  $this->data['clearsale_status'] = $this->config->get('clearsale_status');
      	}else{
                  $this->data['clearsale_status'] = Type_Status::Tp_Desabilitado;
      	}

        if (isset($this->request->post['clearsale_homologacao'])) {
                  $this->data['clearsale_homologacao'] = $this->request->post['clearsale_homologacao'];
        }elseif($this->config->get('clearsale_homologacao')) {
                  $this->data['clearsale_homologacao'] = $this->config->get('clearsale_homologacao');
      	}else{
                  $this->data['clearsale_homologacao'] = Type_Status::Tp_Desabilitado;
      	}

	     $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_module'),
            '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('module/clearsale', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['action'] = $this->url->link('module/clearsale', '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['url_callback'] = $this->urlloja->link('', '', 'NONSSL', false) . 'clearsalecallback';

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

        $this->response->setOutput($this->render('module/clearsale'));
    }

    private function validate() {

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

        if ($this->request->post['clearsale_tipo_integracao'] == Type_ClearSaleIntegracao::Tp_Start) {
            if(trim($this->request->post['clearsale_codintegracao']) == ""){
                $this->error['codintegracao'] =  $this->language->get('error_codintegracao');
            }
        }else{
            if(trim($this->request->post['clearsale_login']) == ""){
                $this->error['login'] =  $this->language->get('error_login');
            }
            if(trim($this->request->post['clearsale_senha']) == ""){
                $this->error['senha'] =  $this->language->get('error_senha');
            }
            if(trim($this->request->post['clearsale_fingerprint']) == ""){
                $this->error['fingerprint'] =  $this->language->get('error_fingerprint');
            }
        }

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


  }
