<?php

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

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

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$eventId = ParamUtil::getRequestNumber("eventId", 0);
$circleId = ParamUtil::getRequestNumber("circleId", 0);//受邀请参加的时候带有此参数

$result = array();

//参数检查
if(empty($unionId) || empty($eventId)) {
	$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'] = $eventId;
$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['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->index_no = $indexNo;
$volunteerEventMemberDat->mobile = $userMst->mobile;

//通过圈子邀请加入的时候
if($circleId > 0) {
	$volunteerEventMemberDat->circle_id = $circleId;
	$volunteerEventMemberDat->is_owner = false;
}
$volunteerEventMemberDat->save();

ErrorLogger::doOutput("Compass...ajax_attend_volunteer_event_personal.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;
}
?>