<!--#include file="base.asp"-->
<%
''' EYCMS 自定义菜单
''' ==================================================================

	sub adddb()
		dim t0,t1,t2,t3,t4,t5
		t0=eycms.enhtml(eycms.fpost("t0",0))
		t1=eycms.getint(eycms.fpost("t1",0),0)
		t2=eycms.enhtml(eycms.fpost("t2",0))
		t3=eycms.getint(eycms.fpost("t3",0),0)
		t4=eycms.enhtml(eycms.fpost("t4",0))
		t5=eycms.getint(eycms.fpost("t5",0),0)
		if eycms.strlen(t0)=0 then eycms.ajaxjson "菜单名称不能为空",0:exit sub
		if eycms.db.dbcount("wx_menu","followid=0")>=3 then
			eycms.ajaxjson "只允许创建三个一级菜单",0:exit sub
		end if
		if t1=3 then
			if t3=0 then
				eycms.ajaxjson "请选择素材",0	
				exit sub
			end if
			dim data:data=eycms.db.dbload(1,"id","wx_master_class","id="&t3&"","")
			if ubound(data)<0 then
				eycms.ajaxjson "素材选择错误",0	
				exit sub
			end if
		end if
		data=array(array("title",t0,4),array("types",t1,1),array("url",t2,1000),array("replyid",t3,10),array("reply",t4,0),array("ordnum",t5,10),array("followid",0,10),array("sonnum",0,10))
		if eycms.db.dbnew("wx_menu",data,"title='"&t0&"' and followid=0")=0 then
			eycms.ajaxjson "菜单名称已存在，请更换",0
		else
			eycms.ajaxjson "保存成功",1
		end if
	end sub
	
	sub editdb()
		dim t0,t1,t2,t3,t4,t5
		t0=eycms.enhtml(eycms.fpost("t0",0))
		t1=eycms.getint(eycms.fpost("t1",0),0)
		t2=eycms.enhtml(eycms.fpost("t2",0))
		t3=eycms.getint(eycms.fpost("t3",0),0)
		t4=eycms.enhtml(eycms.fpost("t4",0))
		t5=eycms.getint(eycms.fpost("t5",0),0)
		if eycms.strlen(t0)=0 then eycms.ajaxjson "菜单名称不能为空",0:exit sub
		if t1=3 then
			if t3=0 then
				eycms.ajaxjson "请选择素材",0	
				exit sub
			end if
			dim data:data=eycms.db.dbload(1,"id","wx_master_class","id="&t3&"","")
			if ubound(data)<0 then
				eycms.ajaxjson "素材选择错误",0	
				exit sub
			end if
		end if
		dim id:id=eycms.getint(eycms.fget("id",0),0)
		data=array(array("title",t0,6,1),array("types",t1,1,0),array("url",t2,1000,1),array("replyid",t3,10,0),array("reply",t4,0,1),array("ordnum",t5,10,0))
		if eycms.db.dbupdate("wx_menu","id="&id&"",data)=1 then
			eycms.ajaxjson "保存成功",1
		end if
	end sub
	
	sub addsondb()
		dim t0,t1,t2,t3,t4,t5
		t0=eycms.enhtml(eycms.fpost("t0",0))
		t1=eycms.getint(eycms.fpost("t1",0),0)
		t2=eycms.enhtml(eycms.fpost("t2",0))
		t3=eycms.getint(eycms.fpost("t3",0),0)
		t4=eycms.enhtml(eycms.fpost("t4",0))
		t5=eycms.getint(eycms.fpost("t5",0),0)
		dim id:id=eycms.getint(eycms.fget("id",0),0)
		
		if eycms.strlen(t0)=0 then eycms.ajaxjson "菜单名称不能为空",0:exit sub
		if t1=3 then
			if t3=0 then
				eycms.ajaxjson "请选择素材",0	
				exit sub
			end if
			dim data:data=eycms.db.dbload(1,"id","wx_master_class","id="&t3&"","")
			if ubound(data)<0 then
				eycms.ajaxjson "素材选择错误",0	
				exit sub
			end if
		end if
		data=eycms.db.dbload(1,"sonnum,types","wx_menu","id="&id&"","")
		if ubound(data)<0 then
			eycms.ajaxjson "参数错误",0:exit sub
		end if
		dim sonnum:sonnum=data(0,0)
		dim ptype:ptype=data(1,0)
		if eycms.db.dbcount("wx_menu","followid="&id&"")>=5 then
			eycms.ajaxjson "最多只允许创建五个二级菜单",0:exit sub
		end if
		data=array(array("title",t0,7),array("types",t1,1),array("url",t2,255),array("replyid",t3,10),array("reply",t4,0),array("ordnum",t5,10),array("followid",id,10),array("sonnum",0,10))
		if eycms.db.dbnew("wx_menu",data,"title='"&t0&"' and followid="&id&"")=0 then
			eycms.ajaxjson "菜单名称已存在，请更换",0
		else
			if ptype>1 then ptype=1
			eycms.db.dbupdate "wx_menu","id="&id&"",array(array("sonnum",sonnum+1,10,0),array("types",ptype,10,0))
			eycms.ajaxjson "保存成功",1
		end if
	end sub
	
	sub deldb()
		dim id:id=eycms.getint(eycms.fget("id",0),0)
		dim data:data=eycms.db.dbload(1,"sonnum,followid","wx_menu","id="&id&"","")
		if ubound(data)<0 then
			eycms.echo "0参数错误":exit sub
		end if
		if data(0,0)>0 then
			eycms.echo "0请先删除子菜单":exit sub
		end if
		if data(1,0)<>0 then
			dim sdata:sdata=eycms.db.dbload(1,"sonnum","wx_menu","id="&data(1,0)&"","")
			dim snum:snum=sdata(0,0)-1
			eycms.db.dbupdate "wx_menu","id="&data(1,0)&"",array(array("sonnum",snum,10,0))
		end if
		eycms.db.dbdel "wx_menu","id="&id
		eycms.echo "1"
	end sub

	sub orderdb()
		dim t0,t1,t2,t3,i
		t0=eycms.enhtml(eycms.fpost("ordnum",0))
		t1=eycms.enhtml(eycms.fpost("cid",0))
		t2=split(t0,", ")
		t3=split(t1,", ")
		if ubound(t2)-ubound(t3)<>0 then eycms.ajaxjson "0&#21442;&#25968;&#38169;&#35823;",0:exit sub
		for i=0 to ubound(t2)
			if eycms.isnum(t2(i)) then
				eycms.db.dbupdate "wx_menu","id="&t3(i)&"",array(array("ordnum",t2(i),10,0,0))
			end if
		next
		eycms.ajaxjson "保存成功",1
	end sub
	
	sub publishdb()	
		if eycms.db.dbcount("wx_menu","followid=0")=0 then
			eycms.echo "0至少添加一个菜单":exit sub
		end if
		if eycms.db.dbcount("wx_menu","followid=0 and sonnum=0 and types=1")>0 then
			eycms.echo "0一级菜单中必须添加子菜单才能发布":exit sub
		end if
		
		dim bdata,sdata,i,j,str,types
		str="{""button"":["
		bdata=eycms.db.dbload("","title,types,url,replyid,sonnum,id","wx_menu","followid=0","ordnum,id")
		for i=0 to ubound(bdata,2)
			if i>0 then
				str=str&","
			end if
			select case bdata(1,i)
				case "2":types="view"
				case "3","4":types="click"
				case else:types=""
			end select
			str=str&"{""name"":"""&bdata(0,i)&""","
			if types<>"" then
				str=str&"""type"":"""&types&""""
				if types="view" then
					dim menuurl:menuurl=bdata(2,i)
					menuurl=replace(menuurl,"{weburl}",weburl)
					menuurl=replace(menuurl,"{webroot}",webroot)
					str=str&",""url"":"""&menuurl&""""
				else
					if bdata(1,i)=3 then
						str=str&",""key"":""master:"&bdata(3,i)&""""
					else
						str=str&",""key"":""text:"&bdata(5,i)&""""
					end if
				end if
			else
				str=str&"""sub_button"":["
				sdata=eycms.db.dbload("","title,types,url,replyid,id","wx_menu","followid="&bdata(5,i)&"","ordnum,id")
				for j=0 to ubound(sdata,2)
					if j>0 then
						str=str&","
					end if
					
					str=str&"{""name"":"""&sdata(0,j)&""""
					select case sdata(1,j)
						case "2":types="view"
						case "3","4":types="click"
					end select
					str=str&",""type"":"""&types&""""
					if types="view" then
						str=str&",""url"":"""&sdata(2,j)&""""
					else
						if sdata(1,j)=3 then
							str=str&",""key"":""master:"&sdata(3,j)&""""
						else
							str=str&",""key"":""text:"&sdata(4,j)&""""
						end if
					end if
					str=str&"}"
				next
				str=str&"]"
			end if
			str=str&"}"
		next
		str=str&"]}"
		dim wx,json,result
		set wx=new eycms_weixin
			result=wx.create_menu(str)
			set json=toobject(result)
			if json.errcode<>"0" then
				eycms.echo "0"&json.errmsg
			else
				eycms.echo "1发布成功"
			end if
		set wx=nothing
	end sub
	
	sub deletedb()
		dim wx,result,json
		set wx=new eycms_weixin
			result=wx.del_menu()
			set json=toobject(result)
			if json.errcode<>"0" then
				eycms.echo "0"&json.errmsg
				exit sub
			end if
		set wx=nothing
		eycms.db.dbdel "wx_menu",""
		eycms.echo "1删除成功"
	end sub
	
	islogin
	checkpagelever 69
	dim act:act=lcase(eycms.fget("act",0))
	dim id:id=eycms.getint(eycms.fget("id",0),0)
	select case act
		case "add":load theme_menu_add
		case "adddb":adddb
		case "addson":load theme_menu_add_son
		case "addsondb":addsondb
		case "edit","edit_son"
			dim rsshow
			set rsshow=eycms.db.exedb("select top 1 * from wx_menu where id="&id&"")
			if rsshow.eof then
				eycms.echo "参数错误":eycms.die
			end if
			load eval("theme_menu_"&act)
		case "editdb":editdb
		case "del":deldb
		case "order":orderdb
		case "publish":publishdb
		case "delete":deletedb
		case else
			load theme_menu
	end select
	eycms.db.dbclose
%>