<?php
namespace Cas\Controller;
use Cas\Dao\LotteryUserExpress;
use KIF\Core\Request;
use KIF\Verify;
use Cas\Module\LotteryEventsTypeTry;
use KIF\String\Filter;
use Cas\Dao\LotteryEventsForms;
use Cas\Dao\UserInfo;
use Cas\Module\Count;
use Cas\Module\Lottery;
use Cas\Dao\LotteryUserFeedback;

use Cas\Module\LotteryLog;
/**
 * 试用
 */
class EventsTry extends EventsController {
	
	public function doDefault() {
		$thisTime = time();
		
		//$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);
		
		# 获取试用信息
		$objLotteryEventsTypeTry = new LotteryEventsTypeTry($uid, $events_id);
		$tryInfo = $objLotteryEventsTypeTry->getEventsData();
		
		# 检查是否移动端
		$title = $tryInfo['weixinShare']['title'] ? $tryInfo['weixinShare']['title'] : $tryInfo['events_name'];
		//$this->checkIsFromMobile($title);
		
		
		
		# 活动状态
		$TIME = time();
		$eventsStatus = false;
		if($TIME < $tryInfo['begin_time'] ){
			$eventsStatus = 'STATUS_NOT_START';
				
		} elseif ($TIME > $tryInfo['end_time']){
			$eventsStatus = 'STATUS_HAS_END';
				
		} elseif ($tryInfo['display'] == \Cas\Dao\LotteryEvents::EVENT_DISPLAY_HIDE) {
			//$eventsStatus = 'STATUS_HIDE'; //先注释了。此隐藏 之印象列表
		}
		
		# 获取用户最新收货地址
		$objDLotteryUserExpress = new LotteryUserExpress();
		$userExpress = $objDLotteryUserExpress -> getUserExpress($uid);
		
		$this->info($tryInfo);
		
		
		# 记录PV UV日志 访问
		$objCount = new Count();
		$plat_form_id = $this->getPlatFormId();
		$objCount->setPassLog( $events_id,$plat_form_id);
		
		# 视频播放
		preg_match_all('/<embed\s{1}src=\"([^\"]+)\"[^>]+>/i', $tryInfo['events_tips'], $matches);
		if ($matches) foreach ($matches[0] as $key => $tmpMatch) {
			$tmp_new_video_html = '<iframe class="video_iframe" height=498 width=510 src="'.$matches[1][$key].'" frameborder=0 allowfullscreen></iframe>';
			$tryInfo['events_tips'] = str_replace($tmpMatch, $tmp_new_video_html, $tryInfo['events_tips']);
		}

		

		
		$this->tpl = 'events_try';
		$this->setOutput('display', $tryInfo['list_display']);
		$this->setOutput('userExpress', $userExpress);
		$this->setOutput('title', $tryInfo['weixinShare']['title'] ? $tryInfo['weixinShare']['title'] : $tryInfo['events_name']);
		$this->setOutput('tryInfo', $tryInfo);
		$this->setOutput('forms', $tryInfo['forms']);
		$this->setOutput('eventsStatus', $eventsStatus);
		$this->setOutput('weixinShare', $tryInfo['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');
		}
		
		$objLotteryEventsTypeTry = new LotteryEventsTypeTry($uid, $events_id);
		
		# 2.活动信息&用户行为验证
		$tmpData = $objLotteryEventsTypeTry -> userTry() ;
		if(!$tmpData->isSuccess()){
			self::ajax_fail_exit($tmpData->getData());
		}
		
		# 3.用户表单验证
		$tryInfo = $objLotteryEventsTypeTry->getEventsData();
		$forms = $tryInfo['forms'];
		$formData = Filter::arrayfilter($_POST, true);
		$objLotteryEventsForms = new LotteryEventsForms();
		$tmpVerifyResult = $objLotteryEventsForms->verifyFormData($forms, $formData);
		if (!$tmpVerifyResult->isSuccess()) {
			self::ajax_fail_exit($tmpVerifyResult->getData());
		}
		
		# 4.保存表单内容
		$info = array_merge($formData, array('uid' => $uid, 'events_id' => $events_id));
		if (!$objLotteryEventsForms->add($info)) {
			self::ajax_fail_exit('数据库操作失败');
		}
		
		# 5.增加行为记录
		$objLotteryEventsTypeTry->setLimitations();
		
		self::ajax_success_exit();
	}
	
	public function display() {
		return $this->render();
	}
}