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

	public function index() {
	$language = $this->load->language('shipping/dlog');
	$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['dlog_valor_extra'])) {
			$this->request->post['dlog_valor_extra'] = ftr_geral::removeMaskToDouble($this->request->post['dlog_valor_extra']);
		}
		$this->model_setting_setting->editSetting('dlog', $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['dlog_descricao'])) {
			$this->data['error_dlog_descricao'] = $this->error['dlog_descricao'];
		} else {
			$this->data['error_dlog_descricao'] = '';
    }

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

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

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

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


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

		$this->data['action'] = $this->url->link('shipping/dlog', '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['dlog_descricao'])) {
			$this->data['dlog_descricao'] = $this->request->post['dlog_descricao'];
		} else if($this->config->get('dlog_descricao')) {
			$this->data['dlog_descricao'] = $this->config->get('dlog_descricao');
		} else {
            $this->data['dlog_descricao'] = $this->language->get('heading_title');
        }

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

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

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

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

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


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

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


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

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

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


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

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

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


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


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


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

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

		$this->template = 'shipping/dlog.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/dlog')) {
			$this->error['warning'] = "Acesso negado!";
		}

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

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

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

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

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

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