<!--#include file="base.asp"-->
<%
''' EYCMS 分组
''' ==================================================================

	sub getlistdb()
		dim wx,datalist,json,arr
		set wx=new eycms_weixin
			datalist=wx.get_user_group_list
			set json=toobject(datalist)
				if instr(datalist,"errcode") then
					eycms.echo json.errmsg
				else
					dim i
					for i=0 to json.groups.length-1
						dim gid,gname,gcount
						gid=viewobject(json.groups,"id",i)
						gname=viewobject(json.groups,"name",i)
						gcount=viewobject(json.groups,"count",i)
						dim result
						result=eycms.db.dbnew("wx_user_group",array(array("groupid",gid,10),array("groupname",gname,50),array("countnum",gcount,10)),"groupid="&gid&"")
						if result=0 then
							eycms.db.dbupdate "wx_user_group","groupid="&gid&"",array(array("groupname",gname,50,1),array("countnum",gcount,10,0))
						end if
					next				
					eycms.echo "1"
				end if
			set json=nothing
		set wx=nothing
	end sub
	
	sub adddb()
		dim t0:t0=eycms.enhtml(eycms.fpost("t0",0))
		if eycms.strlen(t0)=0 then eycms.ajaxjson "分组名称不能为空",0:exit sub
		if eycms.db.dbnew("wx_user_group",array(array("groupname",t0,6),array("countnum",0,10)),"groupname='"&t0&"'")=0 then
			eycms.ajaxjson "分组名称已存在，请更换",0
		else
			dim wx,data,json
			set wx=new eycms_weixin
				data=wx.create_user_group(t0)
				set json=toobject(data)
				if instr(data,"errcode") then
					eycms.ajaxjson json.errmsg,0
				else
					dim gid:gid=json.group.id
					eycms.db.dbupdate "wx_user_group","groupname='"&t0&"'",array(array("groupid",gid,10,0))
					eycms.ajaxjson "保存成功",1
				end if
			set wx=nothing
		end if
	end sub
	
	sub editdb()
		dim t0:t0=eycms.enhtml(eycms.fpost("t0",0))
		if eycms.strlen(t0)=0 then eycms.ajaxjson "分组名称不能为空",0:exit sub
		dim id:id=eycms.getint(eycms.fget("id",0),0)
		if eycms.db.dbcount("wx_user_group","groupname='"&t0&"' and id<>"&id&"")>0 then
			eycms.ajaxjson "0分组名称已存在，请更换！",0:exit sub
		end if
		dim data:data=eycms.db.dbload(1,"groupid","wx_user_group","id="&id&"","")
		if ubound(data)>=0 then
			dim gid:gid=data(0,0)
			eycms.db.dbupdate "wx_user_group","id="&id&"",array(array("groupname",t0,6,1))
			dim wx,json,result
			set wx=new eycms_weixin
				result=wx.update_user_group(gid,t0)
				set json=toobject(result)
				if json.errcode<>"0" then
					eycms.ajaxjson json.errmsg,0
				else
					eycms.ajaxjson "保存成功",1
				end if
				set json=nothing
			set wx=nothing
		else
			eycms.ajaxjson "参数错误",0
		end if
	end sub
	
	sub deldb()
		dim id:id=eycms.getint(eycms.fget("id",0),0)
		dim data:data=eycms.db.dbload(1,"groupid","wx_user_group","id="&id&"","")
		if ubound(data)>=0 then
			dim gid:gid=data(0,0)		
			dim wx,json,result
			set wx=new eycms_weixin
				result=wx.delete_user_group(gid)
				set json=toobject(result)
				if json.errcode="0" or json.errcode<>"-1" then
					eycms.db.dbdel "wx_user_group","id="&id&""
					eycms.echo "1"
				else
					eycms.echo json.errcode
				end if
				set json=nothing
				
			set wx=nothing
		else
			eycms.echo "参数错误"
		end if
	end sub

	islogin
	checkpagelever 71
	dim act:act=lcase(eycms.fget("act",0))
	dim keyword:keyword=eycms.enhtml(eycms.fget("keyword",1))
	select case act
		case "getlist":getlistdb
		case "add":load theme_user_group_add
		case "adddb":adddb
		case "edit"
			dim id:id=eycms.getint(eycms.fget("id",0),0)
			dim rsshow
			set rsshow=eycms.db.exedb("select top 1 * from wx_user_group where id="&id&"")
			if rsshow.eof then
				eycms.echo "参数错误":eycms.die
			end if
			load theme_user_group_edit
		case "editdb":editdb
		case "del":deldb
		case else:load theme_user_group
	end select
	eycms.db.dbclose
%>