<?php

// 获取指定圈子的成员列表
require_once ("../user_include.inc");

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

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$circleId = ParamUtil::getRequestNumber("circleId", 0);//圈子id
$name = ParamUtil::getRequestString("name");//圈子id

$result = array();

//参数检查
if(empty($unionId) || empty($circleId)) {
	$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);
}
$userMst = $userList[0];

$ownerId = 0;
$isOwner = false;//是否为圈主

//判断圈子是否存在
$circleDat = CircleDat::getById($circleId);
if(empty($circleDat)) {
	$result["message"] = "圈子不存在！";
	responseNG($result);
}

if($circleDat->owner_id == $userMst->id) {
	$isOwner = true;//是否为圈主
	$ownerId = $userMst->id;
}

//获取圈子成员数量
$param = array();
$param['circle_id'] = $circleDat->id;
$param['delete_flg'] = false;
$memberCount = CircleMemberDat::getListCount($param);

//获取圈子成员列表
$memberList = array();
$param = array();
$param['circle_id'] = $circleDat->id;
if(!empty($name)) {
	$param['name_ALT'] = "%" . $name . "%";
}
$param['delete_flg'] = false;
$tmpMemberList = CircleMemberDat::getList($param, "is_owner desc, name", "asc");
if(empty($tmpMemberList)) {
	$result["isOwner"] = false;
	$result["ownerId"] = 0;
	$result["memberCount"] = 0;
	$result["memberList"] = array();
	responseOK($result);
}

//圈主
if($tmpMemberList[0]->is_owner==true) {
	$ownerList = array();
	$ownerList[] = $tmpMemberList[0];
	$memberList["圈主(1)"] = $ownerList;
	array_shift($tmpMemberList);
	}

//根据拼音首字母分组
$pinyin = new Pinyin();
$tmpOrderList = array();
foreach($tmpMemberList as $tmpMember) {
	//获取拼音首字母
	$firstChar = $pinyin->getFirstChar($tmpMember->name);
	//是否已经存在key
	if (array_key_exists($firstChar,$tmpOrderList)){
		array_push($tmpOrderList[$firstChar], $tmpMember);
	} else {
		$tmp = array();
		$tmp[] = $tmpMember;
		$tmpOrderList[$firstChar] = $tmp;
	}
}
ksort($tmpOrderList);

//组装返回数据
$otherList = array();
if(!empty($tmpOrderList)) {
	foreach($tmpOrderList as $key => $keyMembers){
	  if($key == "#") {
		  $otherList = $keyMembers;
	  } else {
		  $memberList[$key . "(" . count($keyMembers) . ")"] = $keyMembers;
	  }
	}
	if(empty(!$otherList)) {
		$memberList["#" . "(" . count($otherList) . ")"] = $otherList;
	}
}

//组装返回数据
$result["isOwner"] = $isOwner;
$result["ownerId"] = $ownerId;
$result["circleDat"] = $circleDat;
$result["userName"] = $userMst->name;
$result["memberCount"] = $memberCount;
$result["memberList"] = $memberList;

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