<?php 
class ControllerMyPoints extends Controller { 

	function index() {
		$customer =& $this->locator->get('customer');      
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		$currency =& $this->locator->get('currency');
		$database =& $this->locator->get('database');
		$request  =& $this->locator->get('request');	
		$view = $this->locator->create('template');

		if (!$customer->isLogged()) {
	  		$session->set('redirect', $url->ssl('points'));
	  		$response->redirect($url->ssl('account_login'));
    	}   	
    	$customer_id=$customer->getId();
		$customer_info=$database->getRow("select current_points,vip_rank,email from customer where customer_id=$customer_id"); 
		$vip_discount=$database->getRow("select discount from vip where vip_rank='".$customer_info['vip_rank']."'");
		$view->set('customer_info', $customer_info);
		$view->set('customer_discount', $vip_discount);
    	
    	if($request->has('page')){
		   $page = $request->get('page');
		}else{
			$page = 1;
		}      
		if($request->has('type')){
			$type = $request->get('type');
		}else{
			$type = 1;
		}
		$view->set('type', $type);		
		$rewards_type = "Order Rewards";
		switch($type){
			case 1: // order
			{
				$sql = "select order_id as id, total as points, date_added, reference from `order` where customer_id=$customer_id order by order_id desc";
				$rewards_type = "Order Rewards";				
				break;
			}
			case 2: // question
			{
				$sql = "select question_id as id, points, date_added from `question` where email='".$customer_info['email']."' order by question_id desc";
				$rewards_type = "Question Rewards";
				break;
			}
			case 3: // review
			{
				$sql = "select review_id as id, points, date_added from `review` where customer_id=$customer_id order by review_id desc";
				$rewards_type = "Review Rewards";
				break;
			}
		}
		
		
		$show_points_rows =  $database->getRows($database->splitQuery($sql, $page, 4));
		
		$show_array = array ();
		foreach ($show_points_rows as $k=>$value) {
			$show_array[] = array ( 'id' => $value['id'],
			                                  'date_added' => $value['date_added'],
			                                  'type' => $rewards_type,
			                                  'points' => $value['points']
			);
			if($type==1){		   	
		   		$show_array[$k]['href'] = $url->href("account_invoice", NULL, array('order_id' => $value['id']));
			}else if($type==2){
				$show_array[$k]['href'] = $url->href("my_questions");
			}else{
				$show_array[$k]['href'] = $url->href("waiting_review");
			}
		}
		$view->set('show_array', $show_array);		
//		print_r($show_points_list); exit;
		$page_data = array();
	    for ($i = 1; $i <= $database->getPages(); $i++) {
				$page_data[] = array(
	  				'text'  => $language->get('text_pages', $i, $database->getPages()),
	  				'value' => $i
			);
	    }
	    $view->set('pages', $page_data);  	      	        		
  		$view->set('page', $page);	
  		$view->set('text_results', $language->get('text_results',$page, $database->getPages()));		
  		$view->set('action', $url->href('my_points', NULL, array('type' => $request->get('type'))));
  		
		$template->set('content', $view->fetch('content/my_points.tpl'));
		$template->set($module->fetchAccount());	
    	$response->set($template->fetch('layout.tpl')); 
	}	
}
?>