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

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

		$this->language->load('product/manufacturer');
		$this->load->model('catalog/manufacturer');
		$this->load->model('catalog/product');
    $this->load->dmodel('tool/image');

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

		$this->document->setTitle($this->language->get('heading_title'));
		$this->data['heading_title']     = $this->language->get('heading_title');
		$this->data['text_index']        = $this->language->get('text_index');
		$this->data['text_empty']        = $this->language->get('text_empty');
		$this->data['text_product']      = $this->language->get('text_product');
		$this->data['text_manufacturer'] = $this->language->get('text_manufacturer');
		$this->data['button_continue']   = $this->language->get('button_continue');
		$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->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_brand'),
			'href'      => $this->url->link('product/manufacturer'),
			'separator' => $this->language->get('text_separator')
		);

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

		$this->data['categories'] = array();
		$results = $this->model_catalog_manufacturer->getManufacturers();

		foreach ($results as $result) {
			if (is_numeric(utf8_substr($result['name'], 0, 1))) {
				$key = '0 - 9';
			} else {
				$key = utf8_substr(utf8_strtoupper($result['name']), 0, 1);
			}

			if (!isset($this->data['manufacturers'][$key])) {
				$this->data['categories'][$key]['name'] = $key;
			}

			if ($result['image']) {
				$image = $this->dmodel_tool_image->resize($result['image'], Type_MiniatureSmall::Tp_width, Type_MiniatureSmall::Tp_height);
			} else {
				$image = $this->dmodel_tool_image->resize('data/sem-imagem.png', Type_MiniatureSmall::Tp_width, Type_MiniatureSmall::Tp_height);
			}

			$this->data['categories'][$key]['manufacturer'][] = array(
				'name'  => $result['name'],
				'href'  => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $result['manufacturer_id']),
				'image' => $image,
				'total' => $this->model_catalog_product->getTotalProducts(['filter_manufacturer_id'=> $result['manufacturer_id']])
			);
		}

		$this->data['continue'] = $this->url->link('common/home','','NONSSL','false');

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

		$this->children = parent::getChildren();
    $this->response->setOutput($this->render('product/manufacturer_list'));
	}

	public function info() {

		$this->loadscripts();

		$this->language->load('product/manufacturer');
		$this->load->model('catalog/manufacturer');

		parent::index();

		$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->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_brand'),
			'href'      => $this->url->link('product/manufacturer'),
			'separator' => $this->language->get('text_separator')
		);

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

		$manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($filterData['manufacturer_id']);

		if ($manufacturer_info) {
			$this->document->setTitle($manufacturer_info['name']);
			$this->document->addScript('catalog/view/javascript/jquery/jquery.total-storage.min.js');

			$this->data['breadcrumbs'][] = array(
				'text'      => $manufacturer_info['name'],
				'href'      => $this->url->link('product/manufacturer/info', parent::getUrlParameters(['manufacturer_id=' . $filterData['manufacturer_id']])),
				'separator' => $this->language->get('text_separator')
			);

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

			$this->data['heading_title'] = $manufacturer_info['name'];
			$this->data['sorts']  = parent::getSortList('product/manufacturer/info', parent::getUrlParameters(['manufacturer_id' => $filterData['manufacturer_id']]));
			$this->data['limits'] = parent::getLimits('product/manufacturer/info', ['manufacturer_id=' . $filterData['manufacturer_id']]);

			$dataPagination = array (
				'total' => $this->model_catalog_product->getTotalProducts(parent::getFilterData()),
				'page'	=> $filterData['page'],
				'limit'	=> $filterData['limit'],
				'text'	=> $this->language->get('text_pagination'),
				'url'		=> $this->url->link('product/manufacturer/info', parent::getUrlParameters(['manufacturer_id' => $filterData['manufacturer_id'], 'page'=> '{page}']))
			);

			$this->data['pagination'] = parent::getPagination($dataPagination);
			$this->data['sort'] 		  = parent::getSort();
			$this->data['order'] 			= parent::getOrder();
			$this->data['limit'] 			= $filterData['limit'];

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

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

			$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->response->setOutput($this->render('product/manufacturer_info'));
		} else {
			parent::getProductNotFound('product/manufacturer/info', ['manufacturer_id' => $filterData['manufacturer_id']]);
		}

	}

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

}
