<%@ 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.SzPageInfo" %>
<%@ page import="net.sz600.system.SzProperties" %>
<%@ page import="net.sz600.system.SzPageHelp" %>
<%@ page import="net.sz600.system.SzLevelNumber" %>
<%@ page import="net.sz600.organize.*" %>
<%@ include file="/sessionInfo.jsp"%>
<%
    //type=all没有限制，same同一部门，tree树下的子部门
    String type = SzConvert.toSpaceStr(request.getParameter("type"));
    if (type.equals("")) {
        type = "all"; //默认是全部
    }
    String objName = SzConvert.toSpaceStr(request.getParameter("objName"));
%>
<%
    String sCond = "";
    int i = 0;
    int j = 0;

    /////判断是否需要限制
    String str_DLID_limit = "0";
    SzUserHelp szUserHelp = new SzUserHelp();
    if (type.equals("same")) {
        str_DLID_limit = szUserHelp.getJuniorLoginInSameDepart(szLoginInfo_SESSION.getDLID());
    } else if (type.equals("tree")) {
        str_DLID_limit = szUserHelp.getJuniorLoginInTreeDepart(szLoginInfo_SESSION.getDLID());
    }

    //读取数据
    SzDepart szDepart = new SzDepart();
    SzDepartInfo szDepartInfo = null;
    Collection collDepart = szDepart.getArrayList();
    Iterator iterDepart = collDepart.iterator();
    HashMap mapDepart = szDepart.getMainHashMap("1=1");

    SzUserPost szUserPost = new SzUserPost();
    SzUserPostInfo szUserPostInfo = null;
    Iterator iterUserPost = null;

    //获取员工，部门，职务的hashMap
    SzLogin szLogin = new SzLogin();
    HashMap mapLogin = szLogin.getMainHashMap("1=1");
    Collection collLogin = null;
    Iterator iterLogin = null;
    SzLoginInfo szLoginInfo = null;

    SzPost szPost = new SzPost();
    HashMap mapPost = szPost.getMainHashMap("1=1");
    Iterator iterPost = null;
    SzPostInfo szPostInfo = null;

    String sUserInfo = "";
    String sPostInfo = "";
    String str_ZWID = "0";
    String str_DLID = "0";
%>
<!----------将数据读进JS数组中----------->
<script language="JavaScript">
    var departCount;
    //部门数组
    departArray = new Array();
    departCount = 0;

    var  userPostCount;
    userPostArray = new Array();
    userPostCount = 0;
<%
    String sBMMC = "";

    while (iterDepart != null && iterDepart.hasNext()) {
        szDepartInfo = (SzDepartInfo)iterDepart.next();
        sBMMC = SzLevelNumber.getLevelSpace(szDepartInfo.getBMBH()) + szDepartInfo.getBMMC();
        i++;
%>
        //部门数组
        departArray[departCount] = new Array("<%=szDepartInfo.getBMID()%>","<%=sBMMC%>");
        departCount++;
<%
        //取得部门下的职务
        sCond = "RZBM=" + szDepartInfo.getBMID() + " and RZZW>0";
        str_ZWID = szUserPost.getRZZWByCond(sCond);

        //通过职务ID获取职务名称，因为只有这样才能有效的排序
        sCond = "ZWID IN (" + str_ZWID + ") ORDER BY ZWDJ ASC";
        iterPost = szPost.getArrayList(sCond).iterator();
        //iterUserPost = szUserPost.getArrayList("RZBM=" + szDepartInfo.getBMID()).iterator();
        while (iterPost != null && iterPost.hasNext()) {
            szPostInfo = (SzPostInfo)iterPost.next();
            //获取姓名、职务名称
            sPostInfo =  "[" + mapPost.get(new Integer(szPostInfo.getZWID())) + "]";
            j++;

            //根据所在的部门以及职务======获取员工
            sCond = "RZBM=" + szDepartInfo.getBMID() + " and RZZW=" + szPostInfo.getZWID();
            str_DLID = szUserPost.getRZDLByCond(sCond);

            //通过用户ID获取用户
            //sCond = "DLID IN (" + str_DLID + ")";
            if (!type.equals("all")) {
                sCond = "DLID IN (" + str_DLID + ") and DLID IN (" + str_DLID_limit + ")";
            } else {
                sCond = "DLID IN (" + str_DLID + ")";
            }
            iterLogin = szLogin.getArrayList(sCond).iterator();
            while (iterLogin != null && iterLogin.hasNext()) {
                szLoginInfo = (SzLoginInfo)iterLogin.next();
                sUserInfo = sPostInfo + mapLogin.get(new Integer(szLoginInfo.getDLID()));
%>
                userPostArray[userPostCount] = new Array("<%=szDepartInfo.getBMID()%>","<%=szLoginInfo.getDLXM()%>","<%=sUserInfo%>");
                userPostCount++;
<%
            }
        }

        //取得部门下的用户，该用户还没有设置职务
        sCond = "RZBM=" + szDepartInfo.getBMID() + " and RZZW=0";
        str_DLID = szUserPost.getRZDLByCond(sCond);

        //通过用户ID获取用户
        //sCond = "DLID IN (" + str_DLID + ")";
        if (!type.equals("all")) {
            sCond = "DLID IN (" + str_DLID + ") and DLID IN (" + str_DLID_limit + ")";
        } else {
            sCond = "DLID IN (" + str_DLID + ")";
        }
        iterLogin = szLogin.getArrayList(sCond).iterator();
        while (iterLogin != null && iterLogin.hasNext()) {
            szLoginInfo = (SzLoginInfo)iterLogin.next();
            sUserInfo = (String)mapLogin.get(new Integer(szLoginInfo.getDLID()));
%>
            userPostArray[userPostCount] = new Array("<%=szDepartInfo.getBMID()%>","<%=szLoginInfo.getDLXM()%>","<%=sUserInfo%>");
            userPostCount++;
<%
        }
    }

    //以下把没有部门的也加进来
    sCond = "RZBM>0";
    str_DLID = szUserPost.getRZDLByCond(sCond);
    //通过用户ID获取用户
    //sCond = "DLID NOT IN (" + str_DLID + ")";
    if (!type.equals("all")) {
       sCond = "DLID NOT IN (" + str_DLID + ") and DLID IN (" + str_DLID_limit + ")";
    } else {
       sCond = "DLID NOT IN (" + str_DLID + ")";
    }
    iterLogin = szLogin.getArrayList(sCond).iterator();
    while (iterLogin != null && iterLogin.hasNext()) {
        szLoginInfo = (SzLoginInfo)iterLogin.next();
        sUserInfo = (String)mapLogin.get(new Integer(szLoginInfo.getDLID()));
%>
        //这里提供的是选择名字
        userPostArray[userPostCount] = new Array("0","<%=szLoginInfo.getDLXM()%>","<%=sUserInfo%>");
        userPostCount++;
<%
    }
%>
    //这个数组单独显示登陆的用户
    var  loginCount;
    loginArray = new Array();
    loginCount = 0;
<%
    if (!type.equals("all")) {
       sCond = " DLID IN (" + str_DLID_limit + ")";
    } else {
       sCond = " 1=1 ";
    }
    iterLogin = szLogin.getArrayList(sCond).iterator();
    //iterLogin = szLogin.getArrayList().iterator();
    while (iterLogin!=null && iterLogin.hasNext()) {
        szLoginInfo = (SzLoginInfo)iterLogin.next();
        sUserInfo = szLoginInfo .getDLXM() + "["  + szLoginInfo.getDLZH() + "]";
%>
        //这里的ID改成名字
        loginArray[loginCount] = new Array("0","<%=szLoginInfo.getDLXM()%>","<%=sUserInfo%>");
        loginCount++;
<%
    }
%>
  //选择部门
  function SelectDepart() {
    //获取选择的部门
    var vBMID = 0;
    vBMID = formSelect.DepartSelecting.value;
    //将部门下的员工放进LoginSelecting
    formSelect.LoginSelecting.length = 0;
    //========显示部门的全体人员========
    if (vBMID == "") {
      ShowAllLogin();
    } else {
      //如果选择了部门，或者选择了没有部门的
      for (var i=0; i<userPostCount; i++) {
        //如果是同一个部门的，就放进去
        //因为员工是按照部门排序的，所以这里可以加一个变量，如果已经加完同一部门的，就跳出循环。
        if (userPostArray[i][0] == vBMID) {
          formSelect.LoginSelecting.options[formSelect.LoginSelecting.length]
          = new Option(userPostArray[i][2], userPostArray[i][1]);
        }
      }//for
    } //if else
  }
  //将全体员工放进LoginSelecting
  function ShowAllLogin() {
    formSelect.LoginSelecting.length = 0;
    for (var i=0; i<loginCount; i++) {
      formSelect.LoginSelecting.options[formSelect.LoginSelecting.length]
      = new Option(loginArray[i][2], loginArray[i][1]);
    }
  }
</script>

<table width="550" align="center">
  <form name="formSelect" method="post">
  <input type="hidden" name="objName" value="<%=objName%>">
  <tr>
    <td colspan="3"><!----------这里显示部门-------->
      <select name="DepartSelecting" style="width:250;" OnChange="SelectDepart();">
        <option value="" >========显示部门的全体人员========</option>
        <option value="0">========显示没有部门的人员========</option>
<%
    iterDepart = collDepart.iterator();
    while (iterDepart!=null && iterDepart.hasNext()) {
        szDepartInfo = (SzDepartInfo)iterDepart.next();
        sBMMC = SzLevelNumber.getLevelSpace(szDepartInfo.getBMBH()) + szDepartInfo.getBMMC();
%>
        <option value="<%=szDepartInfo.getBMID()%>"><%=sBMMC%></option>
<%
    }
%>
      </select>
    </td>
  </tr>
  <tr>
    <td width="150" align="right">
      <select name="LoginSelecting" size="15" style="width:250;" OnDblClick="MoveToRight(formSelect.LoginSelecting, formSelect.LoginSelected);">
      </select>
    </td>
    <td width="50">
      <table>
        <tr><td><input type="button" name="btnToRight" value=" >"  OnClick="MoveToRight(formSelect.LoginSelecting, formSelect.LoginSelected);"></td></tr>
        <tr><td><input type="button" name="btnToLeft"  value=" <"  OnClick="MoveToLeft(formSelect.LoginSelected, formSelect.LoginSelecting);"></td></tr>
      </table>
    </td>
    <td width="150" align="left">
      <select name="LoginSelected" size="15" style="width:250;" OnDblClick="MoveToLeft(formSelect.LoginSelected, formSelect.LoginSelecting);">
      </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 j = 0;
    var oSelect = formSelect.LoginSelected;
    var iLen = oSelect.options.length;

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

    var objName = formSelect.objName.value;
    //top.dialogArguments.formUpdate.GZDLXM.value = "tttttttt";
    var oWindow = window.dialogArguments;
    if (j == 1) {
      eval("oWindow." + objName + ".value='" + vName + "';");
    } else {
      eval("oWindow." + objName + ".value='';");
    }
    window.close();
  }
</script>
<script language="javascript">
  //初始化的时候
  ShowAllLogin();
</script>
