<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.Collection" %>
<%@ page import="java.util.Map" %>
<%@ 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"%>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="<%=strSkin_SESSION%>/css2007.css" rel="stylesheet" type="text/css">
<SCRIPT src="../../share/js/ftiens4_tree2.js"></SCRIPT>
</head>
<body>
<%
    //多部门，多员工，多职务

    //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 objDesc = SzConvert.toSpaceStr(request.getParameter("objDesc"));
    String objDescName = SzConvert.toSpaceStr(request.getParameter("objDescName"));
    String objPostId = SzConvert.toSpaceStr(request.getParameter("objPostId"));
    String objPostName = SzConvert.toSpaceStr(request.getParameter("objPostName"));

    String s = "";

    s = "";
      //+ "<input type='radio' name='DepartSelecting' value='' OnClick='ShowAllLogin2();'>显示全体员工"
      //+ "<input type='radio' name='DepartSelecting' value='0' OnClick='ClickDepart();'>显示没有部门的员工";    

%>
<%@ include file="/share/organize/user_js_include.jsp"%>
<table width="360" align="center" border="1" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableClass" cellpadding="1" cellspacing="0">
  <form name="formSelect" method="post">
  <input type="hidden" name="objId" value="<%=objId%>">
  <input type="hidden" name="objName" value="<%=objName%>">
  <input type="hidden" name="objDesc" value="<%=objDesc%>">
  <input type="hidden" name="objDescName" value="<%=objDescName%>">
  <input type="hidden" name="objPostId" value="<%=objPostId%>">
  <input type="hidden" name="objPostName" value="<%=objPostName%>">
  <tr>
    <td colspan="3" align="center">
    <% if (!objDescName.equals("")) { %>   
      <input type="checkbox" name="depart_checkall" value="1" OnClick="javascript:CheckAll(formSelect, depart_checkall, 'DepartSelected');" >部门全选&nbsp;
    <% } %>   
      <input type="button" name="btnOk" value="确定" OnClick="javascript:FunOk();">
      <input type="button" name="btnClose" value="关闭" OnCLick="javascript:window.close();">
    </td>
  </tr>
  <tr class="trClassTitle">
    <td>部门选择</td>
  </tr>
  <tr>
    <td width="36%" height="350" valign="top"  id="tdDepart"><!----------这里显示部门-------->
<%

	//读取部门
	SzUserHelp szUserHelp = new SzUserHelp();
	//Map map = szUserHelp.getDepartStaticMap();
	Iterator iter = SzUserHelp.getDepartStaticList(szCOMPANY_SESSION).iterator();
	SzDepartInfo szDepartInfo = new SzDepartInfo();
	boolean flag = true;

	String sBMMC = "";
	String sBMBH = "";
	String sBMBH_SJ = "";
		
    int iBMID = 0;
    SzDepart szDepart = new SzDepart();
    out.println(szDepart.printFirstLevelDepart(szCOMPANY_SESSION));

%> 
    <div id="divDepart"></div>
    </td>
  </tr>
  </form>
</table>
</body>
<SCRIPT language="javascript" src="../../share/js/commonTool.js"></script>
<SCRIPT language="javascript">
  //根据部门ID，获取部门名称
  function GetDepartName(vBMID) {
    var vBMMC = "";
    for (var i=0; i<departCount_JS; i++) {
      if (departArray_JS[i][0] == vBMID) {
        vBMMC = departArray_JS[i][1];
        break;
      }
    }
    return vBMMC;
  }
  
  function GetPostName(vZWID) {
    var vZWMC = "";
    for (var i=0; i<postCount_JS; i++) {
      if (postArray_JS[i][0] == vZWID) {
        vZWMC = postArray_JS[i][1];
        break;
      }
    }
    return vZWMC;
  }  
  
  //选择部门，显示用户
  function ClickDepart() {
    //员工
    var objId   = formSelect.objId.value;
    var objName = formSelect.objName.value;
    //部门
    var objDesc = formSelect.objDesc.value;
    var objDescName = formSelect.objDescName.value;
    //职务
    var objPostId = formSelect.objPostId.value;
    var objPostName = formSelect.objPostName.value;

    //获取选择的部门
    var vBMID = "0";
    //vBMID = ValueOfRadio(formSelect.DepartSelecting);
    vBMID = ValueOfRadio2("DepartSelecting");
    /*
    tdUser.innerHTML = "";
    var vHTML = "<table width='100%' border='0'>";
    var j = 0;
    //========显示部门的全体人员========
      //如果选择了部门，或者选择了没有部门的
    for (var i=0; i<userPostCount_JS; i++) {
        //如果是同一个部门的，就放进去
        //因为员工是按照部门排序的，所以这里可以加一个变量，如果已经加完同一部门的，就跳出循环。
        if (userPostArray_JS[i][0] == vBMID) {
          if (j % 2 == 0) {
            vHTML = vHTML + "<tr><td>";
            vHTML = vHTML + "<input type='checkbox' name='LoginSelecting' value='" + i + "'>" + userPostArray_JS[i][2] + "&nbsp;";
            //vHTML = vHTML + "<input type='radio' name='LoginSelecting' value='" + i + "'>" + userPostArray_JS[i][2] + "&nbsp;";
            vHTML = vHTML + "</td>";
          } else {
            vHTML = vHTML + "<td>";
            vHTML = vHTML + "<input type='checkbox' name='LoginSelecting' value='" + i + "'>" + userPostArray_JS[i][2] + "&nbsp;";
            //vHTML = vHTML + "<input type='radio' name='LoginSelecting' value='" + i + "'>" + userPostArray_JS[i][2] + "&nbsp;";
            vHTML = vHTML + "</td></tr>";
          }
          j ++;
        } // if == ends
    }//for

    vHTML = vHTML + "</table>"
    tdUser.innerHTML = vHTML;
    */
    //2009-04-15
    var j = 0;
    var oLogin;
    var oLoginName;
    var oTd;
    for (var i=0; i<userPostCount_JS; i++) {
        //oLogin = formSelect.LoginSelecting[i];
        //oLoginName = document.getElementById("LoginName" + i);
        oTd = document.getElementById("td" + i); 
        if (userPostArray_JS[i][0] == vBMID) {
        	//显示
        	//oLogin.style.display = "";
        	//oLoginName.style.display = "";
        	oTd.style.display = "";
        } else {
			//隐藏
			//oLogin.style.display = "none";
			//oLoginName.style.display = "none";
			oTd.style.display = "none";
        }
    }//for
  }

  //默认显示全部
  //将全体员工放进LoginSelecting
  function ShowAllLogin2() {
    tdUser.innerHTML = "";
    var vHTML = "<table width='100%' border='0'>";
    var j = 0;
    for (var i=0; i<userPostCount_JS; i++) {
        if (j % 2 == 0) {
            vHTML = vHTML + "<tr><td id='td" + i + "'>";
            vHTML = vHTML + "<input type='checkbox' name='LoginSelecting' value='" + i + "'>";
            vHTML = vHTML + "<span id='LoginName" + i + "'>" + userPostArray_JS[i][2] + "</span>" + "&nbsp;";
            //vHTML = vHTML + "<input type='radio' name='LoginSelecting' value='" + i + "'>" + userPostArray_JS[i][2] + "&nbsp;";
            vHTML = vHTML + "</td>";
        } else {
            vHTML = vHTML + "<td id='td" + i + "'>";
            vHTML = vHTML + "<input type='checkbox' name='LoginSelecting' value='" + i + "'>" 
            vHTML = vHTML + "<span id='LoginName" + i + "'>" + userPostArray_JS[i][2] + "</span>" + "&nbsp;";
            //vHTML = vHTML + "<input type='radio' name='LoginSelecting' value='" + i + "'>" + userPostArray_JS[i][2] + "&nbsp;";
            vHTML = vHTML + "</td></tr>";
        }
        j ++;
    }
    vHTML = vHTML + "</table>"
    tdUser.innerHTML = vHTML;
  }
  
  function ShowAllLogin3() {
    var oLogin;
    var oLoginName;
    var oTd;    
    for (var i=0; i<userPostCount_JS; i++) {
        //oLogin = formSelect.LoginSelecting[i];
        //oLoginName = document.getElementById("LoginName" + i);
        oTd = document.getElementById("td" + i); 
        oTd.style.display = "";
    }
  }  
 
  //显示指定的一级部门下的部门
  function ShowAllDepart2(vBMBH_PARENT) {
    var oTd;
    
    for (var i=0; i<departCount_JS; i++) {
        oTd = document.getElementById("departtd" + i);
        if (vBMBH_PARENT == "" || departArray_JS[i][2] != null && departArray_JS[i][2].indexOf(vBMBH_PARENT) == 0) {            
            oTd.style.display = "";
        } else {
			oTd.style.display = "none";
        }
    }    
  }
  
  //显示所有部门，第一次的时候调用
  function ShowAllDepart3() {
    //var oWindow = window.dialogArguments;
    var oWindow = window.opener;
    var vId = "";
    var oSelect = null;
    if (objDesc != "") {
      vId = eval("oWindow.document." + objDesc + ".value;");
    }
    ShowAllDepart3_ini(vId);
  }

  function ShowAllDepart3_ini(vId) {
    divDepart.innerHTML = "";
    var vHTML = "<table width='100%' border='0'>";
    //var vHTML = "";
    var j = 0;
    var vBMBH_P
    vId = "," + vId + ",";
    var vCheck = "";
    
    for (var i=0; i<departCount_JS; i++) {
        if (departArray_JS[i][2] != null) {
	  if (vId.indexOf("," +departArray_JS[i][0] + ",") >=0) {
	     //存在，选中
             vCheck = "checked";
          } else {
             vCheck = "";
          }
          vHTML = vHTML + "<tr  id='departtd" + i + "'><td>";
        	vHTML = vHTML + "<input type='checkbox' name='DepartSelected' value='" + departArray_JS[i][0] + "' " + vCheck + " >";
        	vHTML = vHTML + getLevelSpace(departArray_JS[i][2], <%=SzLevelNumber.I_NUMBER_LEN%>) +  departArray_JS[i][1];
        	vHTML = vHTML + "</td></tr>";
        } 
    }    
    vHTML = vHTML + "</table>"
    divDepart.innerHTML = vHTML;
  }  
  
  //选择部门
  function SelectFirstDepart() {
	//alert(formSelect.firstDepartNumber.value);
	//只显示一级下的部门
	var vBMBH_PARENT = formSelect.firstDepartNumber.value;
	ShowAllDepart2(vBMBH_PARENT);
  }  
  
  function getLevelSpace(number, vNumberLen) {
    var vLen = number.length;
    var vCount = vLen / vNumberLen;
    
    var vReturn = "";
    for(var i=1; i<vCount; i++) {
		vReturn += "&nbsp;&nbsp;";
    }
    return vReturn;
  }
</SCRIPT>
<script language="javascript">
    //用户
    var objId   = formSelect.objId.value;
    var objName = formSelect.objName.value;
    //部门
    var objDesc = formSelect.objDesc.value;
    var objDescName = formSelect.objDescName.value;
    //职务
    var objPostId = formSelect.objPostId.value;
    var objPostName = formSelect.objPostName.value;
    
  function FunOk() {
    
    var vId = "0";
    var vName = "";
    var vDesc = "0";
    var vDescName = "";
    var vPostId = "0";
    var vPostName = "";
    //确定，获取
    var i = 0;
    var j = 0;

    //html
    var vHTML = "";
    var vHTML_depart = "[部门]";
    var vHTML_user = "<br>[员工]";
    var vHTML_post = "<br>[职务]";
    //这里是数组的index
    var vIndex = -1;
    var vLen = 0;
    //选择部门
    //var oSelect = formSelect.DepartSelected;
    var oSelect = null;
    oSelect = document.getElementsByName("DepartSelected");
    if (oSelect != null) {
      //获取部门userPostArray_JS
      j = 0;
      vLen = oSelect.length;
      //只有一个部门的时候
      if (vLen == null) {
        vLen = 1;
      }
      for (i=0; i<vLen; i++) {
        //是否选中？
        if (vLen == 1) {
        	if (oSelect.checked != true) {
          		continue;
        	}
        	//这个是部门ID
        	vIndex = oSelect.value;          
        } else {
        	if (oSelect[i].checked != true) {
          		continue;
        	}
        	//这个是部门ID
        	vIndex = oSelect[i].value;        	
        }
        vDesc += "," + vIndex;
        //从部门数组里获取部门名称
        if (j==0) {
          vDescName =  GetDepartName(vIndex);
        } else {
          vDescName += "," + GetDepartName(vIndex);
        }
        //增加部门
        j++
      } //for i=0;
    } //if();

    //选择员工
    //oSelect = formSelect.LoginSelecting;
    oSelect = document.getElementsByName("LoginSelecting");
    //alert(oSelect.length);
    if (oSelect != null) {
      //获取部门
      j = 0;
      vLen = oSelect.length;
      //当只有一个的时候
      if (vLen == null) {
        vLen = 1;
      }
      for (i=0; i<vLen; i++) {      
        //是否选中？
        if (vLen == 1) {
          //部门下只有一个人员的时候
          if (oSelect.checked != true) {
          	continue;
        	}
        	//这个是员工数组的index
        	vIndex = oSelect.value;        
        } else {
          if (oSelect[i].checked != true) {
          	continue;
        	}
        	//这个是员工数组的index
        	vIndex = oSelect[i].value;
        }
        
        vId += "," + userPostArray_JS[vIndex][1];
        if (j==0) {
          vName = userPostArray_JS[vIndex][2];
        } else {
          vName += "," + userPostArray_JS[vIndex][2];
        }
        j++
      } //for i=0;
    } //if();

    //选择职务
    //oSelect = formSelect.PostSelected;
    oSelect = document.getElementsByName("PostSelected");
    if (oSelect != null) {
      j = 0;
      vLen = oSelect.length;
      for (i=0; i<vLen; i++) {
        //是否选中？
        if (oSelect[i].checked != true) {
          continue;
        }
        //这个是职务ID
        vIndex = oSelect[i].value;
        vPostId += "," + vIndex;
        //从部门数组里获取部门名称
        if (j==0) {
          vPostName =  GetPostName(vIndex);
        } else {
          vPostName += "," + GetPostName(vIndex);
        }
        //增加部门
        j++
      } //for i=0;
    } //if();

    //var oWindow = window.dialogArguments;
    var oWindow = window.opener;
    /*
    eval("oWindow.formUpdate.RYDLID1.value ='" + vId + "';");
    eval("oWindow.formUpdate.RYDLXM1.value ='" + vName + "';");
        
    eval("oWindow.formUpdate.RYDLID2.value ='" + vDesc + "';");
    eval("oWindow.formUpdate.RYDLXM2.value ='" + vDescName + "';");

    eval("oWindow.formUpdate.RYDLID3.value ='" + vPostId + "';");
    eval("oWindow.formUpdate.RYDLXM3.value ='" + vPostName + "';");
    */
    if (objId != "") {
    	eval("oWindow.document." + objId + ".value ='" + vId + "';");
    }
    if (objName != "") {
    	eval("oWindow.document." + objName + ".value ='" + vName + "';");
    }
    if (objDesc != "") {        
    	eval("oWindow.document." + objDesc + ".value ='" + vDesc + "';");
    }
    if (objDescName != "") {    
    	eval("oWindow.document." + objDescName + ".value ='" + vDescName + "';");
	}
	if (objPostId != "") {
    	eval("oWindow.document." + objPostId + ".value ='" + vPostId + "';");
    }
    if (objPostName != "") {
    	eval("oWindow.document." + objPostName + ".value ='" + vPostName + "';");
	}          
    window.close();
  }
  
  //部门全选
  //部门全选
  function CheckAll(oFormList, oChkCheckAll, vChkId) {
    var e;
    //2011-11-04
    //var vLen = oFormList.elements.length
    var obj = document.getElementsByName(vChkId);
    vLen = obj.length;

    for (var i=0; i<vLen; i++) {
      //e = oFormList.elements[i];
      //if (e.name == vChkId) {
      //  e.checked = oChkCheckAll.checked;
      //}
      e = obj[i];
      e.checked = oChkCheckAll.checked;
    }
  }
  
  ShowAllDepart3("");

</script>

