<%@ 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.basicdata.*" %>
<%@ page import="net.sz600.formEngine2.*" %>
<%@ page import="net.sz600.formEngine.*" %>
<%@ page import="net.sz600.system.*" %>
<%@ include file="/sessionInfoWeixin.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="<%=strLevel_SESSION%>app/images/soarwell_css.css" rel="stylesheet" type="text/css" />
 <script type="text/javascript" src="<%=strLevel_SESSION%>app/share/appWinOpen.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"));
    
	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;
	collInfo = bdOpenSelect.getValueList(strTable, iDLID_SESSION, isBarManager_SESSION, "");
	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(",");	
%>
<!----------将数据读进JS数组中----------->
<script language="JavaScript">
    //当打开选择窗口的时候，已经可以获得值
    //var oWindow = window.dialogArguments;
    var oWindow = window.parent;
    var vRelateObject = "<%=relateObject%>";
    var vRelateField = "<%=relateField%>";
    var vRelateValue = "";
    if (vRelateObject != "" && vRelateField != "") {
    	vRelateValue = eval("oWindow.formUpdate." + vRelateObject + ".value");
    } 

    var typeCount;
    //部门数组
    typeArray = new Array();
    typeCount = 0;
<%
    if (collType != null) {
        iterType = collType.iterator();
    }

    while (iterType != null && iterType.hasNext()) {
    	s_type = (String[])iterType.next();
    	
        i++;
%>
        //部门数组
        typeArray[typeCount] = new Array("<%=s_type[0]%>", "<%=s_type[1]%>");
        typeCount++;
<%
    }
%>
    var  infoCount;
    infoArray = new Array();
    infoCount = 0;
<%
   //System.out.println("================");
    while (iterInfo!=null && iterInfo.hasNext()) {
        s_info = (String[])iterInfo.next();
%>
        //这里的ID改成名字
        infoArray[infoCount] = new Array("<%=s_info[0]%>","<%=s_info[1]%>","<%=s_info[2]%>","<%=s_info[3]%>","<%=s_info[4]%>",
        																	"<%=s_info[5]%>","<%=s_info[6]%>","<%=s_info[7]%>","<%=s_info[8]%>","<%=s_info[9]%>",
        																	"<%=s_info[10]%>","<%=s_info[11]%>","<%=s_info[12]%>","<%=s_info[13]%>","<%=s_info[14]%>",
        																	"<%=s_info[15]%>","<%=s_info[16]%>","<%=s_info[17]%>","<%=s_info[16]%>","<%=s_info[17]%>",
        																	"<%=s_info[18]%>","<%=s_info[19]%>");  
        infoCount++;
<%
    }
%>

    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 SelectType() {
    //获取选择的部门
    var vBMID = 0;
    vBMID = formSelect.TypeSelecting.value;
    //将部门下的员工放进InfoSelecting
    formSelect.InfoSelecting.length = 0;
    //========显示部门的全体人员========
    var vValue = "";
    if (vBMID == "") {
      ShowAllInfo("");
    } else {
      //如果选择了部门，或者选择了没有部门的
      
      for (var i=0; i<infoCount; i++) {

        //如果是同一个部门的，就放进去
        //因为员工是按照部门排序的，所以这里可以加一个变量，如果已经加完同一部门的，就跳出循环。
        if (infoArray[i][0] == vBMID) {
      	  vValue = "(" + infoArray[i][2] + ")";
      	  if (infoArray[i][3] != "") {
      		  vValue += " " + infoArray[i][3];
      	  }
      	  if (infoArray[i][4] != "") {
      		  vValue += " " + infoArray[i][4];
      	  }        
          formSelect.InfoSelecting.options[formSelect.InfoSelecting.length]
          = new Option(vValue, i);
        }
      }//for
    } //if else
  }
  //将全体员工放进InfoSelecting,增加vRealteValue，为空时表示全部，否则增加条件
  function ShowAllInfo(vRelateValue) {
    var vValue = "";
    
    var infoSelecting = document.getElementById("infoSelecting");
    var vHTML = "";

    for (var i=0; i<infoCount; i++) {    	
      if (vRelateField != "" && vRelateValue != "" && infoArray[i][1] != vRelateValue) {
      	continue;
      } 
      vValue = "(" + infoArray[i][2] + ")";
      if (infoArray[i][3] != "") {
      	vValue += " " + infoArray[i][3];
      }
      if (infoArray[i][4] != "") {
      	vValue += " " + infoArray[i][4];
      }          
      //formSelect.InfoSelecting.options[formSelect.InfoSelecting.length] = new Option(vValue, i);
          
      vHTML = vHTML + selectHTML(i, vValue); 
    }
    //vHTML = vHTML + "aa";

    infoSelecting.innerHTML = vHTML;
  }
  
  function SearchOk() {
    var vValue = "";
    var vSearch = formSelect.txtSearch.value;
	var infoSelecting = document.getElementById("infoSelecting");
    var vHTML = "";

    if (vSearch == "") {  
      ShowAllInfo();          
    } else {
      for (var i=0; i<infoCount; i++) {
        var vValue = infoArray[i][2];
        if (vValue.indexOf(vSearch) > -1) {
     
        	vValue = "(" + infoArray[i][2] + ")";
      	  if (infoArray[i][3] != "") {
      		  vValue += " " + infoArray[i][3];
      	  }
      	  if (infoArray[i][4] != "") {
      		  vValue += " " + infoArray[i][4];
      	  }  
      	  //formSelect.InfoSelecting.options[formSelect.InfoSelecting.length]= new Option(vValue, i);
      	  vHTML = vHTML + selectHTML(i, vValue);      
        }	
      }   
      infoSelecting.innerHTML = vHTML;	   
    }//if else


  } 
  
  function selectHTML(i, vValue) {
    var vHTML = "";
    var selectType = formSelect.selectType.value;
    if (selectType == "0") {
      vHTML = "<div><input type='radio' name='InfoSelected' id='InfoSelected' value='" + i  + "'>" + vValue + "</div>";
    } else {
	  vHTML = "<div><input type='checkbox' name='InfoSelected' id='InfoSelected' value='" + i  + "'>" + vValue + "</div>";    
    }
    return vHTML;
  }
</script>
<table width="100%" align="center" border="1" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableClass" cellpadding="1" cellspacing="0">
  <form name="formSelect" method="post">
  <input type="hidden" name="index" value="<%=strIndex%>">
  <input type="hidden" name="func" value="<%=sFunc%>">
  <input type="hidden" name="selectType" value="<%=selectType%>">  
  <input type="hidden" id="selectType2" value="22">   
  <tr>
    <td>
     查找：<input type="text" name="txtSearch" class="txtClass100">
          <input type="button" class="btnClassShort" value="查找" OnClick="SearchOk();">   
      <input type="button" name="btnOk"    class="btnClass" value="确定" OnClick="javascript:FunOk();">
      <input type="button" name="btnClose" class="btnClass" value="关闭" OnCLick="javascript:CloseDivWin();">
          
    </td>
  </tr>
  
  <tr>
    <td><!----------这里显示部门-------->
      分类：<select name="TypeSelecting" style="width:250;" OnChange="SelectType();">
        <option value="" >========显示全部========</option>
<%
    if (collType != null) {
        iterType = collType.iterator();
    }
    while (iterType!=null && iterType.hasNext()) {
        s_type = (String[])iterType.next();
%>
        <option value="<%=s_type[0]%>"><%=s_type[1]%></option>
<%
    }
%>
      </select>
    </td>
  </tr>

  <tr>
    <td width="150" height="200" align="left" valign="top" id="infoSelecting">
    </td>
  </tr>
  </form>
</table>
<script language="javascript">
  function FunOk() {


    //确定，获取
    var vName = "";
    var vId = "0";
    var j = 0;
    var iLen = 0;
    var vIndex = formSelect.index.value;    
    var selectType = formSelect.selectType.value;

    //2011-12-07增加判断，只能选择一个
    var k = 0;
    if (selectType == "0") {
    	//单选
      var oChk = document.getElementsByName("InfoSelected");

      var vCheckValue = "";
      var vLen = 0;
      //2008-01-19,增加判断是否为空，不判断的话可能出现undefine的情况。
      if(oChk == null) {
      }
      if (oChk.checked == true) {
        vCheckValue = oChk.value;
      } else {
        vLen = oChk.length;
      }

      for (var i=0; i<vLen; i++) {
        if (oChk[i].checked == true) {
          vCheckValue = oChk[i].value;
          break;
        }
      }

      for (k=0; k<objCount; k++) {
        //取到数组第3个开始
        if (infoArray[vCheckValue][k + 2] == "") {
        	continue;
        }
      	valueArray[k] = infoArray[vCheckValue][k + 2];    	
      	j = 1;     	
      } //var k          	
    } else {
      //多选
      var oChk = document.getElementsByName("InfoSelected");
      var vCheckValue = "";
      var vLen = 0;
      if (oChk == null) {      
      }
      if (oChk.checked == true) {
        vCheckValue = oChk.value;
      } else {
        vLen = oChk.length;
      }
      for (var i=0; i<vLen; i++) {
        if (oChk[i].checked == true) {
           vCheckValue = oChk[i].value;
           for (k=0; k<objCount; k++) {
             //取到数组第3个开始
             if (infoArray[vCheckValue][k + 2] == "") {
        	   continue;
             }
      	     if (i==0) {
        	  valueArray[k] = infoArray[vCheckValue][k + 2];
      	     } else {
        	  valueArray[k] += "," + infoArray[vCheckValue][k + 2];
      	     }       	
      	     j = 1;     	
           } //var k  
        }
      }//for i
    } //else   

    //var oWindow = window.dialogArguments;
    //var oWindow = window.opener;
    var oWindow = window.parent;

		//j == 1表示已经选择了。
    if (j == 1) {
    	
      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.document." + objName + ".value='" + valueArray[k] + "';");      		
      		} else {
        		eval("oWindow.document." + 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();
    DivWinClose(oWindow);
  }
  
</script>
<script language="javascript">
  //初始化的时候
  ShowAllInfo(vRelateValue);
</script>
</body>
</html>
