<?php

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

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

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

//用户所在【省市区】
$province = ParamUtil::getRequestString("province");
$city = ParamUtil::getRequestString("city");
$district = ParamUtil::getRequestString("district");

$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);
}

//每页显示条数
$rowCount = 10;
$offset = $page * $rowCount;

$userMst = $userList[0];

//志愿者活动
$param = array();
if(DEBUG_MODE==false) {
	$param['province'] = $province;
	$param['city'] = $city;
	$param['district'] = $district;
}
$param['status'] = "OK";
$param['delete_flg'] = false;
//社会用户和机构用户的时候，添加【是否允许社会人士参加】
if($userMst->role == 5 || $userMst->role == 6) {
	$param['include_social_user'] = true;
}
$volunteerEventList = array();
$tmpVolunteerEventList = VolunteerEventDat::getList($param,'id','desc', $offset, $rowCount);
$volunteerEvenCount = VolunteerEventDat::getListCount($param);
$pageCount = ceil($volunteerEvenCount/$rowCount);

//查询所在区域能力分汇总
//能力分汇总
$abilityPoint = 0;
$sql = "select sum(ability_point) as ability_point from user_mst where delete_flg = false and school_no is not null and province='{$province}'";
if(!empty($city)) {
	$sql .= " and city='{$city}'";
}
if(!empty($district)) {
	$sql .= " and district='{$district}'";
}

$db = &CompassDBManager::getInstance();
$tmp = $db->executeQuery($sql);
if(!empty($tmp)) {
	$abilityPoint = $tmp[0]['ability_point'];
}
$result["abilityPoint"] = $abilityPoint;

//设置状态和招募范围
foreach($tmpVolunteerEventList as $tmp) {
	$tmp->status_title = EventStatus::getTitleByName($tmp->status);
	$tmp->scope = "校内";
	if($tmp->include_social_user) {
		$tmp->scope = "校内.社会人士";
	}
	$volunteerEventList[] = $tmp;
}


//组装返回数据
$result["rowCount"] = $rowCount;
$result["page"] = $page;
$result["pageCount"] = $pageCount;
$result["volunteerEventList"] = $volunteerEventList;

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