<?php

class ControllerSpecific4861InformationReseller extends Controller
{
  public function index()
  {

    $this->load->type('tipocomum');

    $language = $this->language->load('specific/'.PREFIX_NAME.'/information/reseller');
    $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_reseller'),
      'href' => $this->url->link('information/reseller', '', 'SSL'),
      'separator' => $this->language->get('text_separator')
    );

    if (isset($this->session->data['success'])) {
      $this->data['success'] = $this->session->data['success'];

      unset($this->session->data['success']);
    } else {
      $this->data['success'] = '';
    }

    if (isset($this->session->data['warning'])) {
      $this->data['error'] = $this->session->data['warning'];
      unset($this->session->data['warning']);
    } else {
      $this->data['error'] = '';
    }

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

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

    $this->document->addScript([
      'catalog/view/theme/vista/public/js/specific/4861/information/reseller.js',
    ]);

    $this->response->setOutput($this->render('information/reseller'));
  }


  public function search(){
    $language = $this->language->load('specific/'.PREFIX_NAME.'/information/reseller');
    $this->data = array_merge($this->data, $language);


    $this->load->controller('postcode/postcode');
    $controllerPostcode = new ControllerPostcodePostcode($this->registry);

    $postcodeSearch = $controllerPostcode->search(true);

    $json = array();

    if (!isset($postcodeSearch['dados'])){
      $json['error'] = 'Cep não encontrado';
    }

    $resellers = array();

    if(!$json){

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

      if (isset($this->request->post['order'])) {
        $order = $this->request->post['order'];
      } else {
        $order = 'DESC';
      }

      $data = array(
        'codigo_municipio' => $postcodeSearch['dados']['codigo_municipio'],
        'order' => $order,
        'start' => ($page - 1) * $this->config->get('config_catalog_limit'),
        'limit' => $this->config->get('config_catalog_limit')
      );

      $this->load->dmodel('specific/' . PREFIX_NAME . '/reseller/reseller');
      $results = $this->{'dmodel_specific_' . PREFIX_NAME . '_reseller_reseller'}->getResellers($data);
      $resellerstotal = $this->{'dmodel_specific_' . PREFIX_NAME . '_reseller_reseller'}->getTotalResellers($data);

      if($results){
        foreach ($results as $result) {

          $mask = (strlen($result['celular']) == 11 ) ? '(##) #####-####' : '(##) ####-####' ;

          $resellers[] = array(
            'name' => $result['name'],
            'email' => $result['email'],
            'endereco' => $result['endereco'],
            'celular' => ($result['celular']!='') ? ftr_geral::mask($result['celular'], $mask) : false
          );
        }
      }

      $pagination = new PaginationJson();
      $pagination->total = $resellerstotal;
      $pagination->page = $page;
      $pagination->limit = $this->config->get('config_admin_limit');
      $pagination->text = $this->language->get('text_pagination');
      $pagination->classjs = 'js-btn-reseller';

      $this->data['pagination'] = $pagination->render();

    }

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

    $this->response->setOutput($this->render('information/reseller_search'));

  }

}
