<?php

// 取消志愿者活动报名
require_once ("../user_include.inc");

ErrorLogger::doOutput("Compass...ajax_cancel_volunteer_event_personal.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'] = $eventId;
$param['user_id'] = $userMst->id;
$param['delete_flg'] = false;
$tmpVolunteerEventMemberList = VolunteerEventMemberDat::getList($param, "id", "desc", 0, 1);
if(empty($tmpVolunteerEventMemberList)) {
	$result["message"] = "数据错误！";
	responseNG($result);
}

$volunteerEventMemberDat = $tmpVolunteerEventMemberList[0];

//个人报名直接取消
if($volunteerEventMemberDat->circle_id == 0) {
	$volunteerEventMemberDat->delete_flg = true;
	$volunteerEventMemberDat->save();
} else {
	//圈子报名
	if($volunteerEventMemberDat->is_owner) {
		//圈主取消的情况下，取消所有成员报名
		$circleId = $volunteerEventMemberDat->circle_id;
		$sql = "update volunteer_event_member_dat set delete_flg=true where delete_flg=false and volunteer_event_id='{$eventId}' and circle_id='{$circleId}'";
		$db = &CompassDBManager::getInstance();
		$db->executeQuery($sql);
	} else {
		//成员取消参加，之取消自己的的参加
		$volunteerEventMemberDat->delete_flg = true;
		$volunteerEventMemberDat->save();
	}
	
}

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