<?php
$loader = $this->registry->get('load');
$loader->controller('product/productDefault');

class Controllerproductmodulesinpage extends ControllerProductDefault
{

    public function index($setting = null)
    {

        if ($this->request->get['name'] == 'special' && isset($this->request->get['order_promotion_id'])) {
            $this->redirect($this->url->link('product/special', 'order_promotion_id=' . $this->request->get['order_promotion_id']));
            /*Retrocompatibilidade para utilizar a special como padrão de listagem das promoções e não perder o link caso cliente tenha em algum menu essa configuração*/
        }

        $this->language->load('product/category');

        parent::index();

        $this->loadscripts();

        $filterData = parent::setFilterData();

        $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->breadcrumbs->setbreadcumbs($this->data['breadcrumbs']);

        $this->document->addScript('catalog/view/javascript/jquery/jquery.total-storage.min.js');

        $this->data['t_result'] = $this->language->get('t_result');
        $this->data['t_results'] = $this->language->get('t_results');
        $this->data['t_order_by'] = $this->language->get('t_order_by');
        $this->data['t_result_not_found'] = $this->language->get('t_result_not_found');

        $this->language->load('module/' . $this->request->get['name']);
        $this->data['heading_title'] = parent::getTitleModulesInPage($this->language->get('heading_title'));
        $this->document->setTitle($this->language->get('heading_title'));
        // Set the last category breadcrumb

        $this->data['breadcrumbs'][] = array(
            'text' => $this->data['heading_title'],
            'href' => $this->url->link('product/modules_in_page&name=' . $this->request->get['name']),
            'separator' => $this->language->get('text_separator')
        );

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

        $dataPagination = array(
            'total' => parent::getTotalPaginationModulesInPage(),
            'page' => $filterData['page'],
            'limit' => $filterData['limit'],
            'text' => $this->language->get('text_pagination'),
        );

        //validação feita para funcionar com a configuração de paginação pelo botão
        $this->data['status_aviseme'] = $this->config->get('avise_me_status');
        if (($this->config->get('config_pagination_type') && ($this->config->get('config_pagination_type') == Type_Pagination::Tp_More)) || $this->ftrgeralregistry->isApp()) {
            $dataPagination['url'] = $this->url->link(
                'product/productList/moduleInPageList',
                parent::getUrlParameters([
                    'name' => $filterData['name'],
                    'page' => '{page}'
                ]),
                'NONSSL',
                'true',
                false,
                false
            );

            if ($filterData['name'] == 'featured') {
                $this->data['pagination'] = null;
                $this->data['paginationajax'] = false;
            } else {
                $this->data['pagination'] = json_encode(parent::getPagination($dataPagination, true), JSON_HEX_APOS);
                $this->data['paginationajax'] = true;
            }

            if ($this->ftrgeralregistry->isApp()) {
                $this->document->addScriptApp(array('catalog/view/theme/appmobile/js/page.pagination.js'));
            } else {
                $this->document->addscript('catalog/view/theme/vista/js/page.pagination.js');
            }


        } else {
            $dataPagination['url'] = $this->url->link(
                'product/modules_in_page',
                parent::getUrlParameters([
                    'name' => $filterData['name'],
                    'page' => '{page}'
                ]));

            $this->data['pagination'] = parent::getPagination($dataPagination);
            $this->data['paginationajax'] = false;
        }

        $this->data['totalProduct'] = $dataPagination['total']; // Criado para o app
        $this->data['sorts'] = parent::getSortList('product/modules_in_page', parent::getUrlParameters());
        $this->data['limits'] = parent::getLimits('product/modules_in_page', null, $filterData['name']);
        $this->data['sort'] = parent::getSort();
        $this->data['order'] = parent::getOrder();
        $this->data['limit'] = $filterData['limit'];

        $this->children = parent::getChildren('product/productList/moduleInPageList');

        $this->data['itembox_border'] = ftr_geral::isType($this->config->get('wg24_itembox_border'), Type_DsimDnao::getEnumList(), Type_DsimDnao::Tp_Sim);

        if ($this->ftrgeralregistry->isApp()) {
            $gridOrList = Type_gridOrListView::Tp_Grid;
        } else {
            $gridOrList = $this->config->get('wg24_cate_list_grid');
        }

        $this->data['gridOrList'] = (isset($gridOrList) && $gridOrList) ? substr($gridOrList, 0, strpos($gridOrList, '_')) : 'grid';

        $this->template = parent::getTemplate('product/modules_in_page.twig');

        $this->response->setOutput($this->render('product/modules_in_page'));
    }

    public function loadscripts()
    {
        $this->document->addScript('catalog/view/theme/vista/js/filter.js');
    }

}
