<?php

class ControllerModuleFidelity extends Controller
{

  private $error = array();

  public function index()
  {

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

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

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

    $this->document->addScript(array(
      'view/javascript/module.fidelity.js'
    ));

    if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
      $this->model_setting_setting->editSetting('fidelity', $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['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/fidelity', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL'),
      'separator' => ' :: '
    );


    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['cancel'] = $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL');
    $this->data['action'] = $this->url->link('module/fidelity', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL');

    $config_data = array(
      'fidelity_status',
      'fidelity_empresa_id',
      'fidelity_mininum_points',
      'fidelity_status_initial',
      'fidelity_status_contemplado',
      'fidelity_category',
      'fidelity_range',
      'fidelity_module',
      'indication_status',
      'indication_status_order',
      'indication_points'
    );

    foreach ($config_data as $conf) {
      if (isset($this->request->post[$conf])) {
        $this->data[$conf] = $this->request->post[$conf];
      } else {
        $this->data[$conf] = $this->config->get($conf);
      }
    }

    $this->data['status_list'] = ftr_geral::getArrayTypes(Type_Status::getEnumList(), $this->data['fidelity_status'], Type_Status::Tp_Desabilitado);
    $this->data['fidelity_category_list'] = ftr_geral::getArrayTypes(Type_Status::getEnumList(), $this->data['fidelity_category'], Type_Status::Tp_Desabilitado);
    $this->data['fidelity_range_list'] = ftr_geral::getArrayTypes(Type_Status::getEnumList(), $this->data['fidelity_range'], Type_Status::Tp_Desabilitado);

    $this->data['indication_status_list'] = ftr_geral::getArrayTypes(Type_Status::getEnumList(), $this->data['indication_status'], Type_Status::Tp_Desabilitado);

    $this->load->dmodel('design/layout');
    $data = array(
      'notarray' => Type_NotListaProdutos::getEnumList()
    );
    $this->data['layouts'] = $this->dmodel_design_layout->getLayouts($data);
    $this->data['positions'] = Type_ModulePosition::getEnumList(true);
    $this->data['status'] = Type_Status::getEnumList();
    $this->data['fidelity_layout'] = $this->dmodel_design_layout->getLayoutByName('fidelidade');

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

    $this->load->dmodel('wsceo/cadastro/empresa');
    $empresas = $this->dmodel_wsceo_cadastro_empresa->consultarEmpresaFidelidade();
    $this->data['empresas'] = array ();
    if (is_object($empresas)) {
        $this->data['error_ceo'] = $empresas;
    } else {
        if ($empresas){
        foreach ($empresas as $key => $empresa) {
            if(is_null($empresa['empresa_id'])){
            $this->data['empresas'][] = array(
                'value' => Type_FidelityConfigs::Tp_Multiempresa,
                'description' => Type_FidelityConfigs::getNickFromValue(Type_FidelityConfigs::Tp_Multiempresa),
                'choice' => ftr_geral::isSelected(Type_FidelityConfigs::Tp_Multiempresa, $this->data['fidelity_empresa_id'])
            );
            }else{
            $this->data['empresas'][] = array(
                'value' => $empresa['empresa_id'],
                'description' => $empresa['razao_social'],
                'choice' => ftr_geral::isSelected($empresa['empresa_id'], $this->data['fidelity_empresa_id'])
            );
            }
        }
        }
    }

    $config_error = array(
      'error_warning',
      'error_fidelity_empresa_id'
    );

    foreach ($config_error as $conf) {
      if (isset($this->error[$conf])) {
        $this->data[$conf] = $this->error[$conf];
      } else {
        $this->error[$conf] = false;
      }
    }

    $this->template = 'module/fidelity.twig';
    $this->children = array(
      'common/header',
      'common/footer'
    );

    $this->response->setOutput($this->render());

  }


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

    if(!property_exists((Object)$this->request->post, 'fidelity_empresa_id') || isset($this->request->post['fidelity_empresa_id'])
        && empty($this->request->post['fidelity_empresa_id']) || (is_null($this->request->post['fidelity_empresa_id']))){
      $this->error['error_fidelity_empresa_id'] = $this->language->get('error_text_fidelity_empresa_id');
    }

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

  public function install()
  {
    $this->load->model('setting/extension');

    $this->model_setting_extension->install('payment', Type_FidelityPayment::Tp_FidelityPayment);

  }

}
