<%@ 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.*" %>
<%@ 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 sType  = SzConvert.toZeroStr(request.getParameter("type"));	//关联的ID
	String sPKEY  = SzConvert.toZeroStr(request.getParameter("PKEY"));	//主表单信息ID
	//String sPFORM = SzConvert.toZeroStr(request.getParameter("PFORM"));	//主表单ID
	//上上级
	String sType2  = SzConvert.toZeroStr(request.getParameter("type2"));	//关联的ID
	String sPKEY2  = SzConvert.toZeroStr(request.getParameter("PKEY2"));	//主表单信息ID
	String sPFORM2 = SzConvert.toZeroStr(request.getParameter("PFORM2"));	//主表单ID
	
	String sFormID = SzConvert.toZeroStr(request.getParameter("FormID"));
	
	String sQueryType  = SzConvert.toSpaceStr(request.getParameter("queryType"));	//查询的类型
	if (sQueryType.equals("")) {
		sQueryType = "-1";
	}	
    //找出关联的信息
    BdRelateForm bdRelateForm = new BdRelateForm();
	boolean hasFlow = bdRelateForm.hasFlow(sType);
    String sPFORM = bdRelateForm.getPFORM(sType);
    
    /////////////////////////////////////////流程相关
    LcFlowControl lcFlowControl = new LcFlowControl();	

    //获取已经办理的信息ID
    String str_InfoID = lcFlowControl.getInfoID(sType, SzConvert.toInt(sFormID), iDLID_SESSION, "manage", sQueryType); 
    
    String addButton = bdRelateForm.addButton(sType);
    String selectButton = bdRelateForm.selectButton(sType, sFormID);	//选择一个

    String sPage = SzConvert.toZeroStr(request.getParameter("page"));
    String sSearch = SzConvert.strWriteData(request.getParameter("txtSearch"));
    String sSearchName = SzConvert.strWriteData(request.getParameter("txtSearchName"));
    String sCond = "1=1";
    //读取数据
    String sSearchCond = SzConvert.strWriteData(request.getParameter("searchCond"));
    String sSortField = SzConvert.toSpaceStr(request.getParameter("txtSortField")); 
    int iSortOrder    = Integer.parseInt(SzConvert.toZeroStr(request.getParameter("txtSortOrder"))); 

    //System.out.println("sSearchCond==" + sSearchCond);
	BdRelateValue bdRelateValue = new BdRelateValue(sType, SzConvert.toInt(sPKEY), sFormID, 0,  sSortField, iSortOrder, 1, 0);
    String sKeyField = bdRelateValue.getKeyField();	
	String strDefaultCond = bdRelateValue.getDefaultCond();
	if (!strDefaultCond.equals("")) {
		sCond += " AND " + strDefaultCond;
	}
	sCond += " AND " + sKeyField + " IN (" + str_InfoID + ")";
	
    if (sSortField.equals("")) { 
        sSortField = bdRelateValue.getSortField();
        iSortOrder = SzProperties.I_SortOrder_DESC;
      } 	
	int iCurrentPage = 1;    
    try {
        iCurrentPage =  Integer.parseInt(sPage);
    }catch(Exception e) {
    }

    Collection coll = bdRelateValue.getArrayList(sCond, sSortField, iSortOrder, iCurrentPage, sSearch); 

    Iterator iter = null;
    if (coll != null) {
    	iter = coll.iterator();
    } else {
    	out.println("读取不到数据");
    	return;
    }
    SzPageInfo pageInfo = bdRelateValue.getPageInfo();
    int iPageCount = pageInfo.getPageCount();
    int iRecordCount = pageInfo.getRecordCount();
    iCurrentPage = pageInfo.getCurrentPage();
    int i = 0;
    
    String strBackUrl = "";
    if (sType2.equals("0")) {
    	strBackUrl = "index.jsp?FormID=" + sPFORM + "";
    } else {
    	//要能找到上上级的
    	strBackUrl = "relate_index.jsp?type=" + sType2 + "&PFORM=" + sPFORM2 + "&PKEY=" + sPKEY2;
    }
 
%>
<!-----上面的工具栏------>
<%@ include file="title.jsp"%>
<table width="100%">
  <form name="formBar" method="post">
  <input type="hidden" name="type" value="<%=sType%>">
  <input type="hidden" name="PKEY" value="<%=sPKEY%>">  
  <input type="hidden" name="PFORM" value="<%=sPFORM%>">
  <input type="hidden" name="type2" value="<%=sType2%>">
  <input type="hidden" name="PKEY2" value="<%=sPKEY2%>">  
  <input type="hidden" name="PFORM2" value="<%=sPFORM2%>">      
  <tr>
    <td><b><%=bdRelateForm.getRelateDesc(sType) %></b>
    	<%=selectButton%>
      <%
      	String s_id[] = {"-1","0","1","2"};
      	String s_name[] = {"全部","办理中","否决","办结"};
      	out.println(SzPageHelp.radioSubmit("queryType", s_id, s_name, sQueryType));
      %>    	
    	<% out.println( SzPageHelp.button("返回","javascript:document.location.href='" + strBackUrl + "';") );%>
    </td>
  </tr>
  </form>
</table>
<!-----信息列表----->
<table width="100%" border="1" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableClass" cellpadding="1" cellspacing="0">
  <form name="formList" method="post">
  <input type="hidden" name="page" value="<%=iCurrentPage%>">
  <input type="hidden" name="txtSearch" value="<%=sSearch%>">
  <input type="hidden" name="txtSortField" value="<%=sSortField%>"> 
  <input type="hidden" name="txtSortOrder" value="<%=iSortOrder%>"> 
  <input type="hidden" name="type" value="<%=sType%>">
  <input type="hidden" name="PKEY" value="<%=sPKEY%>">  
  <input type="hidden" name="PFORM" value="<%=sPFORM%>">   
  <input type="hidden" name="type2" value="<%=sType2%>">
  <input type="hidden" name="PKEY2" value="<%=sPKEY2%>">  
  <input type="hidden" name="PFORM2" value="<%=sPFORM2%>">     
  <tr class="trClassTitle">
    <td width="5"><input type="checkbox" name="chkCheckAll" OnClick="javascript:CheckAll(formList, chkCheckAll, 'chkId');"></td>
	<%=bdRelateValue.printTableTitle()%>     
	<% if (hasFlow) { out.println("<td>总状态</td>"); } %>  	   	
    <td>操作</td>
  </tr>
<%
    String value_str[] = null;
    int iKeyValue = 0;
    int iListFieldLen = 0;
    if (hasFlow) {
    	iListFieldLen = 1 + bdRelateValue.getListFieldLen();
    } else {
    	iListFieldLen = bdRelateValue.getListFieldLen() ;
    }    
    int iKeyIndex = bdRelateValue.getKeyIndex();
    int iFormID = 0;
    
    //找出关联的信息
    ArrayList listRelate = null;
    Iterator iterRelate = null;
    BdRelateFormInfo bdRelateFormInfo = null;    
    String sRelateInfo = "";
    //取得表单的信息
    String moduleName = "";
    String formName = "";
	//根据表单取得moduleName, formName
	LcForm lcForm = new LcForm();
	LcFormInfo lcFormInfo = null;     
	boolean hasSend = false;

    while (iter != null && iter.hasNext()) {
    	value_str = (String[])iter.next();
    	if (value_str == null) {
    		continue;
    	}
    	iFormID = Integer.parseInt(value_str[0]);
    	iKeyValue = Integer.parseInt(value_str[iKeyIndex]);
        hasSend = lcFlowControl.hasSend(iFormID, iKeyValue);
        if (iFormID > 0) {
        	lcFormInfo = (LcFormInfo)lcForm.getInfoByKey(iFormID);    
        	if (lcFormInfo != null) {
        		moduleName = lcFormInfo.getBDLJ();
        		formName = lcFormInfo.getBDBY1();  
        	}
        	listRelate = bdRelateForm.getRelateList(moduleName, formName);
        	//sRelateInfo = bdRelateForm.relateButton(iFormID, listRelate, iKeyValue, sType, sPFORM, sPKEY);
        }    	
    	i++;
%>
  <tr  onMouseOver="javascript:this.className='trMouseOver'" onMouseOut="javascript:this.className='trMouseOut'">
    <td width="5" title="<%=i%>">
      <input type="checkbox" name="chkId" id="chkId" value="<%=iKeyValue%>">
    </td>
    <%=bdRelateValue.printTableValue(value_str)%>    
    <% if (hasFlow) { out.println("<td>" + lcFlowControl.getFlowStateName(iFormID, iKeyValue) + "&nbsp;</td>"); } %>      
    <td>
      <a href="javascript:FormSubmit(formPage,'relate_update.jsp?OPERATOR=detail&KEY=<%=iKeyValue%>&FormID=<%=iFormID%>');">详情</a>             
    </td>
  </tr>
<%
    }
    out.println(SzPageHelp.outIndexTableTr(i, SzProperties.I_PageSize, iListFieldLen));
%>
    
  </form>  
</table>   
<!-----下面的工具栏------>
<table width="100%">
  <form name="formPage" method="post">
  <input type="hidden" name="page" value="<%=iCurrentPage%>">
  <input type="hidden" name="txtSearch" value="<%=sSearch%>">
  <input type="hidden" name="txtSortField" value="<%=sSortField%>"> 
  <input type="hidden" name="txtSortOrder" value="<%=iSortOrder%>"> 
  <input type="hidden" name="type" value="<%=sType%>">
  <input type="hidden" name="PKEY" value="<%=sPKEY%>">  
  <input type="hidden" name="PFORM" value="<%=sPFORM%>">  
  <input type="hidden" name="type2" value="<%=sType2%>">
  <input type="hidden" name="PKEY2" value="<%=sPKEY2%>">  
  <input type="hidden" name="PFORM2" value="<%=sPFORM2%>">      
  <tr>
    <td align="right">
      <%@ include file="/share/page/page_include.jsp"%>
    </td>
  </tr>
  </form>
</table> 
<form name="formHidden" method="post">
  <input type="hidden" name="page" value="<%=iCurrentPage%>">
  <input type="hidden" name="txtSearch" value="<%=sSearch%>">
  <input type="hidden" name="txtSortField" value="<%=sSortField%>"> 
  <input type="hidden" name="txtSortOrder" value="<%=iSortOrder%>"> 
  <input type="hidden" name="type" value="<%=sType%>">
  <input type="hidden" name="PKEY" value="<%=sPKEY%>">  
  <input type="hidden" name="PFORM" value="<%=sPFORM%>">   
  <input type="hidden" name="type2" value="<%=sType2%>">
  <input type="hidden" name="PKEY2" value="<%=sPKEY2%>">  
  <input type="hidden" name="PFORM2" value="<%=sPFORM2%>">    
</form>
<script language="javascript" src="../../share/js/commonTool.js"></script>
<script language="javascript">
  function Add() {
  	//传递当前formID, 主表单的关联ID,主表单的信息ID
  	var PFORM = formBar.PFORM.value;
  	var PKEY = formBar.PKEY.value;
  	var type = formBar.type.value;
  	var FormID = formBar.relateForm.value;
  	if (FormID != "" && FormID != "0") {
  		var vUrl = "relate_update.jsp?OPERATOR=add&PKEY=" + PKEY + "&type=" + type + "&FormID=" + FormID + "&PFORM=" + PFORM;
  		//alert(vUrl);
  		document.location.href = vUrl;
  	}
  }
  
  //只列出其中一种附属表单
  function Select() {
  	//传递当前formID, 主表单的关联ID,主表单的信息ID
  	var FormID = formBar.relateFormSelect.value;
  	
  	var vUrl = "relate_manage.jsp?FormID=" + FormID;
	formHidden.action = vUrl;
	formHidden.submit();  	
  }  
 
  //进入选择窗口,选择已经存在的数据,并且建立关联
  function RelateWin() {
  	//传递当前formID, 主表单的关联ID,主表单的信息ID
  	var PFORM = formBar.PFORM.value;
  	var PKEY = formBar.PKEY.value;
  	var type = formBar.type.value;
  	var vUrl = "relate_select.jsp?PKEY=" + PKEY + "&type=" + type + "&PFORM=" + PFORM;
	window.showModalDialog(vUrl,window,"status=no;dialogWidth=680px;dialogHeight=580px");      
  }  
 
</script>
