<?php

class ControllerModuleFuturaFiltroValor extends Controller {

    private $error = array();

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

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

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

        $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');

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

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

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

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

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

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
            $this->model_setting_setting->editSetting('futurafiltrovalor', $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['token'] = $this->session->data['token'];
        $this->data['cancel'] = $this->url->link('index', 'token=' . $this->session->data['token'], 'SSL');

        // declaração das variaveis, pegando o nome expresso na linguagem

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

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

        $this->data['text_enabled']                     = $this->language->get('text_enabled');
        $this->data['text_disabled']                    = $this->language->get('text_disabled');
        $this->data['text_futurafiltrovalor_status']    = $this->language->get('text_futurafiltrovalor_status');
        $this->data['text_color_btn_price']             = $this->language->get('text_color_btn_price');
        $this->data['text_color_btn_price_houver']      = $this->language->get('text_color_btn_price_houver');
        $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['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');


          //ate aqui

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

        if (isset($this->session->data['success'])) {
    	    $this->data['success'] = $this->session->data['success'];
            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }


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

    		$this->data['modules'] = array();

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

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

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

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

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

        if (isset($this->request->post['futurafiltrovalor_module'])) {
            $layouts = array();
            foreach ($this->request->post['futurafiltrovalor_module'] as $module) {
                if (in_array($module['layout_id'], $layouts)) {
                    $this->error['warning'] = $this->language->get('error_duplicate');
                    break;
                }
                $layouts[] = $module['layout_id'];
            }
        }

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

  }
