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

class ControllerProductSpecial extends ControllerProductDefault {
	public function index($setting = null) {

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

		$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');

		parent::index();

		$filterData = parent::setFilterData() ;

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

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

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home','','NONSSL','false'),
			'separator' => false
		);


		if($filterData['order_promotion_id']){


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

		    $this->load->model('catalog/special_action');
            $special_action = $this->model_catalog_special_action->getSpecialAction($filterData['order_promotion_id']);

            if ($special_action) {
                $this->data['breadcrumbs'][] = array(
                    'text'      => $special_action['description'],
                    'href'      => $this->url->link('product/special', parent::getUrlParameters(['order_promotion_id' => $filterData['order_promotion_id']])),
                    'separator' => $this->language->get('text_separator')
                );
            }

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

            $dataTotal = array(
                'filter_order_promotion' => $filterData['order_promotion_id']
            );

            $dataPagination = array (
                'total' => $this->model_catalog_product->getTotalProducts($dataTotal),
                '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/productSpecialList', parent::getUrlParameters(['page'=> '{page}']), 'NONSSL', 'true', false, false) ;
                $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/special', parent::getUrlParameters(['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/special', parent::getUrlParameters());
            $this->data['limits']     = parent::getLimits('product/special');
            $this->data['sort'] 	  = parent::getSort();
            $this->data['order'] 	  = parent::getOrder();
            $this->data['limit'] 	  = $filterData['limit'];

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

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

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


        }else {
            parent::getProductNotFound('product/special');
        }

	}

}
