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

	public function index() {

		$language = $this->language->load('shipping/iFieldSimuleFrete');
		$this->data = array_merge($this->data, $language);

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

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

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

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL'));
		}


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

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

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

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

  		$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->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);

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

		$this->data['action'] = $this->url->link('shipping/iFieldSimuleFrete', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['cancel'] = $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL');

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		if (utf8_strlen($this->request->post['ifield_title']) > 25) {
      $this->error['ifield_title'] = $this->language->get('error_ifield_title');
    }

		if (utf8_strlen($this->request->post['ifield_tokenAPI']) < 1) {
      $this->error['ifield_tokenAPI'] = $this->language->get('error_ifield_tokenAPI');
    }

		if (utf8_strlen($this->request->post['ifield_cepOrigem']) < 1) {
      $this->error['ifield_cepOrigem'] = $this->language->get('error_ifield_cepOrigem');
    }


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