<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Admin/AdminMain.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
	<link href="../../Scripts/flora/flora.all.css" rel="stylesheet" type="text/css" />
	<script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
	<script src="../../Scripts/ui.core.js" type="text/javascript"></script>
	<script src="../../Scripts/ui.tabs.js" type="text/javascript"></script>
	<script src="../../Scripts/Admin.js" type="text/javascript"></script>
	<script src="../../Scripts/jquery.loading.js" type="text/javascript"></script>
	<script src="../../Scripts/category.js" type="text/javascript"></script>
	<script src="../../Scripts/jquery.mask.js" type="text/javascript"></script>
	<script type="text/javascript">
		$(function () { $('#tables > ul').tabs(); });
		function checkForm() {
			var result = true;
			$("input[oldvalue]").each(function () {
				if ($.trim($(this).val()) == "") {
					alert("字段名称必须填写。");
					$(this).val("");
					$(this).focus();
					result = false;
				}
			});
			return result;
		}
		//删除
		function syncDeleteItem(e) {
			if (confirm("确认删除此字段？")) {
				var name = $(e).attr("name");
				var value = $(e).val();
				var table = name.replace(/(\w+)(\d+)(\w+)/, "$1");
				$.mask();
				$.post("DeleteField", { "table": table, "field": value }, function (data) {
					$(e).parent().remove();
					alert(data);
					$.unmask();
				});
			}
		}
		//更改
		function syncChangeField(e) {
			var name = $(e).attr("name");
			var value=$(e).val();
			var oldvalue = $(e).attr("oldvalue");
			var table = name.replace(/(\w+)(\d+)(\w+)/, "$1");
			if ($.trim(value) == "") {
				alert("字段名称必须填写。");
				$(e).val("");
				e.focus();
				return;
			}
			if (value==oldvalue) {
				return;
			}
			$.mask();
			$.post("UpdateField", { "table": table, "field": value, "oldfield": oldvalue }, function (data) {
				if (!data.success) {
					$(e).val(oldvalue);
					$(e).focus();
				}
				alert(data.message);
				$.unmask();
			}, "json");
		}
	</script>
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	自定义数据
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
	<form method="post" id="formSave" action="" onsubmit="return checkForm()">
	<div id="tables">
		<ul>
<%
	var tables = CustomFields.Context.Elements();
	var i=0;
	foreach (var table in tables)
	{
		i++;
%>
			<li><a href="#fragment-<%= i %>"><span><%= table.Attribute("text").Value%></span></a></li>
<%			
	}
%>
		</ul>
<%
	i=0;
	foreach (var table in tables)
	{
		i++;
%>
		<div id="fragment-<%= i %>">
		<%
		var j = 0;
		foreach (var node in table.Elements())
		{
			j++;
		%>
		<div>
			字段名称：<input type="text" onblur="syncChangeField(this)" name="<%= table.Attribute("name").Value %><%= j %>name" value="<%= node.Attribute("name").Value %>" oldvalue="<%= node.Attribute("name").Value %>" />
			默认值：<input type="text" name="<%= table.Attribute("name").Value %><%= j %>defaultvalue" value="<%= node.Attribute("defaultvalue").Value%>" />
			<input type="button" value="删除" onclick="syncDeleteItem($('input[name=\'<%= table.Attribute("name").Value %><%= j %>name\']'))" />
		</div>
		<%
		}
		%>
			<fieldset>
			<legend>添加</legend>
				字段名称：<input type="text" name="<%= table.Attribute("name").Value %>0name" value="" />
				默认值：<input type="text" name="<%= table.Attribute("name").Value %>0defaultvalue" value="" />
			</fieldset>
			<br class="clearfloat" />
		</div>
<%			
	}
%>
		<br class="clearfloat" />
	</div>
	<p>
		<input type="submit" value="保 存" />
	</p>
	</form>
</asp:Content>

