$(function(){
	$(".onlogin .headnavbtn li").eq(0).addClass("navcurrent");
	$(".workmenu>ul>li").eq(1).addClass("nowLi");
	loginStatus();//判断个人是否登录
	valUser();
})
//咨询确认
$(function() {
	$(".onlogin .headnavbtn li").eq(0).addClass("navcurrent");
	var consultId = GetQueryString('consultId');
	var sendId = $.cookie("userid");
	var professorId = "professorId";
	var newtemp;
	consucentfun(consultId);

	/*点击确认接受咨询*/
	$("#consuOk").on("click", function() {
		$.MsgBox.Confirm("消息提醒", "确认回复此需求?", function() {
			okconsufun(consultId, sendId, professorId);
		});
	})

	//点击谢绝理由按钮
	$(".rejectTo").on("click", function() {
		$(this).parent(".basicBtn").hide();
		$(".demandBox").slideDown();
	})

	//谢绝理由选择
	$(".mutChecked").on('click', 'li', function() {
		if($(this).is('.checkedLi')) {
			$(this).removeClass('checkedLi');
		} else {
			$(this).addClass('checkedLi');
		}
	});

	//拒绝字数限制
	$(".frmtype").on('keyup', function() {
		var obj = $(this);
		checkLen(obj);
	});

	//谢绝理由提交
	$("#consuNo").on('click', function() {
		var frmtype = $(".frmtype").val();
		var arr = $(".mutChecked li.checkedLi span"),
			temp = [];
		for(var i = 0; i < arr.length; i++) {
			temp.push(arr[i].innerHTML);
		}
		if(frmtype == "") {
			newtemp = temp;
		} else {
			newtemp = temp.concat([frmtype]);
		}
		//console.log(JSON.stringify(newtemp))
		if(newtemp == "") {
			$.MsgBox.Alert('提示', "至少选择一个或输入谢绝缘由");
		} else {
			submtbut(consultId, sendId, newtemp);
		}
	});

})

/*确认接受咨询*/
function okconsufun(consultId, sendId, professorId) {
	$.ajax({
		url: "/ajax/consult/agree",
		data: {
			"consultId": consultId
		}, //咨询ID
		dataType: 'json', //服务器返回json格式数据
		type: 'POST', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒;
		async: false,
		success: function(data) {
			if(data.success) {
				//console.log(JSON.stringify(data.success));
				window.location.href = "diloags.html?sendId=" + sendId + "&attrParams=" + professorId + "&consultId=" + consultId;
			}
		},
		error: function() {
			$.MsgBox.Alert('提示', "服务器链接超时");
		}
	});
}

/*获取咨询内容*/
function consucentfun(consultId) {
	$.ajax({
		url: "/ajax/consult/qapro",
		data: {
			"consultId": consultId
		}, //咨询ID
		dataType: 'json', //服务器返回json格式数据
		type: 'GET', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒;
		async: false,
		success: function(data) {
			if(data.success) {
				//console.log(JSON.stringify(data.success));
				id = data.data.professor.id;
				var createTime = data.data.createTime.substr(0, 4) + "年" + data.data.createTime.substr(4, 2) + "月" + data.data.createTime.substr(6, 2) + "日 " + data.data.createTime.substr(8, 2) + ":" + data.data.createTime.substr(10, 2);
				$("#dialog_consultTitle").text(data.data.consultTitle);
				$("#dialog_consultTime").text(createTime);
				$("#dialog_consultType").text(data.data.consultType);
				$("#dialog_consultContent").text(data.data.consultContant);
				consufun(id);
			}
		},
		error: function() {
			$.MsgBox.Alert('提示', "服务器链接超时");
		}
	});
}

/*获取咨询者内容*/
function consufun(id) {
	$.ajax({
		url: "/ajax/professor/editBaseInfo/" + id,
		dataType: 'json', //服务器返回json格式数据
		timeout: 10000, //超时时间设置为10秒 ; 
		async: false,
		type: 'GET', //HTTP请求类型
		success: function(data) {
			if(data.success && data.data) {
				//console.log(JSON.stringify(data));
				var $info = data.data || {};

				$("#nameS").text($info.name);

				if($info.hasHeadImage == 1) {
					$(".imgProfess").attr("src", "/images/head/" + $info.id + "_l.jpg");
				} else {
					$(".imgProfess").attr("src", "/images/default-photo.jpg");
				}

				//认证
				var oSty=autho($info.authType,$info.orgAuth,$info.authStatus);
				$("#usermark").addClass(oSty.sty);
				$("#usermark").attr("title",oSty.title);

				if($info.title) {
					if($info.office) {
						$("#title").text($info.title + ",")
					} else {
						$("#title").text($info.title)
					}
				}
				if($info.office) {
					if($info.orgName) {
						$("#office").text($info.office + ",")
					} else {
						$("#office").text($info.office)
					}
				}
				$("#organization").text($info.orgName);
			}
		},
		error: function() {
			$.MsgBox.Alert('提示', "服务器链接超时");
		}
	});
}

/*拒绝字数限制*/
function checkLen(obj) {
	var maxChars = 300; //最多字符数  
	if(obj.val().length > maxChars) {
		obj.val() = obj.val().substring(0, maxChars);
	}
	var curr = maxChars - obj.val().length;
	$(".countNum").text(curr.toString());
};

/*谢绝理由提交*/
function submtbut(consultId, sendId, newtemp) {
	$.ajax({
		url: "/ajax/consult/reject",
		data: {
			"consultId": consultId,
			"professorId": sendId,
			"reasons": newtemp
		}, //咨询ID
		dataType: 'json', //服务器返回json格式数据
		type: 'POST', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒;
		traditional: true, //传数组必须加这个
		success: function(data) {
			if(data.success) {
				$.MsgBox.Alert('提示', "已谢绝对方的咨询——您可以再看看其他需求。");
				setInterval (function(){
					window.location.href = "index.html";
				},1000);
			}
		},
		error: function() {
			$.MsgBox.Alert('提示', "服务器链接超时");
		}
	});
}