<%@ page contentType="text/html;charset=UTF-8"%>
<%@page import="com.blueland.bpm.engine.config.model.*,java.util.*"%>
<%@ include file="/commons/taglibs.jsp"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<%@ include file="/commons/meta.jsp"%>
<base href="<%=basePath%>">
<title>系统全局分类表新增</title>
<script>
$(document).ready(function() {
	var data;
	var dataTypeRow = "<tr><td style='width:20%'>objectType</td><td><input class='form-control'  type='text' name='wf_objectType' id='wf_objectType' value='${businessDataObject.objectType}'/></td></tr>";
	$("#myTable1").append(dataTypeRow);
	try{
		data = '${businessDataObject.bizData}';
		//先按照xml格式去解析
        var fieldsXml = $.parseXML(data);
		var xmlRootName = fieldsXml.documentElement.nodeName;
        $("#wf_xmlRootName").val(xmlRootName);
		$(fieldsXml).find(xmlRootName + " >*").each(function(i){
				var fieldValue=$(this).text();
				if (fieldValue == null || typeof (fieldValue) == "undefined" || fieldValue == 'null') {
					fieldValue = "";
				}
				var html="<tr><td style='width:20%'>"+this.nodeName+"</td><td><input class='form-control' type='text' name='"+this.nodeName+"' id='"+this.nodeName+"' value='"+fieldValue+"'/></td></tr>";
				$("#myTable1").append(html);
        });
        $("#wf_dataType").val("xml");
	}catch(e){
		try{
			//xml格式解析数据出错，说明返回的数据不是xml格式，则按照json格式解析
			var obj = eval('(' + data + ')');
			for(var key in obj){ 
				var html="<tr><td style='width:10%'>"+key+"</td><td><input class='form-control' type='text' name='"+key+"' id='"+key+"' value='"+obj[key]+"'/></td></tr>";
				$("#myTable1").append(html);
		    }
		    $("#wf_dataType").val("json");
		}catch(e){
			//如果json格式解析报错，则说明也不是json格式，则用一个textarea展示字符串
			var html="<tr><td colspan='2'><textarea id='txtBizData' name='txtBizData' style='margin: 0px; width: 100%; height: 381px;'>"+data+"</textarea></td></tr>";
			$("#myTable1").append(html);
			$("#wf_dataType").val("txt");
		}
	}
	
	var fields;
	try{
		fields="${fields}";
		//alert(data);
        var fieldsXml = $.parseXML(fields);
		$(fieldsXml).find("fields >*").each(function(i){
				var fieldValue=$(this).text();
				if (fieldValue == null || typeof (fieldValue) == "undefined" || fieldValue == 'null') {
					fieldValue = "";
				}
				var html="<tr><td style='width:20%'>"+this.nodeName+"</td><td><input class='form-control' type='text' name='"+this.nodeName+"' id='"+this.nodeName+"' value='"+fieldValue+"'/></td></tr>";
				$("#myTable2").append(html);
        });
	}catch(e){alert("系统解析wf_fields数据失败,请您联系管理员排查问题！wf_fields["+fields+"]");}
});

	function update1() {
		var url = "${ctx}/taskMgr/updateTaskBusinessDataObject.do";
		$.post(url,$('#myform1').serialize(),
			function(result) {
				alert(result);
			});
	}
	
	function update2() {
		var url = "${ctx}/taskMgr/updateTaskFieldsData.do";
		$.post(url,$('#myform2').serialize(),
			function(result) {
				alert(result);
			});
	}
	
	
</script>
</head>
<body class="white-page">

	<form action="${ctx}/taskMgr/updateTaskBusinessDataObject.do" method="post" id="myform1">
					<div class="widget-box">
					<input type="hidden" id="wf_dataType" name="wf_dataType" />
					<input type="hidden" id="wf_curActInstId" name="wf_curActInstId" value="${wf_curActInstId}"></input>
					<input type="hidden" id="wf_xmlRootName" name="wf_xmlRootName" />
						<input type="button" class="btn btn-sm btn-primary"
							onclick="update1();" id="submitButton" value="保存业务对象" /> <input
							type="button" class="btn btn-sm btn-primary"
							value="关 闭" onclick="art.dialog.close();" />
							
							<div class="widget-body">
								<div>
									<table class='table table-bordered' border="0" cellspacing="0"
										cellpadding="0" id="myTable1">
										
									</table>
								</div>
							</div>
					</div>
	</form>
	
	<c:if test="${fields!=null}">
	<form action="${ctx}/taskMgr/updateTaskFieldsData.do" method="post" id="myform2">
					<div class="widget-box">
					<input type="hidden" id="wf_curActInstId" name="wf_curActInstId" value="${wf_curActInstId}"></input>
						<input type="button" class="btn btn-sm btn-primary"
							onclick="update2();" id="submitButton" value="保存Field" />
							
							<div class="widget-body">
								<div>
									<table class='table table-bordered' border="0" cellspacing="0"
										cellpadding="0" id="myTable2">
										
									</table>
								</div>
							</div>
					</div>
	</form>
	</c:if>
</body>
</html>