<?php

class ControllerShippingfretedinamico extends Controller {
	private $error = array();

	public function index() {
		$this->load->language('shipping/fretedinamico');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('setting/setting');
		$this->load->type('tipocomum');
		$this->load->helper('ftr_geral');

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

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			if (isset($this->request->post['fretedinamico_valcalc'])) {
				$this->request->post['fretedinamico_valcalc'] = ftr_geral::removeMaskToDouble($this->request->post['fretedinamico_valcalc']);
			}			
            $this->model_setting_setting->editSetting('fretedinamico', $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['heading_title']          = $this->language->get('heading_title');
		$this->data['text_yes']               = $this->language->get('text_yes');
		$this->data['text_no']                = $this->language->get('text_no');
		$this->data['button_save']            = $this->language->get('button_save');
		$this->data['button_cancel']          = $this->language->get('button_cancel');
		$this->data['text_entrgnome']         = $this->language->get('text_entrgnome');
		$this->data['text_status']            = $this->language->get('text_status');
		$this->data['text_ordem']             = $this->language->get('text_ordem');
		$this->data['text_disabled']          = $this->language->get('text_disabled');
		$this->data['text_enabled']           = $this->language->get('text_enabled');
		$this->data['text_dinamicopcao']      = $this->language->get('text_dinamicopcao');
		$this->data['text_dinamicopcao_sub']  = $this->language->get('text_dinamicopcao_sub');
		$this->data['text_val_calculo']       = $this->language->get('text_val_calculo');
		$this->data['text_val_visualizacao']  = $this->language->get('text_val_visualizacao');

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

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

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

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

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


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

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

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

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

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

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

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

		
		if (isset($this->request->post['fretedinamico_valcalc'])) {
			$this->data['fretedinamico_valcalc'] = $this->request->post['fretedinamico_valcalc'];
		} else {
			$this->data['fretedinamico_valcalc'] = str_replace(',', '.', $this->config->get('fretedinamico_valcalc'));
		}
		
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render('shipping/fretedinamico'));
	}

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


        if (strlen($this->request->post['fretedinamico_opcao']) == ''){
			$this->error['fretedinamico_opcao'] = 'Campo Obrigatório!';
        }


        if (strlen($this->request->post['fretedinamico_subopcao']) == ''){
			$this->error['fretedinamico_subopcao'] = 'Campo Obrigatório!';
        }

        if (strlen($this->request->post['fretedinamico_valvisu']) == ''){
			$this->error['fretedinamico_valvisu'] = 'Campo Obrigatório!';
        }

        if (strlen($this->request->post['fretedinamico_valcalc']) == ''){
			$this->error['fretedinamico_valcalc'] = 'Campo Obrigatório!';
        }



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

}
