<?php
namespace Cas\Controller;
use Cas\Dao\LotteryData;

use Cas\Module\Lottery;

use Cas\Module\LotteryEventsLimitations;

use Cas\Module\LotteryEventsTypeScratch;
use KIF\Core\Request;
use KIF\Verify;
use Cas\Dao\UserInfo;
use Cas\Module\Count;
/**
 * 刮刮卡
 * lihuanchun
 */
class EventsScratch extends EventsController {
	
	/**
	 * 页面:默认刮刮卡首页
	 */
	public function doDefault() {
		
		# 获取活动ID
		//$events_id = Request::g('events_id');
		$events_id = $this->getEventsCreateTimeToId();
		if (!Verify::unsignedInt($events_id)) {
			self::fail_exit('无效id');
		}
		
		# 获取用户信息
		$objUserData = new UserInfo();
		$uid = $this->getRunTimeUid();
		$userData = $objUserData -> get($uid);
		$this->setOutput('userData', $userData);
		
		# 获取当前活动数据
		$objLotteryEventsTypeScratch = new LotteryEventsTypeScratch($uid, $events_id);
		$scratchData = $objLotteryEventsTypeScratch->getEventsData();
		
		# 检查是否移动端
		$title = $scratchData['events']['weixinShare']['title'] ? $scratchData['events']['weixinShare']['title'] : $scratchData['events']['events_name'];
		$this->checkIsFromMobile($title);
		
		# 获取登录地址
		$url = Request::g('url');
		$plat_form_id = $this->getPlatFormId();
		$objMLottery  = new Lottery();
		$thisUrl  = $objMLottery -> getEventsUrl($scratchData['events']['type'],$scratchData['events']['create_time'],$url);
		$loginUrl = $this -> getLoginUrl($thisUrl);
		$this->setOutput('loginUrl', $loginUrl);

		# 判断是否需要登录
		$ckLogin = $this->ckLogin();
		$this->setOutput('ckLogin', $ckLogin);
		
		# 检查当前活动是否需要登录
		$authorize =$scratchData['events']['authorize'];
		if ($authorize && $loginUrl) {
				if($ckLogin==true){
					self::redirect($loginUrl);
				}
		}
		
		
		# 活动状态
		$TIME = time();
		$eventsStatus = false;
		$objMLotteryEventsLimitations  = new LotteryEventsLimitations();
		if($TIME < $scratchData['events']['begin_time'] ){
			$eventsStatus = 'STATUS_NOT_START';
		} elseif ($TIME > $scratchData['events']['end_time']){
			$eventsStatus = 'STATUS_HAS_END';
		} elseif (!$objMLotteryEventsLimitations -> ckEventRestrictions($uid, Request::ip(), $events_id)){
			$eventsStatus = 'STATUS_HAS_RESTRTICTIONS';
		}

		print_r($scratchData);die;
		
		$this->info($scratchData);

		# 记录PV UV日志 访问
		$objCount = new Count();
		$objCount->setPassLog( $events_id,$plat_form_id);
		
		$this->tpl = 'events_scratch';



		$this->setOutput('display', $scratchData['events']['list_display']);
		$this->setOutput('title', $scratchData['events']['weixinShare']['title'] ? $scratchData['events']['weixinShare']['title'] : $scratchData['events']['events_name']);
		$this->setOutput('scratchData', $scratchData);
		$this->setOutput('uid', $uid);
		$this->setOutput('weixinShare', $scratchData['events']['weixinShare']);
		$this->setOutput('eventsStatus', $eventsStatus);
	}
	
	
	/**
	 * 事件:判断当前用户是否能继续参与
	 */
	public function doCKEventRestrictions(){
		$uid = $this->getRunTimeUid();
		if (!Verify::unsignedInt($uid)) {
			self::ajax_fail_exit('无效uid');
		}
		$events_id = Request::g('events_id');
		if (!Verify::unsignedInt($events_id)) {
			self::ajax_fail_exit('无效events_id');
		}
		
		$objMLotteryEventsLimitations = new LotteryEventsLimitations();
		# 记录用户
		$objMLotteryEventsLimitations->setUserEventNum($uid, $events_id); // 记录用户参与UID(天)
		$objMLotteryEventsLimitations->setIpEventNum(Request::ip(), $events_id);// 记录用户参与IP(天)
		$objMLotteryEventsLimitations->setUserParticipationNum($uid, $events_id); // 记录用户参与次数
		
		
		$objMLotteryEventsLimitations  = new LotteryEventsLimitations();
		if(!$objMLotteryEventsLimitations -> ckEventRestrictions($uid, Request::ip(), $events_id)){
			self::ajax_fail_exit('达到了上线');
		}
		self::ajax_success_exit('还可以参与');
	}
	
	
	
	/**
	 * 操作:ajax用户领取 礼品
	 */
	public function doUserRestrictions(){
		$uid = $this->getRunTimeUid();
		if(!$uid){
			self::ajax_fail_exit('UID错误');
		}
		# 获取活动ID
		$events_id = Request::g('events_id');
		if (!Verify::unsignedInt($events_id)) {
			self::ajax_fail_exit('无效id');
		}
		
		$lottery_data_id = Request::g('lottery_data_id');
		if (!Verify::unsignedInt($events_id)) {
			self::ajax_fail_exit('无效lottery_data_id');
		}
		
		$objMlottery = new Lottery();
		$lotteryData = $objMlottery -> getOneLotteryData($lottery_data_id);
		if($lotteryData['uid'] != $uid || $lotteryData['events_id']!=$events_id ){
			self::ajax_fail_exit('领取失败');
		}
		
		$objLotteryEventsTypeScratch = new LotteryEventsTypeScratch($uid, $events_id);
		
		$objLotteryEventsTypeScratch ->userReceive($lottery_data_id);
		
		# 修改当前获奖记录 标记为 已领取
		$info = array(
			'scratch_receive' => LotteryData::EVENT_SCRATCH_RECEIVE_TRUE,
		);
		$objMlottery->upOneLetteryData($info,array('id' => $lottery_data_id));
		self::ajax_success_exit('领取成功') ;
	}
	
	
	/**
	 * 操作:页面初始化 ajax请求操作
	 */
	public function doScratch() {
		
		# 检查用户UID & 获取用户ID
		$events_id = Request::g('events_id');
		if (!Verify::unsignedInt($events_id)) {
			self::ajax_fail_exit(array(
				'errcode'	=> 'INVALID_ID',
			));
		}
		$uid = $this->getRunTimeUid();
		
		# 获取当前活动数据
		$objLotteryEventsTypeScratch = new LotteryEventsTypeScratch($uid, $events_id);
		$scratchData = $objLotteryEventsTypeScratch->getEventsData();
		
		
		
		# 抽奖操作
		$winData = array();
		$tmpWinData = $objLotteryEventsTypeScratch->userScratch();
		
		if ($tmpWinData->isSuccess()) {
			$winData = $tmpWinData->getData();
			$lottery_data_id = $winData['id'];
			
			
			$newWinData = $winData['run_time_data']['prize'][$winData['prize_id']];
			$newWinData['lottery_data_id'] = $lottery_data_id;
			if(isset($winData['virtual_data']) && !empty($winData['virtual_data'])){
				$newWinData['virtual_data']  = $winData['virtual_data'];
			}
		}
		self::ajax_success_exit($newWinData);
	}
	
	/**
	 * 显示
	 */
	public function display() {
		return $this->render();
	}
}