<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.Collection" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="net.sz600.tool.SzConvert" %>
<%@ page import="net.sz600.database.*" %>
<%@ page import="net.sz600.basicdata.*" %>
<%@ page import="net.sz600.formEngine2.*" %>
<%@ page import="net.sz600.formEngine.*" %>
<%@ page import="net.sz600.system.*" %>
<%@ include file="/sessionInfo.jsp"%>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="format-detection" content="telephone=no" />
<link href="<%=strSkin_SESSION%>/css2007.css" rel="stylesheet" type="text/css">
<script language="javascript" src="../../share/js/selectTool.js"></script>
<script language="javascript" src="../../share/js/commonTool.js"></script>
</head>
<body>
<%
    String strTable = SzConvert.toSpaceStr(request.getParameter("tableName"));
    String strIndex = SzConvert.toZeroStr(request.getParameter("index"));
    String sFunc = SzConvert.toSpaceStr(request.getParameter("func"));
    
    String TypeSelecting = SzConvert.strWriteData(request.getParameter("TypeSelecting"));
    String sRelateValue = SzConvert.strWriteData(request.getParameter("RelateValue"));
    //System.out.println("sRelateValue==" + sRelateValue);
    
		BdOpenSelect bdOpenSelect = new BdOpenSelect();
		BdOpenSelectInfo tableInfo = bdOpenSelect.getInfo(strTable);

		String relateField = "";
		String relateObject = "";
		if (tableInfo != null) {
			relateObject = tableInfo.getRelateObject();
			relateField = tableInfo.getRelateField(); 
		}
		String selectType = tableInfo.getGLLX();

%>
   
<%
  int i = 0;
  int j = 0;
  //字段：ID，name
  Collection collType = null;
  Iterator iterType = null;
	collType = bdOpenSelect.getTypeList(strTable, iDLID_SESSION, isBarManager_SESSION);
	if (collType != null) {
		iterType = collType.iterator();
	}
	String s_type[] = new String[2];
	
	//字段：分类关联字段，检索条件字段
	Collection collInfo = null;
	Iterator iterInfo = null;
	String sSortField = "";
	int iSortOrder = 0;
	int iPageSize = SzProperties.I_PageSize;
	int iCurrentPage = SzConvert.toInt(request.getParameter("page"));
	if (iCurrentPage <=0) {
		iCurrentPage = 1;
	}
	String sSearch = SzConvert.strWriteData(request.getParameter("txtSearch"));
	collInfo = bdOpenSelect.getValueList(strTable, iDLID_SESSION, isBarManager_SESSION, sRelateValue,
			 sSortField, iSortOrder, iCurrentPage, iPageSize, sSearch, TypeSelecting);
  SzPageInfo pageInfo = bdOpenSelect.getPageInfo();
  int iPageCount = pageInfo.getPageCount();
  int iRecordCount = pageInfo.getRecordCount();
  iCurrentPage = pageInfo.getCurrentPage();
    
	if (collInfo != null) {
		iterInfo = collInfo.iterator();
	}
	
	String s_info[] = new String[20];  //自带2个，显示的18个
	
	//控件ID
	String strObj = tableInfo.getFieldDesc();
	strObj = strObj.replaceAll(";", ",");
	String obj_str[] = strObj.split(",");	
%>


<table width="100%" border="1" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableClass" cellpadding="1" cellspacing="0">
  <form name="formSelect" method="post">
	<input type="hidden" name="tableName" value="<%=strTable%>">
  <input type="hidden" name="txtSortField" value="<%=sSortField%>"> 
  <input type="hidden" name="txtSortOrder" value="<%=iSortOrder%>"> 	
  <input type="hidden" name="index" value="<%=strIndex%>">
  <input type="hidden" name="func" value="<%=sFunc%>">
  <input type="hidden" name="selectType" value="<%=selectType%>">
  <input type="hidden" name="RelateValue" value="<%=sRelateValue%>">
  <tr>
    <td colspan="21"><!----------这里显示部门-------->
      <select name="TypeSelecting" style="width:250;" OnChange="this.form.submit();">
        <option value="" >========显示全部========</option>
<%
    if (collType != null) {
        iterType = collType.iterator();
    }
    while (iterType!=null && iterType.hasNext()) {
        s_type = (String[])iterType.next();
%>
        <option value="<%=s_type[0]%>"  <% if (TypeSelecting.equals(s_type[0])) { out.println("selected");} %>><%=s_type[1]%></option>
<%
    }
%>
      </select>
      查找：<input type="text" name="txtSearch" class="txtClass1" value="<%=sSearch%>">
          <input type="button" class="btnClass" value="查找" OnClick="this.form.submit();">
      <input type="button" name="btnOk"    class="btnClass" value="确定" OnClick="javascript:FunOk();">
      <input type="button" name="btnClose" class="btnClass" value="关闭" OnCLick="javascript:window.close();">
          
    </td>
  </tr>
<%
   //System.out.println("================");
		//int j = 0;
		int iLen = obj_str.length + 1;
		int iLenSpace = obj_str.length + 1;
    if (!tableInfo.getFieldType().equals("")) {
    	iLen ++;
    	iLenSpace ++;
    }
    if (!tableInfo.getRelateField().equals("")) {
			iLen ++;
			iLenSpace ++;
    }

    while (iterInfo!=null && iterInfo.hasNext()) {
        s_info = (String[])iterInfo.next();
        j ++;
        out.println("<tr>");
        if (selectType.equals("0")) {
        	out.println("<td width='10'><input type='radio' name='InfoSelected' id='InfoSelected' value='" + j + "'></td>");
        } else {
        	out.println("<td width='10'><input type='checkbox' name='InfoSelected' id='InfoSelected' value='" + j + "'></td>");
        }
        if (!tableInfo.getFieldType().equals("")) {
        	out.print("<td id='infoArray" + j + "_" + "0'>");
        	out.print(s_info[0]);
        	out.println("</td>");        	
        }
        if (!tableInfo.getRelateField().equals("")) {
        	out.print("<td id='infoArray" + j + "_" + "1'>");
        	out.print(s_info[1]);
        	out.println("</td>");
        }
        
        for (i=2; i<20 && i<=iLen; i++) {
        	out.print("<td id='infoArray" + j + "_" + i + "'>");
        	out.print(s_info[i]);
        	out.println("</td>");
        }
        out.println("</tr>");
    }
    out.println(SzPageHelp.outIndexTableTr(j , iPageSize, iLenSpace ));
%>
		<input type="hidden" name="count" id="count" value="<%=j %>">
  </form>
</table>
<table width="100%">
  <form name="formPage" method="post">
	<input type="hidden" name="tableName" value="<%=strTable%>">
  <input type="hidden" name="index" value="<%=strIndex%>">
  <input type="hidden" name="func" value="<%=sFunc%>">
  <input type="hidden" name="selectType" value="<%=selectType%>">  
  <input type="hidden" name="txtSortField" value="<%=sSortField%>"> 
  <input type="hidden" name="txtSortOrder" value="<%=iSortOrder%>"> 
  <input type="hidden" name="TypeSelecting" value="<%=TypeSelecting%>">
  <input type="hidden" name="txtSearch" value="<%=sSearch%>">
  <input type="hidden" name="RelateValue" value="<%=sRelateValue%>">  
  <tr>
    <td align="right">
      <%@ include file="/share/page/page_only_include.jsp"%>
    </td>
  </tr>
  </form>
</table>
<script language="JavaScript">
    //当打开选择窗口的时候，已经可以获得值
    var oWindow = window.opener;
    var vRelateObject = "<%=relateObject%>";
    var vRelateField = "<%=relateField%>";
    var sRelateValue = "<%=sRelateValue%>";
    var vRelateValue = "";
    if (vRelateObject != "" && vRelateField != "") {
    	vRelateValue = eval("oWindow.formUpdate." + vRelateObject + ".value");
    	if (vRelateValue != sRelateValue) {
    		//重新提交一次：这里获取条件值，并且重新提交一次
    		formSelect.RelateValue.value = vRelateValue;
    		formSelect.submit();
    		
    	}
    } 
</script> 
<script language="javascript">

    var objCount;
    objArray = new Array();
    objCount = 0;
    
    var valueArray = new Array();
<%
    int iObjCount = 0;
    if (obj_str != null) {
    	iObjCount = obj_str.length;
    }
    for (i =0; i<iObjCount; i++) {
%>
        objArray[objCount] = new Array("<%=obj_str[i]%>");  
        valueArray[objCount] = new Array("");         
        objCount++;
<%    	
    }    	
%>  
  function FunOk() {
    //确定，获取
    var vName = "";
    var vId = "0";
    var j = 0;
    var oSelect = formSelect.InfoSelected;
    
    var iLen = formSelect.count.value;
    var vIndex = formSelect.index.value;
    var oTd = "";
    var v = "";
    
    var selectType = formSelect.selectType.value;
    //2011-12-07增加判断，只能选择一个
    /*
    if (selectType == "0" && iLen >= 2) {
			alert("只能选择一个");
			return false;
    } 
    */   

    var k = 0;
    var flag = false;

    for(var i =0; i<iLen; i++) {
      //数组infoArray的index。以序号作为数组的一维
      //vId = oSelect.options(i).value;
      if (iLen == 1) {
      	if (oSelect.checked == true) {
      	  vId = oSelect.value;
      	} else {
      		continue;
      	}      	
      } else {
      	if (oSelect[i].checked == true) {
      		vId = oSelect[i].value;
      	} else {
      		continue;
      	}      	
      }
      flag = false;
      
      for (k=0; k<objCount; k++) {
      	oTd = document.getElementById("infoArray" + vId + "_" + (k+2));
        //取到数组第3个开始
        if (oTd == null) {
        	continue;
        }
        v = oTd.innerText;
        if (v == "") {
        	continue;
        }
        flag = true;
      	if (j==0) {
        	valueArray[k] = v;
      	} else {
        	valueArray[k] += "," + v;
      	}       	
      	   	
      } //var k
      if (flag) {
      	j ++;      
      }
    } //var i

    //var oWindow = window.dialogArguments;
    var oWindow = window.opener;
		//j == 1表示已经选择了。
    if (j >0) {
      for (k=0; k<objCount; k++) {        
        if (objArray[k] == "" || objArray[k] == null ) {
        	continue;
        }
				objName = "formUpdate." + objArray[k];
      	if (objArray[k] != "") {
      		if (vIndex == "-1") {
        		eval("oWindow." + objName + ".value='" + valueArray[k] + "';");      		
      		} else {
        		eval("oWindow." + objName + "[" + vIndex + "].value='" + valueArray[k] + "';");
      		}
      	}
      }
    } 
    else {
      
      for (k=0; k<objCount; k++) {
         
				objName = "formUpdate." + objArray[k];		
      	if (objArray[k] != "") {
      		if (vIndex == "-1") { 
        		eval("oWindow." + objName + ".value='';");     		
      		} else {
      			eval("oWindow." + objName + "[" + vIndex + "].value='';"); 
      		}      	        	
      	}
      }
      
    } 
    var vFunc = formSelect.func.value;

    if (vFunc != null && vFunc != "") {
	//执行JS函数
        eval("oWindow." + vFunc + "();"); 
    }	

    window.close();

  }
</script>
<script language="javascript">
  //初始化的时候
  //ShowAllInfo(vRelateValue);
  window.resizeTo(700,500);
</script>
</body>
</html>
