<%@ page contentType="text/html; charset=UTF-8" buffer="0kb"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@page import="java.util.Collection"%>
<%@page import="com.teemlink.qm.base.model.WebUser"%>
<%@page import="com.teemlink.qm.config.DepartmentAPI"%>
<%@page import="cn.myapps.common.util.SpringApplicationContextUtil"%>
<%@page import="com.teemlink.km.common.model.*"%>
<%@page import="cn.myapps.common.data.DataPackage"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="com.teemlink.qm.base.model.ResultWarp"%>
<%@page import="com.teemlink.qm.base.model.Department"%>

<%String
		contextPath = request.getContextPath();
	response.setHeader("Pragma","No-Cache");
	response.setHeader("Cache-Control","No-Cache");
	response.setDateHeader("Expires",   0);
%>

<html>
<o:MultiLanguage value="FRONTMULTILANGUAGETAG">
	<head>
		<title>{*[Select]*}</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<link rel="stylesheet" href="../css/main-front.css" type="text/css">
		<link rel="stylesheet" href="../css/dtree.css">
		<script src="../js/util.js"></script>
		<script src="../js/dtree.js"></script>

		<script src="../js/dtree-user.js"></script>
		<script src="../js/jquery-1.8.3.js"></script>
		<script type="text/javascript" src="../js/other/jquery.artDialog.source.js?skin=aero"></script>
		<script type="text/javascript" src="../js/other/iframeTools.source.js"></script>
		<script type="text/javascript" src="../js/other/jquery-framedialog.js"></script>
		<script src="../js/other/obpm-jquery-bridge.js"></script>
		<script type="text/javascript" src="../js/other/jquery.cookie.js"></script>
		<script type="text/javascript" src="../js/other/jquery.hotkeys.js"></script>
		<script type="text/javascript" src="../js/other/jquery.jstree.js"></script>
		<script type="text/javascript" src="../js/other/jquery.jstree.checkbox.js"></script>
		<script type="text/javascript" src="../js/other/json2.js"></script>
		<script type="text/javascript" src="../js/other/jquery.slimscroll.min.js"></script>

		<script language="JavaScript">
			var args = OBPM.dialog.getArgs();
			var parentObj = args['parentObj'];
			/*存放deptid*/
			var targetid=args['targetid'];
			/*存放deptname*/
			var viewName=args['receivername'];
			//是否重复
			function isDuplicate(values , value){
				for (var i=0; i<values.length; i++) {
					if (values[i] == value) {
						return true;
					}
				}
				return false;
			}

			function doReturn() {

				var sis = document.getElementsByName("_selectitem");

				var array = new Array();
				if (sis != null && sis.length > 0) {
					for (var i=0; i<sis.length; i++) {
						var e = sis[i];
						if (e.type == 'checkbox') {
							if (e.checked && e.value) {
								var rtn = {};
								rtn.text = jQuery(e).attr("text");
								rtn.value = e.value;
								array.push(rtn);
							}
						}
					}
				}
				OBPM.dialog.doReturn(array);


			}


			function doInit(){
				if(args) {
					var value = args.value;
					var readonly = args.readonly;

					// 只读情况下，隐藏按钮条
					if (readonly) {
						var btnbarDiv = document.getElementById("btnbarDiv");
						btnbarDiv.style.display = "none";
					}

					if (value){
						var sis = document.getElementsByName("_selectitem");
						var str = value;

						var checkedArray = str.split(";");
						var temp = [];
						for (var i=0; i < checkedArray.length; i++) {
							if (sis) {
								for (var j=0; j < sis.length; j++) {
									var e = sis[j];
									if (!isDuplicate(temp, e.value)) {
										toggleCheck(e, checkedArray);
										temp.push(e.value);
									}
								}
							}
						}

					}
				}
			}

			function toggleCheck(oEl, checkedValues){
				for (var i=0; i < checkedValues.length; i++) {
					if(oEl.value == checkedValues[i] && oEl.value != ''){
						oEl.checked = true;
						d.openTo(checkedValues[i]);
					}
				}
			}

			function doSelect(rtn){
				var array = new Array();
				array.push(rtn);

				OBPM.dialog.doReturn(array);
			}

			function setDivSize(){
				var bodyH = document.body.clientHeight;
				var btH = document.getElementById("btnbarDiv").clientHeight;
				document.getElementById("selDepConDiv").style.height = (bodyH - btH - 40)+"px";
			}

			jQuery(window).resize(function(){
				setDivSize();
			});

			window.onload = function(){
				doInit();
				setDivSize();
			}


			/* 设置值到目标文本框中 */
			function resetTargetValue(){
				var targetValue="";
				var names="";
				var emails="";
				jQuery("input[name='_selectitem']:checked").each(function(){
					targetValue+=jQuery(this).attr("value")+";";//多个用户用“;”分隔
					names+=jQuery(this).attr("text")+";";
				});
				targetValue=targetValue.substring(0,targetValue.length-1);
				names=names.substring(0,names.length-1);
				//parentObj.document.getElementsByName(targetid)[0].value=targetValue;
				if (parentObj.document.getElementById(targetid)) {
					parentObj.document.getElementById(targetid).value=targetValue;
				}
				if (parentObj.document.getElementById(viewName)) {
					parentObj.document.getElementById(viewName).value=names;
					parentObj.document.getElementById(viewName).title=bulitTitle(names);
				}
				//parentObj.findUserName(targetValue);
			}

			jQuery(document).ready(function(){
				jQuery("input[type='checkbox']").click(function(){
					resetTargetValue();
				});
			});
		</script>
	</head>
	<body style="overflow:hidden;">
	<form name="formList" method="post" action="">
		<tr style="display:none;">
			<td colspan="2">
				<input type="hidden" name="" value="_orderby"/> </td>
		</tr>

		<div id="btnbarDiv" class="btnbarDiv">
			<div>
				<button type="button" class="button-class" onClick="doReturn();">
					<img src="../images/act_4.gif">确定
				</button>
			</div>
		</div>
		<div id="selDepConDiv" class="selDepConDiv">
			<div id="deplist" class="commFont">
				<script type="text/javascript">
					var contextPath = '<%=contextPath%>';
					var d = new dTree('d', 'deplist');
					//d.config.multiSelect = args.multiSelect != undefined ? args.multiSelect : true;
					d.config.multiSelect =  true;
					<%
                    //DepartmentHelper process = new cn.myapps.authtime.department.service.DepartmentHelper();
                    WebUser user = WebUser.getUser(request);
                    String id = user.getDomainid();
					DepartmentAPI departmentAPI = SpringApplicationContextUtil.getBean(DepartmentAPI.class);
					Map<String,String> content = new HashMap<>();
					ResultWarp<DataPackage<Department>> resultWarp = departmentAPI.getDepartments(id,content,"1","10000");
					Collection dept = resultWarp.getData().datas;
                    //Collection dept = process.getDepartmentListByDomain(id);
                    //DepartmentVO deptVO = new cn.myapps.common.model.department.DepartmentVO();
                    //DepartmentVO superior = deptVO.getSuperior();
                    request.setAttribute("dept",dept);
                    %>
					<c:forEach var="list" items="${dept}">
					<c:choose>
					<c:when test="${list.superior.id != null && list.superior.id!=''}">
					<c:choose>
					<c:when test="${list.superior.valid == 1 && list.valid!=''}">
					d.add(
							'${list.id}',
							'${list.superior.id}',
							'${list.name}',
							'javascript:doSelect({value: \'${list.id}\', text: \'${list.name}\'});',
							'${list.id}',
							'',
							'<%=contextPath%>/qm/images/dept.gif',
							'<%=contextPath%>/qm/images/dept.gif',
							'',
							'')
					</c:when>
					</c:choose>
					</c:when>
					<c:otherwise>
					<c:choose>
					<c:when test="${list.valid == 1}">
					d.add(
							'${list.id}',
							'-1',
							'${list.name}',
							'javascript:doSelect({value: \'${list.id}\', text: \'${list.name}\'});',
							'${list.id}',
							'',
							'<%=contextPath%>/qm/images/dept.gif',
							'',
							'',
							'')
					</c:when>
					</c:choose>
					</c:otherwise>
					</c:choose>
					</c:forEach>

					document.write(d);
				</script>
			</div>
		</div>
	</form>
	</body>
</o:MultiLanguage>
</html>