<!--#include file="../../../inc/cms_admin.asp" -->
<%
dim SID,PID, S_LID, S_PID, S_Order, S_Type, S_Name, S_URL, S_Level, S_AddTime, S_Title, S_Key, S_Desc, S_Path, S_Template, C_Template, S_Folder, C_Folder, S_FileName, C_FileName, S_OnOff, S_TID, S_GID, S_Exc,S_Pic,S_Postion,wherestr,sortlink,addbutton,readonly,navicon,Levellink,Model_type,Model_Name,Model_table,S_ico,s_other1,s_other2
dim sql,S_EnName,usertype,table,col,colval,idval,id,sortid,act,stype,postion,sorttype
act=getForm("act","both")
stype=getForm("stype","both")
postion=getForm("postion","both")
stype=getform("stype","both")
table=getForm("table","both")
colval=getForm("colval","both")
SID=getForm("SID","both")
PID=getForm("PID","both")
sortid=getForm("sortid","get")
Select case act	
	case "setcol"	:	dbconn("update [dbpre]sort set S_OnOff="&colval&"  where  SID="&SID&"")	
	case "add" 		: 	addSort
	case "adds" 	: 	addsSort
	case "quick" 	:	quickAdd
	case "edit" 	:	editSort
	case "del" 		: 	delSort	
	case "saveid" 	: 	saveid
	case "moveid" 	: 	moveSort
	case "Loadsort" : 	Loadsort
	case "Sort_List" : echo "["&Sort_List(0)&"]"
End Select

sub saveid
	on error resume next
	dim colid,colname,colval,sql1,sql2
	colid=getForm("colid","post")
	colname=getForm("colname","post")
	colval=getForm("colval","post")	
	sql1="update [dbpre]Sort Set "&colname&"='"&colval&"' Where SID="&colid
	sql2="update [dbpre]Sort Set "&colname&"="&colval&" Where SID="&colid
	dbconn(sql1)
	if Err then Err.clear	: dbconn(sql2)	
	if Err then	die Err.description else echo 1
end sub

sub Loadsort
	PID=dbselect("[dbpre]sort","S_PID","sid="&sortid&"")
 	getsortOption 0,PID,"",sortid
end sub

Sub getSort
	stype=getForm("stype","both")
	if not isnul(SID) then		
		sql ="select * from [dbpre]Sort where SID="&SID
		dim rs : set rs = conn.exec(sql,"r1")
		if not rs.eof then 
			SID=rs("SID")
			S_LID=rs("S_LID")
			S_PID=rs("S_PID")
			S_Order=rs("S_Order")
			S_Type=rs("S_Type")
			S_Name=rs("S_Name")
			S_EnName=rs("S_EnName")
			S_URL=rs("S_URL")
			S_Level=rs("S_Level")
			S_AddTime=rs("S_AddTime")
			S_Path=rs("S_Path")
			S_Template=rs("S_Template")
			S_Exc=rs("S_Exc")
			S_ico=rs("S_ico")
			S_other1=rs("S_other1")
			S_other2=rs("S_other2")
		end if
		rs.close : set rs=nothing
	else		
		back "没有这条记录"
	end if 
End Sub

Sub moveSort
	dim id,ids,i,moveSortID
	id			=	getForm("moveID","both") 			: if isnul(id) then alertgo "请选择要操作的内容","-1"
	moveSortID	=	getForm("moveSortID","both")
	ids=split(id,",")
	for i=0 to ubound(ids)
		if moveSortID="0" then 
			S_Level="1"
			S_TID=ids(i)
		else
			dim rs 	: set rs=Conn.Exec("select S_Level, S_TID, S_Path from [dbpre]Sort where SID="&moveSortID,"r1")
			S_Level=rs(0)+1
			S_TID=rs(1)
		end if		
		sql="update [dbpre]Sort set S_PID="&moveSortID&", S_Level="&S_Level&", S_TID="&S_TID&" where SID ="&ids(i)
		dbconn(sql)
		editsubid(ids(i))
	next		
	layerMsg("移动成功")
End Sub

Function editnowid(SID)
	dim rs,S_Path,S_Level
	PID=dbselect("[dbpre]Sort","S_pid","SID="&SID)
	if pid>0 then
		set rs=dbsqlr1("select top 1 S_TID,S_Level,S_Path from [dbpre]Sort where SID="&PID)
		if not rs.eof then	
			dbsql "update [dbpre]Sort set S_TID="&rs("S_TID")&", S_Level="&rs("S_Level")+1&", S_Path='"&rs("S_Path")&","&sid&"' where SID="&sid
		end if
	else
		dbconn "update [dbpre]Sort set S_TID="&sid&", S_Level=1, S_Path='"&sid&"' where SID="&sid
	end if
End Function

Sub editSort
	S_type=getForm("S_type", "both")
	SID=getForm("SID", "post")
	S_PID=getForm("S_PID", "post")
	S_Order=getForm("S_Order", "post")
	S_Type=getForm("S_Type", "post")
	S_Name=getForm("S_Name", "post")
	S_EnName=getForm("S_EnName", "post")
	S_URL=getForm("S_URL", "post")
	S_Title=getForm("S_Title", "post")
	S_Key=getForm("S_Key", "post")
	S_Desc=getForm("S_Desc", "post")
	S_Template=getForm("S_Template", "post")
	C_Template=getForm("C_Template", "post")
	S_ico=getForm("S_ico", "post")	
	S_other1=getForm("S_other1", "post")	
	S_other2=getForm("S_other2", "post")	
	S_Postion=getForm("S_Postion", "post")	
	if isnul(S_Name) then alertgo "分类名称不能为空","-1"
	if S_Type<>dbselect("[dbpre]Sort","S_Type","SID="&SID&"") then dbsql("update [dbpre]content set C_Type='"&S_Type&"' where C_SID="&SID&"")
	sql= "update [dbpre]Sort set S_PID="&S_PID&",  S_Order="&S_Order&", S_Type='"&S_Type&"',  S_LID="&S_LID&", S_GID="&S_GID&", S_Name='"&S_Name&"', S_EnName='"&S_EnName&"', S_URL='"&S_URL&"', S_Title='"&S_Title&"', S_Key='"&S_Key&"', S_Desc='"&S_Desc&"', S_Template='"&S_Template&"', C_Template='"&C_Template&"', S_Folder='"&S_Folder&"', C_Folder='"&C_Folder&"', S_FileName='"&S_FileName&"', C_FileName='"&C_FileName&"', S_EditTime ='"&sqltime&"', S_Exc='"&S_Exc&"',S_Postion='"&S_Postion&"',S_Pic='"&S_Pic&"',S_ico='"&S_ico&"' ,S_other1='"&S_other1&"' ,S_other2='"&S_other2&"'  where SID="&SID
	dbconn(sql)	
	if S_type="about" then 
	if dbcount("[dbpre]About","A_SID="&SID&"")=0 then dbsql "insert into [dbpre]About(A_SID,A_LID,A_name,A_Enname,A_Order,A_OnOff,A_AddTime,A_EditTime,A_Key,A_Desc)values("&SID&","&S_LID&",'"&S_Name&"','"&S_EnName&"',9,1,'"&date()&"','"&date()&"','','')"
	end if
	editnowid(SID)
	layertrue("保存成功")
End Sub	

Sub delSort
	dim id	:	id=getForm("id","both")
	if isnul(id) then alertgo "请选择要操作的内容","-1"
	dim ids,i
	ids=split(id,",")
	for i=0 to ubound(ids)
		if not isnul(ids(i)) then 
			dim subids : subids=endstr(getSortid(ids(i),3),",")
			dim subid : subid=split(subids,",")
			dim j
			if runmode=1 then
				for j=0 to ubound(subid)
					delList(trim(subid(j)))
				next
				dim rs, sql, filepath			
				sql="select * from [dbpre]Content as a, [dbpre]Sort as b where a.C_SID=b.SID and b.SID in ("&subids&")"
				set rs=dbsql(sql)
				do while not rs.eof	
					filepath=getContentLink(rs("SID"),rs("CID"),rs("S_Folder"),rs("C_GID"),rs("C_Folder"),rs("C_FileName"),rs("S_AddTime"),rs("C_PageName"),rs("S_GID"))
					if isfile(filepath) then delFile filepath	
					rs.movenext
				loop
			end if
			dbconn "delete from [dbpre]Content where C_SID in ("&subids&")"
		end if
	next
End Sub

Function delList(Byval SID)
	if not ifnum(SID) then exit Function
	dim rs	:	set	rs=dbsql("select SID,S_Folder,S_FileName,S_type from [dbpre]sort,[dbpre]Model where S_type = Model_type and SID="&SID)
	dim S_FileName,htmlFile,CacheFile
	S_FileName=replacestr(repnul(rs("S_FileName")), "{SID}", SID)	
	S_FileName=replacestr(S_FileName, "{page}", 1)		
	htmlFile  	= sitepath&htmlDir&rs("S_Folder")&S_FileName&".html"
	CacheFile	= sitepath&CachePath&Cachepre&SID&"_1.html"
	if isfile(htmlFile) then delFile htmlFile
	if isfile(CacheFile) then delFile CacheFile
End Function

Function Sort_List(SID)
	Dim rs,json,listjson
	sql="select S_TID,SID,S_Type,S_postion,S_Level,S_enname,S_name,S_OnOff,S_order,S_url,Model_type,Model_Name,Model_table,(select count(*) from [dbpre]Sort where S_PID=t.SID"&whereStr&") as c from [dbpre]Model as a,[dbpre]Sort t where S_LID="&zset.lid&" and S_Type=Model_type and S_PID="&SID&" order by S_order asc , SID asc"
	'echo sql
	set rs=dbsqlr1(sql)	
	if not rs.eof then  	
	  Set json = jsObject()
		do while not rs.eof
			select case  rs("S_Type")				 
			  case "about"	
				sortlink=getSortLink(rs("s_type"), rs("SID"), rs("s_url"),0,1)	
				addbutton="onclick=""parent.$('#left"&rs("SID")&"').click()"""			   
			  case "links"
				sortlink=rs("S_URL")			
				addbutton=""
			  case "brand"
				sortlink=sitepath&"brandlist/?"&rs("SID")&"_1"&siteExt					
				addbutton=""	 
			  case else
				sortlink=getSortLink(rs("s_type"), rs("SID"), rs("s_url"),0,1)	
				if rs("Model_table")="content" and rs("S_onoff")=1 then	addbutton="onclick=""parent.$('#left"&rs("SID")&"').click()""" else addbutton=""
			end select
			
			if rs("S_Level")=1 and rs("c")>0 then 		
				Levellink="<a class='open'><i class='S_Level Level"&rs("S_Level")&"'>Parent"&rs("SID")&"</i></a>"
			elseif rs("S_Level")=1 and rs("c")=0 then 
				Levellink="<i class='S_Level Level0'></i>"
			else
				Levellink="<i class='S_Level Level"&rs("S_Level")&" Parent"&rs("S_TID")&"'></i>"
			end if
				 json("id") = rs("SID")	
				 json("S_name") = Levellink&"　<input type='text' class='title' id='S_name-"&rs("SID")&"' placeholder='"&rs("S_name")&"' value='"&rs("S_name")&"'><a href='"&sortlink&"' target='_blank''>　<i class='fa fa-external-link'></i></a>"	
				 json("S_enname") = "<input type='text' class='title' id='S_enname-"&rs("SID")&"' placeholder='"&rs("S_enname")&"'  value='"&rs("S_enname")&"'>"
				 json("S_type") = "<a href='javascript:void(0)' "&addbutton&">"&rs("Model_Name")&"</a>"	
				 json("S_url") = "<input type='text' class='title S_URL' id='S_url-"&rs("SID")&"' placeholder='"&rs("S_url")&"' value='"&rs("S_url")&"' >"				
				 json("S_order") = "<input type='number' class='order'  min='0' max='99' id='S_order-"&rs("SID")&"' placeholder="&rs("S_order")&" value="&rs("S_order")&">"	
			  	 json("S_onoff") = "<button type='button'  onclick=setsort('S_OnOff','"&rs("SID")&"') id='"&rs("SID")&"S_OnOff' class='btn btn-"&rs("S_OnOff")&"  dim'><i class='fa fa-toggle-on'></i></button>"	
			   	 json("S_edit") = "<button type='button' class='btn btn-info dim' title='添加子类' onclick=opennew('sort_add.asp?Stype="& rs("S_Type")&"&PID="&rs("SID")&"')><i class='fa fa-plus'></i></button> <button type='button' class='btn btn-info dim' title='编辑分类' onclick=opennew('sort_edit.asp?Stype="& rs("S_Type")&"&SID="&rs("SID")&"')><i class='fa fa-pencil'></i></button> "
				 listjson=listjson& json.jsString()&","
			if rs("c")>0 then			
				listjson=listjson&Sort_List(rs("SID"))&","
			end if
		 rs.movenext
		loop
		Sort_List=endstr(listjson,",")		
	else
	 	Sort_List= sql
	end if
closers rs	
end function

Sub getMobel(stype,action)	
	dim rs,sel,sql,wherestr
	select case action
		case "adds"	:	wherestr="Model_OnOff=1 and (Model_table ='about' or Model_table='content')"
		case else : wherestr="Model_OnOff=1"
	end select
	sql="select model_type,model_name,Model_list_TP,Model_Content_TP,Model_list_FD,Model_Content_FD,Model_list_Name,Model_Content_Name  from [dbpre]model where "&wherestr
	set rs=dbsql(sql)
		Do While Not rs.Eof	
			IF CSTR(stype)=CSTR(rs(0)) Then sel = "selected=""selected""" else sel="" end if
			echo "<option stp="""& rs(2) &""" ctp="""& rs(3) &""" sfd="""& rs(4) &""" cfd="""& rs(5) &""" sname="""& rs(6) &""" cname="""& rs(7) &""" value="""& rs(0) &""" "&sel&">"&rs(1)&"</option>" & vbcr				
			rs.MoveNext
		Loop
		Closers rs
	end Sub
	
Sub tempList(selectname,selectedtemp)
	dim path,fileListArray,i,fileAttr,folderListArray,folderAttr,parentPath
	path=sitePath&"Template/pc/"&zset.pcTemplate&zset.PchtmlPath
	echop path	
	'if not isFolder(path) then createFolder path,"folderdir"	
	if not isfolder(path) then	
		echo "<option value="""" >未找到模版</option>"
		response.end
	end if
	if isnul(selectedtemp) then echo "<option value="""" ></option>"
	fileListArray= getFileList(path)
	if instr(fileListArray(0),",")>0 then	
		for  i = 0 to ubound(fileListArray)
			dim selectedstr:selectedstr=""
			fileAttr=split(fileListArray(i),",")
			if selectedtemp=fileAttr(0) then selectedstr=" selected"
			echo "<option value="""&fileAttr(0)&""" "&selectedstr&">"&fileAttr(0)&"</option>"
		next		
	end if	
End Sub
%>