<?php

// 获取志愿者活动列表
require_once ("../user_include.inc");

ErrorLogger::doOutput("Compass...ajax_get_volunteer_event_detail.php....Start.", 0);

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$eventId = ParamUtil::getRequestNumber("eventId", 0);

$result = array();

//参数检查
if(empty($unionId)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

//判断用户是否存在
$param = array();
$param['unionid'] = $unionId;
$param['delete_flg'] = false;
$userList = UserMst::getList($param,'id','desc', 0, 1);
if(empty($userList)) {
	$result["message"] = "用户不存在！";
	responseNG($result);
}

//查询志愿者活动
$volunteerDat = VolunteerEventDat::getById($eventId);
if(empty($volunteerDat)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

$userMst = $userList[0];

//是否已经报名参加
$isMember = false;
$isComplete = false;
$param = array();
$param['volunteer_event_id'] = $eventId;
$param['user_id'] = $userMst->id;
$param['delete_flg'] = false;
$tmpList = VolunteerEventMemberDat::getList($param, "id", "desc", 0, 1);
if(!empty($tmpList)) {
	$isMember = true;
	if($tmpList[0]->status == "FINISH" || $tmpList[0]->status == "TIME") {
		$isComplete = true;
	}
}

//该用户是否为圈主
$isCircleOwner = false;
$param = array();
$param['owner_id'] = $userMst->id;
$param['delete_flg'] = false;
$tmpCount = CircleDat::getListCount($param);
if($tmpCount > 0) {
	$isCircleOwner = true;
}

//报名是否已经结束
$isEnrollFinish = false;
if(date("Y-m-d H:i:s") >= $volunteerDat->enroll_time) {
	$isEnrollFinish = true;
}

//获取已经报名人员列表
$personalMemberList = array();
$circleMemberList = array();
$param = array();
//【个人】
$param = array();
$param['volunteer_event_id'] = $userMst->id;
$param['circle_id'] = 0;
$param['delete_flg'] = false;
$tmpMemberList1 = VolunteerEventMemberDat::getList($param, "id", "desc");
if(!empty($tmpMemberList1)) {
	foreach($tmpMemberList1 as $tmpMember) {
		$tmpUser = UserMst::getById($tmpMember->user_id);
		$tmpArray= array("name"=>$tmpUser->name);
		$personalMemberList[] = $tmpArray;
	}
}

//【圈子】
$param = array();
$param['volunteer_event_id'] = $userMst->id;
$param['circle_id_MIN'] = 1;
$param['is_owner'] = true;
$param['delete_flg'] = false;
$tmpMemberList2 = VolunteerEventMemberDat::getList($param, "id", "desc");
if(!empty($tmpMemberList2)) {
	foreach($tmpMemberList2 as $tmpMember) {
		$tmpUser = UserMst::getById($tmpMember->user_id);
		$param2 = array();
		$param2['volunteer_event_id'] = $userMst->id;
		$param2['circle_id'] = $tmpMember->circle_id;
		$param2['delete_flg'] = false;
		$tmpMemberCount = VolunteerEventMemberDat::getListCount($param2);
		$tmpArray= array("name"=>$tmpUser->name, "enrollCount"=>$tmpMemberCount);
		$circleMemberList[] = $tmpArray;
	}
}

//数据加工
$status_title = "征集中";
if($volunteerDat->status == "FINISH") {
	$status_title = "活动结束";
}
$volunteerDat->status_title = $status_title;
$volunteerDat->scope = "校内";
if($volunteerDat->include_social_user) {
	$volunteerDat->scope = "校内.社会人士";
}


//组装返回数据
$result["isEnrollFinish"] = $isEnrollFinish;
$result["isComplete"] = $isComplete;
$result["isCircleOwner"] = $isCircleOwner;
$result["isMember"] = $isMember;
$result["volunteerEventDat"] = $volunteerDat;
$result["personalMemberList"] = $personalMemberList;
$result["circleMemberList"] = $circleMemberList;

ErrorLogger::doOutput("Compass...ajax_get_volunteer_event_detail.php....End.", 0);

//返回结果
responseOK($result);

function responseNG($result) {
	$result = array("status"=>"NG", "result"=>$result);
	print json_encode($result);
	exit;
}
function responseOK($result) {
	$result = array("status"=>"OK", "result"=>$result);
	print json_encode($result);
	exit;
}
?>