<?php

$loader = $this->registry->get('load');
$loader->controller('module/filter');

class ControllerModuleFidelity extends ControllerModuleFilter
{
  protected function index($setting)
  {
    if (!$this->config->get('fidelity_status')) {
      return false;
    }

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

    $data = parent::getFilters();

    if ($this->config->get('fidelity_category')) {
      $this->data['category'] = $this->getChild('module/category', ['fidelity' => true]);
    }

    if ($this->config->get('fidelity_range')) {

      if (isset($this->request->get['filrange'])) {
        $filtrosranges = explode(',', (string)$this->request->get['filrange']);
        $clearRange = true;
      } else {
        $clearRange = false;
        $filtrosranges = false;
      }

      $this->data['clearRange'] = $clearRange;
      $this->load->dmodel('fidelity/fidelity');

      $ranges = $this->dmodel_fidelity_fidelity->getRangePontos($data);

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

      foreach ($ranges as $range) {
        $value = $range['minimo'].'.'.$range['maximo'];
        $this->data['ranges'][] = array(
          'range' => $range['range'],
          'qtdprodutos' => $range['total'],
          'minimo' => $range['minimo'],
          'maximo' => $range['maximo'],
          'value' => $value,
          'checked' => (($filtrosranges) && in_array($value, $filtrosranges)) ? "checked" : ""
        );
      }

    }


    $this->data['linkclearrange'] = $this->url->link($this->request->get['route']);

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

    $this->render('module/fidelity');
  }

}
