<%@ page contentType="text/html;charset=gbk" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.Collection" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="net.sz600.basicdata.*" %>
<%@ page import="net.sz600.tool.SzConvert" %>
<%@ page import="net.sz600.database.SzPageInfo" %>
<%@ page import="net.sz600.system.SzProperties" %>
<%@ page import="net.sz600.system.SzLevelNumber" %>
<%@ page import="net.sz600.system.SzPageHelp" %>
<%@ page import="net.sz600.flow.*" %>
<%@ page import="net.sz600.formEngine2.*" %>
<%@ page import="net.sz600.weboffice.*" %>
<%@ include file="/sessionInfo.jsp"%>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<link href="<%=strSkin_SESSION%>/css2007.css" rel="stylesheet" type="text/css">
</head>
<%
   	String sOPERATOR = SzConvert.toSpaceStr(request.getParameter("OPERATOR"));
	String type = SzConvert.toSpaceStr(request.getParameter("type"));
    String name = SzConvert.toSpaceStr(request.getParameter("name"));
    String index = SzConvert.toZeroStr(request.getParameter("index"));
    
    OfficeControl officeControl = new OfficeControl();
	OfficePrintTemp officePrintTemp = new OfficePrintTemp(0);
	OfficeTemplate officeTemplate = null; 
	OfficeBlock officeBlock = null;
	OfficeTag officeTag = null;
	OfficeBlock block = null;
	Collection collBlocks = null;
	Iterator iterBlocks = null;
	Collection collTags = null;
	Iterator iterTags = null;
	
	if (!name.equals("") && !type.equals("")) {
		officeTemplate = officePrintTemp.getTemplate(name);	
		collBlocks = officeTemplate.getBlocks();
		if (collBlocks != null) {
			iterBlocks = collBlocks.iterator();
		}	
		if (!sOPERATOR.equals("newBlock")) {
			//根据type跟index取
			officeBlock = officeTemplate.getOfficeBlock(type, index);
		}
	}
	String blockName="";
	String table = "";
	String cond = "";
	String maxLine = "";
	String order = "";
	//String index = "";
	String tableIndex = "0";
	String rowIndex = "0";
	String relateType = "0";
	String relateID = "0";
	
	int iSize = 0;
	if (officeBlock != null) {
		blockName = SzConvert.toSpaceStr( officeBlock.getAttrs().get("name"));
		table = SzConvert.toSpaceStr(officeBlock.getAttrs().get("table"));
		cond = SzConvert.toSpaceStr(officeBlock.getAttrs().get("cond"));
		maxLine = SzConvert.toSpaceStr(officeBlock.getAttrs().get("maxLine"));
		order =  SzConvert.toSpaceStr(officeBlock.getAttrs().get("order"));
		index = SzConvert.toSpaceStr(officeBlock.getAttrs().get("index"));
		relateID = SzConvert.toSpaceStr(officeBlock.getAttrs().get("relateID"));
		relateType = SzConvert.toZeroStr(officeBlock.getAttrs().get("relateType"));
		tableIndex = SzConvert.toSpaceStr(officeBlock.getAttrs().get("tableIndex"));
		rowIndex = SzConvert.toSpaceStr(officeBlock.getAttrs().get("rowIndex"));
		collTags = officeBlock.getTags();
		if (collTags != null) {
			iSize = collTags.size();
			iterTags = collTags.iterator();
		}
	} else {
		blockName = type;
		maxLine = "0";
		index = officeControl.newIndex();
		if (blockName.equals("FJ")) {
			cond = "FJXX=";
		}
	}

%>
<!-----上面的工具栏------>
<%@ include file="/xml/formEngine2/title.jsp"%>
<table width="100%">
  <form name="formBar" method="post">
  <input type="hidden" name="OPERATOR" value="<%=sOPERATOR%>">
  <tr>
    <td>    
      <%=SzPageHelp.saveButton()%>
      <%=SzPageHelp.button("返回","javascript:document.location.href='index.jsp';") %>
      <%=SzPageHelp.button("删除","javascript:DeleteCheck();") %>
      <%=SzPageHelp.button("增加block","javascript:NewCheck();") %>
    </td>
  </tr>
  </form>
</table>
<!-----信息列表----->
<form name="formUpdate" method="post">
<!-- 列出已经有的blocks -->
<%
    String tempName = "";
	String tempIndex = "0";
	OfficeBlock officeBlockT = null;
	while (iterBlocks != null && iterBlocks.hasNext()) {
		officeBlockT = (OfficeBlock)iterBlocks.next();
		
		tempName = officeBlockT.getAttrs().get("name");
		tempIndex = officeBlockT.getAttrs().get("index");

%>
<a href="updateBlock.jsp?OPERATOR=updateBlock&type=<%=tempName%>&name=<%=name%>&index=<%=tempIndex %>">[<%=tempName%>]<%=tempIndex%></a>
<%		
	}
		
%>
<table width="100%" border="1" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableClass" cellpadding="1" cellspacing="0">
  
  <input type="hidden" name="OPERATOR" value="<%=sOPERATOR%>">
  <input type="hidden" name="name" value="<%=name%>">
  <input type="hidden" name="moduleName1" value="">
  <input type="hidden" name="formName1" value="">  
  <tr>
    <td width="150">类型</td>
    <td><input type="text" class="txtClass60" name="blockName" value="<%=blockName%>" readonly>
        序号&nbsp;
       <input type="text" class="txtClass60" name="index" value="<%=index%>" readonly>
    </td>
  </tr>  
  <% if (!blockName.equals("QZ") && !blockName.equals("MX")) { %>  
  <tr>
    <td>SQL表名</td>
    <td><input type="text" class="txtClass2" name="table" value="<%=table%>"></td>
  </tr>
  <% } else  if (blockName.equals("MX")) { %>
  <tr>
    <td>SQL表名</td>relateType
    <td><input type="text" class="txtClass" name="table" value="<%=table%>">
    	或表单的关联ID
        <select  name="relateType" style="width:50;">
        <option value="">====</option>
        <option value="0" <% if (relateType.equals("0")) { %>selected <% } %> >下级</option>
        <option value="5" <% if (relateType.equals("5")) { %>selected <% } %> >兄弟</option>
        </select>    	
    	<input type="text" class="txtClass" name="relateID" value="<%=relateID%>">
    </td>
  </tr>  
  <% } %>
    
  <% if (!blockName.equals("QZ")) { %>   
  <tr>
    <td>SQL条件</td>
    <td><input type="text" class="txtClass2" name="cond" value="<%=cond%>">
    	  	  <input type="button" value="选择" class="btnClassShort" OnClick="javascript:SelectWin('field', 'formUpdate.cond', '','','formUpdate.table')">
    </td>
  </tr>
  <% } %>
  <% if (blockName.equals("MX")) { %>
  <tr>
    <td>SQL排序</td>
    <td><input type="text" class="txtClass2" name="order" value="<%=order%>">
    </td>
  </tr> 
  <tr>
    <td>word中明细表的序号</td>
    <td><input type="text" class="txtClass" name="tableIndex" value="<%=tableIndex%>">
    	[序号从1开始]
    </td>
  </tr>
  <tr>
    <td>明细表中明细行的序号</td>
    <td><input type="text" class="txtClass" name="rowIndex" value="<%=rowIndex%>">
    	[序号从0开始]
    </td>
  </tr>  
  <tr>
    <td>明细换页行数</td>
    <td><input type="text" class="txtClass" name="maxLine" value="<%=maxLine%>">
    	[应用于明细表的打印换页行数，0为不限制]
    </td>
  </tr>      
  <% } %>
</table>  
<table width="100%" border="1" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableClass" cellpadding="1" cellspacing="0">
  <tr class="trClassTitle">
    <% if (blockName.equals("GW")) { %>
    <td>标签名称</td>    
    <td width="130" nowrap>字段名称</td>    
    <td>多行</td>
    <td>说明</td>
    <td>取值</td>
    <td>关联ID</td>
    <td>运算</td>       
    <td>关联表名/运算值</td>
    <td>关联字段/分割/成立值</td>
    <td>关联条件/不成立值</td>    
    <td>截取</td>
    <td>长度</td>
    <td>空格</td>
    <% } else if (blockName.equals("FJ")) { %>
    <td>标签名称</td>    
    <td>字段名称</td>    
    <td>多行</td>
    <td>说明</td>    
    <% } else if (blockName.equals("QZ")) { %>
    <td>标签名称</td>
    <td>审批步骤名称</td>
    <td>说明</td>
    <% } else if (blockName.equals("MX")) { %>
    <td>标签名称</td>    
    <td>字段名称</td>    
    <td>说明</td>
    <td>取值</td>
    <td>合值标签</td>     
    <td>关联表名</td>
    <td>关联字段</td>
    <td>关联条件</td>
    <td>截取</td>
    <td>长度</td>
    <td>空格</td>    
    <% } else if (blockName.equals("TJ")) { %>
    <td>字段名称</td>
    <td>取值</td>       
    <td>关联ID</td>    
    <td>运算符号</td>    
    <td>运算值</td>        
    <td>说明</td>    
    <% } %>
  </tr>
<%
	
	int i = 0;
	iSize = iSize + 10;
	String tagName = "";
	String field = "";
	String valueType = "";
	String operType = "";	//运算符号
	String mutil = "";
	String desc = "";
	String total = "";
	String relateID2 = "";		//关联ID
	String relateTable = ""; 	//关联表名或者是运算的值
	String relateField = "";	//关联字段或者是运算成立后的值
	String relateCond  = "";	//关联条件或者是运算成立后的值
	String truncFirst = "0";
	String truncLen = "0";
	String space = "";
	String sMessage = "";
	String step = "";
	for (i = 0; i<iSize; i++) {
		tagName = "";
		field = "";
		valueType = "0";
		operType = "0";
		mutil = "0";
		if (blockName.equals("FJ")) { 
			mutil = "1";	
			field = "FJMC";
		} else if (blockName.equals("TJ")) { 
			tagName = " = ";
		}
		desc = "";
		relateID2 = "";
		relateTable = ""; 
		relateField = "";
		relateCond  = "";
		truncFirst = "0";
		truncLen = "0";
		space = "0";
		step = "";
		if (iterTags != null && iterTags.hasNext()) {
			officeTag = (OfficeTag)iterTags.next();
			if (officeTag != null) {
				tagName = SzConvert.toSpaceStr(officeTag.getAttrs().get("name"));
				field = SzConvert.toSpaceStr(officeTag.getAttrs().get("field"));
				valueType = SzConvert.toZeroStr(officeTag.getAttrs().get("valueType"));
				operType = SzConvert.toZeroStr(officeTag.getAttrs().get("operType"));
				mutil = SzConvert.toSpaceStr(officeTag.getAttrs().get("mutil"));
				desc = SzConvert.toSpaceStr(officeTag.getAttrs().get("desc"));
				total = SzConvert.toSpaceStr(officeTag.getAttrs().get("total"));
				relateID2 = SzConvert.toSpaceStr(officeTag.getAttrs().get("relateID2"));
				relateTable = SzConvert.toSpaceStr(officeTag.getAttrs().get("relateTable"));
				relateField = SzConvert.toSpaceStr(officeTag.getAttrs().get("relateField"));
				relateCond = SzConvert.toSpaceStr(officeTag.getAttrs().get("relateCond"));
				truncFirst = SzConvert.toZeroStr(officeTag.getAttrs().get("truncFirst"));	
				truncLen = SzConvert.toZeroStr(officeTag.getAttrs().get("truncLen"));	
				space = SzConvert.toZeroStr(officeTag.getAttrs().get("space"));
				
				step = SzConvert.toZeroStr(officeTag.getAttrs().get("step"));
				//=========================================================
				//****说明:把关联ID换位置;把运算符号换位置.				
				if (valueType.equals("6") || valueType.equals("7")) {
					operType = valueType;
					valueType = "0";
					sMessage = "需要重新保存";
				}
				if (valueType.equals("4") || valueType.equals("5")) {
					if (relateID2.equals("") && !relateTable.equals("")) {
						relateID2 = relateTable;
						sMessage = "需要重新保存";
					}
				}
				//==========================================================
			}
		}
%>
	<tr>
    <% if (blockName.equals("GW")) { %>
	  <td><input type="text" class="txtClass100" name="tagName<%=i %>" value="<%=tagName%>"></td>
	  <td><input type="text" class="txtClass80" name="field<%=i %>" value="<%=field%>">
	  <input type="button" value="选择" class="btnClassShort" OnClick="javascript:SelectWin('field', 'formUpdate.field<%=i%>', 'formUpdate.desc<%=i%>','formUpdate.tagName<%=i%>','formUpdate.table')">
	  </td>
	  <td><input type="checkbox" value="1" name="mutil<%=i %>" <% if (mutil.equals("1")) { %>checked <% } %> >
	  </td>
	  <td><input type="text" class="txtClass100" name="desc<%=i %>" value="<%=desc%>"></td>
	  <td>
        <select  name="valueType<%=i%>" style="width:50;">
        <option value="">====</option>
        <option value="0" <% if (valueType.equals("0")) { %>selected <% } %> >字段</option>
        <option value="1" <% if (valueType.equals("1")) { %>selected <% } %> >常量</option>
        <option value="2" <% if (valueType.equals("2")) { %>selected <% } %> >关联</option>
        <option value="3" <% if (valueType.equals("3")) { %>selected <% } %> >上级</option>
        <option value="4" <% if (valueType.equals("4")) { %>selected <% } %> >下级</option>
        <option value="5" <% if (valueType.equals("5")) { %>selected <% } %> >兄弟</option>
        </select>	  
	  </td> 
	  <td><input type="text" class="txtClass100" name="relateIDB<%=i %>" value="<%=relateID2%>"></td>
	  <td>
        <select  name="operType<%=i%>" style="width:50;">
        <option value="">====</option>
        <option value="6" <% if (operType.equals("6")) { %>selected <% } %> >等于</option>
        <option value="7" <% if (operType.equals("7")) { %>selected <% } %> >包含</option>        
        </select>	  
	  </td>	   
	  <td><input type="text" class="txtClass100" name="relateTable<%=i %>" value="<%=relateTable%>"></td>
	  <td><input type="text" class="txtClass80" name="relateField<%=i %>" value="<%=relateField%>"></td>
	  <td><input type="text" class="txtClass100" name="relateCond<%=i %>" value="<%=relateCond%>"></td>
	  <td><input type="text" class="txtClassPage" name="truncFirst<%=i %>" value="<%=truncFirst%>"></td>
	  <td><input type="text" class="txtClassPage" name="truncLen<%=i %>" value="<%=truncLen%>"></td>
	  <td><input type="text" class="txtClassPage" name="space<%=i %>" value="<%=space%>"></td>
    <% } else if (blockName.equals("FJ")) { %>
	  <td><input type="text" class="txtClass" name="tagName<%=i %>" value="<%=tagName%>"></td>
	  <td><input type="text" class="txtClass100" name="field<%=i %>" value="<%=field%>">
	  </td>
	  <td><input type="checkbox" value="1" name="mutil<%=i %>" <% if (mutil.equals("1")) { %>checked <% } %> >
	  </td>
	  <td><input type="text" class="txtClass" name="desc<%=i %>" value="<%=desc%>"></td>
    <% } else if (blockName.equals("QZ")) { %>
	  <td><input type="text" class="txtClass" name="tagName<%=i %>" value="<%=tagName%>"></td>
	  <td><input type="text" class="txtClass" name="step<%=i %>" value="<%=step%>"></td>
	  <td><input type="text" class="txtClass" name="desc<%=i %>" value="<%=desc%>"></td>
    <% } else if (blockName.equals("MX")) { %>
	  <td><input type="text" class="txtClass100" name="tagName<%=i %>" value="<%=tagName%>"></td>
	  <td><input type="text" class="txtClass80" name="field<%=i %>" value="<%=field%>">
	  <input type="button" value="选择" class="btnClassShort" OnClick="javascript:SelectWin('field', 'formUpdate.field<%=i%>', 'formUpdate.desc<%=i%>','formUpdate.tagName<%=i%>','formUpdate.table')">
	  </td>
	  <td><input type="text" class="txtClass100" name="desc<%=i %>" value="<%=desc%>"></td>
	  <td>
        <select  name="valueType<%=i%>" style="width:50;">
        <option value="">====</option>
        <option value="0" <% if (valueType.equals("0")) { %>selected <% } %> >字段</option>
        <option value="2" <% if (valueType.equals("2")) { %>selected <% } %> >关联</option>
        <option value="6" <% if (valueType.equals("6")) { %>selected <% } %> >序号</option>
        </select>
  	  </td>	  
	  <td><input type="text" class="txtClass" name="total<%=i %>" value="<%=total%>"></td>	  
	  <td><input type="text" class="txtClass100" name="relateTable<%=i %>" value="<%=relateTable%>"></td>
	  <td><input type="text" class="txtClass80" name="relateField<%=i %>" value="<%=relateField%>"></td>
	  <td><input type="text" class="txtClass100" name="relateCond<%=i %>" value="<%=relateCond%>"></td>
	  <td><input type="text" class="txtClassPage" name="truncFirst<%=i %>" value="<%=truncFirst%>"></td>
	  <td><input type="text" class="txtClassPage" name="truncLen<%=i %>" value="<%=truncLen%>"></td>
	  <td><input type="text" class="txtClassPage" name="space<%=i %>" value="<%=space%>"></td>	  
    <% } else if (blockName.equals("TJ")) { %>
	  <td><input type="text" class="txtClass" name="field<%=i %>" value="<%=field%>">
	  	  <input type="button" value="选择" class="btnClassShort" OnClick="javascript:SelectWin('field', 'formUpdate.field<%=i%>', 'formUpdate.desc<%=i%>','','formUpdate.table')">
	  </td>
	  <td>
        <select  name="mutil<%=i%>" style="width:50;">
        <option value="">====</option>
        <option value="0" <% if (mutil.equals("0")) { %>selected <% } %> >字段</option>
        <option value="3" <% if (mutil.equals("3")) { %>selected <% } %> >上级</option>
        <option value="4" <% if (mutil.equals("4")) { %>selected <% } %> >下级</option>
        <option value="5" <% if (mutil.equals("5")) { %>selected <% } %> >兄弟</option>
        </select>	  
	  </td>  
	  <td><input type="text" class="txtClass100" name="relateTable<%=i %>" value="<%=relateTable%>"></td>	  
	  <td><input type="text" class="txtClass100" name="tagName<%=i %>" value="<%=tagName%>"></td>
	  <td><input type="text" class="txtClass100" name="valueType<%=i %>" value="<%=valueType%>"></td>	
	  <td><input type="text" class="txtClass" name="desc<%=i %>" value="<%=desc%>"></td>	    
    <% } %>    	
	</tr>
<%		
	}
%>
    <input type="hidden" name="tagSize" value="<%=i %>">
</table>  
<div align="center"><font color="red"><b><%=sMessage%></b></font></div>
</form>  
<!-----下面的工具栏------>
<table width="100%">
  <form name="formPage" method="post">
	<input type="hidden" name="OPERATOR" value="<%=sOPERATOR%>">
  </form>
</table>
<script language="javascript" src="../../../share/js/commonTool.js"></script>
<script language="javascript">

  function FormSave(formName, url) {
    //检查是否可以保存
    if (!notSpace(formName.blockName, "类型")) {
      return false;
    }  
    if (!notNum(formName.index, "序号")) {
      return false;
    }        
    formName.action = url;
    formName.submit();
  }
  
  function DeleteCheck() {
  	if (confirm("删除将不能恢复,是否继续?")) {
  		var name= formUpdate.name.value;
  		var blockName= formUpdate.blockName.value;
  		var index = formUpdate.index.value;
  		document.location.href="control.jsp?OPERATOR=deleteBlock&name=" + name + "&blockName=" + blockName+ "&index=" +index; 
  	}
  }
  
  function NewCheck() {
    var type = formUpdate.blockName.value;
    var name = formUpdate.name.value;
    document.location.href="updateBlock.jsp?OPERATOR=newBlock&type=" + type + "&name=" + name;
  }
  
  
  function SelectWin(vType,  vObject, vDesc, vTagName, vTableObject) {
    var moduleName1 = formUpdate.moduleName1.value;
    var formName1 = formUpdate.formName1.value; 
    var url = "field_select.jsp?type=" + vType 
            + "&object=" + vObject 
            + "&desc=" + vDesc
            + "&tagName=" + vTagName
            + "&tableobject=" + vTableObject
            + "&moduleName1=" + moduleName1 + "&formName1=" + formName1;
    window.showModalDialog(url,window,"status=no;dialogWidth=200px;dialogHeight=370px");  
  }  
</script>
