<?php
class ControllerModuledealpro extends Controller {
    protected function index($setting) {
        static $module = 0;
        $this->language->load('module/dealpro'); 
        

        $this->document->addScript('catalog/view/javascript/jquery.countdown.js');
        $this->document->addStyle('catalog/view/theme/default/stylesheet/dealpro.css');

        $this->data['heading_title'] = $this->language->get('heading_title'); 
        $this->data['buynow'] = $this->language->get('text_buynow');
        $this->data['yousave'] = $this->language->get('text_yousave'); 
        $this->data['percentsave'] = $this->language->get('text_percentsave'); 
        $this->data['price'] = $this->language->get('text_price'); 
        $this->data['itemleft'] = $this->language->get('text_itemleft'); 
        $this->data['button_cart'] = $this->language->get('button_cart');
        $this->load->model('catalog/product'); 
        $this->load->model('tool/image');
        $this->data['products'] = array();
        $this->data['dealpro_yousavebutton'] = $this->config->get('dealpro_yousavebutton');
        $this->data['dealpro_itemleftbutton'] = $this->config->get('dealpro_itemleftbutton');
        $this->data['dealpro_pricesavebutton'] = $this->config->get('dealpro_pricesavebutton');
        $this->data['dealpro_percentsavebutton'] = $this->config->get('dealpro_percentsavebutton');
        $this->data['dealpro_buynowbutton'] = $this->config->get('dealpro_buynowbutton');
		
		$this->data['dealpro_buttonsize'] = $this->config->get('dealpro_buttonsize');
		$showas = $this->data['dealpro_showas'] = $this->config->get('dealpro_showas');
		$this->data['dealpro_slideeffect'] = $this->config->get('dealpro_slideeffect');
		$this->data['dealpro_titlesize'] = $this->config->get('dealpro_titlesize');
		$countdesc = $this->config->get('dealpro_countdesc');
		$counttype = $this->config->get('dealpro_counttype');
		$this->data['modulestyle'] = 'width:'.$this->config->get('dealpro_width').'px;'.'min-height:'.$this->config->get('dealpro_height').'px;';
		$this->data['boxheight'] = 'height:'.$this->config->get('dealpro_height') - 20 .'px;';
		$this->data['fontsize'] = "font-size:".$this->config->get('dealpro_producttextsize').'px;';
		
		if ($showas == 'slide'){
			$this->document->addScript('catalog/view/javascript/jquery.cycle.all.min.js');
		}
        
        
        //$module =   $this->config->get('dealpro_module');

        $products = explode(',', $this->config->get('dealpro_product'));     

        foreach ($products as $product_id) {
            $product_info = $this->model_catalog_product->getProduct($product_id);
            
            $oRow = $this->db->query("SELECT* FROM " . DB_PREFIX . "product_special WHERE product_id = ".$product_id);
            
            foreach ($oRow->rows as $row  ){
               $date_start = $row['date_start']; 
               $date_end = $row['date_end'];
               
            }
            //echo "<pre>"; var_dump($product_info); echo "</pre>";
            
            
            if ($product_info) {
                if ($product_info['image']) {
                    $image = $this->model_tool_image->resize($product_info['image'], $setting['image_width'], $setting['image_height']);
                } else {
                    $image = false;
                }

                if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
                    $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
                } else {
                    $price = false;
                }
                        
                if ((float)$product_info['special']) {
                    $special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
                } else {
                    $special = false;
                }
                
                if ((float)$product_info['special']) {
                    $yousave= $this->currency->format($this->tax->calculate($product_info['price'] - $product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
                } else {
                    $yousave = false;
                }
                
                if ($this->config->get('config_review_status')) {
                    $rating = $product_info['rating'];
                } else {
                    $rating = false;
                }
                
                $timeleft = strtotime($date_end) - strtotime(date('Y-m-d'));
				
                if ($timeleft > 0){
                    $day = explode('-', $date_end);
                    $yleft  = $day[0];
                    $mleft  = $day[1];
                    $dleft  = $day[2];
              
                }
                else {
                    $yleft  = 0;
                    $mleft  = 0;
                    $dleft  = 0;    
                }
				
				
				
                if ($counttype == 'words'){
					$description = explode(' ',strip_tags(htmlspecialchars_decode($product_info['description'])));               
	                $strDesc = array();
	                if($countdesc>0){
	                    $totalword = $countdesc;
	                }else{
	                    $totalword = count($description);
	                }
	                
	                for($i = 0 ; $i<$totalword; $i++){
	                    if ($description[$i] != ""){
	                      $strDesc[]= $description[$i]   ;  
	                    }
	                   
	                }
	                $desc = implode(' ', $strDesc);
				}else{
					$description = strip_tags(htmlspecialchars_decode($product_info['description']));
					$desc = substr($description,0,$countdesc);
				}
                
                   
                $this->data['products'][] = array(
                    'product_id'    => $product_info['product_id'],
                    'thumb'         => $image,
                    'name'          => $product_info['name'],
                    'description'   => $desc,
                    'price'         => $price,
                    'special'       => $special,
                    'rating'        => $rating,
                    'reviews'       => sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']),
                    'href'          => $this->url->link('product/product', 'product_id=' . $product_info['product_id']),
                    'date_start'    => $date_start,
                    'date_end'      => $date_end,
                    'percentsave'   => ceil(((($product_info['price']- $product_info['special'])/$product_info['price'])*100)).'%',
                    'yousave'       => $yousave,
                    'yleft'         => $yleft,
                    'mleft'         => $mleft,
                    'dleft'         => $dleft,
                    'hleft'         => 24 - date('H'), 
                    'mileft'        => 60-date('i'),
                    'sleft'         => rand(0,60),
                    'timeleft'      => $timeleft,
                    'quantity'      => $product_info['quantity'],
                );
                
            }
        }
        
        $this->data['module'] = $module++;

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

        $this->render();
    }
	protected function date_diff($end)
	{	
		$endtime = strtotime($end);
	
		$today = time () ;
		$difference =($endtime-$today) ;
		
		$days =(int) ($difference/86400) ;
		
		return $days;   
	}


}
//ALTER TABLE `product_special` CHANGE `date_start` `date_start` DATETIME NOT NULL DEFAULT '0000-00-00',
//CHANGE `date_end` `date_end` DATETIME NOT NULL DEFAULT '0000-00-00'
?>