var m_selectTransferCusUid = "";//当前选中微信号转移客服uid
var m_selectTransferWechatUin = "";//当前选择转移微信号uin
var m_selectTransferWechatCusid = "";//当前选择转移微信号所属人员ID
var iOnline = 0;//在线
var iDeopped = 0;//掉线
var BisFistLoad = true;
var m_TranFerType = "";//转移微信号操作标识（1：首页转移微信号  ，2：切换客服中转移微信号）
$(function () {
});
//切换人员
function chanagePersonShow() {
    //绑定部门
    homeObj.getdepttreeandwechatcount();
}

//部门信息回调
function AsyncGetDeptTreeAndWechatCountResult(jsonresult) {
	if (jsonresult.success) {
		var depthtml = ShowDeptAndPerson(jsonresult).dept;
		var personhtml =ShowDeptAndPerson(jsonresult).person;
		$("#ulChangeDept").html(depthtml);
		$("#ulChangePerson").html(personhtml);
    	//$("#PersionAllShow .kefu-jinru[data-rel=popover]").popover();
    	//$("#PersionAllShow .kefu-chakan[data-rel=popover]").popover();
    	$("#spCompanyWechatOnline").html(iOnline);
    	$("#spCompanyWechatDeopped").html(iDeopped);
    	
		$("#chanagePersonMessage").modal("show");
    	
    } else {
        ShowError("myPageAlert", "部门信息获取异常！：" + jsonResult.error);
    }
}


//查看其它客服html
function ShowDeptAndPerson(jsonResult) {
	var nowCusUserId = homeObj.getcususerid();
	var nowLoginCusUserId = homeObj.getlogincususerid();
	var jsonTree = jsonResult.list;
	var strdeptcontent = "";//部门html
	var strpersoncontent = "";//人员html

	iOnline = 0;//在线
	iDeopped = 0;//掉线
	var iuserc = 0;
	var wechat = 0;

	for (var i = 0; i < jsonTree.length; i++) {
		var jsonTreeUser = jsonTree[i].user;
		if (jsonTreeUser.length > 0) {
			//左侧部门
			strdeptcontent += "<li  id=\"dept" + i + "\" onclick=\"selectdept("+i+")\">";
			strdeptcontent += "<span>" + jsonTree[i].dgname + "</span>";
			strdeptcontent += "</li>";
			//对应右侧人员
			strpersoncontent += "<div class=\"shortcut-element-list\">";
			strpersoncontent += "<h5><span>" + jsonTree[i].dgname + "</span></h5>";

			for (var j = 0; j < jsonTreeUser.length; j++) {
				strpersoncontent += "<div class=\"row\">";
				//人员
				iOnline = iOnline + Number(jsonTreeUser[j].onlinecount);
				iDeopped = iDeopped + Number(jsonTreeUser[j].droppedcount);
				var strSeePerson = "";
				if (nowCusUserId == jsonTreeUser[j].userid) {
					//不可切换当前客服
					strSeePerson = "";
				} else if (nowCusUserId != jsonTreeUser[j].userid && jsonTreeUser[j].wechats.length < 1) {
					if (nowLoginCusUserId == jsonTreeUser[j].userid) {
						//可切换当前登陆人
						strSeePerson = "<a href=\"#\" class=\"color-green pull-right\"  onclick=\"ChangeCusUser('" + jsonTreeUser[j].userid + "','" + jsonTreeUser[j].name + "')\" >查看客服</a><span class=\"pull-right color-green marg-rig-10\">|</span>";
					} else {
						//非当前登陆人没有没有微信号不可切换
						strSeePerson = "";
					}
				} else {
					strSeePerson = "<a href=\"#\" class=\"color-green pull-right\"  onclick=\"ChangeCusUser('" + jsonTreeUser[j].userid + "','" + jsonTreeUser[j].name + "')\" >查看客服</a><span class=\"pull-right color-green marg-rig-10\">|</span>";
				}
				strpersoncontent += "<div class=\"col-xs-12 pad-10 dept-box\">";

				strpersoncontent += "<div class=\"col-xs-12 form-inline dept-num-header\">";
				strpersoncontent += "<img src=\"../img/online-user.png\" />" + jsonTreeUser[j].name+ "<i class=\"icon iconfont icon-round color-green\"></i> <span>" + jsonTreeUser[j].onlinecount + "</span><i class=\"icon iconfont icon-round color-orange\"></i> <span>" + jsonTreeUser[j].droppedcount + "</span>";
				strpersoncontent += strSeePerson + "<a href=\"#\" class=\"color-green pull-right\" onclick=\"Misstalk('" + jsonTreeUser[j].userid + "')\"\">今日对话</a>";
				strpersoncontent += "</div>";
				strpersoncontent += "<ul class=\"list-inline list-unstyled number-list\">";
				var jsonTreeUserWechat = jsonTreeUser[j].wechats;
				for (var k = 0; k < jsonTreeUserWechat.length; k++) {
					//微信号
					var itemWechatState = jsonTreeUserWechat[k].loginstate;
					var WechatState = "";
					if (itemWechatState == "6") {
						WechatState = "登陆中";
					} else if (itemWechatState == "7") {
						WechatState = "登陆中";
					} else if (itemWechatState == "8") {
						WechatState = "在线";
					} else if (itemWechatState == "9") {
						WechatState = "网络异常";
					} else if (itemWechatState == "10") {
						WechatState = "登出";
					} else if (itemWechatState == "11") {
						WechatState = "掉线";
					} else if (itemWechatState == "12") {
						WechatState = "手机网络异常";
					}
					var strWeChatOnLineState = ""
					if (itemWechatState == "8") {
						strWeChatOnLineState = "：" + jsonTreeUserWechat[k].createtime;
					}
					//转移微信号
					var strWechatTransfer = itemWechatState == "8" ? "<button class=\"btn btn-success btn-small\" onclick=\"WechatTransferCusShow('" + jsonTreeUserWechat[k].uin + "','" + jsonTreeUserWechat[k].cususerid + "','2')\">转移</button>" : "";
					strpersoncontent += "<li class=\"col-xs-4\">";
					strpersoncontent += "<div class=\"number-box\">";
					strpersoncontent += "<img src=\"../img/kefutouxiang.png\" />";
					strpersoncontent += "<div class=\"number-mess\">";
					var remark = jsonTreeUserWechat[k].remark == "" ? "" : "(" + jsonTreeUserWechat[k].remark + ")";
					strpersoncontent += "<p class=\"p5\">" + jsonTreeUserWechat[k].nickname + remark + "</p>";
					strpersoncontent += "<p class=\"p2\">在线时长：<span>" + WechatState + strWeChatOnLineState + "</span></p>";
					strpersoncontent += "</div>";
					strpersoncontent += strWechatTransfer;
					strpersoncontent += "</div>";
					strpersoncontent += "</li>";
				}
				strpersoncontent += "</ul>";
				strpersoncontent += "</div>";
				strpersoncontent += "</div>";
			}
			
		}
		strpersoncontent += "</div>";
		
	}
	


	strdeptcontent += "<div class=\"lanPos\"></div>";
	return { dept: strdeptcontent, person: strpersoncontent };
}
function selectdept(num) {
	$("#ulChangeDept .active").removeClass("active");
	$("#dept" + num).addClass("active");
	lanPosScroll();
}
//切换客服
function ChangeCusUser(uid,cususename) {
    $("#chanagePersonMessage").modal("hide");
    m_ResetLoadingDivFlag = 0;
    $("#ChangeUserLoad").show();
    homeObj.changecususerid(uid, cususename);
}
//切换客服折叠css
function ChangeFoldCss(uid) {
	if ($("#isfold" + uid).hasClass("fa-angle-down")) {
		$("#isfold" + uid).removeClass("fa-angle-down").addClass("fa-angle-up");
		return;
	}
	if ($("#isfold" + uid).hasClass("fa-angle-up")) {
		$("#isfold" + uid).removeClass("fa-angle-up").addClass("fa-angle-down");
	}
}
//部门折叠
function ChangeDeptCss(num) {
	if ($("#dept" + num + " i").hasClass("fa fa-minus-square-o")) {
		$("#dept" + num + " i").removeClass("fa fa-minus-square-o").addClass("fa fa-plus-square-o");
		return;
	}
	if ($("#dept" + num + " i").hasClass("fa fa-plus-square-o")) {
		$("#dept" + num + " i").removeClass("fa fa-plus-square-o").addClass("fa fa-minus-square-o");
	}
}
//切换客服刷新按钮
function RefreshChangeCus() {
	chanagePersonShow();
	$("#aRefreshChangeCus").css("color", "#a1a1a1");
	$("#aRefreshChangeCus").removeAttr("onclick");
	$("#aRefreshChangeCus").css("cursor", "");
	//防连点
	setTimeout(function () {
		$("#aRefreshChangeCus").css("color", "");
		$("#aRefreshChangeCus").css("cursor", "pointer");
		$("#aRefreshChangeCus").attr("onclick", "RefreshChangeCus();");
	}, 10000);

}
//--------------------------------------------------切换客服End--------------------------------------------------------------


//--------------------------------------------------微信号转移Start----------------------------------------------------------
//微信号转移给权限范围内客服（不包含当前登陆人）
function WechatTransferCusShow(wechatuin, cusuid, type) {
	Antipenetration(event);
	m_TranFerType = type;
	if (wechatuin.length < 1) {
		return;
	}
	m_selectTransferWechatUin = wechatuin;
	m_selectTransferWechatCusid = cusuid;//转移微信号所属人（切换客服转移微信号）
	//获取所有客服
    homeObj.getdepttreeandwechatcountbytransfer();
}
//获取所有客服回调
function AsyncGetDeptTreeAndWechatCountByTransferResult(jsonResult) {
    if (jsonResult.success) {
         $("#TransferPersonShow").html(ShowDeptAndPersonByTransfer(jsonResult));
         $("#divWechatTransferCus").modal("show");
    } else {
        ShowError("myPageAlert", "部门信息获取异常！：" + jsonResult.error);
    }
}

//可转移客服html
function ShowDeptAndPersonByTransfer(jsResult) {
	//当前登录人员id
	var nowNowCusUserId = homeObj.getcususerid();
	var strcontent = "";
	var jsonTree = jsResult;

	for (var i = 0; i < jsonTree.list.length; i++) {
		strcontent += " <div class=\"row check-other-customer\">";
		if (jsonTree.list[i].user.length > 0) {
			//部门
			strcontent += "<div class=\"col-xs-12 kefu-head\">";
			strcontent += "<p class=\"p5\">" + jsonTree.list[i].dgname + "</p>";
			strcontent += "</div>";
			for (var j = 0; j < jsonTree.list[i].user.length; j++) {
				var item = jsonTree.list[i];
				if (m_selectTransferWechatCusid.length > 0) {
					strcontent += "<div class=\"col-xs-3\" id=\"Transfer" + item.user[j].userid + "\" onclick=\"WechatTransferCus('" + item.user[j].userid + "')\" >";
					strcontent += "<div class=\"col-xs-12 form-inline kefu-card\">";
					strcontent += "<div class=\"left-kfimg\">";
					strcontent += "<img src=\"../img/kefutouxiang.png\">";
					strcontent += "</div>";
					strcontent += "<div class=\"right-kfmess\">";
					strcontent += "<span class=\"kefu-name\">" + item.user[j].name + "</span><span>|</span><span class=\"color-gry\">" + item.user[j].wechatcount + "</span>";
					strcontent += "</div>";
					strcontent += "</div>";
					strcontent += "</div>";
				} else {
					//不包含当前客服
					if (nowNowCusUserId != item.user[j].userid) {
						strcontent += "<div class=\"col-xs-3\" id=\"Transfer" + item.user[j].userid + "\" onclick=\"WechatTransferCus('" + item.user[j].userid + "')\" >";
						strcontent += "<div class=\"col-xs-12 form-inline kefu-card\">";
						strcontent += "<div class=\"left-kfimg\">";
						strcontent += "<img src=\"../img/kefutouxiang.png\">";
						strcontent += "</div>";
						strcontent += "<div class=\"right-kfmess\">";
						strcontent += "<span class=\"kefu-name\">" + item.user[j].name + "</span><span>|</span><span class=\"color-gry\">" + item.user[j].wechatcount + "</span>";
						strcontent += "</div>";
						strcontent += "</div>";
						strcontent += "</div>";
					}
				}
			}
			strcontent += "</div>";
		}

	}
	return strcontent;
}
//更新选择转移微信号的客服id
function WechatTransferCus(uid) {
	$(".kefu-check").removeClass("kefu-check");
	$("#Transfer" + uid).children(":first").addClass(" kefu-check");
	m_selectTransferCusUid = uid;
}
//微信号转移给客服
function BtnWechatTransferCusRun() {
	if (m_selectTransferCusUid.length < 1) {
		ShowMsg("myPageAlert", "请选择客服后再进行转移！");
		return;
	}
	var strLoginUid = homeObj.getlogincususerid();//当前登录客服ID
	if (m_selectTransferWechatCusid.length > 0) {
		if (strLoginUid == m_selectTransferWechatCusid && m_selectTransferCusUid == m_selectTransferWechatCusid) {
			ShowMsg("myPageAlert", "不可以把自己的微信号转移给自己");
			return;
		}
		if (m_selectTransferCusUid == m_selectTransferWechatCusid) {
			ShowMsg("myPageAlert", "不可以把所选微信号转移给当前微信号所属人");
			return;
		}
	}
	
	var strJson = $.toJSON({
		uin: m_selectTransferWechatUin,
		cusuerid: m_selectTransferCusUid,
		oldcusuerid: m_selectTransferWechatCusid
	});
	homeObj.wechattransfercus(strJson);
}
//取消微信号转移给客服
function BtnWechatTransferCusCanale() {
	m_selectTransferCusUid = "";
	m_selectTransferWechatUin = "";
}

//微信号转移给客服回调
function AsyncWechatTransferCusResult(jsonResult) {
	if (jsonResult.success) {
		alwaysShowMsg("myPageAlert", "微信号转移成功！");
	} else {
		ShowError("myPageAlert", "微信号转移异常！：" + jsonResult.error);
	}
	if (m_TranFerType == "2") {
		chanagePersonShow();
	}
	$("#divWechatTransferCus").modal("hide");
	m_selectTransferCusUid = "";
	m_selectTransferWechatUin = "";
}


