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

	public function index() {
		$this->load->type('tipocomum');
		$this->load->language('module/modulofutura');

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

		$this->load->model('setting/setting');
		$this->load->dmodel('wsceo/cadastro/empresa');
		$this->load->helper('ftr_geral');

		$this->document->addScript('admin/public/js/module/modulofutura.js');

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

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate()) ) {
			$this->model_setting_setting->editSettingModuloFutura('modulofutura', $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'));
		}

		$language = $this->load->language('module/modulofutura');
		$this->data = array_merge($this->data, $language);

		$this->data['link'] = $this->url->link('catalog/product/update', '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['futura_serverftp'])) {
			$this->data['error_serverftp'] = $this->error['futura_serverftp'];
		} else {
			$this->data['error_serverftp'] = '';
		}

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

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

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

		$this->data['action'] = $this->url->link('module/modulofutura', '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->children = array(
			'common/header',
			'common/footer',
		);

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		if (isset($this->request->post['futura_id_empresa'])) {
			$this->data['futura_id_empresa'] = $this->request->post['futura_id_empresa'];
		} else {
			$this->data['futura_id_empresa'] = $this->config->get('futura_id_empresa');
		}		
		$empresas = $this->dmodel_wsceo_cadastro_empresa->consultarEmpresa();
		$this->data['empresas'] = array ();
		if ( is_array($empresas) && count($empresas)>0 ){
			foreach ($empresas as $key => $empresa) {
				$this->data['empresas'][] = array(
					'id'     => $empresa['id'],
					'name'   => $empresa['fantasia'],
					'select' => (ftr_geral::isIsset($this->config->get('futura_id_empresa')) && ($this->config->get('futura_id_empresa') == $empresa['id'])) ? true : false
				);
			}
		}		

		$this->load->model('localisation/order_status');
		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();

		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();

		$this->load->model('localisation/weight_class');
		$this->data['weight_classes'] = $this->model_localisation_weight_class->getWeightClasses();

		$this->load->model('setting/store');
		$this->data['stores'] = $this->model_setting_store->getStores();

		$this->load->model('localisation/length_class');
		$this->data['length_classes'] = $this->model_localisation_length_class->getLengthClasses();

        $this->data['userGroup'] = ($this->user->getUserGroup() == Type_UserGroupAdm::Tp_UserFutura) ? true : false ;

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

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

		if (!$this->request->post['futura_serverftp']) {
			$this->error['futura_serverftp'] = $this->language->get('error_serverftp');
		}

		if (!$this->request->post['futura_userftp']) {
			$this->error['futura_userftp'] = $this->language->get('error_userftp');
		}

		if (!$this->request->post['futura_senhaftp']) {
			$this->error['futura_senhaftp'] = $this->language->get('error_senhaftp');
		}

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

	}
}
