//用户id
var userid = $.cookie("userid");
var sendId = GetQueryString('sendId');
var attrParams = GetQueryString('attrParams');
var consultId = GetQueryString('consultId');
var consultStatus = GetQueryString('consultStatus');
var assessStatus = GetQueryString('assessStatus');
var thanksStatus = GetQueryString('thanksStatus');
clickLookBtn2(sendId, attrParams, consultId, consultStatus, assessStatus, thanksStatus)
	//getConsultData参数
var pageSize = 5;
var pageNo, isbind, status, timeType, sortType;

//返回按钮
$("#backBtn").on("click", function() {
	if(attrParams == 'consultId') {
		window.location.href = "demand.html";
	} else {
		window.location.href = "consult.html";
	}
})

//列表最后回复
function lastReplyFn(sendId, consultId) {

	var lastReplyTimeData, lastReplyTime, lastReplyCon;
	$.ajax({
		url: "/ajax/tidings/qaLastRevovery",
		async: false,
		data: {
			"consultId": consultId, //咨询ID
			"senderId": sendId //登录者ID
		},
		success: function(response) {
			//			console.log(response)
			if(response["data"] == null || response["data"] == "" || response["data"] == undefined) {
				lastReplyTimeData = '';
				lastReplyTime = '';
				lastReplyCon = '';
			} else {
				lastReplyTimeData = response["data"]["createTime"];
				lastReplyTime = lastReplyTimeData.substr(0, 4) + "-" + lastReplyTimeData.substr(4, 2) + "-" + lastReplyTimeData.substr(6, 2) + " " + lastReplyTimeData.substr(8, 2) + ":" + lastReplyTimeData.substr(10, 2)
				lastReplyCon = response["data"]["tidingsContant"];
			}

		},
		error: function(error) {
			$.MsgBox.Alert('提示', "最后回复数据失败");
		}

	});

	return {
		"lastReplyTime": lastReplyTime,
		"lastReplyCon": lastReplyCon

	};
};

//咨询点击回复，对话页字符串拼接函数
//data:要处理的数据，htmlStr:需要拼接到页面的元素，position
function dialogContentStrFn(data) {
	var htmlStr = "";
	var dialogitem, float, dilinfobox, dilarrow, dilarrow_bor, chatImg;
	for(var i = 0; i < data.length; i++) {
		if(data[i]["professor"]["id"] == userid) {
			dialogitem = "dialogrightitem";
			float = "floatR";
			dilinfobox = "dilinfobox_r";
			dilarrow = "dilarrow_r";
			dilarrow_bor = "dilarrow_r_bor";
			//判断自己 是否有头像
			if(data[i]["professor"]["hasHeadImage"] == 0) {
				chatImg = 'images/default-photo.jpg';
			} else {
				chatImg = "images/head/" + data[i]["professor"]["id"] + "_l.jpg";
			}
		} else {
			dialogitem = "dialogleftitem";
			float = "floatL";
			dilarrow = "dilarrow_l";
			dilinfobox = "dilinfobox_l";
			dilarrow_bor = "dilarrow_l_bor";
			//判断对方 是否有头像
			if(data[i]["professor"]["hasHeadImage"] == 0) {
				chatImg = 'images/default-photo.jpg';
			} else {
				chatImg = "images/head/" + data[i]["professor"]["id"] + "_l.jpg";
			}
		}
		htmlStr +=
			"<div class='" + dialogitem + "'>" +
			"<div class='dilmod'>" +
			"<div class='diluser " + float + "'>" +
			"<a  target='_blank' class='userface headRadius'>" +
			"<img src='" + chatImg + "' width='100%' height='100%'>" +
			"</a>" +
			"</div>" +
			"<div class='dilinfobox " + dilinfobox + " " + float + "'>" +
			"<div class='delinfocon clearfix'>" +
			"<div class='dilarrow " + dilarrow + "'></div>" +
			"<div class='dilarrow " + dilarrow_bor + "'></div>" +
			"<div class='delinfomain " + float + "'>" +
			"<div class='cont'>" +
			"<p>" + data[i]["tidingsContant"] + "</p>" +
			"</div>" +
			"</div>" +
			"</div>" + //dilinfobox
			"</div>" + //dilinfobox
			"</div>" + //dilmod
			"</div>" + //dialogrightitem
			"</div> "
	};

	return htmlStr;
};

//咨询和回复中点击查看或者回复
function clickLookBtn2(sendId, attrParams, consultId, consultStatus, assessStatus, thanksStatus) {

	//收到咨询 
	if(attrParams == "professorId") {
		//调取页面
		dialogHeadFn2("/ajax/consult/qapro", attrParams, consultId, thanksStatus);
		dialogContentDataHandle2(consultId);
		$("#dialog_sendBtn").click(function() {
			sendDialogContentFn2(sendId, consultId)
		});
	}
	//收到回复
	else if(attrParams == "consultId") {

		if(consultStatus == 0) { //进行中
			dialogHeadFn2("/ajax/consult/qacon", attrParams, consultId, thanksStatus);
			dialogContentDataHandle2(consultId);
			$("#dialog_sendBtn").click(function() {
				sendDialogContentFn2(sendId, consultId)
			});
		} else if(consultStatus == 2) { //待回复
			dialogHeadFn2("/ajax/consult/qacon", attrParams, consultId, thanksStatus);
			dialogContentDataHandle2(consultId);
		} else if(consultStatus == 3) { //被感谢
			dialogHeadFn2("/ajax/consult/qacon", attrParams, consultId, thanksStatus);
			dialogContentDataHandle2(consultId);
		} else if(consultStatus == 1) { //完成

			if(assessStatus == 0) { //未评价
				assessFn2(consultId, attrParams, thanksStatus)
			} else if(assessStatus == 1) {
				dialogHeadFn2("/ajax/consult/qacon", attrParams, consultId, thanksStatus);
				dialogContentDataHandle2(consultId);
				$("#dialog_sendBtn").click(function() {
					sendDialogContentFn2(sendId, consultId)
				});
			}

		}

	}

};

//对话页咨询内容展开和收起
var cur_status = "less";

function showMore() {
	//alert("show_more_init!");
	var charNumbers = $("#dialog_consultContent").html().length; //总字数
	var limit = 20; //显示字数
	if(charNumbers > limit) {
		var orgText = $("#dialog_consultContent").html(); //原始文本
		var orgHeight = $("#dialog_consultContent").height(); //原始高度
		var showText = orgText.substring(0, limit); //最终显示的文本
		var showText = showText + "...";
		$("#dialog_consultContent").html(showText);
		var contentHeight = $("#dialog_consultContent").height(); //截取内容后的高度
		$("#dialog_switch").click(function() {
			if(cur_status == "less") {
				$("#dialog_consultContent").height(contentHeight).html(orgText).animate({
					height: orgHeight
				}, {
					duration: "slow"
				});
				$(this).html("收起");
				cur_status = "more";
			} else {
				$("#dialog_consultContent").height(orgHeight).html(showText).animate({
					height: contentHeight
				}, {
					duration: "fast"
				});
				$(this).html("展开");
				cur_status = "less";
			}
		});
	} else {
		$("#dialog_switch").hide();
	}
};

//对话标题处理函数
function dialogHeadFn2(url, attrParams, consultId, thanksStatus) {
	$.ajax({
		url: url, //
		data: {
			"consultId": consultId
		},
		type: "get",
		aysnc: false,
		success: function(response) {
			var myData = response["data"];
			var startTime = myData["createTime"].substr(0, 4) + "年" + myData["createTime"].substr(4, 2) + "月" + myData["createTime"].substr(6, 2) + "日 " + myData["createTime"].substr(8, 2) + ":" + myData["createTime"].substr(10, 2);
			//咨询
			if(attrParams == 'professorId') {

				//进行中
				if(myData["consultStatus"] == 0) {
					$("#dil_consultStatus").text("进行中"); //咨询状态
					$(".dialogfoot").css("display", "block"); //显示发送对话部分
					$("#dialog_consultTitle").text(myData["consultTitle"]); //咨询主题
					$("#dialog_consultTime").text(startTime); //咨询发起时间
					$("#dialog_consultType").text(myData["consultType"]); //咨询目的
					$("#dial_consultName").text(myData["professor"]["name"]); //与@@@对话
					$("#dialog_consultContentContainer").hide() //咨询内容
				} else if(myData["consultStatus"] == 3) { //已谢绝
					var finishTime = myData["finishTime"].substr(0, 4) + "年" + myData["finishTime"].substr(4, 2) + "月" + myData["finishTime"].substr(6, 2) + "日 " + myData["finishTime"].substr(8, 2) + ":" + myData["finishTime"].substr(10, 2)
					$("#dil_consultStatus").text("已谢绝"); //咨询状态
					$("#dialog_consultTitle").text(myData["consultTitle"]); //咨询主题
					$("#dialog_consultTime").text(startTime); //咨询发起时间
					$("#dialog_consultType").text(myData["consultType"]); //咨询目的
					$("#dial_consultName").text(myData["professor"]["name"]); //与@@@对话
					$("#dialog_consultContent").html(myData["consultContant"]) //咨询内容
					$("#no_title").text(finishTime); //谢绝时间
					$(".rejectOver").show(); //显示谢绝内容和时间模块
					$(".dialogfoot").css("display", "none"); //隐藏发送对话部分
					noReason(consultId); //谢绝内容
				}
				//咨询已完成
				else {
					$("#dil_consultStatus").text("已完成"); //咨询状态
					$("#dialog_consultTitle").text(myData["consultTitle"]); //咨询主题
					$("#dialog_consultTime").text(startTime); //咨询发起时间
					$("#dialog_consultType").text(myData["consultType"]); //咨询目的
					$("#dial_consultName").text(myData["professor"]["name"]); //与@@@对话
					$("#dialog_consultContent").html(myData["consultContant"]) //咨询内容

					$(".dialogfoot").css("display", "none"); //隐藏发送对话部分
					//如果评价
					if(myData["assessStatus"] == 1) {

						var startConut = myData["assessStar"];
						for(var i = 0; i < startConut; i++) {
							$("#showAllAssess .evastar2").eq(i).addClass("addStar");
						}
						$("#showAllAssess").removeClass("displayNone"); //总体评价

						$("#dialog_assessContent").removeClass("displayNone");
						$("#dialog_assessContentText").html(myData["assessContant"]);

						//如果感谢
						if(myData["thanksStatus"] == 1) {

							if(myData["thanksMoney"] != null && myData["thanksMoney"] != undefined && myData["thanksMoney"] != "" && myData["thanksMoney"] != 0) {

								$("#dialog_thanks").removeClass("displayNone"); //感谢
								$("#dialog_thanksMoney").text(myData["thanksMoney"]);
							}

						} else { //未感谢
							return false;
						}

					} else { //未评价
						return false;
					}

				}

			}
			//回复
			else if(attrParams == 'consultId') {

				//回复进行中
				if(myData["consultStatus"] == 0) {
					$("#dil_consultStatus").text("进行中"); //咨询状态
					$(".dialogfoot").css("display", "block"); //显示发送对话部分
					$("#dialog_consultTitle").text(myData["consultTitle"]); //咨询主题
					$("#dialog_consultTime").text(startTime); //咨询发起时间
					$("#dialog_consultType").text(myData["consultType"]); //咨询目的
					$("#dial_consultName").text(myData["professor"]["name"]); //与@@@对话
					$("#dialog_consultContentContainer").hide() //咨询内容
					$("#confirmBtn").removeClass("displayNone"); //确认完成按钮显示
					//点击确认完成
					$("#confirmBtn").click(function() {
							clickConfirmFn(consultId, thanksStatus);
						})
						//发表评论
					$("#sendAssessBtn").click(function() {
						clickSendAssessBtn2(consultId, attrParams, thanksStatus)
					});
					//稍后评价
					$("#noAssessBtn").click(function() {
						noAssessFn2(consultId, attrParams, thanksStatus);
					});
					//点击关闭
					$("#closeAssessBtn").click(function() {
						noAssessFn2(consultId, attrParams, thanksStatus);
					});
				} else if(myData["consultStatus"] == 2) {
					$("#dil_consultStatus").text("待回复"); //咨询状态
					$(".dialogfoot").css("display", "block"); //显示发送对话部分
					$("#dialog_consultTitle").text(myData["consultTitle"]); //咨询主题
					$("#dialog_consultTime").text(startTime); //咨询发起时间
					$("#dialog_consultType").text(myData["consultType"]); //咨询目的
					$("#dial_consultName").text(myData["professor"]["name"]); //与@@@对话
					$("#dialog_consultContentContainer").hide() //咨询内容
					$(".dialogfoot").css("display", "none"); //隐藏发送对话部分
				} else if(myData["consultStatus"] == 3) {
					var finishTime = myData["finishTime"].substr(0, 4) + "年" + myData["finishTime"].substr(4, 2) + "月" + myData["finishTime"].substr(6, 2) + "日 " + myData["finishTime"].substr(8, 2) + ":" + myData["finishTime"].substr(10, 2)
					$("#dil_consultStatus").text("被谢绝"); //咨询状态
					$("#dialog_consultTitle").text(myData["consultTitle"]); //咨询主题
					$("#dialog_consultTime").text(startTime); //咨询发起时间
					$("#dialog_consultType").text(myData["consultType"]); //咨询目的
					$("#dial_consultName").text(myData["professor"]["name"]); //与@@@对话
					$("#dialog_consultContent").html(myData["consultContant"]) //咨询内容
					$("#no_title").text(finishTime); //谢绝时间
					$(".rejectOver").show(); //显示谢绝内容和时间模块
					$(".dialogfoot").css("display", "none"); //隐藏发送对话部分
					noReason(consultId); //谢绝内容
				} else { //回复完成
					$("#dil_consultStatus").text("已完成"); //咨询状态
					$("#dialog_consultTitle").text(myData["consultTitle"]); //咨询主题
					$("#dialog_consultTime").text(startTime); //咨询发起时间
					$("#dialog_consultType").text(myData["consultType"]); //咨询目的
					$("#dial_consultName").text(myData["professor"]["name"]); //与@@@对话
					$("#dialog_consultContent").html(myData["consultContant"]) //咨询内容
					$(".dialogfoot").css("display", "none"); //隐藏发送对话部分
					//回复完成未评价
					if(myData["assessStatus"] == 0) {

						assessFn2(consultId, attrParams, thanksStatus)

					} else {
						//评价星级
						var startConut = myData["assessStar"];
						for(var i = 0; i < startConut; i++) {
							$("#showAllAssess .evastar2").eq(i).addClass("addStar");
						}
						$("#showAllAssess").removeClass("displayNone"); //总体评价
						//						$("#dialog_thanksBtn").removeClass("displayNone");

						$("#dialog_assessContent").removeClass("displayNone");
						$("#dialog_assessContentText").html(myData["assessContant"]);
						$("#dialog_consultContentContainer").show() //咨询内容
						$("#confirmBtn").hide();
						//未感谢
						if(myData["thanksStatus"] == 0) {

							//							$("#dialog_thanksBtn").removeClass("dispalyNone");
						} else {
							//感谢金额
							//							$("#dialog_thanks").removeClass("displayNone");
							//							$("#dialog_thanksMoney").text(myData["thanksMoney"]);

						}

					}

				}

			};

			//咨询内容展开收起效果
			/*if($("#dialog_consultContent")){
				
				showMore();
			};*/

		},
		error: function(error) {
			//对话标题信息
		}
	});

}

//谢绝理由显示
function noReason(consultId) {
	$.ajax('/ajax/consultReject/reasons', {
		data: {
			"consultId": consultId,
		}, //咨询ID
		dataType: 'json', //服务器返回json格式数据
		type: 'GET', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			if(data.success && data.data) {
				var cent = "";
				for(var i = 0; i < data.data.length; i++) {
					cent += ' ' + data.data[i].sort + '、' + data.data[i].rejectReason;
				}
				$("#no_cent").html("感谢您的咨询，但很抱歉，由于以下原因，暂时不能接受您的咨询：" + cent + "。");
			}
		},
		error: function() {
			$.MsgBox.Alert('提示', "服务器链接超时");
		}
	});
}

//点击确认完成
function clickConfirmFn(consultId, thanksStatus) {
	$.MsgBox.Confirm('科袖提示', '是否确定完成', function() {
		finishFn(consultId)
	});
}

//点击确认完成弹窗中完成
function finishFn(consultId, thanksStatus) {
	//更新咨询状态
	$.ajax({
		url: "/ajax/consult/finishTime",
		type: "post",
		data: {
			"consultId": consultId, //咨询ID
			"consultStatus": "1", //咨询状态 0-进行中，1-已完成
		},
		success: function(response) {
			console.log(response);
			$(".blackcover").remove();
			$("body").css("position", "");
			assessFn2(consultId, "consultId", thanksStatus);
		},
		error: function(error) {
			//更新咨询状态失败
		}
	});

};

//============对话内容数据处理函数
function dialogContentDataHandle2(consultId) {
	$.ajax({
		"url": "/ajax/tidings/qacon", //对话内容显示接口
		"type": "get",
		"data": {
			"consultId": consultId
		},
		"success": function(response) {
			$("#dialogContent").empty("");
			var dialogConData = response["data"];
			var dialogStr;
			dialogStr = dialogContentStrFn(dialogConData);
			$("#dialogContent").prepend(dialogStr);
			$("#dialogContent").removeClass("dialog");
			var height = $("#dialogContent").height();

			$("#dialogContent").addClass("dialog");
			$("#dialogContent").scrollTop(height);
		},
		error: function(error) {

		}
	});

};

//发送按钮处理函数
function sendDialogContentFn2(sendId, consultId) {
	var tidingsContant = $("#dialog_sendContent").val();

	//console.log(tidingStr);
	$.ajax({
		"url": "/ajax/tidings",
		"type": "post",
		"data": {
			"tidingsContant": tidingsContant,
			"senderId": sendId,
			"consultId": consultId
		},
		"success": function(response) {
			//	console.log(response);//返回对话内容的id
			if(response.success) {

				dialogContentDataHandle2(consultId);
			}
		},
		"error": function(error) {
			//对话发送消息失败
			console.log(error);
		}
	});
	$("#dialog_sendContent").val("");
};

//评价
function assessFn2(consultId, attrParams, thanksStatus) {

	ConsultComment();
	$("#sendAssessBtn").css({
		"cursor": "auto",
		"background-color": "#dadada"
	});
	//评价星星样式
	var start;

	function clickStar() {
		//发表评论
		var cont1 = $("#assessCon").val();
		if(cont1)
			$("#sendAssessBtn").css({
				"cursor": "pointer",
				"background-color": "#ff9900",
				" border": ":1px solid #ff8500"
			});
		$("#sendAssessBtn").click(function() {
			clickSendAssessBtn2(consultId, attrParams, thanksStatus)
		});
		$(".evastar2").removeClass('addStar');
		start = $(this).index() + 1;
		for(var i = 0; i < start; i++) {
			if(i < start) {
				$(".evastar2").eq(i).addClass("addStar");
			} else {
				$(".evastar2").eq(i).removeClass('addStar');
			}
		}
		$("#startCount").val($(".addStar").length);
	};
	$(".evastar2").bind("click", clickStar);

	$("#assessCon").on("keyup", function() {
		var ass1 = $("#assessCon").val();
		if(ass1.trim() == "") {
			if($("#sendAssessBtn").css("background-color") == "rgb(255, 153, 0)") {
				$("#sendAssessBtn").css({
					"cursor": "auto",
					"background-color": "#dadada"
				});
			}

		} else {
			if($("#startCount").val().trim()) {
				$("#sendAssessBtn").css({
					"cursor": "pointer",
					"background-color": "#ff9900",
					" border": ":1px solid #ff8500"
				});
			}
		}
	})

	//稍后评价
	$("#noAssessBtn").click(function() {
		noAssessFn2(consultId, attrParams, thanksStatus)
	});
	//点击关闭
	$("#closeAssessBtn").click(function() {
		noAssessFn2(consultId, attrParams, thanksStatus)
	});
};

//发送评价
//点击发表评论：保存发表内容和星级
function clickSendAssessBtn2(consultId, attrParams, thanksStatus) {
	var assessStar = $("#startCount").val();
	var assessContant = $("#assessCon").val();
	if($("#sendAssessBtn").css("background-color") != "rgb(255, 153, 0)") {
		return;
	}
	var assessContentStr = {
		"consultId": consultId, //咨询ID
		"assessStatus": "1", //评价状态 0-未评价，1-已评价
		"assessStar": assessStar, //评价星级
		"assessContant": assessContant //评价内容
	};

	//保存
	$.ajax({
		"url": "/ajax/consult/assess", //点击发表评论接口
		"type": "post",
		"data": assessContentStr,
		"success": function(response) {
			$(".blackcover").remove();
			$("body").css("position", "");
			//发表评价后进入对话页
			dialogHeadFn2("/ajax/consult/qacon", attrParams, consultId, thanksStatus);
			dialogContentDataHandle2(consultId);
		},
		"error": function(error) {
			//发表评论失败
			$.MsgBox.Alert('提示', "评论失败");
		}

	});

};

//稍后评价、
function noAssessFn2(consultId, attrParams, thanksStatus) {
	$.ajax({
		url: "/ajax/consult/qacon", //对话标题信息接口
		data: {
			"consultId": consultId,
			"readStatus": "1"
		},
		type: "get",
		aysnc: false,
		success: function(response) {
			var myData = response["data"];
			var startTime2 = myData["createTime"].substr(0, 4) + "年" + myData["createTime"].substr(4, 2) + "月" + myData["createTime"].substr(6, 2) + "日 " + myData["createTime"].substr(8, 2) + ":" + myData["createTime"].substr(10, 2);

			$(".blackcover").remove();
			$("body").css("position", "");

			//稍后评价说明已经完成
			$("#dil_consultStatus").text("已完成"); //咨询状态
			$("#dialog_consultTitle").text(myData["consultTitle"]); //咨询主题
			$("#dialog_consultTime").text(startTime2); //咨询发起时间
			$("#dialog_consultType").text(myData["consultType"]); //咨询目的
			$("#dial_consultName").text(myData["professor"]["name"]); //与@@@对话
			$(".dialogfoot").css("display", "none"); //隐藏发送对话部分
			$("#dialog_consultContentContainer").css("display", "none");
			$("#confirmBtn").hide();
			$("#dialog_assessBtn").removeClass("displayNone");
			$("#dialog_assessBtn").click(function() {
				assessFn2(consultId, attrParams, thanksStatus);
				location.reload(true);
			});

		},
		error: function(error) {

		}
	});

	dialogContentDataHandle2(consultId);
};