<?php
namespace Cas\Controller;
use Cas\Module\LotteryEventsTypeCode;
use KIF\Core\Request;
use KIF\Verify;
use KIF\String\Filter;
use Cas\Dao\LotteryEventsForms;
use Cas\Dao\UserInfo;
use Cas\Module\Count;
use Cas\Dao\Platform;
use Cas\Module\Lottery;
/**
 * 领取 “码”
 */
class EventsCode extends EventsController {
	
	public function doDefault() {
		//$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);
		
		# 获取试用信息
		$objLotteryEventsTypeCode = new LotteryEventsTypeCode($uid, $events_id);
		$codeInfo = $objLotteryEventsTypeCode->getEventsData();
		
		# 检查是否移动端
		$title = $codeInfo['weixinShare']['title'] ? $codeInfo['weixinShare']['title']: $codeInfo['events_name'];
		$this->checkIsFromMobile($title);
		
		# 获取登录地址
		$url = Request::g('url');
		$plat_form_id = $this->getPlatFormId();
		$objMLottery  = new Lottery();
		$thisUrl  = $objMLottery -> getEventsUrl($codeInfo['type'],$codeInfo['create_time'],$url);
		$loginUrl = $this -> getLoginUrl($thisUrl);
		$this->setOutput('loginUrl', $loginUrl);

		# 判断是否需要登录
		$ckLogin = $this->ckLogin();
		$this->setOutput('ckLogin', $ckLogin);
		
		
		
		# 检查当前活动是否需要登录
		$authorize = $codeInfo['authorize'];
		if ($authorize && $loginUrl) {
				if($ckLogin==true){
					self::redirect($loginUrl);
				}
		}
		
		
		
		# 活动状态
		$TIME = time();
		$eventsStatus = false;
		if($TIME < $codeInfo['begin_time'] ){
			$eventsStatus = 'STATUS_NOT_START';
			
		} elseif ($TIME > $codeInfo['end_time']){
			$eventsStatus = 'STATUS_HAS_END';
			
		} elseif ($codeInfo['display'] == \Cas\Dao\LotteryEvents::EVENT_DISPLAY_HIDE) {
			//$eventsStatus = 'STATUS_HIDE'; //先注释了。此隐藏 之印象
		}

		$this->info($codeInfo);
		# 记录PV UV日志 访问
		$objCount = new Count();
		$objCount->setPassLog( $events_id,$plat_form_id);
		
		$this->tpl = 'events_code';
		$this->setOutput('display', $codeInfo['list_display']);
		$this->setOutput('title', $title);
		$this->setOutput('codeInfo', $codeInfo);
		$this->setOutput('forms', $codeInfo['forms']);
		$this->setOutput('eventsStatus', $eventsStatus);
		$this->setOutput('weixinShare', $codeInfo['weixinShare']);
	}
	
	/**
	 * 领取优惠
	 */
	public function doApply() {
		# 1.必要参数验证
		$uid = $this->getRunTimeUid();
		if (!Verify::unsignedInt($uid)) {
			self::ajax_fail_exit('无效用户id');
		}
		
		$events_id = Request::g('events_id');
		if (!Verify::unsignedInt($events_id)) {
			self::ajax_fail_exit('无效活动id');
		}
		
		$objLotteryEventsTypeCode = new LotteryEventsTypeCode($uid, $events_id);
		$codeData = $objLotteryEventsTypeCode->getEventsData();
		
		# 检查当前活动是否需要登录 & 获取登录地址
		$plat_form_id = $this->getPlatFormId();
		$objPlatForm  = new Platform();
		$form = $objPlatForm -> get($plat_form_id);
		$url = $form['url'];
		$objMLottery  = new Lottery();
		$thisUrl  = $objMLottery -> getEventsUrl($codeData['type'],$codeData['create_time'],$url);
		$loginUrl = $this -> getLoginUrl($thisUrl);
		$authorize = $codeData['authorize'];
		if ($authorize && $loginUrl) {
			$ckLogin = $this->ckLogin();
			if($ckLogin==true){
				self::ajax_fail_exit(array('url' =>$loginUrl));
			}
		}
		
		# 3.验证活动&用户行为
		$tmpData = $objLotteryEventsTypeCode ->ckUserEventsCode();
		if(!$tmpData->isSuccess()){
			self::ajax_fail_exit($tmpData->getData());
		}
		
		# 4.验证表单
		$forms = $codeData['forms'];
		$formData = Filter::arrayfilter($_POST, true);
		$objLotteryEventsForms = new LotteryEventsForms();
		$tmpVerifyResult = $objLotteryEventsForms->verifyFormData($forms, $formData);
		if (!$tmpVerifyResult->isSuccess()) {
			self::ajax_fail_exit($tmpVerifyResult->getData());
		}
		
		# 5.发“码”
		$userMsg = $formData;
		$tmpResult = $objLotteryEventsTypeCode->userCode($userMsg);
		if (!$tmpResult->isSuccess()) {
			self::ajax_fail_exit($tmpResult->getData());
		}
		
		# 6.保存表单
		$returnData = $tmpResult->getData();
		$info = array_merge($formData, array('uid' => $uid, 'events_id' => $events_id,'code_data'=>$returnData['codeData']));
		if (!$objLotteryEventsForms->add($info)) {
			self::ajax_fail_exit('数据库操作失败');
		}
		
		# 7.增加行为记录
		$objLotteryEventsTypeCode->setLimitations($returnData['codeData']['data']);
		
		self::ajax_success_exit($tmpResult->getData());
	}
	
	
	public function display() {
		return $this->render();
	}
}