<?php

class ControllerSpecific4861AccountHome extends Controller
{
  public function index()
  {

    if (!$this->customer->isLogged()) {
      $this->session->data['redirect'] = $this->url->link('account/account', '', 'SSL');

      $this->redirect($this->url->link('account/register', '', 'SSL'));
    }

    $this->data['back'] = $this->url->link('account/account', '', 'SSL');


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

    $language = $this->language->load('specific/'.PREFIX_NAME.'/account/home');
    $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_material'),
      'href' => $this->url->link('account/home', '', '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']);

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

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

    $data = array(
      'status' => Type_Status::Tp_Habilitado,
      'order' => $order,
      'start' => ($page - 1) * $this->config->get('config_admin_limit'),
      'limit' => $this->config->get('config_admin_limit')
    );

    $this->load->dmodel('specific/' . PREFIX_NAME . '/material/apoio');
    $material_total = $this->{'dmodel_specific_' . PREFIX_NAME . '_material_apoio'}->getTotalMaterials($data);
    $results = $this->{'dmodel_specific_' . PREFIX_NAME . '_material_apoio'}->getMaterials($data);

    $materials= array();
    if($results){

      $this->load->dmodel('tool/image');
      $this->load->dmodel('tool/files');

      foreach ($results as $result){

        if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
          $image = $this->dmodel_tool_image->resize($result['image'], $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height'));
        } else {
          $image = $this->dmodel_tool_image->resize('no_image.jpg', $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height'));
        }

        if($result['arquivo'] && file_exists($this->ftrgeralregistry->getPathRecord() . $result['arquivo']) ){
          $download = $this->dmodel_tool_files->filepath($result['arquivo'], 100, 100);
        }else{
          $download = false;
        }

        $materials[] = array(
          'material_id' => $result['material_id'],
          'titulo' => $result['titulo'],
          'image' => $image,
          'download' =>  $download,
        );
      }
    }

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

    $url = $this->getLinkUrl(['page']);
    $pagination = new Pagination();
    $pagination->total = $material_total;
    $pagination->page = $page;
    $pagination->limit = $this->config->get('config_admin_limit');
    $pagination->text = $this->language->get('text_pagination');
    $pagination->url = $this->url->link('account/home', $url . '&page={page}', 'SSL');

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

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


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

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

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

    $this->response->setOutput($this->render('account/home'));
  }

  public function getLinkUrl($remove = array())
  {
    $url = '';

    foreach ($remove as $key => $value) {
      unset($this->request->get[$value]);
    }

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

    return $url;

  }

}
