<%@ page contentType="text/html;charset=gbk" %>
<%@ 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.system.*" %>
<%@ include file="/sessionInfo.jsp"%>
<html>
<body>
<link href="<%=strSkin_SESSION%>/css2007.css" rel="stylesheet" type="text/css">
<%
    String strTable = SzConvert.toSpaceStr(request.getParameter("tableName"));
    String strIndex = SzConvert.toZeroStr(request.getParameter("index"));
    String sFunc = SzConvert.toSpaceStr(request.getParameter("func"));
    
	BdReadSelectXML readSelectXML = new BdReadSelectXML();
	//获取已经设置的详细信息
	/*
	BdSelectTableInfo tableInfo = (BdSelectTableInfo)readSelectXML.getTableInfoFromXML(strTable);
	if (tableInfo == null) {
		out.println("没有设置关联的信息！");
		return;
	}
	*/
	BdSelectControl bdSelectControl = new BdSelectControl(strTable);
	BdSelectTableInfo tableInfo = bdSelectControl.getTableInfo();
	String relateField = "";
	String relateObject = "";
	if (tableInfo != null 
			&& !tableInfo.getRelateField().equals("") 
			&& !tableInfo.getRelateObject().equals("")) {
		relateObject = tableInfo.getRelateObject();
		relateField = tableInfo.getRelateField(); 
	}
	String selectType = tableInfo.getGLLX();

%>
<%
    int i = 0;
    int j = 0;

    Collection collType = null;
    Iterator iterType = null;
	collType = bdSelectControl.getTypeList(iDLID_SESSION, isBarManager_SESSION, szCOMPANY_SESSION);
	if (collType != null) {
		iterType = collType.iterator();
	}
	String s_type[] = new String[3];
	

	Collection collInfo = null;
	Iterator iterInfo = null;
	collInfo = bdSelectControl.getArrayList(iDLID_SESSION, isBarManager_SESSION, szCOMPANY_SESSION);
	if (collInfo != null) {
		iterInfo = collInfo.iterator();
	}

	int iFieldLen = bdSelectControl.getFieldLen();
	String s_info[] = new String[15];
	
	//控件ID
	String obj_str[] = bdSelectControl.getObjStr();
	//SzOut.printArray(obj_str);
%>
<!----------将数据读进JS数组中----------->
<script language="JavaScript">
    //当打开选择窗口的时候，已经可以获得值
    var oWindow = window.dialogArguments;
    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[3]%>", "<%=s_type[2]%>");
        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]%>");  
        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;
    //========显示部门的全体人员========
    if (vBMID == "") {
      ShowAllInfo("");
    } else {
      //如果选择了部门，或者选择了没有部门的
      for (var i=0; i<infoCount; i++) {
        //如果是同一个部门的，就放进去
        //因为员工是按照部门排序的，所以这里可以加一个变量，如果已经加完同一部门的，就跳出循环。
        if (infoArray[i][0] == vBMID) {
          formSelect.InfoSelecting.options[formSelect.InfoSelecting.length]
          = new Option(infoArray[i][2], i);
        }
      }//for
    } //if else
  }
  //将全体员工放进InfoSelecting,增加vRealteValue，为空时表示全部，否则增加条件
  function ShowAllInfo(vRelateValue) {
    formSelect.InfoSelecting.length = 0;
    for (var i=0; i<infoCount; i++) {
      if (vRelateField != "" && vRelateValue != "" && infoArray[i][1] != vRelateValue) {
      	continue;
      } 
      formSelect.InfoSelecting.options[formSelect.InfoSelecting.length]
      = new Option(infoArray[i][2], i);
    }
  }
  
  function SearchOk() {
    var vValue = "";
    var vSearch = formSelect.txtSearch.value;
    
    if (vSearch == "") {  
      ShowAllInfo();          
    } else {
      formSelect.InfoSelecting.length = 0;
      for (var i=0; i<infoCount; i++) {
        var vValue = infoArray[i][2];
        if (vValue.indexOf(vSearch) > -1) {
      	  formSelect.InfoSelecting.options[formSelect.InfoSelecting.length]
      	  = new Option(infoArray[i][2], i);      
        }	
      }      
    }	
  } 
</script>

<table width="550" align="center">
  <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%>">  
  <tr>
    <td colspan="3"><!----------这里显示部门-------->
      <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[2]%>"><%=s_type[3]%></option>
<%
    }
%>
      </select>
      查找：<input type="text" name="txtSearch" class="txtClass1">
          <input type="button" class="btnClassSearch" value="查找" OnClick="SearchOk();">
    </td>
  </tr>
  <tr>
    <td width="150" align="right">
      <select name="InfoSelecting" size="15" style="width:250;" OnDblClick="MoveSelectedOption(formSelect.InfoSelecting, formSelect.InfoSelected);">
      </select>
    </td>
    <td width="50">
      <table>
        <tr><td><input type="button" name="btnToRight" class="btnClassShort" value=">>" OnClick="MoveAllOption(formSelect.InfoSelecting, formSelect.InfoSelected);" style="display:none;"></td></tr>
        <tr><td><input type="button" name="btnToRight" class="btnClassShort" value=" >"  OnClick="MoveSelectedOption(formSelect.InfoSelecting, formSelect.InfoSelected);"></td></tr>
        <tr><td><input type="button" name="btnToLeft"  class="btnClassShort" value=" <"  OnClick="MoveSelectedOption(formSelect.InfoSelected, formSelect.InfoSelecting);"></td></tr>
        <tr><td><input type="button" name="btnToLeft"  class="btnClassShort" value="<<" OnClick="MoveAllOption(formSelect.InfoSelected, formSelect.InfoSelecting);" style="display:none;"></td></tr>
      </table>
    </td>
    <td width="150" align="left">
      <select name="InfoSelected" multiple size="15" style="width:250;" OnDblClick="MoveSelectedOption(formSelect.InfoSelected, formSelect.InfoSelecting);">
      </select>
    </td>
  </tr>
  <tr>
    <td colspan="3" align="center">
      <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>
  </form>
</table>
<script language="javascript" src="../../share/js/selectTool.js"></script>
<script language="javascript">
  function FunOk() {
    //确定，获取
    var vName = "";
    var vId = "0";
    var j = 0;
    var oSelect = formSelect.InfoSelected;
    var iLen = oSelect.options.length;
    var vIndex = formSelect.index.value;
    
    var selectType = formSelect.selectType.value;
    //2011-12-07增加判断，只能选择一个
    if (selectType == "0" && iLen >= 2) {
			alert("只能选择一个");
			return false;
    }    

    var k = 0;

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



    var oWindow = window.dialogArguments;
	//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." + 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);
</script>
</body>
</html>