<%@ page language="C#" autoeventwireup="true" inherits="Msg_MsgList, App_Web_oey1parl" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>无标题页</title>
    <script src="../Scripts/MyDate.js"></script>
    <script src="../Scripts/UserMsgClass.js"></script>
    <script type="text/javascript">
        //禁止返回键
        javascript:window.history.forward(1);
        //加载最近联系人
        function LoadLastMsgUserList()
        {
            window.parent.ShowOrHideToolBar(true);
            //var userlist = "";// manifest="MsgList.appcache" 

            if(localStorage.getItem("LastMsgUserList-"+window.parent._UserNo))
            {
                var userlist = localStorage.getItem("LastMsgUserList-"+window.parent._UserNo);

                var useritem = userlist.split('\n');
                
                for (var i=0;i<useritem.length;i++)
                {
                    try
                    {
                        var userno = useritem[i];
                        if(userno == "")
                        {
                            continue;
                        }
                        
                        //判断用户对象是否存在
                        if (document.getElementById("SisenMESSUserItem-" + userno))
                        {
                            continue;//
                        }

                        var tempuserinfo = userno + "\n";
                        var msgcounttemp = GetUserNoReadMsgCount(window.parent._UserNo,userno);
                        if (localStorage.getItem("UserInfo-" + userno)) 
                        {
                            tempuserinfo = localStorage.getItem("UserInfo-" + userno);
                        }
                        
                        var tempDdd = tempuserinfo.split('\n');
                        
                        var username = tempDdd[0];
                        var userdetail = tempDdd[1];
                        var usericonpath = "../UserIcon/SisenMESS-User-None-Icon.gif";
                        if(localStorage.getItem("userIcon-" + userno))
                        {
                            usericonpath = localStorage.getItem("userIcon-" + userno);
                        }
                        
                        var htmltxt = "";
                        
                        //得到最后通讯摘要
                        var msglastsun = GetUserLastMsgSummary(window.parent._UserNo,userno);

                        if(msglastsun=="")
                        {
                            msglastsun = userdetail;
                        }
                        
                        if(msgcounttemp>0)
                        {
                            htmltxt = GetUserMsgShowItem(userno, username, msglastsun, userdetail, msgcounttemp, "visible", usericonpath);
                        }
                        else
                        {
                            htmltxt = GetUserMsgShowItem(userno, username, msglastsun, userdetail, msgcounttemp, "hidden", usericonpath);
                        }
                        document.getElementById("MsgUserList").innerHTML = document.getElementById("MsgUserList").innerHTML + htmltxt;
                    }
                    catch (e)
                    {
                        
                    }
                }
            }
            
            //保存列表
            SaveLastMsgUserList();
        }
        //保存用户(最近联系人)
        function SaveLastMsgUserList()
        {
            var userlist = "";// manifest="MsgList.appcache" 

            for(var i=0 ;i<MsgUserList.children.length;i++)
            {
                try
                {
                    var userno = MsgUserList.children[i].id.replace(/SisenMESSUserItem-/g,"");
                    
                    userlist += userno + "\n";
                    
                    var username = document.getElementById("UserInfo-UserName-" + userno).innerText;
                    
                    var userdetail = document.getElementById("UserInfo-UserDetail-" + userno).innerText;
                                        
                    //localStorage.setItem("LastMsgUserList-" + userno,username + "\n" + userdetail);
                    //userno + "-LastMsgUserMsgCount-" + sender
                    localStorage.setItem(window.parent._UserNo+"-LastMsgUserMsgCount-" + userno,document.getElementById("msg_count_" + userno).innerText);
                    //document.getElementById("msg_count_" + sender).innerText
                    
                    //var usericonpath = document.getElementById("userIcon-" + userno).src;

                    //localStorage.setItem("userIcon-" + userno, usericonpath);
                }
                catch (ex)
                {
                }
            }
            
            localStorage.setItem("LastMsgUserList-"+window.parent._UserNo, userlist);
            
//            if(localStorage.getItem("MsgUserList"))
//            {
//                userlist = localStorage.MsgUserList;
//            }
//            userlist = "(" + userno + ")" + userlist.replace("(" + userno + ")", "");
//            
//            localStorage.setItem("MsgUserList",userlist);
            
            //alert(userlist);
        }
        
        
        //得到用户Item（visible、hidden）
        function GetUserMsgShowItem(userno,username,usershowsun,userdetail,msgCount,isVisible,usericonpath)
        {
            userdetail += "&nbsp;";
            var htmltxt = "<div id = \"SisenMESSUserItem-" + userno + "\" class ='MsgListDivPro' onclick=\"OpenUserMsg('" + userno + "','" + username + "');\" >";
            htmltxt += "<table width=\"100%\" height=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
            htmltxt += "<tr>";
            htmltxt += "<td style=\"width:48px; height:48px;\" align=\"center\"><img id = 'userIcon-" + userno + "' style='border:1px #e0e1e3 solid;background-color:#FFFFFF' src=\"" + usericonpath + "\" width=\"32\" height=\"32\"></td>";
            htmltxt += "<td style='color:#4f5152;'><span id ='UserInfo-UserName-" + userno + "'>" + username  + "</span><span class = 'UserDetail'>&nbsp;&nbsp;[</span><span class = 'UserDetailPro' id ='UserInfo-UserDetail-" + userno + "'>" + userdetail + "</span><span class = 'UserDetail'>]</span><br><span class = 'UserDetail'>"+usershowsun+"<span></td>";
            htmltxt += "<td align='right'>";
            htmltxt += " <table id ='MsgCount-" + userno + "' style='height:16px;visibility:" + isVisible + "' border='0' cellspacing='0' cellpadding='0'>";
            htmltxt += " <tr style='height:16px; '>";
            htmltxt += " <td style='width:5px; background-image:url(../Images/y1.gif);background-size:5px 16px;'></td>";
            htmltxt += " <td style='width:5px; height:16px; background-image:url(../Images/y2.gif);background-size:40px 16px; font-size:10px; color:#FFFFFF;' align='center'>";
            htmltxt += " <div style='margin-top:2px;width:100%;text-align:right;' id ='msg_count_" + userno + "'>" + msgCount + "</div>";
            htmltxt += " </td>";
            htmltxt += " <td style='width:6px; background-image:url(../Images/y3.gif);background-size:6px 16px'></td>";
            htmltxt += " </tr>";
            htmltxt += " </table>";
            htmltxt += "</td>";//<span id ='msg_count_" + tempmsg[0] + "'>1</span>
            htmltxt += "<td style='width:4px;'></td>";                    
            htmltxt += "</tr>";
            htmltxt += "</table>";
            htmltxt += "</div>";
            
            return htmltxt;
        }
        
        //接收到用户消息
        function RecvUserMsg(sender,msg,recvdate)
        {
            if (document.getElementById("SisenMESSUserItem-" + sender))
            {
                //说明这个用户存在
                var newitem = document.getElementById("SisenMESSUserItem-" + sender);
                if(MsgUserList.firstChild != newitem)
                {
                    newitem.remove();
                    MsgUserList.insertBefore(newitem,MsgUserList.firstChild);
                }
                
                document.getElementById("MsgCount-" + sender).style.visibility = 'visible';
                var count = parseInt(document.getElementById("msg_count_" + sender).innerText) + 1;
                document.getElementById("msg_count_" + sender).innerText = count;
            }
            else
            {
                var htmltxt = GetUserMsgShowItem(sender,sender,"","","1","visible","../UserIcon/SisenMESS-User-None-Icon.gif");
                    
                document.getElementById("MsgUserList").innerHTML = htmltxt + document.getElementById("MsgUserList").innerHTML;
                    
                    
                //XMLHttpGet(sender);
            }
            
            //保存列表
            SaveLastMsgUserList();

        }
        
        var xmlHttpRequest = null;
        //收到消息，发现没有用户信息，就到服务器端下载用户信息
        function XMLHttpGet(userno)
        { 
            xmlHttpRequest = new XMLHttpRequest();
            xmlHttpRequest.open("POST", "../SisenMESS-Mobile-WebService.asmx/GetUserInfo", true);
            // 当发生状态变化时就调用这个回调函数
            xmlHttpRequest.onreadystatechange = ajaxCallBack;
            // 使用post提交时必须加上下面这行代码
            xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            
            // 向服务器发出一个请求
            xmlHttpRequest.send("userNo=" + userno);
            
        }
        
        function ajaxCallBack()
        {
            if(xmlHttpRequest.readyState == 4)
            {
                if(xmlHttpRequest.status == 200)
                {
                    var content = xmlHttpRequest.responseText;
                    
                    var tempmsg = content.split('\n');
                    
                    document.getElementById("UserInfo-UserName-" + tempmsg[0]).innerText = tempmsg[1];
                    
                    document.getElementById("UserInfo-UserDetail-" + tempmsg[0]).innerText = tempmsg[2] + "&nbsp;";
                    
                    //var htmltxt = "<div id = \"SisenMESSUserItem-" + tempmsg[0] + "\" class ='MsgListDivPro' onclick=\"OpenUserMsg('" + tempmsg[0] + "');\" >";
                    //htmltxt += "<table width=\"100%\" height=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
                    //htmltxt += "<tr>";
                    //htmltxt += "<td style=\"width:40px; \" align=\"center\"><img style='border:1px #e0e1e3 solid;background-color:#FFFFFF' src=\"../UserIcon/SisenMESS-User-None-Icon.gif\" width=\"32\" height=\"32\"></td>";
                    //htmltxt += "<td style='color:#4f5152;'>" + tempmsg[1]  + "<br><span class = 'UserDetail'>" + tempmsg[2]  + "</span></td>";
                    //htmltxt += "<td align='right'>";
                    //htmltxt += " <table id ='MsgCount-" + tempmsg[0] + "' style='height:17px;visibility:visible' border='0' cellspacing='0' cellpadding='0'>";
                    //htmltxt += " <tr style='height:17px; '>";
                    //htmltxt += " <td style='width:5px; background-image:url(../Images/y1.gif)'></td>";
                    //htmltxt += " <td style='width:6px; height:17px; background-image:url(../Images/y2.gif); font-size:10px; color:#FFFFFF;' align='center'>";
                    //htmltxt += " <div style='margin-top:2px;width:100%;text-align:right;' id ='msg_count_" + tempmsg[0] + "'>1</div>";
                    //htmltxt += " </td>";
                    //htmltxt += " <td style='width:6px; background-image:url(../Images/y3.gif)'></td>";
                    //htmltxt += " </tr>";
                    //htmltxt += " </table>";
                    //htmltxt += "</td>";//<span id ='msg_count_" + tempmsg[0] + "'>1</span>
                    //htmltxt += "<td style='width:4px;'></td>";                    
                    //htmltxt += "</tr>";
                    //htmltxt += "</table>";
                    //htmltxt += "</div>";
                    
                    //var htmltxt = GetUserMsgShowItem(tempmsg[0],tempmsg[1],tempmsg[2],"1","visible");
                    
                    //document.getElementById("MsgUserList").innerHTML = htmltxt + document.getElementById("MsgUserList").innerHTML;
                    
                    //保存列表
                    SaveLastMsgUserList();
                }
            }
        }
        
        //打开用户聊天窗口
        function OpenUserMsg(userno,username)
        {
            if(!window.parent._IsLogin)
            {
                return;
            }
            //把用户头像的链接记录一下，在聊天窗口里直接使用。
            window.parent._TempMsgUserIconUrl = document.getElementById("userIcon-" + userno).src;

            document.getElementById("msg_count_" + userno).innerText = "0";
            document.getElementById("MsgCount-" + userno).style.visibility = 'hidden';
            
            SetUserNoReadMsgCount(window.parent._UserNo,userno,0);
            window.location.href = "MsgShow.aspx?UserNo=" + userno+"&UserTName="+username.replace(/&/g,"＆").replace(/=/g,"＝");
        }
        // 网页得到焦点

        widnow.focus = function(){
            window.parent.ShowOrHideToolBar(true);
        }
        function getURL(url) 
        { 
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", url, true); 
            xmlhttp.send(); 
            if(xmlhttp.readyState==4) 
            { 
                if(xmlhttp.Status != 200) alert("不存在"); 
                return xmlhttp.Status==200; 
            }
            return false; 
        } 
    </script>
    <link href="../Css/MsgList.css" rel="stylesheet" />
</head>
<body style="background-color:#FFFFFF;">
    <form id="form1" runat="server">
    <br />
    <br />
    <div id = "MsgUserList" runat="server">
       
    </div> 
    </form>
</body>
</html>
