<%@ 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.*" %>
<%@ include file="/sessionInfo.jsp"%>
<%
    String objId    = SzConvert.toSpaceStr(request.getParameter("objId"));
    String objName  = SzConvert.toSpaceStr(request.getParameter("objName"));
    String strTable = SzConvert.toSpaceStr(request.getParameter("tableName"));
    String strForm  = SzConvert.toSpaceStr(request.getParameter("formName"));
%>
<%
    String sTable     = "";
    String sFieldID   = "";
    String sFieldName = "";
    String sFieldType = "";
    String sFieldDesc = "";
    String sCond      = "";
    int i = 0;
    int j = 0;

    SzDataInfo szDataInfo = null;
    SzData szData = new SzData();

    ReadSelectXML readXML = new ReadSelectXML();
    SzSelectTableInfo tableInfo = readXML.getTableInfoFromXML(strTable);

    //读取分类
    sTable     = "";
    sFieldID   = "";
    sFieldName = "";
    sFieldType = "";
    sFieldDesc = "";
    sCond      = "";
    Collection collType = null;
    Iterator iterType = null;

    if (!tableInfo.getGLGLBM().equals("")) {

      sTable     = tableInfo.getGLGLBM();
      sFieldID   = tableInfo.getGLGLID();
      sFieldName = tableInfo.getGLGLMC();
      sFieldType = "";
      sFieldDesc = "";
      sCond = tableInfo.getGLGLTJ();


      //2008-02-21对关联条件增加变化
      sCond = sCond.replaceAll("《@szCOMPANY_SESSION》", szCOMPANY_SESSION);
      sCond = sCond.replaceAll("《@DLID》", Integer.toString(iDLID_SESSION));
      if (sCond.startsWith("!")) {
        if (!isBarManager_SESSION) {
          sCond = sCond.replaceFirst("!", "");
        } else {
          sCond = "1=1";
        }
      }
      szData.setTableInfo(sTable, sFieldID, sFieldName);
      collType = szData.getArrayList(sCond);
      iterType = collType.iterator();
    }

    //读取
    sTable     = tableInfo.getGLBM();
    sFieldID   = tableInfo.getFieldID();
    sFieldName = tableInfo.getFieldName();
    sFieldType = tableInfo.getFieldType();
    sFieldDesc = tableInfo.getFieldDesc();
    sCond = tableInfo.getGLTJ();
    //2008-02-21对关联条件增加变化
    sCond = sCond.replaceAll("《@szCOMPANY_SESSION》", szCOMPANY_SESSION);
    sCond = sCond.replaceAll("《@DLID》", Integer.toString(iDLID_SESSION));
    if (sCond.startsWith("!")) {
      if (!isBarManager_SESSION) {
        sCond = sCond.replaceFirst("!", "");
      } else {
        sCond = "1=1 ORDER BY " + sFieldName;
      }
    }

    if (!sFieldType.equals("") && !sFieldDesc.equals("")) {
       szData.setTableInfo(sTable, sFieldID, sFieldName, sFieldType, sFieldDesc);
    } else {
       szData.setTableInfo(sTable, sFieldID, sFieldName);
    }

    Iterator iterInfo = szData.getArrayList(sCond).iterator();

%>
<!----------将数据读进JS数组中----------->
<script language="JavaScript">
    var typeCount;
    //部门数组
    typeArray = new Array();
    typeCount = 0;

<%
    if (collType != null) {
        iterType = collType.iterator();
    }
    while (iterType != null && iterType.hasNext()) {
        szDataInfo = (SzDataInfo)iterType.next();
        i++;
%>
        //部门数组
        typeArray[typeCount] = new Array("<%=szDataInfo.getID()%>", "<%=szDataInfo.getName()%>");
        typeCount++;
<%
    }
%>
    var  infoCount;
    infoArray = new Array();
    infoCount = 0;
<%
    while (iterInfo!=null && iterInfo.hasNext()) {
        szDataInfo = (SzDataInfo)iterInfo.next();
%>
        //这里的ID改成名字
        infoArray[infoCount] = new Array("<%=szDataInfo.getID()%>","<%=szDataInfo.getName()%>", "<%=szDataInfo.getType()%>");
        infoCount++;
<%
    }
%>
  //选择部门
  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][2] == vBMID) {
          formSelect.InfoSelecting.options[formSelect.InfoSelecting.length]
          = new Option(infoArray[i][1], infoArray[i][0]);
        }
      }//for
    } //if else
  }
  //将全体员工放进InfoSelecting
  function ShowAllInfo() {
    formSelect.InfoSelecting.length = 0;
    for (var i=0; i<infoCount; i++) {
      formSelect.InfoSelecting.options[formSelect.InfoSelecting.length]
      = new Option(infoArray[i][1], infoArray[i][0]);
    }
  }
  
  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][1];
        if (vValue.indexOf(vSearch) > -1) {
      	  formSelect.InfoSelecting.options[formSelect.InfoSelecting.length]
      	  = new Option(infoArray[i][1], infoArray[i][0]);      
        }	
      }      
    }	
  }  
</script>

<table width="550" align="center">
  <form name="formSelect" method="post">
  <input type="hidden" name="objId" value="<%=objId%>">
  <input type="hidden" name="objName" value="<%=objName%>">
  <input type="hidden" name="formName" value="<%=strForm%>">
  <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()) {
        szDataInfo = (SzDataInfo)iterType.next();
%>
        <option value="<%=szDataInfo.getID()%>"><%=szDataInfo.getName()%></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="MoveToRight(formSelect.InfoSelecting, formSelect.InfoSelected);">
      </select>
    </td>
    <td width="50">
      <table>
        <tr><td><input type="button" name="btnToRight" value=" >"  OnClick="MoveToRight(formSelect.InfoSelecting, formSelect.InfoSelected);"></td></tr>
        <tr><td><input type="button" name="btnToLeft"  value=" <"  OnClick="MoveToLeft(formSelect.InfoSelected, formSelect.InfoSelecting);"></td></tr>
      </table>
    </td>
    <td width="150" align="left">
      <select name="InfoSelected" size="15" style="width:250;" OnDblClick="MoveToLeft(formSelect.InfoSelected, formSelect.InfoSelecting);">
      </select>
    </td>
  </tr>
  <tr>
    <td colspan="3" align="center">
      <input type="button" name="btnOk" value="确定" OnClick="javascript:FunOk();">
      <input type="button" name="btnClose" 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;

    for(var i =0; i<iLen; i++) {
      if (i==0) {
        vId = oSelect.options(i).value;
        vName = oSelect.options(i).text;
      } else {
        vId += "," + oSelect.options(i).value;
        vName += " " + oSelect.options(i).text;
      }
      j = 1;
    }

    var objId = formSelect.objId.value;
    var objName = formSelect.objName.value;

    var oWindow = window.dialogArguments;
    if (j == 1) {
      if (objName != "") {
        eval("oWindow." + objName + ".value='" + vName + "';");
      }
      if (objId != "") {
        eval("oWindow." + objId + ".value=" + vId + ";");
      }
    } else {
      if (objName != "") {
        eval("oWindow." + objName + ".value='';");
      }
      if (objId != "") {
        eval("oWindow." + objName + ".value=0;");
      }
    }
    
    //是否需要提交
    var formName = formSelect.formName.value;
    if (formName != "") {
      eval("oWindow." + formName + ".submit();");
    }
    window.close();
  }
</script>
<script language="javascript">
  //初始化的时候
  ShowAllInfo();
</script>