<!--#include file="admin_conn.asp"-->
<%
chkLogin
dim action : action = be("get","action")

select case action
	case "editall" : editall
	case "last" : moveTolast
	case "next" : moveTonext
	case else : headAdmin "友情链接管理" : main
end select
dispseObj()

sub editall
	dim ids,id,l_name,l_sort,l_url
	ids = split(replace(be("post","l_id")," ",""),",")
	For Each id In ids
		l_name = be("post","l_name" &id)
		l_sort = be("post","l_sort" &id) 
		l_url = be("post","l_url" &id)
		if isN(l_name)  then   writeE "名称不能为空"
		if isN(l_url) then writeE "地址不能为空"
		if isN(l_sort) then  l_sort= objdb.DB("SELECT MAX(l_sort) FROM {pre}link","rs1")(0)+1
		if not isNum(l_sort) then writeE "排序号不能为空"
		objdb.Update "{pre}link",Array("l_name","l_url", "l_sort"),Array(l_name,l_url,l_sort),"l_id="&id
	Next
	writeA "修改完毕"
end sub

sub moveToLast
	Dim l_id : l_id = be("get","l_id")
	Dim CurSort : CurSort = objdb.DB("SELECT l_sort FROM {pre}link WHERE l_id = " & l_id,"rs1")(0)
	Dim Lessthan : Lessthan = objdb.DB("SELECT COUNT(*) FROM {pre}link WHERE l_sort < " & CurSort,"rs1")(0)
	
	if Lessthan>0 then 
	    Dim l_sort : l_sort=objdb.DB("SELECT top 1 l_sort FROM {pre}link  WHERE l_sort<"&CurSort&" ORDER BY l_sort DESC","rs1")(0)
	    objdb.Update "{pre}link" ,Array("l_sort"),Array(l_sort-1),"l_id="&l_id
	else
		objdb.Update "{pre}link",Array("l_sort"),Array(CurSort-1),"l_id="&l_id
	end if
	redirect getReferer()
end sub 

sub moveToNext
	Dim l_id : l_id = be("get","l_id")
	Dim CurSort : CurSort = objdb.DB("SELECT l_sort FROM {pre}link WHERE l_id = " & l_id,"rs1")(0)
	Dim Lessthan : Lessthan = objdb.DB("SELECT COUNT(*) FROM {pre}link WHERE l_sort > " & CurSort,"rs1")(0)
	
	if Lessthan>0 then 
		Dim l_sort : l_sort=objdb.DB("select top 1 l_sort FROM {pre}link  WHERE l_sort>"&CurSort&" ORDER BY l_sort DESC","rs1")(0)
		objdb.Update "{pre}link" ,Array("l_sort"),Array(l_sort+1),"l_id="&l_id
	else
		objdb.Update "{pre}link",Array("l_sort"),Array(CurSort+1),"l_id="&l_id
	end if
	redirect getReferer()
end sub

sub main
	Dim pagenum : pagenum = be("all","page")
	if isN(pagenum) or not isNum(pagenum) then pagenum = 1
	if pagenum < 1 then pagenum = 1
	pagenum = clng(pagenum)
	Dim sql : sql = "SELECT l_id,l_name,l_type,l_url,l_sort FROM {pre}link ORDER BY l_sort,l_id ASC"
	Dim rs : Set rs = objdb.DB(sql,"rs1")
%>
<script language="javascript">
$(document).ready(function(){
	$("#form2").validate({
		rules:{
			l_name:{
				required:true,
				stringCheck:true,
				maxlength:64
			},
			l_url:{
				required:true,
				maxlength:254
			},
			l_logo:{
				maxlength:254
			},
			l_sort:{
				number:true
			},
			l_type:{
				required:true
			}
		}
	});
	$('#form1').form({
		onSubmit:function(){
			if(!$("#form1").valid()) {return false;}
		},
	    success:function(data){
	        $.messager.alert('系统提示', data, 'info',function(){
	        location.href=location.href;
	        });
	    }
	});
	$('#form2').form({
		onSubmit:function(){
			if(!$("#form2").valid()) {return false;}
		},
	    success:function(data){
	        $.messager.alert('系统提示', data, 'info');
	    }
	});
	$("#btnDel").click(function(){
			if(confirm('确定要删除吗')){
				$("#form1").attr("action","admin_ajax.asp?action=del&flag=batch&tab={pre}link");
				$("#form1").submit();
			}
			else{return false}
	});
	$("#btnEdit").click(function(){
		$("#form1").attr("action","?action=editall");
		$("#form1").submit();
	});
	$("#btnAdd").click(function(){
		$('#form2').form('clear');
		$("#flag").val("add");
		$('#win1').window('open'); 
	});
	$("#btnCancel").click(function(){
		location.href= location.href;
	});
});
function edit(id)
{
	$('#form2').form('clear');
	$("#flag").val("edit");
	$('#win1').window('open');
	$('#form2').form('load','admin_ajax.asp?action=getinfo&tab={pre}link&col=l_id&val='+id);
}
</script>
<form action="" method="post" name="form1" id="form1">
<table class="tb">
	<tr>
	<td width="5%">&nbsp;</td>
	<td>友情链接名称</td>
	<td width="25%">地址</td>
	<td width="10%">类型</td>
	<td width="10%">排序</td>
	<td width="15%">操作</td>
	</tr>
	<%
		if rs.eof then
	%>
    <tr><td align="center" colspan="6">没有任何记录!</td></tr>
    <%
		else
			Dim i
			rs.PageSize = app_pagenum
	    	rs.AbsolutePage = pagenum
	  		for i = 1 to rs.PageSize
	  			if rs.eof then exit for
	%>
    <tr>
	  <td><input name="l_id" type="checkbox" id="l_id" value="<%=rs("l_id")%>" /></td>
      <td><input type="text" name="l_name<%=rs("l_id")%>" value="<%=rs("l_name")%>" size="30"/></td>
	  <td><input name="l_url<%=rs("l_id")%>" type="text" value="<%=rs("l_url")%>"  size="30"/></td>
      <td><% if rs("l_type") ="font" then writeA "文字链接" else writeA "图片链接" end if %></td>
	  <td><input name="l_sort<%=rs("l_id")%>" type="text" value="<%=rs("l_sort")%>"  size="5"/></td>
      <td>
	  <a href="admin_link.asp?action=last&l_id=<%=rs("l_id")%>">上移</a> |
	  <a href="admin_link.asp?action=next&l_id=<%=rs("l_id")%>">下移</a> |
	  <a href="javascript:void(0)" onclick="edit('<%=rs("l_id")%>');return false;">修改</a> |
	  <a href="admin_ajax.asp?action=del&tab={pre}link&l_id=<%=rs("l_id")%>" onClick="return confirm('确定要删除吗?');">删除</a></td>
    </tr>
	<%
			rs.MoveNext
		next
	end if
	%>
	<tr>
	<td colspan="6">全选<input type="checkbox" name="chkall" id="chkall" class="checkbox" onClick="checkAll(this.checked,'l_id')" />
	&nbsp;<input type="button" id="btnDel" value="批量删除" class="input" />
	&nbsp;<input type="button" id="btnEdit" value="批量修改" class="input"/>
	&nbsp;<input type="button" id="btnAdd" value="添加"  class="input"/>
	</td></tr>
    <tr align="center">
      <td colspan="6">
	   <%=pagelist_manage(rs.PageCount,pagenum,rs.RecordCount,app_pagenum,"admin_link.asp?page={p}")%>
       </td>
    </tr>
</table>
</form>
<div id="win1" class="easyui-window" title="窗口" style="padding:5px;width:400px;" closed="true" closable="false" minimizable="false" maximizable="false">
<form action="admin_ajax.asp?action=save&tab={pre}link" method="post" name="form2" id="form2">
<table class="tb">
	<input id="l_id" name="l_id" type="hidden" value="">
	<input id="flag" name="flag" type="hidden" value="">
	<tr>
	<td width="20%">网站名称：</td>
	<td><input id="l_name" size=50 value="" name="l_name">
	</td>
	</tr>
	<tr>
	<td>网站地址：</td>
	<td><input id="l_url" size=50 value="http://" name="l_url" >
	</td>
	</tr>
	<tr>
	<td>链接类型：</td>
	<td>
	<select id="l_type" name="l_type">
	<option value="font">文字链接</option>
	<option value="pic">图片链接</option>
	</select>
	</td>
	</tr>
	<tr>
	<td>logo地址：</td>
	<td>
	<input id="l_logo" size="50" value="http://" name="l_logo" >
	</td>
	</tr>
	<tr>
	<td>排序号：</td>
	<td><input id="l_sort" size="10" value="" name="l_sort" >
	</td>
	</tr>
    <tr align="center">
      <td colspan="2"><input class="input" type="submit" value="保存" id="btnSave"> <input class="input" type="button" value="返回" id="btnCancel"> </td>
    </tr>
</table>
</form>
</div>
</body>
</html>
<%
objdb.C(rs)
end sub
%>