<?php

class ControllerFidelityCheckout extends Controller
{
  public function index()
  {

    $query = $this->db->query("select count(order_id) as total from oc_order where fidelity = 1 and id_futura is null and customer_id = ".$this->customer->getId()." and order_status_id = " . $this->config->get('fidelity_status_initial'));

    if($query->row['total'] > 0){
      $this->session->data['warning'] = 'Por favor aguarde a finalização do seu resgate de pontos para resgatar outros produtos!';
      $this->redirect($this->url->link('fidelity/cart', '', 'SSL'));
    }

    if($this->fidelitycart->getTotal() > $this->customer->getPoints()){
      $this->redirect($this->url->link('fidelity/cart'));
    }

    // Validate cart has products and has stock.
    if ((!$this->fidelitycart->hasProducts()) || (!$this->fidelitycart->hasStock() && !$this->config->get('config_stock_checkout'))) {
      $this->redirect($this->url->link('fidelity/cart'));
    }

    if ($this->config->get('fidelity_mininum_points') && ($this->config->get('fidelity_mininum_points') != '')) {
      if (($this->fidelitycart->getSubTotal() < $this->config->get('fidelity_mininum_points'))) {
        $this->redirect($this->url->link('fidelity/cart'));
      }
    }

    $this->loadScript();

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

    $this->document->setTitle($this->language->get('heading_title'));
    $this->document->addScript('catalog/view/javascript/jquery/colorbox/jquery.colorbox-min.js');
    $this->document->addStyle('catalog/view/javascript/jquery/colorbox/colorbox.css');


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

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

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

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

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

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

    $this->data['logged'] = $this->customer->isLogged();
    $this->data['shipping_required'] = $this->fidelitycart->hasShipping();

    /*
    * INICIO: Cria as variaveis para controle de exibição na .tpl por Dionis Mendanha
    */
    $arrayDataExibir = array(
      'fields_register_brazil_status_telefone',
      'fields_register_brazil_status_celular',
      'fields_register_brazil_status_telefone2',
      'fields_register_brazil_status_sexo',
      'fields_register_brazil_status_data_nascimento',
      'fields_register_brazil_status_rg'
    );

    foreach ($arrayDataExibir as $dataexibir) {
      if (!ftr_geral::isIsset($this->config->get($dataexibir)) || $this->config->get($dataexibir)) {
        $this->data[$dataexibir] = true;
      } else {
        $this->data[$dataexibir] = false;
      }
    }
    /*
    * FIM: Cria as variaveis para controle de exibição na .tpl por Dionis Mendanha
    */


    $this->data['google_captcha'] = ($this->config->get('google_recaptcha_site_key') && $this->config->get('google_recaptcha_secret_key') && $this->config->get('wg24_google_recaptcha_status')) ? 1 : 0;

    if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/fidelity/checkout.tpl')) {
      $this->template = $this->config->get('config_template') . '/template/fidelity/checkout.tpl';
    } else {
      $this->template = 'default/template/fidelity/checkout.tpl';
    }


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

    if (isset($this->request->get['quickconfirm'])) {
      $this->data['quickconfirm'] = $this->request->get['quickconfirm'];
    }


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

  }

  public function country()
  {
    $json = array();

    $this->load->model('localisation/country');

    $country_info = $this->model_localisation_country->getCountry($this->request->get['country_id']);

    if ($country_info) {
      $this->load->model('localisation/zone');

      $json = array(
        'country_id' => $country_info['country_id'],
        'name' => $country_info['name'],
        'iso_code_2' => $country_info['iso_code_2'],
        'iso_code_3' => $country_info['iso_code_3'],
        'address_format' => $country_info['address_format'],
        'postcode_required' => $country_info['postcode_required'],
        'zone' => $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']),
        'status' => $country_info['status']
      );
    }

    $this->response->setOutput(json_encode($json));
  }

  public function loadScript()
  {
      $this->document->addScript('public/js/postcode');

    if ($this->ftrgeralregistry->isApp()) {
      $this->document->addScriptApp(array(
        'catalog/view/theme/appmobile/public/js/page.checkout.js',
          'public/js/postcode.js'
      ));
    }
  }
}
