<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>Hello MUI</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<link rel="stylesheet" href="css/mui.min.css">
		<style>
			<style>html,
			body {
				background-color: #efeff4;
			}
			
			.mui-content>.mui-table-view:first-child {
				margin-top: 0px;
			}
			
			.title {
				margin: 20px 15px 10px;
				color: #6d6d72;
				font-size: 15px;
			}
			
			.oa-contact-cell.mui-table .mui-table-cell {
				padding: 11px 0;
				vertical-align: middle;
			}
			
			.oa-contact-cell {
				position: relative;
				margin: -11px 0;
			}
			
			.oa-contact-avatar {
				width: 75px;
			}
			
			.oa-contact-avatar img {
				border-radius: 50%;
			}
			
			.oa-contact-content {
				width: 100%;
			}
			
			.oa-contact-name {
				margin-right: 20px;
			}
			
			.oa-contact-name,
			oa-contact-position {
				float: left;
			}
		</style>
		</style>
	</head>

	<body>
		<div class="mui-content">
			<ul class="mui-table-view mui-table-view-striped mui-table-view-condensed" id="table">
			</ul>
		</div>
	</body>
	<script src="js/mui.min.js"></script>
	<script src="js/userinfos.js"></script>
	<script type="text/javascript" src="js/serverurl.js"></script>
	<script type="text/javascript" charset="utf-8">
		(function($, doc) {
			mui.plusReady(function() {
				if(CheckLogin()) {
					var B = window.plus.bridge;
					var Yuntx = {
						ReadOffLineMessage: function(uln, unickname, successCallback, errorCallback) {
							var success = typeof successCallback !== 'function' ? null : function(args) {
									successCallback(args);
								},
								fail = typeof errorCallback !== 'function' ? null : function(code) {
									errorCallback(code);
								},
								callbackID = B.callbackId(success, fail);
							return B.exec("Yuntx", "ReadOffLineMessage", [callbackID, uln, unickname]);
						},
						OpenChat: function(uln, tln, successCallback, errorCallback) {
							var success = typeof successCallback !== 'function' ? null : function(args) {
									successCallback(args);
								},
								fail = typeof errorCallback !== 'function' ? null : function(code) {
									errorCallback(code);
								},
								callbackID = B.callbackId(success, fail);
							return B.exec("Yuntx", "OpenChat", [callbackID, uln, tln]);
						}
					};
					window.plus.Yuntx = Yuntx;
					var list = JSON.parse(localStorage.getItem('taigangteacherMsgList') || '[]');
					LoadList(list);
					GetMessage();
				}

			});
		}(mui, document));

		function GetMessage() {
			plus.Yuntx.ReadOffLineMessage(users.ID, users.username,
				function(contact) { //成功
					SaveMsgList(contact);
				},
				function(status) { //失败
					plus.nativeUI.toast("获取消息失败!");
				}
			);
		}

		function OpenChat(id) {
			plus.Yuntx.OpenChat(users.ID, id,
				function(contact) { //成功
					plus.nativeUI.closeWaiting();
				},
				function(status) { //失败
					plus.nativeUI.closeWaiting();
				});
		}

		function SaveMsgList(msg) {
			var list = JSON.parse(localStorage.getItem('taigangteacherMsgList') || '[]');
			var obj = JSON.parse(msg);
			if(list != undefined && list != null && list.length > 0) {
				for(var i in obj) {
					var hasid = false;
					for(j in list) {
						if(list[j].Form == obj[i].Form) {
							list[j] = obj[i];
							hasid = true;
							break;
						} else {
							continue;
						}
					}
					if(!hasid) {
						list.push(obj[i]);
					}
				}
			} else {
				for(j in obj) {
					var hasidnew = false;
					for(k in list) {
						if(list[k].Form == obj[j].Form) {
							list[k] = obj[j];
							hasidnew = true;
							break;
						} else {
							continue;
						}
					}
					if(!hasidnew) {
						list.push(obj[j]);
					}
				}
			}
			localStorage.setItem('taigangteacherMsgList', JSON.stringify(list));
			LoadList(list);
		}

		function DeleteMsgList(id) {
			var list = JSON.parse(localStorage.getItem('taigangteacherMsgList') || '[]');
			if(list.length > 0) {
				for(var i in list) {
					if(list[i].Form == id) {
						list.splice(i, 1);
						break;
					} else {
						continue;
					}
				}
				LoadList(list);
			}
		}

		function longPress(id) {
			timeOutEvent = 0;
			var btnArray = [{
				title: "删除消息记录",
				style: "destructive"
			}];
			plus.nativeUI.actionSheet({
				cancel: "取消",
				buttons: btnArray
			}, function(e) {
				var index = e.index;
				if(index==1)
				{
					DeleteMsgList(id);
				}
			});
		}

		function LoadList(contact) {
			var table = document.getElementById("table");
			table.innerHTML = "";
			if(contact != null && contact.length > 0) {
				for(var i in contact) {
					var li = document.createElement("li");
					li.setAttribute("userid",""+contact[i].Form+"");
					li.addEventListener("touchstart", function(id) {
						timeOutEvent = setTimeout("longPress('"+this.getAttribute('userid')+"')", 500);
						e.preventDefault();
					});
					li.addEventListener("touchend", function(id) {
						clearTimeout(timeOutEvent);
						if(timeOutEvent != 0) {}
						return false;
					});
					li.innerHTML = '<li class="mui-table-view-cell" onclick="OpenChat(\'' + contact[i].Form + '\');" id="' + contact[i].Form + '">' +
						'<div class="mui-slider-cell">' +
						'<div class="oa-contact-cell mui-table">' +
						'<div class="oa-contact-avatar mui-table-cell">' +
						'<img src="images/geren.png" />' +
						'</div>' +
						'<div class="oa-contact-content mui-table-cell">' +
						'<div class="mui-clearfix">' +
						'<h4 class="oa-contact-name">' + (contact[i].NickName != undefined && contact[i].NickName != null&& contact[i].NickName !='' ? contact[i].NickName : contact[i].Form) + '</h4>' +
						'<span class="oa-contact-position mui-h6" style="float: right;">' + contact[i].MsgTime + '</span>' +
						'</div>' +
						'<p class="oa-contact-email mui-h6">' +
						'聊天记录' +
						'</p>' +
						'</div>' +
						'</div>' +
						'</div>' +
						'</li>';
					table.appendChild(li, table.firstChild);
				}

			}
		}
	</script>

</html>