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

	public function index() {
		$language = $this->load->language('shipping/intelipost');
		$this->data = array_merge($this->data, $language);
		$this->document->setTitle($this->language->get('heading_title'));

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

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			if (isset($this->request->post['intelipost_valor_extra'])) {
				$this->request->post['intelipost_valor_extra'] = ftr_geral::removeMaskToDouble($this->request->post['intelipost_valor_extra']);
			}				

			$this->model_setting_setting->editSetting('intelipost', $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->load->type('tipocomum');
    $this->load->type('tipofrete');

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

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

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


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

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

		$this->data['action'] = $this->url->link('shipping/intelipost', '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');

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

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

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

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

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

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

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

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

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

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

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


		foreach(Type_Status::getEnumList() as $key  => $value) {
			$this->data['status_intelipost_list'][] = array(
			  'key'      => $key,
			  'selected' => ftr_geral::isSelected($key, $this->data['intelipost_status']),
			  'value'    => $value
			);
    }

		foreach(Type_IntelipostTipoCote::getEnumList() as $key  => $value) {
			$this->data['intelipost_tipo_cotacao_list'][] = array(
			  'key'      => $key,
			  'selected' => ftr_geral::isSelected($key, $this->data['intelipost_tipo_cotacao']),
			  'value'    => $value
			);
    }

		foreach(Type_AcrescimoDesconto::getEnumList() as $key  => $value) {
			$this->data['intelipost_tipo_valor_extra_list'][] = array(
			  'key'      => $key,
			  'selected' => ftr_geral::isSelected($key, $this->data['intelipost_tipo_valor_extra']),
			  'value'    => $value
			);
    }

		$this->template = 'shipping/intelipost.twig';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	private function validate() {

		$this->load->helper('ftr_validacao');


		if (!$this->user->hasPermission('modify', 'shipping/intelipost')) {
			$this->error['warning'] = "Acesso negado!";
		}

  	if(!isset($this->request->post['intelipost_descricao'])||strlen($this->request->post['intelipost_descricao']) == ''){
    	$this->error['intelipost_descricao'] = $this->language->get('error_intelipost_descricao');
    }

  	if(!isset($this->request->post['intelipost_cep_origem'])||strlen(str_replace('-','',$this->request->post['intelipost_cep_origem'])) == ''){
    	$this->error['intelipost_cep_origem'] = $this->language->get('error_intelipost_cep_origem');
    }

  	if(!isset($this->request->post['intelipost_api_key'])||strlen($this->request->post['intelipost_api_key']) == ''){
    	$this->error['intelipost_api_key'] = $this->language->get('error_intelipost_api_key');
  	}

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