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

class Controllerspecific4861modulestarterkit extends ControllerProductProductList {

	public function index($setting=null) {
		$this->language->load('module/starter_kit');
    $this->data['heading_title']  = $this->config->get('starter_kit_title') ? $this->config->get('starter_kit_title') : $this->language->get('heading_title');

    if (!$this->config->get('starter_kit_product')) {
      return false;
    }

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

    $data = array(
      'start' => 0,
      'limit' => '',
      'filter_quantity' => $this->ftrgeralregistry->getStatusAviseme(),
      'productsId' => $this->config->get('starter_kit_product')
    );

    $results = $this->cache->get('product_starter_kit.'.md5(implode($data)) . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id'));

    if(!$results){
      $this->load->model('catalog/product');
      $results = $this->model_catalog_product->getProducts($data);
      $this->cache->set('product_starter_kit.'.md5(implode($data)) . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id'), $results);
    }

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

    foreach ($results as $product) {
      if ($product['image']) {
        $image = $this->dmodel_tool_image->resizeWebp($product['image'], $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height'));
        if(!is_null($image)){
          $image = array_map(function($array) use ($product){
            return $array.'?version=' . preg_replace("/[^0-9]/", "", $product['data_atualizacao_replicacao']);
          }, $image);
        }
      } else {
        $image = $this->dmodel_tool_image->resizeWebp('data/sem-imagem.png', $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height'));
      }

      if ($product['image']) {
        $thumb = $this->dmodel_tool_image->resizeWebp($product['image'], 68, 68);
        if(!is_null($thumb)){
          $thumb = array_map(function($array) use ($product){
            return $array.'?version=' . preg_replace("/[^0-9]/", "", $product['data_atualizacao_replicacao']);
          }, $thumb);
        }
      } else {
        $thumb = $this->dmodel_tool_image->resizeWebp('data/sem-imagem.png', 68, 68);
      }

      $this->data['aspect_ratio'] = str_replace(',', '.', ($this->config->get('config_image_category_height') / $this->config->get('config_image_category_width') * 100)) . '%';

      $this->data['product_idfutura'] = $product['product_id'];

      $language = $this->load->language('product/product');
      $this->data = array_merge($this->data, $language);

      $product = array(
        'product_id' => $product['product_id'],
        'price_pr' => $product['price'],
        'model' => $this->config->get('config_code_list_product') ? $product['model'] : $model = false,
        'thumb' => $image,
        'thumb_mini' => $thumb,
        'name' => $product['name'],
        'ratio' => ($this->data['aspect_ratio']) ? 'style="height: 0; padding-bottom: ' . $this->data['aspect_ratio'] . '"' : '',
        'class_one_line' => (strlen($product['name']) <= 18) ? 'one-line' : '',
      );
      $this->data['products'][] = $product;
    }

    $this->data['exibeModule'] = count($results) > 0;

    $this->response->setOutput($this->render('module/starter_kit'));

		$this->render('module/starter_kit');
	}

}
