<!--#include file="../../../inc/cms_admin.asp" -->
<%
dim UID,U_GID,U_LID,U_OnOff,U_order,sex,userName,Password,rePassword,newPassword,Question,Answer,RegTime,TrueName,face,Province,City,District,Address,Post,tel,Mobile,Email,QQ,U_Desc,Adminrand,LastLoginTime,LastLoginIP,LoginCount
dim GID,IsAdmin,G_Name,G_Desc,G_OnOff,G_Mark,G_Menu,G_Sort,G_Order
dim sql,U_EnName,usertype,table,col,colval,idval,id,sortid
dim keyword, page, psize, orderby, ordsc
dim act : act=getForm("act","get")
dim stype : stype=getForm("stype","get")
dim postion : postion=getForm("postion","get")
table=getForm("table","both")
col=getForm("col","both")
colval=getForm("colval","both")
idval=getForm("idval","both")
id=isnum(getForm("id","both"))
uid=isnum(getForm("uid","both"))
gid=getForm("gid","both")
if isnul(gid) then  gid=getcookie("gid") else setcookie "gid",gid
keyword=getForm("keyword","both")
page=getForm("page","get")
psize=getForm("psize","get")
orderby=getForm("orderby","get")
ordsc=getForm("ordsc","get")
Select case act	
	case "setuser"	:	dbconn("update [dbpre]user set U_OnOff="&colval&"  where  UID="&uid&"")	
	case "setgroup"	:	dbconn("update [dbpre]user_group set G_OnOff="&colval&"  where  GID="&id&"")	
	case "getcity"	:	getcity
	case "edit" : edituser
	case "editadmin"	:	editadmin
	case "add" : adduser
	case "del" : dbconn("delete from [dbpre]User where UID="&id)
	case "delgroup" : dbconn("delete from [dbpre]User_group where GID="&id)	
	case "addg" : addAdminGroup
	case "editg" : editAdminGroup
	case "addusergroup" : addusergroup
	case "editusergroup" : editusergroup
	case "smssend" : 	echo sendsms(colval,idval)	
	case "saveid"	:	saveid
	case "savegid"	:	savegid
End Select

Sub saveid
	dim colid		:	colid=getForm("colid","both")
	dim colname		:	colname=getForm("colname","both")
	dim colval		:	colval=getForm("colval","both")
	dim sql1 		:	sql1="update [dbpre]user Set "&colname&"='"&colval&"' Where UID="&colid
	dim sql2 		:	sql2="update [dbpre]user Set "&colname&"="&colval&" Where UID="&colid	
	dbconn(sql1)	:	if err then dbconn(sql2)
End Sub

Sub savegid
	dim colid		:	colid=getForm("colid","both")
	dim colname		:	colname=getForm("colname","both")
	dim colval		:	colval=getForm("colval","both")
	dim sql1 		:	sql1="update [dbpre]User_Group Set "&colname&"='"&colval&"' Where GID="&colid
	dim sql2 		:	sql2="update [dbpre]User_Group Set "&colname&"="&colval&" Where GID="&colid	
	dbconn(sql1)	:	if err then dbconn(sql2)
End Sub

Sub getcity
dim rs
	Province=getForm("Province","both")	
	city=getForm("city","both")	
	if not isnul(Province) then
		set rs=dbsql("select title,entitle from [dbpre]region where OnOff=1 and depth=2 and pid=(select top 1 id  from [dbpre]region where title='"&Province&"') order by R_order asc")
		do while not rs.eof					  				
		 echo"<option value="&rs("title")&">"&rs("entitle")&rs("title")&"</option>"
		rs.movenext
		loop
		closers rs
	elseif  not isnul(city) then
		set rs=dbsql("select title,entitle from [dbpre]region where OnOff=1 and depth=3 and pid=(select top 1 id  from [dbpre]region where title='"&city&"') order by R_order asc")
		do while not rs.eof					  				
		 echo"<option value="&rs("title")&">"&rs("entitle")&rs("title")&"</option>"
		rs.movenext
		loop
		closers rs
	end if	
End Sub	


Sub getuserGroup
	if not isnul(Gid) then
		dim rs : set rs = dbsql("select * from [dbpre]User_Group where IsAdmin=0 and GID="&Gid)
		if rs.eof then 
			'back "没有这条记录"
		else
			ID=rs("GID")
			G_Name=rs("G_Name")
			G_Desc=rs("G_Desc")
			G_OnOff=rs("G_OnOff")
			G_Mark=rs("G_Mark")
			G_Order=rs("G_Order")
		end if
		rs.close : set rs=nothing
	else		
		'back "没有这条记录"
	end if
End Sub


Sub adduserGroup	
	dim G_Name
	G_Name=getForm("G_Name","post")
	G_Desc=getForm("G_Desc","post")
	G_OnOff=getCheck(getForm("G_OnOff","post"))
	G_Mark=getForm("G_Mark","post")
	G_Order=getForm("G_Order","post")
	
	if isNul(G_Name) then back"请填写组名称"
	if not ifNum(G_Mark) then back"请正确填写折扣值"
	if not ifNum(G_Order) then G_Order=9		
	sql="insert into [dbpre]User_Group( IsAdmin, G_Name, G_Desc, G_OnOff, G_Mark, G_Order) values(0,'"&G_Name&"', '"&G_Desc&"', 1, "&G_Mark&",  "&G_Order&")"
	dbconn sql
	layertrue "添加成功"
End Sub

Sub edituserGroup
	G_Name=getForm("G_Name","post")
	G_Desc=getForm("G_Desc","post")
	G_Mark=getForm("G_Mark","post")
	if not ifNum(GID) then back "组ID不正确！"	
	if isNul(G_Name) then back"请填写组名称"
	if not ifNum(G_Mark) then back"请正确填写折扣值"	
	sql="update [dbpre]User_Group set G_Name='"&G_Name&"', G_Desc='"&G_Desc&"',  G_Mark="&G_Mark&"  where GID="&GID
	dbconn sql
	layertrue "修改成功"
End Sub

Sub getAdminGroup
	if not isnul(Gid) then
		dim rs : set rs = dbsql("select * from [dbpre]User_Group where IsAdmin=1 and GID="&Gid)
		if rs.eof then 
			'back "没有这条记录"
		else
			ID=rs("GID")
			IsAdmin=rs("IsAdmin")
			G_Name=rs("G_Name")
			G_Desc=rs("G_Desc")
			G_OnOff=rs("G_OnOff")
			G_Mark=rs("G_Mark")
			G_Menu=rs("G_Menu")
			G_Sort=rs("G_Sort")
			G_Order=rs("G_Order")
		end if
		rs.close : set rs=nothing
	else		
		'back "没有这条记录"
	end if
End Sub


Sub addAdminGroup	
	dim G_Name
	IsAdmin=1
	G_Name=getForm("G_Name","post")
	G_Desc=getForm("G_Desc","post")
	G_OnOff=getCheck(getForm("G_OnOff","post"))
	G_Mark=getForm("G_Mark","post")
	if G_Mark>getsession("adminmark") then G_Mark=getsession("adminmark")-1
	G_Menu=getForm("G_Menu","post")
	G_Sort=""
	G_Order=getForm("G_Order","post")
	
	if isNul(G_Name) then back"请填写组名称"
	if not ifNum(G_Mark) then back"请正确填写权限值"
	if not ifNum(G_Order) then G_Order=9
		
	sql="insert into [dbpre]User_Group( IsAdmin, G_Name, G_Desc, G_OnOff, G_Mark, G_Menu, G_Sort, G_Order) values("&IsAdmin&", '"&G_Name&"', '"&G_Desc&"', "&G_OnOff&", "&G_Mark&", '"&G_Menu&"', '"&G_Sort&"', "&G_Order&")"
	dbconn sql
	layertrue "添加成功"
End Sub

Sub editAdminGroup
	IsAdmin=1
	G_Name=getForm("G_Name","post")
	G_Desc=getForm("G_Desc","post")
	G_OnOff=1
	G_Mark=getForm("G_Mark","post")
	if G_Mark>=getsession("adminmark") then G_Mark=getsession("adminmark")-1
	G_Menu=getForm("G_Menu","post")
	G_Sort=""	
	if not ifNum(GID) then back "组ID不正确！"	
	if isNul(G_Name) then back"请填写组名称"
	if not ifNum(G_Mark) then back"请正确填写权限值"	
	sql="update [dbpre]User_Group set G_Name='"&G_Name&"', G_Desc='"&G_Desc&"', G_Menu='"&G_Menu&"',G_Sort='"&G_Sort&"', G_Mark="&G_Mark&", G_OnOff="&G_OnOff&" where GID="&GID
	dbconn sql
	layertrue "修改成功"
End Sub

Sub getuser
dim rs,id
	 UID=getForm("UID","get")
	if not isnul(UID) then	
		sql ="select * from [dbpre]user where UID="&UID
		set rs = conn.exec(sql,"r1")		
	else
		back "没有这条记录"
	end if 
	
	if rs.eof then 
			back "没有这条记录"
		else
			UID=rs("UID")
			U_GID=rs("U_GID")
			U_LID=rs("U_LID")
			U_OnOff=rs("U_OnOff")
			U_order=rs("U_order")
			sex=rs("sex")
			userName=rs("userName")
			Password=rs("Password")
			Question=rs("Question")
			Answer=rs("Answer")
			RegTime=rs("RegTime")
			TrueName=rs("TrueName")
			face=rs("face")
			Province=rs("Province")
			City=rs("City")
			District=rs("District")
			Address=rs("Address")
			Post=rs("Post")
			tel=rs("tel")
			Mobile=rs("Mobile")
			Email=rs("Email")
			QQ=rs("QQ")
			U_Desc=rs("U_Desc")
			Adminrand=rs("Adminrand")
			LastLoginTime=rs("LastLoginTime")
			LastLoginIP=rs("LastLoginIP")
			LoginCount=rs("LoginCount")
		end if
	rs.close : set rs=nothing
End Sub


Sub editadmin
	UID=isNum(getForm("UID", "get"))
	U_GID=isNum(getForm("U_GID", "post"))
	userName=getForm("userName", "post")	
	Password=getForm("Password", "post")
	rePassword=getForm("rePassword", "post")
	TrueName=getForm("TrueName", "post")	
	if not isnul(Password) and not isnul(rePassword) then 
		if Password=rePassword then newPassword ="[Password]='"&md5(Password,16)&"',"
	end if
	Question=getForm("Question", "post")	
	Answer=getForm("Answer", "post")	
	face=getForm("face", "post")
	if isnul(userName) then back "用户名称不能为空"	
	sql= "update [dbpre]user set U_GID="&U_GID&", [userName]='"&userName&"',"&newPassword&"Question='"&Question&"',Answer='"&Answer&"',TrueName='"&TrueName&"',face='"&face&"' where UID="&UID
	dbconn(sql)
	if cint(uid)=getsession("adminid") then 		
		setcookie "adminface",face		
	end if
	layertrue("修改成功")
End Sub	

Sub edituser	
	UID=isnum(getForm("UID", "get"))
	U_GID=isnum(getForm("U_GID", "post"))	
	sex=getForm("sex", "post")	
	userName=getForm("userName", "post")	
	Password=getForm("Password", "post")
	rePassword=getForm("rePassword", "post")
	if not isnul(Password) and not isnul(rePassword) then 
		if Password=rePassword then newPassword ="[Password]='"&md5(Password,16)&"',"
	end if
	Question=getForm("Question", "post")	
	Answer=getForm("Answer", "post")	
	RegTime=getForm("RegTime", "post")	
	TrueName=getForm("TrueName", "post")	
	Province=getForm("Province", "post")	
	City=getForm("City", "post")	
	District=getForm("District", "post")	
	Address=getForm("Address", "post")	
	Post=getForm("Post", "post")	
	tel=getForm("tel", "post")	
	Mobile=getForm("Mobile", "post")	
	Email=getForm("Email", "post")	
	QQ=getForm("QQ", "post")	
	face=getForm("face", "post")
	U_Desc=getForm("U_Desc", "post")	
	if isnul(userName) then back "用户名称不能为空"	
	sql= "update [dbpre]user set U_GID="&U_GID&",[userName]='"&userName&"',sex='"&sex&"',"&newPassword&"Question='"&Question&"',Answer='"&Answer&"',TrueName='"&TrueName&"',[Province]='"&Province&"',[City]='"&City&"',[District]='"&District&"',[Address]='"&Address&"',Post='"&Post&"',tel='"&tel&"',Mobile='"&Mobile&"',Email='"&Email&"',QQ='"&QQ&"',face='"&face&"',U_Desc='"&U_Desc&"' where UID="&UID
	dbconn(sql)
	layertrue("修改成功")
End Sub	

Sub adduser
	U_GID=getForm("U_GID", "post")
	if isnul(U_GID) then U_GID=4
	U_LID=isNum(zset.lid)
	U_OnOff=1
	U_order=9
	sex=getForm("sex", "post")	
	userName=getForm("userName", "post")	
	if isnul(userName) then back "登录名不能为空"
	Password=md5(getForm("Password", "post"),16)
	if isnul(Password) then back "密码不能为空"
	Question=getForm("Question", "post")	
	Answer=getForm("Answer", "post")	
	RegTime=now()
	TrueName=getForm("TrueName", "post")	
	Province=getForm("Province", "post")	
	City=getForm("City", "post")	
	District=getForm("District", "post")	
	Address=getForm("Address", "post")	
	Post=getForm("Post", "post")	
	tel=getForm("tel", "post")	
	Mobile=getForm("Mobile", "post")	
	Email=getForm("Email", "post")	
	QQ=getForm("QQ", "post")
	face=getForm("face", "post")
	U_Desc=getForm("U_Desc", "post")
	if dbcount("[dbpre]user","userName='"&userName&"'")>0 then back "登录名已存在"
	sql="insert into [dbpre]user (U_GID,U_LID,U_OnOff,U_order,sex,userName,[Password],Question,Answer,RegTime,TrueName,face,[Province],[City],[District],[Address],Post,tel,Mobile,Email,QQ,U_Desc) values("&U_GID&","&U_LID&","&U_OnOff&","&U_order&",'"&sex&"','"&userName&"','"&Password&"','"&Question&"','"&Answer&"','"&RegTime&"','"&TrueName&"','"&face&"','"&Province&"','"&City&"','"&District&"','"&Address&"','"&Post&"','"&tel&"','"&Mobile&"','"&Email&"','"&QQ&"','"&U_Desc&"')"		
	dbconn(sql)
	layertrue("添加成功")
End Sub	


Sub listuser
	dim i,orderStr,whereStr,Sql,rs,allPage,allRecordset,numPerPage,GroupSelect,smsbutton,realbtn,poweroff,adminidlist
	numPerPage=psize
	if isnum(numPerPage) =0 then numPerPage=10
	if isnul(orderby) then orderby="uid"
	if isnul(ordsc) then ordsc="1"
	if ordsc="1" then orderStr="desc" else ordsc="0" : orderStr="asc"
	if isNul(page) then page=1 else page=clng(page)
	if page=0 then page=1
	orderStr=" order by "&orderby&" "&orderStr &""

	if not isNul(keyword) then 
		whereStr = whereStr&" and (userName like '%"&keyword&"%' or TrueName like '%"&keyword&"%' or mobile like '%"&keyword&"%'  or email  like '%"&keyword&"%' or QQ like '%"&keyword&"%')"		 
	else
		whereStr = ""
	end if
	if isnum(gid)>0 then whereStr=whereStr& "and U_gid="&gid
	sql="select * from [dbpre]User,[dbpre]user_group where U_GID=GID and IsAdmin=0 "&whereStr&orderStr
	'echop sql
	set rs = dbsqlr1(Sql)
	rs.pagesize = numPerPage
	allRecordset = rs.recordcount : allPage= rs.pagecount
	if page>allPage then page=allPage
	if allRecordset=0 then
		if not isNul(keyword) then
		    echo "<tr align=""center""><td colspan='11' height=""28"">关键字 <font color=red>"""&keyword&"""</font> 没有记录</td></tr>" 
		else
		    echo "<tr align=""center""><td colspan='11' height=""28"">您可以搜索会员，右上角!</td></tr>"
		end if
	else
		rs.absolutepage = page
		for i = 1 to numPerPage		
			GroupSelect="<select name='GID"&rs("UID")&"' placeholder='"&rs("U_GID")&"' id='U_GID-"&rs("UID")&"'>"	 
				GroupSelect=GroupSelect&getGroupOption(rs("U_GID"),0)	
				 GroupSelect=GroupSelect&"</select>"
			 if smsmark=1 and checkstr(rs("Mobile"),"mobile") then smsbutton="<button type='button' class='btn btn-success dim' onclick=smssend('"&rs("mobile")&"') title='给他发短信息'><i class='fa fa fa-commenting'></i></button>" else smsbutton=""						
			echo"<tr>"&vbcrlf& _			
				"<td>"&rs("UID")&"</td>"&vbcrlf& _	
				"<td><a href='javascript:void(0)' onclick=opennew('User_edit.asp?UID="&rs("UID")&"')>"&rs("userName")&"</a></td>"&vbcrlf& _	
				"<td>"&rs("truename")&"</td>"&vbcrlf& _	
				"<td>"&GroupSelect&"</td>"&vbcrlf& _	
				"<td>"&rs("mobile")&"</td>"&vbcrlf& _
				"<td>"&rs("email")&"</td>"&vbcrlf& _
				"<td>"&rs("LastLoginTime")&"</td>"&vbcrlf& _				
				"<td><input type='number' name='U_order' class='order'  min='0' max='99' id='U_order-"&rs("UID")&"' placeholder="&rs("U_order")&" value="&rs("U_order")&"></td>"&vbcrlf& _	
				"<td><button type='button' id='"&rs("UID")&"U_OnOff' onclick=setuser('U_OnOff','"&rs("UID")&"') class='btn btn-"&rs("U_OnOff")&" dim'><i class='fa fa-toggle-on'></i></i></button></td>"&vbcrlf& _				
				
			   	"<td>"&smsbutton&"　<button type='button' class='btn btn-info dim' onclick=opennew('User_edit.asp?UID="&rs("UID")&"')><i class='fa fa-pencil'></i></button>　<button type='button'  onclick=deluser('"&rs("UID")&"')  class='btn btn-danger dim'><i class='fa fa-times'></i></button></td>"&vbcrlf& _
			"</tr>"&vbcrlf  	
		rs.movenext
			if rs.eof then exit for
		next
		echo"<tr class=""pagenavi"">"&vbcrlf& _
			"<td colspan=""11"" height=""28"" style=""padding-left:20px;"">"&vbcrlf& _			
			"页数："&page&"/"&allPage&"  每页"&numPerPage &" 总记录数"&allRecordset&"条 <a href=""?Stype="&Stype&"&keyword="&keyword&"&page=1&psize="&psize&"&orderby="&orderby&"&ordsc="&ordsc&"""><i class='fa fa-step-backward'></i></a> <a href=""?Stype="&Stype&"&keyword="&keyword&"&page="&(page-1)&"&psize="&psize&"&orderby="&orderby&"&ordsc="&ordsc&"""><i class='fa fa-chevron-left'></i></a> "&vbcrlf
		echo getadminpage(page, 10, allPage, "gid",gid,keyword)
		echo"<a href=""?Stype="&Stype&"&keyword="&keyword&"&page="&(page+1)&"&psize="&psize&"&orderby="&orderby&"&ordsc="&ordsc&"""><i class='fa fa-chevron-right'></i></a> <a href=""?Stype="&Stype&"&keyword="&keyword&"&page="&allPage&"&psize="&psize&"&orderby="&orderby&"&ordsc="&ordsc&"""><i class='fa fa-step-forward'></i></a>"&vbcrlf
			
		echo"每页显示"&getPageSize("10",psize)&getPageSize("50",psize)&getPageSize("200",psize)&getPageSize("500",psize)
				
		echo"条"&vbcrlf& _	
		"</td>"&vbcrlf& _
		"</tr>"&vbcrlf
		end if
closers rs
End Sub	

Function getPageSize(ps,nps)
	if isnul(nps) then nps="10"
	if ps=nps then 
		getPageSize="<a class='active'>"&ps&"</a>"
	else
		getPageSize="<a href=""?keyword="&keyword&"&page="&page&"&psize="&ps&"&orderby="&orderby&"&ordsc="&ordsc&""">"&ps&"</a>"
	end if
End Function 
%>