<?php

// 获取在线咨询详情
require_once ("../user_include.inc");

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

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

$result = array();

//参数检查
if(empty($unionId) || empty($faqId)) {
	$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];

//获取在线答疑数据
$faqDat = array();
$tmpFaqDat = FaqDat::getById($faqId);
if(empty($tmpFaqDat)) {
	$result["message"] = "数据不存在!";
	responseNG($result);
}
//获取发布者信息
$isOwner = false;
if($userMst->id == $tmpFaqDat->user_id) {
	$isOwner = true;
	//为发布者的情况下
	$faqDat["headImg"] = $userMst->header_img;
	$faqDat["name"] = $userMst->name;
} else {
	$isOwner = false;
	//获取发布者信息
	$ownerUser = UserMst::getById($tmpFaqDat->user_id);
	//为发布者的情况下
	$faqDat["headImg"] = $ownerUser->header_img;
	$faqDat["name"] = $ownerUser->name;
}
$faqDat["content"] = $tmpFaqDat->content;
//|分割图片为数组
$images = explode("|", $tmpFaqDat->images);
$images = array_filter($images);
$faqDat["images"] = $images;
$faqDat["publishDate"] = str_replace('-','.',substr($tmpFaqDat->registration_date, 0, 10));

//获取回复数据
$faqDetailList = array();
$param = array();
$param['faq_id'] = $faqId;
$param['delete_flg'] = false;
$tmpDetailList = FaqDetailDat::getList($param,'id','desc');
if(!empty($tmpDetailList)) {
	//查找回答者信息
	foreach($tmpDetailList as $tmp) {
		$faqDetailDat = array();
		$author = UserMst::getById($tmp->user_id);
		$faqDetailDat["id"] = $tmp->id;
		$faqDetailDat["headImg"] = $author->header_img;
		$faqDetailDat["name"] = $author->name;
		$faqDetailDat["isBest"] = $tmp->is_best;
		$faqDetailDat["content"] = $tmp->content;
		$faqDetailDat["replyTime"] = str_replace('-','.',substr($tmp->registration_date, 0, 10));
		$faqDetailList[] = $faqDetailDat;
	}
}

//组装返回数据
$result["isOwner"] = $isOwner;
$result["faqDat"] = $faqDat;
$result["faqDetailList"] = $faqDetailList;

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