<?php

// 获取志愿者活动报名【圈子】
require_once ("../user_include.inc");

ErrorLogger::doOutput("Compass...ajax_attend_volunteer_event_circle.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];

//是否已经报名
$param = array();
$param['volunteer_event_id'] = $userMst->id;
$param['user_id'] = $userMst->id;
$param['delete_flg'] = false;
$tmpList = VolunteerEventMemberDat::getList($param, "id", "desc", 0, 1);
if(!empty($tmpList)) {
	$result["message"] = "不能重复报名！";
	responseNG($result);
}

//查找圈子信息
$param = array();
$param['owner_id'] = $userMst->id;
$param['delete_flg'] = false;
$tmpCircleDatList = CircleDat::getList($param, "id", "asc", 0, 1);
if(empty($tmpCircleDatList)) {
	$result["message"] = "不是圈主！";
	responseNG($result);
}
$circleDat = $tmpCircleDatList[0];

//报名顺序
$param = array();
$param['volunteer_event_id'] = $userMst->id;
$param['delete_flg'] = false;
$tmpCount = VolunteerEventMemberDat::getListCount($param);
$indexNo = sprintf("%05d", $tmpCount+1);

//报名表插入数据
$volunteerEventMemberDat = new VolunteerEventMemberDat();
$volunteerEventMemberDat->volunteer_event_id = $volunteerDat->id;
$volunteerEventMemberDat->user_id = $userMst->id;
$volunteerEventMemberDat->circle_id = $circleDat->id;
$volunteerEventMemberDat->is_owner = true;
$volunteerEventMemberDat->index_no = $indexNo;
$volunteerEventMemberDat->mobile = $userMst->mobile;
$volunteerEventMemberDat->save();

//查找圈子成员，给圈子成员发送邀请消息
$param = array();
$param['circle_id'] = $circleDat->id;
$param['user_id_NOT'] = $userMst->id;//不包含自己
$param['delete_flg'] = false;
$tmpCircleMemberList = CircleMemberDat::getListCount($param);
if(!empty($tmpCircleMemberList)) {
	foreach(tmpCircleMemberList as $tmpCircleMember) {
		$userMessageDat = new UserMessageDat();
		$userMessageDat->user_id = $tmpCircleMember->user_id;
		$userMessageDat->title = "活动参加邀请通知";
		$userMessageDat->comment = "嘀嘀嘀！圈主邀请您参加活动，快点击进入，参加报名吧！";
		
		//添加附件附件
		$userMessageDat->attach_dat_type = "VOLUNTEER";
		$userMessageDat->attach_dat_id = $circleDat->id;
		$userMessageDat->attach_dat_path = "pages/volunteersEventDetail/volunteersEventDetail?volunteersEventId={$volunteerDat->id}&circleId={$circleDat->id}";
	}
}


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

//返回结果
$result["message"] = "报名成功！";
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;
}
?>