<%@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"-->
<%
	response.buffer = false
	dim toid,uid,imtype,content,dopost,id_hash,sendtime,writetime
	uid = Session("Login_uid")
	dopost = Request("dopost")
	if dopost="send" then
		toid = Request.Form("toid")
		imtype = Request.Form("type")
		content = Request.Form("content")
		id_hash = idhsah(toid,uid)
		id_hash = md5(trim(id_hash))
		sendtime  = now()
		writetime = sendtime
		'Response.write "1"
		'Response.write(id_hash)
		set rs=server.createobject("adodb.recordset")
			pmsql = "INSERT INTO jinke_userchatlog(touid,uid,sendtime,writetime,hasview,content,id_hash,type)"
			pmsql=pmsql & " VALUES "
			pmsql=pmsql & "('"&toid&"',"
			pmsql=pmsql & "'" & uid&"',"
			pmsql=pmsql & "'" & sendtime&"',"
			pmsql=pmsql & "'" & writetime&"',"
			pmsql=pmsql & "'0',"
			pmsql=pmsql & "'" & content & "',"
			pmsql=pmsql & "'" & id_hash& "',"
			pmsql=pmsql & "'" & imtype&"')"
		'Response.Write pmsql
			on error resume next
		conn.Execute pmsql,recaffected	
		if err<>0 then
		  Response.Write("No update permissions!")
		else 
		  Response.Write("<h3>" & recaffected & " record added</h3>")
		end if
	else
			'建立对象
			Set getpmrs = Server.CreateObject("ADODB.RecordSet")
			getpmsql = "SELECT p.id,p.touid,p.uid,p.sendtime,p.hasview,p.content,p.type,u.uname,u.sign,u.avatar FROM jinke_userchatlog p INNER JOIN jinke_user u ON u.uid = p.uid where p.touid="& uid & " and p.hasview=0"
			'打开SQL
			getpmrs.Open getpmsql,conn
			
			'判断数据库是否有筛选的数据
			If getpmrs.Eof And getpmrs.Bof Then
			
			'数据库没有数据打印提示信息
			'Response.Write("数据库中没有数据或者数据连接错误")
			
			'数据库有数据打印提示信息
			Else
			
			'循环显示数据
			Do While Not getpmrs.Eof
			
			'换行循环输出数据
			'Response.Write getpmrs("id")&"<br/>"
			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)*1000	
					avatar=getpmrs("avatar")
				if isnull(avatar) then
				avatar = "/theme/default/images/avatar.jpg"
				end if			
					username=getpmrs("uname")
				set pms = MoLibJson.new()
					pms.datatype="array"
					set pm = pms.put()
					'avatar="/theme/default/images/avatar.jpg"
					pm.put "avatar",avatar
					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
				'打印聊天记录
				response.Write pms.toString()
								
				set pmuprs = server.createobject("adodb.recordset")
					pmupsql = "UPDATE jinke_userchatlog SET "
					pmupsql = pmupsql & "hasview=1,"
					pmupsql = pmupsql & "writetime='"&now()&"'"
					pmupsql = pmupsql & " where id="&pid
					'response.Write pmupsql
					on error resume next
  					conn.Execute pmupsql
			'下一条数据
			getpmrs.MoveNext
			
			'循环
			Loop
			
			'结束判断
			End If
			
			'关闭记录集
			getpmrs.Close			
			'释放数据库连接
			Set getpmrs = Nothing
			
	end if
	conn.close
	
	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		
%>