<%@ 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.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 objId = SzConvert.toSpaceStr(request.getParameter("objId"));
    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 = null;
    //这里列出全部是因为，一个人可能在多个部门里。
    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.getDLID()%>","<%=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.getDLID()%>","<%=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.getDLID()%>","<%=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();
    while (iterLogin!=null && iterLogin.hasNext()) {
        szLoginInfo = (SzLoginInfo)iterLogin.next();
        sUserInfo = szLoginInfo .getDLXM() + "["  + szLoginInfo.getDLZH() + "]";
%>
        loginArray[loginCount] = new Array("0","<%=szLoginInfo.getDLID()%>","<%=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="objId" value="<%=objId%>">
  <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" multiple size="15" style="width:250;" OnDblClick="MoveSelectedOption(formSelect.LoginSelecting, formSelect.LoginSelected);">
      </select>
    </td>
    <td width="50">
      <table>
        <tr><td><input type="button" name="btnToRight" value=">>" OnClick="MoveAllOption(formSelect.LoginSelecting, formSelect.LoginSelected);"></td></tr>
        <tr><td><input type="button" name="btnToRight" value=" >"  OnClick="MoveSelectedOption(formSelect.LoginSelecting, formSelect.LoginSelected);"></td></tr>
        <tr><td><input type="button" name="btnToLeft"  value=" <"  OnClick="MoveSelectedOption(formSelect.LoginSelected, formSelect.LoginSelecting);"></td></tr>
        <tr><td><input type="button" name="btnToLeft"  value="<<" OnClick="MoveAllOption(formSelect.LoginSelected, formSelect.LoginSelecting);"></td></tr>
      </table>
    </td>
    <td width="150" align="left">
      <select name="LoginSelected" multiple size="15" style="width:250;" OnDblClick="MoveSelectedOption(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">
  //第一个表示对象，form，第二个表示字符串
  function FormSubmit(oFormName, vUrl) {
    oFormName.action = vUrl;
    oFormName.submit();
  }

  function GoUrl(vUrl) {
    document.location.href = vUrl;
  }

  //javascript参数的调用：
  //checkAll(formList,chkCheckAll,'chkId')
  //第一个表示对象，第二个表示对象，第三个表示字符串
  function CheckAll(oFormList, oChkCheckAll, vChkId) {
    var e;
    for (var i=0; i<oFormList.elements.length; i++) {
      e = oFormList.elements[i];
      if (e.name == vChkId) {
        e.checked = oChkCheckAll.checked;
      }
    }
  }

  //MoveSelectedOption(oFrom, oTo)
  //移动选中的option
  function MoveSelectedOption(oFrom, oTo) {
    var iLen = oFrom.length
    var i = 0;
    //将选中的oFrom的option加到to中
    for (i=0; i<iLen; i++) {
      if (oFrom.options[i].selected) {
        oTo.options[oTo.length] = new Option(oFrom.options[i].text,oFrom.options[i].value);
      }
    }
    //将选中的oFrom的option设置为null
    for (i=iLen-1; i>-1; i--) {
      if (oFrom.options[i].selected) {
        oFrom.options[i]=null;
      }
    }
    //选中一个后将按纽设置为disable
  }

  //MoveAllOption(oFrom, oTo)
  //移动选中的option
  function MoveAllOption(oFrom, oTo) {
    var iLen = oFrom.length
    var i = 0;

    //将oFrom的option加到to中
    for (i=0; i<iLen; i++) {
      oTo.options[oTo.length] = new Option(oFrom.options[i].text,oFrom.options[i].value);
    }
    //将oFrom的option设置为null
    for (i=iLen-1; i>-1; i--) {
      oFrom.options[i]=null;
    }
  }

  function SelectAllOption(oSelect) {
    var iLen = oSelect.length;

    for (var i=0; i<iLen; i++){
        oSelect.options[i].selected = true;
    }
  }

  function FunOk() {

    //确定，获取
    var vId = "0";
    var vName = "";
    var j = 0;
    var oSelect = formSelect.LoginSelected;
    var iLen = oSelect.options.length;

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

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