<?php
namespace Cas\Controller;
use Cas\Module\LotteryEventsLimitations;

use KIF\Core\Request;
use KIF\Verify;
use Cas\Module\LotteryEventsTypeVote;
use Cas\Dao\UserInfo;
use Cas\Dao\Platform;
use Cas\Module\Count;
use Cas\Module\Lottery;
/**
 * 
 * 投票
 */
class EventsVote extends EventsController {
	
	public function doDefault() {
		//$events_id = Request::g('events_id');
		$events_id = $this->getEventsCreateTimeToId();
		if (!Verify::unsignedInt($events_id)) {
			self::fail_exit('无效id');
		}
		
		
		$url = Request::g('url');
		
		# 判断是否需要登录
		$ckLogin = $this->ckLogin();
		$this->setOutput('ckLogin', $ckLogin);
		
		# 获取用户信息
		$objUserData = new UserInfo();
		$uid = $this->getRunTimeUid();
		$userData = $objUserData -> get($uid);
		$this->setOutput('userData', $userData);
		
		$objLotteryEventsTypeVote = new LotteryEventsTypeVote($uid, $events_id);
		$voteInfo = $objLotteryEventsTypeVote->getEventsData();
		
		# 检查是否移动端
		$title = $voteInfo['weixinShare']['title'] ? $voteInfo['weixinShare']['title'] : $voteInfo['events_name'];
		$this->checkIsFromMobile($title);
		
		# 获取登录地址
		$plat_form_id = $this->getPlatFormId();
		$objMLottery  = new Lottery();
		$thisUrl  = $objMLottery -> getEventsUrl($voteInfo['type'],$voteInfo['create_time'],$url);
		$loginUrl = $this -> getLoginUrl($thisUrl);
		$this->setOutput('loginUrl', $loginUrl);
		
		# 检查当前活动是否需要登录
		$authorize = $voteInfo['authorize'];
		if ($authorize && $loginUrl) {
				if($ckLogin==true){
					self::redirect($loginUrl);
				}
		}
		
		
		# 活动状态
		$TIME = time();
		$eventsStatus = false;
		if($TIME < $voteInfo['begin_time'] ){
			$eventsStatus = 'STATUS_NOT_START';
		
		} elseif ($TIME > $voteInfo['end_time']){
			$eventsStatus = 'STATUS_HAS_END';
		
		} elseif ($voteInfo['display'] == \Cas\Dao\LotteryEvents::EVENT_DISPLAY_HIDE) {
			//$eventsStatus = 'STATUS_HIDE'; //先注释了。此隐藏 之印象列表
		} 
		
		# 活动是否已经到达了当前用户上线
		$objMLotteryEventsLimitations = new LotteryEventsLimitations();
		if(!$objMLotteryEventsLimitations->ckEventRestrictions($uid,Request::ip(), $events_id)){
			$eventsStatus = 'STATUS_LIMITATIONS'; //先注释了。此隐藏 之印象列表
		}
		
		# 投票项颜色
		$cell_colors = array('v_color_1','v_color_2','v_color_3','v_color_4','v_color_5','v_color_6','v_color_7','v_color_8');
		shuffle($cell_colors);
		
		$total_vote_num = 0;
		foreach ($voteInfo['vote'] as $tmpkey => $tmpvote) {
			$total_vote_num += ($tmpvote['num']+$tmpvote['add_num']);
		}
		
		$voteInfo['total_vote_num'] = $total_vote_num;
		
		foreach ($voteInfo['vote'] as $tmpkey => $tmpvote) {
			$voteInfo['vote'][$tmpkey]['per_num'] = round(($tmpvote['num']+$tmpvote['add_num'])/$total_vote_num*100);
		}

		$this->info($voteInfo);
		
		# 记录PV UV日志 访问
		$objCount = new Count();
		$objCount->setPassLog( $events_id,$plat_form_id);
		
		# 视频播放
		preg_match_all('/<embed\s{1}src=\"([^\"]+)\"[^>]+>/i', $voteInfo['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>';
			$voteInfo['events_tips'] = str_replace($tmpMatch, $tmp_new_video_html, $voteInfo['events_tips']);
		}
		
		$this->tpl = 'events_vote';
		$this->setOutput('title', $voteInfo['weixinShare']['title'] ? $voteInfo['weixinShare']['title'] : $voteInfo['events_name']);
		$this->setOutput('display', $voteInfo['list_display']);
		$this->setOutput('vote_type', $voteInfo['vote_type']);
		$this->setOutput('voteInfo', $voteInfo);
		$this->setOutput('weixinShare', $voteInfo['weixinShare']);
		$this->setOutput('eventsStatus', $eventsStatus);
		$this->setOutput('cell_colors', json_encode($cell_colors));
	}
	
	public function doVote() {
		$events_id = Request::g('events_id');
		if (!Verify::unsignedInt($events_id)) {
			self::ajax_fail_exit('无效id');
		}
		
		$vote_id = Request::g('voteid');
		if (!Verify::unsignedInt($events_id)) {
			self::ajax_fail_exit('无效投票项id');
		}
		
		$uid = $this->getRunTimeUid();
		$objLotteryEventsTypeVote = new LotteryEventsTypeVote($uid, $events_id);
		$voteInfo = $objLotteryEventsTypeVote->getEventsData();
		
		# 检查当前活动是否需要登录 & 获取登录地址
		$plat_form_id = $this->getPlatFormId();
		$objPlatForm  = new Platform();
		$form = $objPlatForm -> get($plat_form_id);
		$url = $form['url'];
		$objMLottery  = new Lottery();
		$thisUrl  = $objMLottery -> getEventsUrl($voteInfo['type'],$voteInfo['create_time'],$url);
		$loginUrl = $this -> getLoginUrl($thisUrl);
		$authorize =$voteInfo['authorize'];
		if ($authorize && $loginUrl) {
			$ckLogin = $this->ckLogin();
			if($ckLogin==true){
				self::ajax_fail_exit(array('url' =>$loginUrl));
			}
		}
		
		
		$tmpResult = $objLotteryEventsTypeVote->vote($vote_id);
		if (!$tmpResult->isSuccess()) {
			self::ajax_fail_exit($tmpResult->getData());
		}
		
		self::ajax_success_exit();
	}
	
	public function display() {
		return $this->render();
	}
}