<?php

class ControllerFidelityAccount extends Controller
{

 public function getUrlLink()
  {

    $url = '';

    if (isset($this->request->get['operacao'])) {
      $url .= '&operacao=' . $this->request->get['operacao'];
    }

    if (isset($this->request->get['range'])) {
      $url .= '&range=' . $this->request->get['range'];
    }

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

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

    return $url;
  }

  public function extractList()
  {

    if(!$this->config->get('fidelity_status')){
      return false;
    }

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

    $this->load->dmodel('fidelity/account');
    $this->load->type('tipocomum');
    $this->load->helper('ftr_geral');

    $this->data['url_resgatar_pontos'] = $this->url->link('fidelity/category', '', 'SSL');

    $this->document->addScript([
      'catalog/view/theme/vista/js/page.extract_list.js',
      'catalog/view/theme/vista/public/js/fidelity/extract-list.js',
    ]);

    $language = $this->load->language('fidelity/extract');
    $this->data = array_merge($this->data, $language);
    $this->document->setTitle($this->language->get('heading_title'));

    $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_account'),
      'href' => $this->url->link('account/account', '', 'SSL'),
      'separator' => $this->language->get('text_separator')
    );

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

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

    if (isset($this->request->get['page'])) {
      $page = $this->request->get['page'];
    } else {
      $page = 1;
    }

    if (isset($this->request->get['operacao'])) {
      $operacao = explode(',', $this->request->get['operacao']);
    } else {
      $operacao = null;
    }

    if (isset($this->request->get['range'])) {
      $range = $this->request->get['range'];
    } else {
      $range = null;
    }

    $this->data['operacao'] = $operacao;

    $this->data['range'] = $range;

    if (isset($this->request->get['range'])) {
      $range = $this->request->get['range'];
    } else {
      $range = Type_LoyaltyRange::Tp_Quinze;
    }

    if (isset($this->request->get['datainicial'])) {
      $datainicial = $this->request->get['datainicial'];
    } else {
      $datainicial = null;
    }

    if (isset($this->request->get['datafinal'])) {
      $datafinal = $this->request->get['datafinal'];
    } else {
      $datafinal = null;
    }

    $data = array(
      'start' => $this->config->get('config_admin_limit'),
      'limit' => (($page - 1) * $this->config->get('config_admin_limit')),
      'customer' => $this->customer->getID_Futura(),
      'operacao' => (is_array($operacao)) ? implode(',',$operacao) : $operacao,
      'range' => $range,
      'datainicial' => $datainicial,
      'datafinal' => $datafinal
    );

    $extractsTotal = $this->dmodel_fidelity_account->getTotalExtractsByCustomer($data);
    $results = $this->dmodel_fidelity_account->getExtractsByCustomer($data);
    $this->data['customer_total_points'] = (int)$this->customer->getPoints();

    $this->data['extracts'] = false;
    if ($results) {
      $extracts = array();
      foreach ($results as $result) {

        $infos = array();

        if (!is_null($result['server_id'])) {
          $infos = array(
            'pedido_id' => $result['server_id'],
          );
        }

        if (!is_null($result['order_id'])) {
          $infos = array(
            'pedido_id' => $result['order_id'],
            'pedido_link' => $this->url->link('account/order/info', 'order_id=' . $result['order_id'])
          );
        }

        $extracts[] = array(
          'saldo' => number_format($result['saldo']),
          'operacao' => $result['operacao'],
          'text_operacao' => Type_LoyaltyOperacao::getNickFromValue($result['operacao']),
          'information' => $infos,
          'validade' => ftr_geral::formatDate($result['validade'], 'br'),
          'indicado' => is_null($result['indicado']) ? false : true,
          'name_indicado' => $result['name_indicado']
        );
      }

      $this->data['home'] = $this->url->link('common/home');
      $this->data['type_operecao'] = Type_LoyaltyOperacao::getEnumListConst();
      $this->data['extracts'] = $extracts;
      $this->data['datainicial'] = $datainicial;
      $this->data['datafinal'] = $datafinal;

      $url = $this->getUrlLink();

      $pagination = new Pagination();
      $pagination->total = $extractsTotal;
      $pagination->page = $page;
      $pagination->limit = $this->config->get('config_admin_limit');
      $pagination->text = $this->language->get('text_pagination');
      $pagination->url = $this->url->link('fidelity/account/extractlist', $url . '&page={page}', 'SSL');
      $pagination->is_app = $this->ftrgeralregistry->isApp();
      $this->data['pagination'] = $pagination->render();
    }

    $this->data['action'] = $this->url->link('fidelity/account/extractlist');

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

    if (!$this->ftrgeralregistry->isApp()) {
      array_push($this->children, 'module/account');
    } else {
      $this->app();
    }

    $this->data['Type_LoyaltyOperacao'] = $this->load->typeForTwig('Type_LoyaltyOperacao');
    $this->data['Type_LoyaltyRange'] = $this->load->typeForTwig('Type_LoyaltyRange');

    $this->data['ftr_geral'] = new ftr_geral();

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


  }

  public function app() {
    $this->document->addScriptApp(array('./catalog/view/theme/appmobile/public/js/page-extract_list.js'));
  }

}
