<?php
class ControllerModuleSpecialDeal extends Controller {
	protected function index($setting) {
		static $module = 0;
	
		$this->language->load('module/special_deal');
 
      	$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->document->addStyle('http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:400,200,300,700&subset=latin,latin-ext');
		$this->document->addStyle('http://fonts.googleapis.com/css?family=Exo:600');
		
		$this->document->addScript('catalog/view/javascript/jquery/jquery.sd.countdown.js');
		$this->document->addScript('catalog/view/javascript/jquery/rhinoslider-1.05.min.js');
		$this->document->addScript('catalog/view/javascript/jquery/mousewheel.js');
		$this->document->addScript('catalog/view/javascript/jquery/jquery.bxslider4.min.js');
		$this->document->addScript('catalog/view/javascript/jquery/jquery.easing.1.3.js');
		$this->document->addScript('catalog/view/javascript/jquery/jquery.fitvids.js'); 
		$this->document->addScript('catalog/view/javascript/jquery/jquery.reveal.js'); 
		
		if (file_exists('catalog/view/theme/' . $this->config->get('config_template') . '/stylesheet/special_deal.css')) {
			$this->document->addStyle('catalog/view/theme/' . $this->config->get('config_template') . '/stylesheet/special_deal.css');
		} else {
			$this->document->addStyle('catalog/view/theme/default/stylesheet/special_deal.css');
		}
		
		$this->data['slide_type'] = $setting['slide_type'];
		$this->data['slide_time'] = $setting['slide_time'] * 1000;
		$this->data['buy_now_behaviour'] = $setting['buy_now_behaviour'];
		
		$remaining_popup_show = true; 
		
		$this->data['display_type'] = $setting['display_type'];
		
		if ($setting['display_type'] == 0 ) { // popup
			$this->data['content_right_class'] = '';
			
			if (!isset($this->session->data['popup_no_times'])) {
				$this->session->data['popup_no_times'] = 1;
			} else {
				++$this->session->data['popup_no_times'];
			}
			
			if ($this->session->data['popup_no_times'] > $setting['popup_no_times']) {
				$remaining_popup_show = false;
			}
			
		} else {
			$this->data['content_right_class'] = 'sd-normal';
		}
		
		$this->data['remaining_popup_show'] = $remaining_popup_show;
		
		$this->data['text_old_price'] = $this->language->get('text_old_price');
		$this->data['text_discount']  = $this->language->get('text_discount');
		$this->data['text_save']      = $this->language->get('text_save');
		$this->data['text_time_left_purchase'] = $this->language->get('text_time_left_purchase');
		$this->data['text_day'] = $this->language->get('text_day');
		$this->data['text_hour'] = $this->language->get('text_hour');
		$this->data['text_minute'] = $this->language->get('text_minute');
		$this->data['text_second'] = $this->language->get('text_second');
		$this->data['text_buy_now'] = $this->language->get('text_buy_now');
		$this->data['text_recommend_friends'] = $this->language->get('text_recommend_friends');

		$this->data['button_cart'] = $this->language->get('button_cart');
		
		$this->load->model('catalog/product');
		$this->load->model('module/special_deal');
		$this->load->model('tool/image');
		

		$this->data['products'] = array();
		
		if (!$this->config->get('special_deal_featured_product')) {
		
			$data = array(
				'sort'  => 'pd.name',
				'order' => 'ASC',
				'start' => 0,
				'limit' => $setting['limit'] 
			);

			$results = $this->model_catalog_product->getProductSpecials($data);
			
		} else {
			
			$results = array();
			
			$products = explode(',', $this->config->get('special_deal_featured_product'));		
			
			foreach ($products as $product_id) {
				$results[] = $this->model_catalog_product->getProduct($product_id);
			}
		}
		
		foreach ($results as $result) {
			if ($result['image']) {
				$image = $this->model_tool_image->resize($result['image'], $setting['image_width'], $setting['image_height']);
			} else {
				$image = false;
			}

			$save = '';
			$discount_percent = '';
			
			if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
				$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
			} else {
				$price = false;
			}
					
			if ((float)$result['special']) { 
				$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
				
				$wt_price   = $this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'));
				$wt_special = $this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'));
				$wt_save    = $wt_price - $wt_special;
				
				$save = $this->currency->format($wt_save);
				$discount_percent = number_format((100 * $wt_save) / $wt_price, 2);
				
			} else {
				$special = false;
			}
			
			
			if ($this->config->get('config_review_status')) {
				$rating = $result['rating'];
			} else {
				$rating = false;
			}
			
			$special_deal_end_date = $this->model_module_special_deal->getSpecialDealEndDate($result['product_id']);
			
			$countdown_from = array();
			$show_countdown = false;
			
			if ($special_deal_end_date) {
				$show_countdown = true;
				$countdown_from = explode('-',$special_deal_end_date);
			}
			
			$product_images = array();
			
			$additional_images = $this->model_catalog_product->getProductImages($result['product_id']);
			
			if ($additional_images) {
				foreach($additional_images as $additional_image) {
					$product_images[] = array(
						'thumb' => $this->model_tool_image->resize($additional_image['image'], $setting['image_width'], $setting['image_height'])
					);	
				}
			}
			
			if ($special) {  // check if special offer is not expired
			
				$this->data['products'][] = array(
					'product_id'        => $result['product_id'],
					'thumb'   	        => $image,
					'additional_images' => $product_images,
					'name'    	 		=> $result['name'],
					'description'		=> utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 250) . '..',
					'price'   	 		=> $price,
					'special' 	 		=> $special,
					'save'              => $save,
					'discount'          => $discount_percent,
					'countdown_from'    => $countdown_from,
					'show_countdown'    => $show_countdown,
					'rating'     		=> $rating,
					'reviews'    		=> sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
					'href'    	 		=> $this->url->link('product/product', 'product_id=' . $result['product_id'])
				);
			}	
		}
		
		if ($setting['random_deal']) {
			shuffle($this->data['products']);
		}
		
		$this->data['module'] = $module++;

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

		if (count($this->data['products']) > 0 && $remaining_popup_show) {
			$this->render();
		}	
	}
}
?>