<?php

class ControllerFidelitySuccess extends Controller
{
  public function index()
  {
    $this->load->helper('ftr_geral');

    if (!$this->customer->isLogged()) {
      $this->redirect($this->url->link('account/logout', '', 'SSL'));
    }

    if (isset($this->session->data['order_id'])) {
      /*
       * sempre que tem um pedido novo reseta as informações da payment_session
       * sessão criada para não perder os dados do pedido caso o cliente de f5 na tela de sucesso.
       */
      unset($this->session->data['payment_session']);
      $this->session->data['payment_session'] = array(
        'order_id' => $this->session->data['order_id'],
        'code' => isset($this->session->data['payment_method']['code']) ? $this->session->data['payment_method']['code'] : false,
        'code_type' => isset($this->session->data['payment_method']['code_type']) ? $this->session->data['payment_method']['code_type'] : false
      );

      $this->fidelitycart->clear();

      unset($this->session->data['shipping_method']);
      unset($this->session->data['shipping_methods']);
      unset($this->session->data['payment_method']);
      unset($this->session->data['payment_methods']);
      unset($this->session->data['guest']);
      unset($this->session->data['comment']);
      unset($this->session->data['order_id']);
      unset($this->session->data['coupon']);
      unset($this->session->data['reward']);
      unset($this->session->data['voucher']);
      unset($this->session->data['vouchers']);

    }


    $this->language->load('checkout/success');

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

    $this->data['breadcrumbs'][] = array(
      'href' => $this->url->link('common/home', '', 'NONSSL', 'false'),
      'text' => $this->language->get('text_home'),
      'separator' => false
    );

    $this->data['breadcrumbs'][] = array(
      'href' => $this->url->link('checkout/cart'),
      'text' => $this->language->get('text_basket'),
      'separator' => $this->language->get('text_separator')
    );

    $this->data['breadcrumbs'][] = array(
      'href' => $this->url->link('checkout/checkout', '', 'SSL'),
      'text' => $this->language->get('text_checkout'),
      'separator' => $this->language->get('text_separator')
    );

    $this->data['breadcrumbs'][] = array(
      'href' => $this->url->link('checkout/success'),
      'text' => $this->language->get('text_success'),
      'separator' => $this->language->get('text_separator')
    );

    $this->breadcrumbs->setbreadcumbs($this->data['breadcrumbs']);


    $this->load->dmodel('layout_personalizado/custom_layout');
    $layout_success = $this->dmodel_layout_personalizado_custom_layout->getContents(Type_LayoutPersonalizado::Tp_LayoutSuccess);

    if (isset($layout_success[0]['descricao']) && isset($layout_success[0]['conteudo_layout'])) {

      $find = array();
      $replace = array();

      // passar exatamente os campos do find na ordem do replace, por causa do str_replace
      $find = array('{{ cli_nome }}', '{{ cli_email }}', '{{ loj_nome }}', '{{ ped_id }}', '{{ loj_link }}');
      $replace = array(
        'cli_nome' => $this->customer->getFirstName() . ' ' . $this->customer->getLastName(),
        'cli_email' => $this->customer->getEmail(),
        'loj_nome' => $this->config->get('config_name'),
        'ped_id' => $this->session->data['payment_session']['order_id'],
        'loj_link' => $this->urlloja->link('common/home', '', 'SSL')
      );

      $format = $layout_success[0]['conteudo_layout'];

      $mensagem = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));

      $this->data['heading_title'] = $layout_success[0]['descricao'];
      $this->data['text_message'] = $mensagem;
    } else {
      $this->data['heading_title'] = $this->language->get('heading_title');
      if ($this->customer->isLogged()) {
        $this->data['text_message'] = $this->language->get('text_customer');
      } else {
        $this->data['text_message'] = $this->language->get('text_customer');
      }
    }


    $this->document->setTitle($this->data['heading_title']);

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

    $this->data['contact'] = $this->url->link('information/contact');
    $this->data['order'] = $this->url->link('account/order/info', 'order_id=' . $this->session->data['payment_session']['order_id']);

    $this->data['continue'] = $this->url->link('common/home', '', 'NONSSL', 'false');

    $this->children = array(
      'common/breadcrumbs',
      'common/column_left',
      'common/column_right',
      'common/content_top',
      'common/content_bottom',
      'common/footer',
      'common/header'
    );

    $this->document->addScriptApp(array(
      'catalog/view/javascript/copyContent.js',
		));
    $this->data['scriptsApp'] = json_encode($this->document->getScriptsApp());

    $this->response->setOutput($this->render('fidelity/success'));
  }


}
