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

    public function index() {
      	$this->load->language('module/futuralimitaritem');
      	$this->document->setTitle($this->language->get('heading_title'));

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

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

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

        $this->data['button_save']	 = $this->language->get('button_save');
        $this->data['button_cancel']  	 = $this->language->get('button_cancel');
        $this->data['heading_title'] 	 = $this->language->get('heading_title');
        $this->data['text_maxitem'] 	 = $this->language->get('text_maxitem');
        $this->data['text_habmaxitem'] 	 = $this->language->get('text_habmaxitem');
        $this->data['text_tipolimite'] 	 = $this->language->get('text_tipolimite');
        $this->data['text_tipopessoa'] 	 = $this->language->get('text_tipopessoa');


        $this->data['action'] = $this->url->link('module/futuralimitaritem', '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['qtd_maxitemcart'])) {
            $this->data['qtd_maxitemcart']= $this->request->post['qtd_maxitemcart'];
        }elseif($this->config->get('qtd_maxitemcart')) {
            $this->data['qtd_maxitemcart']= $this->config->get('qtd_maxitemcart');
	      }else{
            $this->data['qtd_maxitemcart']= 0;
        }

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

      	if (isset($this->request->post['tipolimite'])) {
                  $this->data['tipolimite']= $this->request->post['tipolimite'];
        }elseif($this->config->get('tipolimite')) {
                  $this->data['tipolimite']= $this->config->get('tipolimite');
      	}else{
                  $this->data['tipolimite']= Type_LimitacaoItems::Tp_Variedades;
        }

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

        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'] = '';
      	}




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

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

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

    }

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

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

        if(isset($this->request->post['qtd_maxitemcart']) && ($this->request->post['qtd_maxitemcart']!='') && ($this->request->post['qtd_maxitemcart']==0) ){
            $this->error['warning'] = $this->language->get('error_qtd_maxitemcart_0');
        }

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

}
