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

	public function index() {
    $language = $this->language->load('module/futuragrade');

    $this->data = array_merge($this->data, $language);

    $this->data['languageForJS'] = array(
      'module/futuragrade' => $language
    );

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

		$this->load->model('setting/setting');
		$this->load->helper('ftr_geral');
    $this->load->type('tipocomum');

    $this->document->addTypesForJs(array(
      Type_DsimDnao::getTypeForJs(),
      Type_Status::getTypeForJs(),
      Type_ModulePosition::getTypeForJs(),
    ));

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

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate()) ) {
			$this->model_setting_setting->editSetting('futuragrade', $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['ftr_geral'] = new ftr_geral();
    $this->data['Type_Status'] = $this->load->typeForTwig('Type_Status');
    $this->data['Type_DsimDnao'] = $this->load->typeForTwig('Type_DsimDnao');

    $this->data['positions'] = Type_ModulePosition::getRemoveArray([Type_ModulePosition::Tp_AppMobile], true);

		$this->data['heading_title']      = $this->language->get('heading_title');
		$this->data['text_enabled']       = $this->language->get('text_enabled');
		$this->data['text_disabled']      = $this->language->get('text_disabled');
		$this->data['entry_status']       = $this->language->get('entry_status');
		$this->data['button_save']        = $this->language->get('button_save');
		$this->data['button_cancel']      = $this->language->get('button_cancel');
		$this->data['entry_order_status'] = $this->language->get('entry_order_status');
    	$this->data['entry_order_filtro'] = $this->language->get('entry_order_filtro');
		$this->data['entry_order_status_cancel'] = $this->language->get('entry_order_status_cancel');
		$this->data['language_default']   = $this->language->get('language_default');
		$this->data['store_default']      = $this->language->get('store_default');
		$this->data['text_conf']          = $this->language->get('text_conf');
		$this->data['text_tipo_ex_cor']    = $this->language->get('text_tipo_ex_cor');
		$this->data['text_mostrar_todos']    = $this->language->get('text_mostrar_todos');
		$this->data['text_quantidade_exibida']    = $this->language->get('text_quantidade_exibida');
		$this->data['text_tipo_ex_button'] = $this->language->get('text_tipo_ex_button');
		$this->data['text_tipo_ex_Img']    = $this->language->get('text_tipo_ex_Img');
		$this->data['text_conf_gerais']    = $this->language->get('text_conf_gerais');
		$this->data['text_conf_filtros']   = $this->language->get('text_conf_filtros');
		$this->data['text_title_specification']   = $this->language->get('text_title_specification');
		$this->data['text_abrev_specification']   = $this->language->get('text_abrev_specification');
		$this->data['text_alert_title_specification']   = $this->language->get('text_alert_title_specification');
		$this->data['text_alert_abrev_title_specification']   = $this->language->get('text_alert_abrev_title_specification');

		$this->data['entry_banner'] = $this->language->get('entry_banner');
		$this->data['entry_limit'] = $this->language->get('entry_limit');
		$this->data['entry_scroll'] = $this->language->get('entry_scroll');
		$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['entry_layout'] = $this->language->get('entry_layout');
		$this->data['entry_position'] = $this->language->get('entry_position');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_link_opcao'] = $this->language->get('entry_link_opcao');
    $this->data['entry_list_product'] = $this->language->get('entry_list_product');
    $this->data['entry_list_product_obs'] = $this->language->get('entry_list_product_obs');

		$this->data['button_add_module'] = $this->language->get('button_add_module');
		$this->data['button_remove'] = $this->language->get('button_remove');

		$this->data['text_content_top'] = $this->language->get('text_content_top');
		$this->data['text_content_bottom'] = $this->language->get('text_content_bottom');
		$this->data['text_column_left'] = $this->language->get('text_column_left');
		$this->data['text_column_right'] = $this->language->get('text_column_right');
		$this->data['text_module'] = $this->language->get('text_module');

		$this->data['text_exibe_cor']           = $this->language->get('text_exibe_cor');
		$this->data['text_exibe_size']          = $this->language->get('text_exibe_size');
		$this->data['text_exibe_specification'] = $this->language->get('text_exibe_specification');
		$this->data['text_sim']                 = $this->language->get('text_sim');
		$this->data['text_nao']                 = $this->language->get('text_nao');

		$this->data['text_img_size'] = $this->language->get('text_img_size');

		$this->data['link'] = $this->url->link('catalog/product/update', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['futuragrade_status'] = $this->config->get('futuragrade_status');

		$this->data['futuragrade_ex_tipo'] = $this->config->get('futuragrade_ex_tipo');

    $futuragrade_quantidade_exibicao = $this->config->get('futuragrade_quantidade_exibicao');
    $this->data['futuragrade_quantidade_exibicao']= isset($futuragrade_quantidade_exibicao) ? $futuragrade_quantidade_exibicao : Type_DefaultValues::Tp_Futuragrade_Quantidade_Exibicao;

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

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

		$this->data['action'] = $this->url->link('module/futuragrade', '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->data['modules'] = array();

		if (isset($this->request->post['futuragrade_order_filtro'])) {
			$this->data['futuragrade_order_filtro'] = $this->request->post['futuragrade_order_filtro'];
		} else {
			$this->data['futuragrade_order_filtro'] = $this->config->get('futuragrade_order_filtro');
			if(!isset($this->data['futuragrade_order_filtro'])){
				$this->data['futuragrade_order_filtro'] = Type_Status::Tp_Habilitado;
			}
		}

		if (isset($this->request->post['futuragrade_opcao_cor'])) {
			$this->data['futuragrade_opcao_cor'] = $this->request->post['futuragrade_opcao_cor'];
		} else {
			$this->data['futuragrade_opcao_cor'] = $this->config->get('futuragrade_opcao_cor');
			if(!isset($this->data['futuragrade_opcao_cor'])){
				$this->data['futuragrade_opcao_cor'] = Type_Status::Tp_Habilitado;
			}
		}

		if (isset($this->request->post['futuragrade_list_product'])) {
			$this->data['futuragrade_list_product'] = $this->request->post['futuragrade_list_product'];
		} else {
			$this->data['futuragrade_list_product'] = $this->config->get('futuragrade_list_product');
			if(!isset($this->data['futuragrade_list_product'])){
				$this->data['futuragrade_list_product'] = Type_Status::Tp_Desabilitado;
			}
		}

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

		if (isset($this->request->post['futuragrade_img_witdh'])) {
		    $this->data['futuragrade_img_witdh'] = $this->request->post['futuragrade_img_witdh'];
		} else{
			if(ftr_geral::isIsset($this->config->get('futuragrade_img_height'))){
					$this->data['futuragrade_img_witdh'] = $this->config->get('futuragrade_img_witdh');
			}else{
					$this->data['futuragrade_img_witdh'] = Type_ProdutoGradeImg::Tp_Width ;
			}
		}

		if (isset($this->request->post['futuragrade_img_height'])) {
		    $this->data['futuragrade_img_height'] = $this->request->post['futuragrade_img_height'];
		} else{
				if(ftr_geral::isIsset($this->config->get('futuragrade_img_height'))){
						$this->data['futuragrade_img_height'] = $this->config->get('futuragrade_img_height');
				}else{
						$this->data['futuragrade_img_height'] = Type_ProdutoGradeImg::Tp_Height ;
				}
		}

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

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

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

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

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

		$this->load->dmodel('design/layout');

		$arrayRemove = array(
			Type_IdLayout::Tp_IdHome,
			Type_IdLayout::Tp_IdProduct,
			Type_IdLayout::Tp_IdConta,
			Type_IdLayout::Tp_IdConfirme,
			Type_IdLayout::Tp_IdContact,
			Type_IdLayout::Tp_IdSiteMap,
			Type_IdLayout::Tp_IdInformation,
		);

    $layouts = $this->dmodel_design_layout->getLayouts();
    $layouts = array_filter($layouts, function ($value) use($arrayRemove) {
                 return !in_array($value['layout_id'], $arrayRemove);
               });
    $this->data['layouts'] = $layouts;

		$this->load->model('design/banner');

		$this->data['banners'] = $this->model_design_banner->getBanners();

		$this->children = array(
			'common/header',
			'common/footer',
		);

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

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

		if(isset($this->request->post['futuragrade_ex_tipo']) && ($this->request->post['futuragrade_ex_tipo']) ){

			 if(isset($this->request->post['futuragrade_img_witdh']) && ($this->request->post['futuragrade_img_witdh']=='') ){
				 $this->error['warning'] =  $this->language->get('error_img_width') ;
			 }

			 if(isset($this->request->post['futuragrade_img_height']) && ($this->request->post['futuragrade_img_height']=='') ){
				 $this->error['warning'] =  $this->language->get('error_img_height') ;
			 }

		}

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

	}
}
