<%@ Control Language="C#" %>
<%@ Import Namespace="Kesion.Publics" %>
<%@ Import Namespace="Kesion.BLL" %>
<%@ Import Namespace="Kesion.Cache" %>
<%@ Import Namespace="Kesion.HtmlTags" %>
<%@ Import Namespace="Kesion.APPCode" %>
<%@ Import Namespace="Kesion.Weixin" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server" type="text/C#">
	dynamic __user = HttpContext.Current.Items["user"];                //初始化用户信息，
	private static dynamic __info = new System.Dynamic.ExpandoObject();   //初始化当前页信息
	bool isLoadPage = true;  //定义页面是否加载
	DataRow dr = null;

	private int MenuID = 0;
	private string action = string.Empty;
	private int ParentID = 0;
	protected void Page_Load(object sender, EventArgs e)
	{
		string DoMethod = BasicField.GetParam("do");
		if (string.IsNullOrEmpty(DoMethod))
		{
			DoMethod = KSCMS.S("action");
		}
		MenuID = Utils.StrToInt(KSCMS.S("menuid"));
		ParentID = Utils.StrToInt(KSCMS.S("parentid"));
		if (DoMethod == "parentup")
		{
			DoParentUpOrder();
		}
		else if (DoMethod == "parentdown")
		{
			DoParentDownOrder();
		}
		else if (DoMethod == "childdown")
		{
			DoChildDownOrder();
		}
		else if (DoMethod == "childup")
		{
			DoChildUpOrder();
		}
	}
	private void DoParentUpOrder()
	{
		Hashtable ht = new Hashtable();
		#region 父级上移
		using (DataTable dt = DataFactory.GetDataTable("select top 1 id,orderid from KS_WeixinMenu Where id =" + MenuID + " and ParentID=0 and TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+" order by id"))
		{
			if (dt.Rows.Count > 0)
			{
				DataRow dr = dt.Rows[0];
				int proid = Utils.StrToInt(DataFactory.GetOnlyValue("select top 1 id from KS_WeixinMenu Where ParentID=0 and  orderid<" + dr["orderid"].ToString() + " and  TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+" order by orderid desc"));
				DataFactory.ExecuteNonQuery("update KS_WeixinMenu set orderid=orderid+1 where id=" + proid + " and TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+"");
				DataFactory.ExecuteNonQuery("update KS_WeixinMenu set orderid=orderid-1 where id=" + MenuID + " and orderid>1 and TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+"");
			}
		}
		Msg.Alert("恭喜，菜单保存成功", BasicField.GetUserUrl("wx/wx_Menu"));
		isLoadPage = false;
		return;
		#endregion
	}

	private void DoParentDownOrder()
	{
		#region 父级下移
		using (DataTable dt = DataFactory.GetDataTable("select top 1 id,orderid from KS_WeixinMenu Where id =" + MenuID + " and ParentID=0 and TypeFlag=" + new TemporaryVar().GroupID + " and userid=" + new TemporaryVar().UserID + " order by id"))
		{
			if (dt.Rows.Count > 0)
			{
				DataRow dr = dt.Rows[0];
				int maxorderid = Utils.StrToInt(DataFactory.GetOnlyValue("select top 1 max(orderid) from KS_WeixinMenu Where ParentID=0 and  TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+" "));
				int proid = Utils.StrToInt(DataFactory.GetOnlyValue("select top 1 id from KS_WeixinMenu Where ParentID=0 and  orderid>" + dr["orderid"].ToString() + " and TypeFlag=" + new TemporaryVar().GroupID + " and userid=" + new TemporaryVar().UserID + " order by orderid asc"));
				DataFactory.ExecuteNonQuery("update KS_WeixinMenu set orderid=orderid+1 where orderid<" + maxorderid + " and  id=" + MenuID + " and  TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+"");
				DataFactory.ExecuteNonQuery("update KS_WeixinMenu set orderid=orderid-1 where id=" + proid + " and orderid<>1 and  TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+"");
			}
		}
		Msg.Alert("恭喜，菜单保存成功", BasicField.GetUserUrl("wx/wx_Menu"));

		#endregion
	}
	private void DoChildUpOrder()
	{
		Hashtable ht = new Hashtable();
		#region 子级上移
		using (DataTable dt = DataFactory.GetDataTable("select top 1 id,orderid from KS_WeixinMenu Where id =" + MenuID + " and ParentID=" + ParentID + " and TypeFlag=" + new TemporaryVar().GroupID + " and userid=" + new TemporaryVar().UserID + " order by id"))
		{
			if (dt.Rows.Count > 0)
			{
				DataRow dr = dt.Rows[0];
				int proid = Utils.StrToInt(DataFactory.GetOnlyValue("select top 1 id from KS_WeixinMenu Where ParentID=" + ParentID + " and  orderid<" + dr["orderid"].ToString() + " and TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+" order by orderid desc"));
				DataFactory.ExecuteNonQuery("update KS_WeixinMenu set orderid=orderid+1 where id=" + proid + " and TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+"");
				DataFactory.ExecuteNonQuery("update KS_WeixinMenu set orderid=orderid-1 where id=" + MenuID + " and orderid>1 and TypeFlag=" + new TemporaryVar().GroupID + " and userid=" + new TemporaryVar().UserID + "");
			}
		}
		Msg.Alert("恭喜，菜单保存成功", BasicField.GetUserUrl("wx/wx_Menu"));

		#endregion
	}

	private void DoChildDownOrder()
	{
		#region 子级下移
		using (DataTable dt = DataFactory.GetDataTable("select top 1 id,orderid from KS_WeixinMenu Where id =" + MenuID + " and parentid=" + ParentID + " and  TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+" order by id"))
		{
			if (dt.Rows.Count > 0)
			{
				DataRow dr = dt.Rows[0];
				int maxorderid = Utils.StrToInt(DataFactory.GetOnlyValue("select top 1 max(orderid) from KS_WeixinMenu Where ParentID=" + ParentID + "  and   TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+" "));
				int proid = Utils.StrToInt(DataFactory.GetOnlyValue("select top 1 id from KS_WeixinMenu Where ParentID=" + ParentID + " and  orderid>" + dr["orderid"].ToString() + " and  TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+" order by orderid asc"));
				DataFactory.ExecuteNonQuery("update KS_WeixinMenu set orderid=orderid+1 where orderid<" + maxorderid + " and  id=" + MenuID + " and  TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+"");
				DataFactory.ExecuteNonQuery("update KS_WeixinMenu set orderid=orderid-1 where id=" + proid + " and orderid<>1 and  TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+"");
			}
		}
		Msg.Alert("恭喜，菜单保存成功", BasicField.GetUserUrl("wx/wx_Menu"));
		#endregion
	}

</script>
<%
	if (!isLoadPage) return;
%>
 <link href="/SysImg/teacher/css/newswx.css" rel="stylesheet" />
 <link rel="stylesheet" type="text/css" href="/admin/weixin/images/font/css/font-awesome.min.css">
 <form name="myForm" method="post" action="<%=BasicField.GetUserUrl("wx/wx_MenuList") %>" id="myForm">
  <div class="messagebox">
     <table width="100%" class="CTable menu_list_table" align="center" border="0" cellpadding="0"  cellspacing="0">
        <tr class="CTitle">
            <td>菜单名称</td>
            <td>显示顺序</td>
            <td>操作</td>
        </tr>
  <%
	  using (DataTable dt = DataFactory.GetDataTable("select  * from KS_WeixinMenu where ParentID=0  and  TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+" order by orderid"))
	  {
		  if (dt.Rows.Count > 0)
		  {
			  foreach (DataRow dr in dt.Rows)
			  {
				  KSCMS.W(" <tr><td style=\"font-weight: bold;\">" + dr["ClassName"].ToString() + "</td><td align=\"center\"> " + dr["OrderID"].ToString() + "</td>" +
							  "  <td align=\"center\">" +
								  "  <a href=\"" + BasicField.GetUserUrl("wx/wx_menuList", "frame-no,action-ParentDown,menuid-" + dr["id"].ToString() + "") + "\">" +
									   " <i class=\"fa fa-arrow-down\" style=\"color: #2694D2;\">" +
									   " </i> </a> <a href=\"" + BasicField.GetUserUrl("wx/wx_MenuList", "frame-no,action-ParentUp,menuid-" + dr["id"].ToString() + "") + "\">" +
									   " <i class=\"fa fa-arrow-up\" style=\"color: #CE9738;\"></i> </a></td> </tr>");
				  using (DataTable dts = DataFactory.GetDataTable("select * from KS_WeixinMenu where ParentID=" + dr["id"].ToString() + "  and  TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+" order by orderid"))
				  {
					  if (dts.Rows.Count > 0)
					  {
						  foreach (DataRow drs in dts.Rows)
						  {
							  KSCMS.W(" <tr> <td style=\"padding-left: 30px;\"><img src=\"/admin/weixin/images/menu.png\" style=\"margin: 5px;\" />" + drs["ClassName"].ToString() + "</td>" +
								  "<td align=\"center\">" + drs["OrderID"].ToString() + "</td>" +
								"<td align=\"center\"> <a href=\"" + BasicField.GetUserUrl("wx/wx_menuList", "frame-no,action-ChildDown,menuid-" + dr["id"].ToString() + ",parentid-" + dr["ParentID"].ToString() + "") + "\">" +
										"<i class=\"fa fa-arrow-down\" style=\"color: #2694D2;\"></i></a>" +
								   " <a href=\"" + BasicField.GetUserUrl("wx/wx_menuList", "frame-no,action-ChildUp,menuid-" + dr["id"].ToString() + ",parentid-" + dr["ParentID"].ToString() + "") + "\">" +
									  "<i class=\"fa fa-arrow-up\" style=\"color: #CE9738;\"> </i></a> </td> </tr>");
						  }
					  }
				  }
			  }
		  }
		  else
		  {
			  KSCMS.W("<tr width=\"100%\"> <td class=\"empty\" colspan=\"7\">你还没有菜单信息 </td></tr>");
		  }
	  }
	  
	  
	   %>
	     </table>
           
        </div>
        <br />
        <div class="openKey" style=" font-weight:normal;">
            排序完，请到自定义菜单的页面点击同步菜单
        </div>


 </form>
