<?php
// +----------------------------------------------------------------------
// | Sunnyi 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.sunnyi.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 商易网络(admin@sunnyi.cn)
// +----------------------------------------------------------------------

// 我的优惠券
class UcCardListAction extends UserCenterAction {
	public function index()
	{
		//初始化分页
    	$page = intval($_REQUEST[C("VAR_PAGE")]);
    	if($page==0)
    	$page = 1;
    	
    	$group_id = intval($_SESSION['group_id']);
    	
    	$limit = (($page-1)*C("PAGE_LISTROWS")).",".(C("PAGE_LISTROWS"));
    	
    	$sql = "select * from ".C("DB_PREFIX")."promote_card as promote_card left join ".
    		   C("DB_PREFIX")."promote_user_card as promote_user_card on promote_card.id = promote_user_card.card_id left join ".
    		   C("DB_PREFIX")."promote as promote on promote.id = promote_card.promote_id left join ".
    		   C("DB_PREFIX")."promote_user_group as promote_user_group on promote_user_group.promote_id = promote.id ".
    		   " where promote.status = 1 and ".
    		   " promote.is_card = 1 and ".
    		   " promote_user_group.user_group_id = ".intval($_SESSION['group_id'])." and ".
    		   " promote_user_card.user_id = ".intval($_SESSION['user_id'])." limit ".$limit;
    		   
    	$list = D()->query($sql);
    	
		$now = gmtTime();
    	foreach($list as $k=>$v)
    	{
    		$list[$k]['card_status'] = L("CARD_OK");
    		if($v['card_limit']<=$v['card_used'])
    		{
    			$list[$k]['card_status'] = L("OVER_USED");
    		}
    		
    		if($v['promote_end_time']<$now)
    		{
    			$list[$k]['card_status'] = L("OVER_TIME");
    		}
    	}
		$this->assign("card_list",$list);
		//分页
		$sql_total = "select count(*) as rs from ".C("DB_PREFIX")."promote_card as promote_card left join ".
    		   C("DB_PREFIX")."promote_user_card as promote_user_card on promote_card.id = promote_user_card.card_id left join ".
    		   C("DB_PREFIX")."promote as promote on promote.id = promote_card.promote_id left join ".
    		   C("DB_PREFIX")."promote_user_group as promote_user_group on promote_user_group.promote_id = promote.id ".
    		   " where promote.status = 1 and ".
    		   " promote.is_card = 1 and ".
    		   " promote_user_group.user_group_id = ".intval($_SESSION['group_id'])." and ".
    		   " promote_user_card.user_id = ".intval($_SESSION['user_id']);
		$total = D()->query($sql_total);
		$total = $total[0]['rs'];
		$page = new Page($total,C("PAGE_LISTROWS"));   //初始化分页对象 		
		$p  =  $page->show();
        $this->assign('pages',$p);
        
		$this->display("Page:user_center_frame");
	}
	
	//兑换优惠券
	public function exchange()
	{
		$user_id = intval($_SESSION['user_id']);
		$card_id = intval($_REQUEST['card_id']);
		
		$card_sn = String::keyGen();

		$promote_item = M("Promote")->getById($card_id);
		
		if($promote_item['card_score']>M("User")->where("id=".$user_id)->getField("score"))
		{
			$res['status'] = 0;
			$res['info'] = L("NOT_ENOUGH_SCORE");
			echo json_encode($res);
			exit;
		}
		else 
		{
			$promote_card_item = array();
			$promote_card_item['promote_id'] = $promote_item['id'];
			$promote_card_item['card_code'] = $card_sn;
			$promote_card_item['card_limit'] = $promote_item['card_limit'];
			
			$card_id = M("PromoteCard")->add($promote_card_item);
			
			$promote_user_card_item = array();
			$promote_user_card_item['user_id'] = $user_id;
			$promote_user_card_item['card_id'] = $card_id;
			
			$promote_user_card_id = M("PromoteUserCard")->add($promote_user_card_item);
			
			if($promote_user_card_id&&$card_id)
			{
				user_score_log($user_id,$card_id,'PromoteCard',"-".$promote_item['card_score'],"#EXCHANGE_CARD_USE_SCORE#");
				$res['status'] = 1;
				$res['info'] = sprintf(L("EXCHANGE_SUCCESS"),$card_sn);
				echo json_encode($res);
				exit;
			}
			else
			{
				$res['status'] = 0;
				$res['info'] = '';
				echo json_encode($res);
				exit;
			}
		}
		
		
	}
	
}
?>