var m_repeatPid = "";//选中查重好友时
var m_RepeatMinTick = 936262421150398406;
var m_RepeatMsgMonth = 0;
var m_ShowTimeRepeatFriendList = [];
var m_HeadImgKey = "";

//重置历史消息记录参数
function ResetRepeatHisMsgPara() {
	m_RepeatMinTick = 936262421150398406;
	//m_MsgPageIndex = 0;
	m_MsgPageSize = 20;
	m_RepeatMsgMonth = "";
	m_ShowRepeatTimeList = [];
	m_ShowTimeRepeatFriendList = [];
	$("#divRepeatFriendTalkContent").html("");
}

//点击查询重复好友
function SearchRepeatFriend() {
    $("#mytbodyRepeatFriend").html("");//清空表格内容
    $("#headtable").html("");//清空表头
    $("#txtrepeatnickname").val(m_SelFriendNickName);//昵称
    $("#txtrepeatRemarkname").val(m_SeiFriendRemarkName);//备注
    $("#txtrepeatSign").val(m_SelFriendSign);//个性签名
    $("#txtrepeatSex").val("");
    $("#txtrepeatPoint").val("");
    BindDataFul();
}
//查询按钮
function BindDataFul() {
    $("#mytbodyRepeatFriend").html("");//清空表格内容
    $(".infor-chongfu-m").css('display', 'none');
    $("#btnHideRepeatHisTalk").hide();
    $("#headtable").html("");//清空表头

    $("#talkRepeat").show();//模态层
    $("#talkRepeatzhezhao").show();//模态层
    var strParaJson = $.toJSON({
        nickname: $("#txtrepeatnickname").val(),
        remarkname: $("#txtrepeatRemarkname").val(),
        sign: $("#txtrepeatSign").val(),
        uin: m_NowSelectUin,
        pid: m_NowSelectPid,
        sex: $("#txtrepeatSex").val(),
        point: $("#txtrepeatPoint").val(),
    });
    talkObj.getrepeatfriends(strParaJson);
}
//查询重复好友回调
function AsyncGetRepeatFriendsResult(jsonResult) {
    $("#noneDataTipsTaskchong").hide();
    $("#talkRecord").attr("style", "height:450px;");
	//模态框数据绑定
    if (jsonResult.success) {
        if (jsonResult.result.list.length > 0) {
            var tableth = "";
            tableth += "<th style=\"width:58px\">序号</th>";
            tableth += "<th>备注</th>";
            tableth += "<th>昵称</th>";
            tableth += "<th>姓名</th>";
            tableth += "<th>联系方式</th>";
            tableth += "<th>性别</th>";
            tableth += "<th>位置</th>";
            tableth += "<th>个性签名</th>";
            tableth += "<th style=\"width:70px\">操作</th>";
            $("#headtable").html(tableth);
            var Content = "";
            var list = jsonResult.result.list;
            for (var i = 0; i < list.length; i++) {
            	Content += "<tr onclick = \"GetItemDoc(this,'" + list[i].pid + "','" + list[i].headimgkey + "')\" style=\"cursor:pointer\">";
                Content += "<td>" + (i + 1) + "</td>";
                Content += "<td>" + list[i].remarkname + "</td>";
                Content += "<td>" + list[i].nickname + "</td>";
                Content += "<td>" + list[i].name + "</td>";
                Content += "<td>" + list[i].mobile + "</td>";

                var strSexMark = "未知";
                if (list[i].sex == "1") {
                    strSexMark = "男";
                } else if (list[i].sex == "2") {
                    strSexMark = "女";
                }
                Content += "<td>" + strSexMark + "</td>";
                var location = "";
                if (list[i].province != "" && list[i].city != "") {
                    location = list[i].province + "-" + list[i].city;
                } else if (list[i].province == "" && list[i].city != "") {
                    location = list[i].city;
                } else if (list[i].province != "" && list[i].city == "") {
                    location = list[i].province;
                }
                Content += "<td>" + location + "</td>";
                Content += "<td><p style=\"width:240px;\">" + list[i].sign + "</p></td>";
                var CountName = list[i].remarkname == ""?list[i].nickname:list[i].nickname+"("+ list[i].remarkname +")";
                Content += "<td><button onclick=\"DeleteRepeatModal(event,'" + CountName + "','" + list[i].pid + "')\" class='btn btn-success  btn-sm' href='javascript:void(0)'>合并</button></td>";
                Content += "</tr>";
            }
            $("#mytbodyRepeatFriend").html(Content);
        } else {
            $("#noneDataTipsTaskchong").show();
            $("#talkRecord").attr("style","height:0px;");
        }
        $("#SerchRepeatFriendModal").modal("show");
    } else {
        ShowError("myPageAlert", "获取数据异常：" + jsonResult.error);
    }
    $("#talkRepeat").hide();
    $("#talkRepeatzhezhao").hide();
}

//合并重复好友
function DeleteRepeatModal(event,name,pid) {
    event.stopPropagation();//阻止事件冒泡
    m_repeatPid = pid;
    $("#spDeletePersonName").text(name);
    $("#DeletePersonModal").modal("show");
}
//取消按钮
function Cancel() {
    $("#DeletePersonModal").modal("hide");
}
//确认合并好友
function CommitPersonDelete() {
    $("#talkRepeat").show();
    $("#talkRepeatzhezhao").show();
    var json = $.toJSON({
        oldpid:m_repeatPid,//选择合并好友pid
        newpid: m_NowSelectPid,//当前talk页选中好友pid
        uin: m_NowSelectUin
    });
    talkObj.donerepeatfriend(json);
    Cancel();//关闭确认合并模态层
}
//合并好友回调
function AsyncDoneRepeatFriendResult(jsonResult) {
    if (jsonResult.success) {
        setTimeout(function () {
            $("#SerchRepeatFriendModal").modal("hide");
            $("#talkRepeat").hide();
            $("#talkRepeatzhezhao").hide();
            ShowMsg("myPageAlert", "操作成功!");
            $("#liFriendHead" + m_NowSelectPid).find("a:first").click();
        }, 4000);
    } else {
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
        $("#talkRepeat").hide();
        $("#talkRepeatzhezhao").hide();
    }
}
//是否已经显示过此时间
function IsRepeatShowTime(timemark) {
	for (var i = 0; i < m_ShowTimeList.length; i++) {
		if (m_ShowRepeatTimeList[i] == timemark)
			return true;
	}
	return false;
}
//选中好友时
function GetItemDoc(obj, pid, headimgkey) {
    if (pid.length == 0) {
        return;
    }
	$("#mytbodyRepeatFriend").find("tr").removeClass("active");
	$(obj).addClass("active");
	m_HeadImgKey = headimgkey;
	m_repeatPid = pid;
	ResetRepeatHisMsgPara();
    var strJson = $.toJSON({
        uin: m_NowSelectUin,
        pid: m_repeatPid
    });
    homeObj.getcachelist(strJson);
}
function AsyncGetCacheListResult(jsonResult) {
    if (jsonResult.success) {
        if (jsonResult.list.length > 0) {
            //缓存的数据
    		BindCacheMsgs(jsonResult.list);
        } else {
            //服务端请求数据
        	GetRepeatFriendHisMsg();
    	}
    	$("#btnHideRepeatHisTalk").css('display', 'inline-block');
    	$(".infor-chongfu-m").css('display', 'block');
    } else {
        ShowError("myPageAlert", "获取数据异常：" + jsonResult.error);
    }
}
//获取历史记录消息
function GetRepeatFriendHisMsg() {
	//防连击
	$("#btnLookRepeatFriendMoreMsg").html("正在获取,请稍候...<br />")
	$("#btnLookRepeatFriendMoreMsg").attr("onclick", "javascript:void(0);");


    var strParaJson = $.toJSON({
        pid: m_repeatPid,
        headimgkey: m_HeadImgKey,
        uin: m_NowSelectUin,
        month: m_RepeatMsgMonth,
        tick: m_RepeatMinTick,
        pagesize: 20
    });
    talkObj.getrepeatfriendhismessage(strParaJson);
}
//回执
function AsyncGetRepeatFriendHisMessageResult(jsonResult) {
	if (jsonResult.success) {
		if ($("#btnLookRepeatFriendMoreMsg").length > 0) {
			$("#btnLookRepeatFriendMoreMsg").remove();
		}

		//聊天记录
		var msgList = jsonResult.result.msgs;
		if (msgList.length > 0) {
			for (var i = 0; i < msgList.length; i++) {
				$("#divRepeatFriendTalkContent").prepend(MakeNewMsgHtml(msgList[i], '1'));
				if (msgList[i].msgtype == "11") {
					initbaidumap(msgList[i], "width:180px;height:200px;", "His");
				}
				//时间显示处理，已经显示过，或者本次读取最后一条
				if (!IsRepeatShowTime(msgList[i].sorttime) || (i + 1) == msgList.length) {
					m_ShowTimeRepeatFriendList.push(msgList[i].sorttime);
					//$("#divTalkContent").prepend("<div class=\"talktip\">" + msgList[i].sorttime + "</div>");
					//地图

				}
			}
			var strContent = "<a id=\"btnLookRepeatFriendMoreMsg\" href=\"javascript:void(0);\" class=\"moremessage-m\" onclick=\"GetRepeatFriendHisMsg();\">更多聊天记录</a><br />";
			$("#divRepeatFriendTalkContent").prepend(strContent);

		} else {
			$("#spNoMoreHis").remove();
			var strContent = "<span class=\"moremessage-m\" id=\"spNoMoreHis\">没有更多记录了...</span><br />";
			$("#divRepeatFriendTalkContent").prepend(strContent);
		}

		m_RepeatMsgMonth = jsonResult.result.month;
		m_RepeatMinTick = jsonResult.result.mintick;
		//m_MsgPageIndex = Number(jsonResult.result.pageindex);
	} else {
		ShowError("myPageAlert", "获取数据异常：" + jsonResult.error);
	}
}


function BindCacheMsgs(msgList) {
	//缓存记录自上往下加载
	if (msgList.length > 0) {
		for (var i = 0; i < msgList.length; i++) {
			$("#divRepeatFriendTalkContent").append(MakeNewMsgHtml(msgList[i], '1'));
			//地图
			if (msgList[i].msgtype == "11") {
				initbaidumap(msgList[i], "width:180px;height:200px;", "His");
			}
			//时间显示处理，已经显示过，或者本次读取最后一条
			if (!IsRepeatShowTime(msgList[i].sorttime) || (i + 1) == msgList.length) {
				m_ShowTimeRepeatFriendList.push(msgList[i].sorttime);
				//$("#divTalkContent").append("<div class=\"talktip\">" + msgList[i].sorttime + "</div>");
			}
		}

		var strContent = "<a id=\"btnLookRepeatFriendMoreMsg\" href=\"javascript:void(0);\" class=\"moremessage-m\" onclick=\"GetRepeatFriendHisMsg();\">更多聊天记录</a><br />";
		$("#divRepeatFriendTalkContent").prepend(strContent);

	} else {
		GetRepeatFriendHisMsg();//获取历史记录
	}
}

//显示隐藏历史聊天记录按钮单击事件
function HideRepeatHisTalk() {

	if ($(".infor-chongfu-m").css("display") == "none") {
		$(".infor-chongfu-m").css('display', 'block');
	} else {
		$(".infor-chongfu-m").css('display', 'none');
	}
	
}