<%@ 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 readonly = SzConvert.toSpaceStr(request.getParameter("readonly"));
    //找出关联的信息
    BdRelateForm bdRelateForm = new BdRelateForm();
    BdRelateFormInfo bdRelateFormInfo = bdRelateForm.getRelateInfo(sType);
    boolean hasFlow = bdRelateForm.hasFlow(sType);   
    String popedom = "0";
    if (bdRelateFormInfo != null) {
    	popedom = SzConvert.toZeroStr(bdRelateFormInfo.getPopedom());
    }
    String addButton = bdRelateForm.addButton(sType);
    String selectButton = bdRelateForm.selectButton(sType, sFormID);	//选择一个
    if (!bdRelateForm.getFormID2().equals("0")) {
    	sFormID = bdRelateForm.getFormID2();
    }
    
    /////////////////////////////////////////流程相关
    LcFlowControl lcFlowControl = new LcFlowControl();	
	

    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 strDefaultCond = bdRelateValue.getDefaultCond();
	if (!strDefaultCond.equals("")) {
		sCond += " AND " + strDefaultCond;
	}
    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;
    }
    
    boolean hasImport = false;
    //System.out.println(sFormID);
    if (!sFormID.equals("0")) {
    	BdValueControl bdValueControl = new BdValueControl();
    	bdValueControl.setModuleInfo(SzConvert.toInt(sFormID));
    	hasImport = bdValueControl.getHasImport();
    }

    //2010-2-21
    BdPopedom bdPopedom = new BdPopedom();        
    String strDenyField = bdPopedom.getDenyField(SzConvert.toInt(sFormID), 0, iDLID_SESSION);    
%>
<!-----上面的工具栏------>
<%@ 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%>">
  <input type="hidden" name="readonly" value="<%=readonly%>">         
  <tr>
    <td><b><%=bdRelateForm.getRelateDesc(sType) %></b>
    	<%
    	if (readonly.equals("false")) {
    	  out.println(addButton);	
    	}
    	out.println(selectButton);    	
    	if (readonly.equals("false")) {
    		out.println( SzPageHelp.button("删除","javascript:FormSubmitConfirm(formList, 'relate_delete.jsp?OPERATOR=delete', '确定要删除？');") );
    		out.println( SzPageHelp.button("引用","javascript:RelateWin();") );
    		out.println(bdPopedom.setPopedomButton(SzConvert.toInt(sFormID), isBarManager_SESSION));
    		out.println(bdPopedom.listPopedomButton(SzConvert.toInt(sFormID), isBarManager_SESSION));
    	}
    	%>	
        <% if (hasImport && readonly.equals("false")) { %>      
         <input type="button" class="btnClass" value="excel导入" OnClick="javascript:ExcelImportWin('<%=sFormID%>','<%=sType%>','<%=sPFORM%>','<%=sPKEY%>');">
        <% } %>           	
    	<% 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%>">   
  <input type="hidden" name="readonly" value="<%=readonly%>">     
  <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;

    String sRelateInfo = "";
    //取得表单的信息
    String moduleName = "";
    String formName = "";
	//根据表单取得moduleName, formName
	LcForm lcForm = new LcForm();
	LcFormInfo lcFormInfo = null;     
	boolean hasSend = false;
	boolean canUpdate = false;

    boolean sameLogin = false;
    boolean sameDepart = false;
	
	String strDenyField2 = "";
    boolean readonly2 = 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]);
    	
    	sameLogin = bdRelateValue.sameLogin(iDLID_SESSION, value_str);        
    	
    	strDenyField2 = bdPopedom.getDenyField(iFormID, iKeyValue, iDLID_SESSION);
        hasSend = lcFlowControl.hasSend(iFormID, iKeyValue);
        canUpdate = true;
        canUpdate = bdRelateForm.canUpdate(iFormID, iKeyValue, popedom);   
        if (hasFlow && hasSend || !canUpdate) {
        	readonly2 = true;
        } else {
        	readonly2 = false;
        }           
        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, readonly2, true);
        }    	

     
    	i++;
    	
%>
  <tr  onMouseOver="javascript:this.className='trMouseOver'" onMouseOut="javascript:this.className='trMouseOut'">
    <td width="5" title="<%=i%>">
      <% if (hasFlow && hasSend || !canUpdate) {%> 
      <input type="checkbox" disabled>
      <% } else { %>
      <input type="checkbox" name="chkId" id="chkId" value="<%=iFormID%>,<%=iKeyValue%>">	  
      <% } %> 
    </td>
    <a href="javascript:FormSubmit(formPage,'relate_update.jsp?OPERATOR=detail&KEY=<%=iKeyValue%>&FormID=<%=iFormID%>');">
    <%=bdRelateValue.printTableValue(value_str, strDenyField, strDenyField2)%>    
    <% if (hasFlow) { out.println("<td>" + lcFlowControl.getFlowStateName(iFormID, iKeyValue) + "&nbsp;</td>"); } %>
	</a>
    <td>
      <a href="javascript:FormSubmit(formPage,'relate_update.jsp?OPERATOR=detail&KEY=<%=iKeyValue%>&FormID=<%=iFormID%>');">详情</a>
      <% if (!hasSend && canUpdate && readonly.equals("false")) { %>
      <a href="javascript:FormSubmit(formPage,'relate_update.jsp?OPERATOR=update&KEY=<%=iKeyValue%>&FormID=<%=iFormID%>');">修改</a>
      <% } %>
      <%= bdPopedom.setPopedomButton2(iFormID, isBarManager_SESSION, sameLogin, iKeyValue) %>
      <%= bdPopedom.listPopedomButton2(iFormID, isBarManager_SESSION, sameLogin, iKeyValue) %>      
      <%=sRelateInfo %>
    </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 ExcelImportWin(vFormID, vType, vPFORM, vPKEY) {
	var url ="../../share/page/excel_import_win.jsp?formID=" + vFormID + "&type=" + vType + "&PFORM=" + vPFORM + "&PKEY=" + vPKEY;
	window.showModalDialog(url,window,"status=no;dialogWidth=580px;dialogHeight=350px");
  }
</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;
  		formBar.action = vUrl;
  		formBar.submit();
  	}
  }
  
  //只列出其中一种附属表单
  function Select() {
  	//传递当前formID, 主表单的关联ID,主表单的信息ID
  	var FormID = formBar.relateFormSelect.value;
  	
  	var vUrl = "relate_index.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");      
  }  
 
  function SetPopedom() {
    var vChkNumber = NumberOfCheck(formList.chkId);
    var vChkValue  = ValueOfCheck(formList.chkId);
    var url = "popedom/update.jsp?FormID=<%=iFormID%>&ChkNumber=" + vChkNumber + "&ChkValue=" + vChkValue;
    window.showModalDialog(url,window,"status=no;dialogWidth=700px;dialogHeight=550px");	
  }
  
  function SetPopedom2(vKeyValue) {
    CheckCheckBox(formList.chkId, ";" + vKeyValue);
    var vChkNumber = "1";
    var vChkValue  = vKeyValue;
    var url = "popedom/update.jsp?FormID=<%=iFormID%>&ChkNumber=" + vChkNumber + "&ChkValue=" + vChkValue;
    window.showModalDialog(url,window,"status=no;dialogWidth=700px;dialogHeight=550px");	
  }  
  
  function ListPopedom() {
    var vChkNumber = NumberOfCheck(formList.chkId);
    var vChkValue  = ValueOfCheck(formList.chkId);
    var url = "popedom/index.jsp?type=-1&FormID=<%=iFormID%>&ChkNumber=" + vChkNumber + "&ChkValue=" + vChkValue;
    window.showModalDialog(url,window,"status=no;dialogWidth=700px;dialogHeight=550px");	
  }  
  
  function ListPopedom2(vKeyValue) {
    CheckCheckBox(formList.chkId, ";" + vKeyValue);
    var vChkNumber = "1";
    var vChkValue  = vKeyValue;
    var url = "popedom/index.jsp?type=-1&FormID=<%=iFormID%>&ChkNumber=" + vChkNumber + "&ChkValue=" + vChkValue;
    window.showModalDialog(url,window,"status=no;dialogWidth=700px;dialogHeight=550px");	
  }  
</script>
