<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="conn.asp"-->
<!--#include file="../islogin.asp" -->
<!--#include file="inc/Mo.Lib.Json.asp"-->
<!--#include file="inc/md5.asp"-->
<!--#include file="inc/idhash.asp"-->
<%
'日期型转时间戳
Function ToUnixTime(strTime, intTimeZone)
	If IsEmpty(strTime) or Not IsDate(strTime) Then strTime = Now
	If IsEmpty(intTimeZone) or Not isNumeric(intTimeZone) Then intTimeZone = 0
	ToUnixTime = DateAdd("h",-intTimeZone,strTime)
	ToUnixTime = DateDiff("s","1970-01-01 00:00:00", ToUnixTime)
End Function		
dim ptype,touid,page,pagesize,chatlog,id_hash,uid,id_hash2
	ptype = Request("type")
	touid = Request("id")
page=Request("page")
uid = Session("Login_uid")
id_hash = idhsah(touid,uid)
id_hash = md5(trim(id_hash))
if page="" then
	page=1
else
	if clng(page)<1 then
		page=1
	end if
end if
pagesize = 10
'得到记录总数
sql="SELECT count(*) as datacount FROM jinke_userchatlog where id_hash='"& id_hash&"'"
Set rs_datacount=server.CreateObject("adodb.recordset")
rs_datacount.open sql,conn,1,1
'得到整形总数
datacount=clng(rs_datacount("datacount"))
rs_datacount.close
'判断记录总数是否大于分页数
if datacount > pagesize then
	if datacount mod pagesize = 0 then
		pagecount=formatnumber(datacount / pagesize,0)
		pagecount=datacount / pagesize
	else
		pagecount=formatnumber(datacount / pagesize,0) + 1
		pagecount=datacount / pagesize + 1
	end if
else
	pagecount=1	
end if
	pagecount=Fix(pagecount)	
	if clng(page)>pagecount then page=pagecount
	
	if page=1 then
		sql = "SELECT TOP "& pagesize & " p.id,p.touid,p.uid,p.sendtime,p.hasview,p.content ,p.type,u.uname,u.avatar FROM jinke_userchatlog p INNER JOIN jinke_user u ON u.uid = p.uid where p.id_hash='"& id_hash & "' order by p.id desc"
	else
		sql ="SELECT TOP " & pagesize & " p.id,p.touid,p.uid,p.sendtime,p.hasview,p.content ,p.type,u.uname,u.avatar FROM jinke_userchatlog p INNER JOIN jinke_user u ON u.uid = p.uid WHERE (((p.ID) Not In (SELECT TOP " & ((page - 1) * pagesize)& " ID FROM jinke_userchatlog where id_hash='"&id_hash&"' order by ID desc)) AND id_hash='"&id_hash&"') ORDER BY p.ID DESC"
end if
  	set getpmrs=server.CreateObject("adodb.recordset")
    getpmrs.open sql,conn,1,1
 	dim pms
	set pms = MoLibJson.new()
		pms.datatype="array"
	'循环显示数据
	Do While Not getpmrs.Eof
		dim username,avatar,pid,guid,pm,gcontent,gsendtime
			guid = getpmrs("uid")
			pid = getpmrs("id")
			imtype = getpmrs("type")
			pid = getpmrs("id")
			gcontent = getpmrs("content")
			gsendtime =ToUnixTime(getpmrs("sendtime"),+8)					
			username=getpmrs("uname")
			avatar=getpmrs("avatar")
			if isnull(avatar) then
			avatar = "/theme/default/images/avatar.jpg"
			end if
			set pm = pms.put()
			pm.put "avatar",avatar
			'avatar="/theme/default/images/avatar.jpg"
			pm.put "username",username
			pm.put "avatar",avatar
			pm.put "id",guid
			pm.put "type",imtype
			pm.put "content",gcontent
			pm.put "mine",false 
			pm.put "timestamp",gsendtime
		'打印聊天记录
	'下一条数据
  	getpmrs.movenext
  loop
  getpmrs.close
  %>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>聊天记录</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="renderer" content="webkit">
<link rel="stylesheet" type="text/css" href="/theme/chat/css/layui.css"/>
<style>
.layim-chat-friend{ padding-bottom:30px;}
.layim-chat{ display:block !important;width: 100% !important;}
.layim-chat-main{width:90%; height:100% !important; overflow-x:; }
.layim-chat-main ul{ max-height:100%; width:100%}
#chatpages{ position:fixed; left:0px; right:0px; bottom:0px; background-color:#fff; text-align:center; padding-top:5px;}
</style>
<script type="text/javascript" src="/theme/chat/layui.js"></script>

</head>

<body>
<script>
layui.use(['layim','laypage','laydate'], function(){
	//var layim=layui.layim,
	var $ = layui.jquery,
	layer = layui.layer,
	laytpl = layui.laytpl,
	laypage = layui.laypage,
	laydate = layui.laydate
	mine = parent.layui.layim.cache().mine;
	//记录json
	var data = <%=pms.toString()%>;
	//遍历聊天记录
	
	layui.each(data,function(index,item){
		var sendtime = laydate.now(item.timestamp * 1000, "YYYY-MM-DD hh:mm:ss"),
		classmine=(item.id==mine.id)?'class="layim-chat-mine"':''//判断是否自己发送	
		,namehtml=(item==mine.id)?'<cite><i>'+sendtime+'</i>'+item.username+'</cite>':'<cite>'+item.username+'<i>'+sendtime+'</i></cite>'
		 //对话列表渲染
		 ,html ='<li '+classmine+'><div class="layim-chat-user"><img src="'+item.avatar+'">'+namehtml+'</div><div class="layim-chat-text">'+parent.layui.layim.content(item.content)+'</div></li>';
		 //列表追加到面板	
		 $(".layim-chat-main ul").append(html);
	});  

  	//处理分页
	laypage({
		cont: 'chatpages',
		pages: <%=pagecount%>, //可以叫服务端把总页数放在某一个隐藏域,再获取。假设我们获取到的是18
		first: '首页', //将首页显示为数字1,。若不显示,设置false即可
		last: '尾页', //将尾页显示为总页数。若不显示,设置false即可
		prev: '上一页', //若不显示,设置false即可
    	next: '下一页', //若不显示,设置false即可
		groups:1,
		skip: false,
		curr: function(){ //通过url获取当前页,也可以同上(pages)方式获取
			var page = location.search.match(/page=(\d+)/);
			return page ? page[1] : 1;
		}(), 
		jump: function(e, first){ //触发分页后的回调
			if(!first){ //一定要加此判断,否则初始时会无限刷新
				var id = location.search.match(/id=(\d+)/),type=location.search.match(/type=([a-z]+)/);
				location.href = '?id='+id[1]+'&type='+type[1]+'&page='+e.curr;
			}
		}
	});
});
</script>
<div class="layim-chat layim-chat-friend">
  <div class="layim-chat-main">
    <ul>
    </ul>
  </div>
  <div id="chatpages"> </div>
</div>
</body>
</html>