<?php
/**
 *      [XCECP] (C) Colorful Network Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
class EventAction{
	function index(){
		global $db;
		$template = "";
		$id = getgpc("id");
		if($id && settype($id,"integer")){
			$sql = "select template from qk_event where id='$id'";
			$template = $db->getOne($sql);
		}
		display($template);
	}
	function view(){
		global $db;
		$template = "";
		$id = getgpc("id");
		if($id && settype($id,"integer")){
			$sql = "select * from qk_event where id='$id'";
			$eventInfo = $db->getRow($sql);
			$template =$eventInfo['template'];
			$pageInfo['title'] = $eventInfo['title']?$eventInfo['title']:'活动';
			$pageInfo['title'] .=" - ".$GLOBALS['siteConf']['siteName'];
			$pageInfo['keywords'] = $eventInfo['keywords'];
			$pageInfo['description'] = $eventInfo['description'];
			assign("pageInfo",$pageInfo);
			display($template);
		}
	}
	function choujiang(){
		global $db;
		$eventId = getgpc("eventId");
		$c = true;
		if(!isset($_SESSION['userId']) || !$_SESSION['userId']){
			$res = array('success'=>false,"message"=>"请先登录");
			$c = false;
		}
		if(!$eventId || !settype($eventId,"integer")){
			$c = false;
			$res = array('success'=>false,"message"=>"活动已经过期或不存在");
		}
		$userId = $_SESSION['userId'];
		if($c){
			$sql = "select * from qk_order where iStatus=20 and userId='$userId'";
			$res = $db->getAll($sql);
			if(!is_array($res) || count($res)==0){
				$res = array('success'=>false,"message"=>"您还没有购买过本站商品");
				$c = false;
			}
		}
		if($c){
			$sql = "select id,name from qk_event_goods where eventId='$eventId'";
			$goods = $db->getAll($sql);
			$key = array_rand($goods);
			$eventGoodsId = $goods[$key]['id'];
			$sql = "select id from qk_user_prize where eventId='$eventId'";
			if($db->getOne($sql)){
				$res = array('success'=>false,"message"=>"您已经参加过本次抽奖，奖品请到用户中心查看");
				$c=false;
			}
		}
		
		if($c){
			$sql = "insert into qk_user_prize set userId='$userId',eventId='$eventId',eventGoodsId='$eventGoodsId',iStatus=0,posttime=UNIX_TIMESTAMP();";
			$db->query($sql);
			$res = array("success"=>true,"message"=>"恭喜您抽中 ".$goods[$key]['name']." 请到用户中心我的奖品处领取");
		}
		$type=1;
		if($res['success']){$type=3;}
		$res = array_merge($res,array("type"=>$type));
			echo json_encode($res);
	}

}
?>