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

use KIF\Dao\AbstractDao;

use Cas\Module\Lottery;

use KIF\Core\Request;
use KIF\Verify;

use Cas\Dao\UserInfo;
use Cas\Dao\LotteryUserFeedback;

use Cas\Module\LotteryEventsTypeTry;
use KIF\String\Filter;
use Cas\Dao\LotteryEventsForms;

/**
 * 反馈评价
 */
class FeedBack extends EventsController {
	
	/**
	 * 页面:默认修改页
	 */
	public function doDefault() {
		# 获取用户信息
		$objUserData = new UserInfo();
		$uid = $this->getRunTimeUid();
		$userData = $objUserData -> get($uid);
		$this->setOutput('userData', $userData);
		
		
		$lottery_data_id = Request::g('lottery_data_id');
		if (!Verify::unsignedInt($lottery_data_id)) {
			self::fail_exit('无效id');
		}
		
		
		
		$objMlottery = new Lottery();
		$lotteryData = $objMlottery-> getOneLotteryData($lottery_data_id);
		
		if(!isset($lotteryData['uid'])||$lotteryData['uid']!=$uid){
			self::echo_404();
		}

		$objLotteryUserFeedback = new LotteryUserFeedback();
		
		$data = array();
		$data['uid'] = $uid;
		$data['events_id'] = $lotteryData['events_id'];
		$data['lottery_data_id'] = $lottery_data_id;
		$data['status'] = 1;


		$ids = $objLotteryUserFeedback->findIdsBy ( $data, 1, $order = ' id desc');
		$feedback = $objLotteryUserFeedback->gets ( $ids );
		
		if ($feedback) {
			$feedback = array_pop($feedback);

			if ($feedback['pic']) {
				$feedback['pic'] = explode(',', $feedback['pic']);
			}
			
		}
		
		$this->setOutput('lottery_data_id', $lottery_data_id);
		$this->setOutput('title', '填写评价信息');
		$this->setOutput('lotteryData', $lotteryData);
		$this->setOutput('feedback', $feedback);
		$up = isset($_REQUEST['up'])?$_REQUEST['up']:null;
		$this->setOutput('action', 'gift');
		$this->setOutput('upAction',$up );
		$this->setOutput('notShowWeixinShare', true);

		$r = Request::g('r');
		if ($r) {
			$r = base64_decode($r);
			$this->setOutput('refer', $r);
			$this->setOutput('action', '');
		}
		$this->setOutput('display', 1);
		$this->tpl = 'feedback';
	}
	
	/**
	 * 事件ajax : 填写评价
	 */
	public function doUp() {

		# 1.必要参数验证
		$uid = $this->getRunTimeUid();
		if (!Verify::unsignedInt($uid)) {
			self::ajax_fail_exit('无效用户id');
		}


		$lottery_data_id = Request::g('lottery_data_id');
		if (!Verify::unsignedInt($lottery_data_id)) {
			self::ajax_fail_exit('无效id');
		}
		
		
		$objMlottery = new Lottery();
		$lotteryData = $objMlottery-> getOneLotteryData($lottery_data_id);
		
		if(!$lotteryData){
			self::ajax_fail_exit('无效id');
		}

		if(!isset($lotteryData['uid'])||$lotteryData['uid']!=$uid){
			self::ajax_fail_exit('无效id');
		}
		
		$events_id = $lotteryData['events_id'];
		if (!Verify::unsignedInt($events_id)) {
			self::ajax_fail_exit('无效id');
		}

		$objMLottery  = new Lottery();
		$eventData = $objMLottery->getOneLotteryEvents ( $events_id );

		if (!$eventData) {
			self::ajax_fail_exit('无效id');
		}

		$type = $eventData['type'];
		if (!in_array($type, array(4,6))) {
			self::ajax_fail_exit('无效类型');
		}

		# 以下为数据验证 
		if ($type == 4) {
			/*
			# 验证是否有试用权限
			$objLotteryEventsTypeTry = new LotteryEventsTypeTry($uid, $events_id);

			$tmpData = $objLotteryEventsTypeTry -> userTry() ;
			if($tmpData->isSuccess()){
				self::ajax_fail_exit('没有申请');
			}
			*/
		}

		if ($type == 6) {
			# 验证是否有邀请函
		}
		
		$objLotteryUserFeedback = new LotteryUserFeedback();
		
		$data = array();
		$data['uid'] = $uid;
		$data['events_id'] = $events_id;
		$data['lottery_data_id'] = $lottery_data_id;
		$data['status'] = 1;
		

		$info = $objLotteryUserFeedback->fetchOne ( $data);

		$data['content'] = Request::g('content');
		$data['pic'] = Request::g('pic');

		if ($info) {

			$tmpResult = $objLotteryUserFeedback->modify($data, array (
				'id' => $info['id'] 
			));
		} else {
			$tmpResult = $objLotteryUserFeedback->add($data);
		}
		
		if (!$tmpResult) {
			self::ajax_fail_exit('错误的反馈信息');
		}
		
		self::ajax_success_exit();
	}
	
	public function display() {
		return $this->render();
	}
}