<%@page import="cn.myapps.common.model.role.Role"%>
<%@page import="cn.myapps.common.model.application.Application"%>
<%@ page contentType="text/html; charset=UTF-8" buffer="0kb"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@ page import="java.util.*"%>
<%@ page import="cn.myapps.designtime.role.service.RoleDesignTimeService"%>
<%@ page import="cn.myapps.runtime.common.service.RunTimeServiceFactory"%>
<%@ page import="cn.myapps.authtime.domain.model.DomainVO"%>
<%@ page import="cn.myapps.authtime.domain.DomainHelper"%>
<%@page import="cn.myapps.authtime.common.service.AuthTimeServiceManager"%>
<%@ page import="cn.myapps.base.web.WebUser"%>
<%@ page import="cn.myapps.constans.Web"%>
<%@ page import="java.util.Collection"%>
<%@ page import="cn.myapps.common.util.SpringApplicationContextUtil" %>
<%
	String contextPath = request.getContextPath();
	response.setHeader("Pragma","No-Cache");
	response.setHeader("Cache-Control","No-Cache");
	response.setDateHeader("Expires",   0);
%>

<%
	StringBuffer applicationids = new StringBuffer();
	WebUser user = AuthTimeServiceManager.getWebUser(request);
	String domain = user.getDomainid();
	RoleDesignTimeService rp = (RoleDesignTimeService) AuthTimeServiceManager.roleRuntimeService();
	DomainVO vo = DomainHelper.getDomainVO(domain);
	Collection<Application> apps = vo.getApplications();
	Collection<Role> rtn = new LinkedHashSet<Role>();
	for (Iterator<Application> iterator = apps.iterator(); iterator
			.hasNext();) {
		Application applicationVO = (Application) iterator.next();
		applicationids.append(applicationVO.getId() + ",");
	}
	if(applicationids.length()>0){
		applicationids.setLength(applicationids.length()-1);
	}
	String name = request.getParameter("sm_name");
	request.setAttribute("sm_name", name);
	rtn = rp.getRolesByApplicationIds(applicationids.toString());
	request.setAttribute("rolelist", rtn);
	request.setAttribute("applicationlist", apps);
%>

<html><o:MultiLanguage>
	<head>
		<title>{*[Select]*}</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<link rel="stylesheet" href="../css/main.css" type="text/css">
		<link rel="stylesheet" href="../css/other/dtreeOfRole.css" type="text/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">

			function doReturn() {
				var sis = document.getElementsByName("_selectItem");
				var rtn = new Array();
				var p = 0;

				if (sis.length != null) {
					for (var i=0; i<sis.length; i++) {
						var e = sis[i];
						if (e.type == 'checkbox') {
							if (e.checked && e.value) {
								rtn[p++] = e.value;
							}
						}
					}
				}
				else {
					var e = sis;
					if (e.type == 'checkbox') {
						if (e.checked && e.value) {
							rtn[p++] = e.value;
						}
					}
				}

				OBPM.dialog.doReturn(rtn.toString());
			}

			function doExit() {
				OBPM.dialog.doReturn();
			}


			function doInit(){
				if(OBPM.dialog) {
					var oField = OBPM.dialog.getArgs()['oField'];
					if (oField.value){
						var sis = document.getElementsByName("_selectItem");
						var str = oField.value;
						str = str.substring(1, str.length - 1);

						var checkedArray = str.split(";");
						for (var i=0; i < checkedArray.size(); i++) {

							if (sis) {
								for (var j=0; j < sis.length; j++) {
									var e = sis[j];
									toggleCheck(e, checkedArray);
								}
							}
						}

					}
				}
			}

			//以 role.id相等 为回显条件
			function toggleCheck(oEl, checkedValues){
				if(oEl.value == ''){
					return;
				}
				oElArray = oEl.value.split("|");
				for (var i=0; i < checkedValues.size(); i++) {
					var str = checkedValues[i];
					var checkedValuesArray = str.split("|");
					if(oElArray[0] == checkedValuesArray[0]){
						oEl.checked = true;
						d.openTo(checkedValues[i]);
					}
				}
			}


			jQuery(document).ready(function(){
				doInit();
				cssListTable();
			});

		</script>
	</head>
	<body class="body-back">
	<form name="formList" method="post" action="">
		<tr style="display:none;">
			<td colspan="2">
				<input type="hidden" name="" value="_orderby"/> </td>
		</tr>

		<table border="0" cellpadding="4" cellspacing="0" width="100%">
			<tr>
				<td class="line-position2" width="50"  valign="top">
					<button type="button" class="button-class" onClick="doReturn();">
						<img src="../images/act_4.gif">确认</button>
				</td>

				<td class="line-position2" width="50" valign="top">
					<button type="button" class="button-class" onClick="OBPM.dialog.doReturn('');">
						<img src="../images/remove.gif">清除</button>
				</td>

				<td class="line-position2" width="50" valign="top">
					<button type="button" class="button-class" onClick="doExit()">
						<img src="../images/act_3.gif">退出</button>
				</td>

			</tr>

			<tr>
				<td colspan="3" class="list-srchbar"></td>
			</tr>
		</table>
		<div id="searchFormTable" class="justForHelp" title="查询角色" style="width: 98%">
			<table class="table_noborder">
				<tr>
					<td class="head-text">
						名称:<input class="input-cmd" type="text" name="sm_name" value='${requestScope["sm_name"]}' size="10" />
						<input class="button-cmd" type="button" onclick="document.forms[0].submit();" value="查询" />
						<input class="button-cmd" type="button" value="重置" onclick="resetAll()" />
					</td></tr>
			</table>
		</div>
		<table border="0" cellpadding="0" cellspacing="1" width="100%">
			<tr class="row-hd">
				<td valign="top"><div class="dtree" style="height:100%;">
					<script type="text/javascript">

						var selArray = new Array();
						var dt = new dTree('dt', 'rolelist','_selectItem')
						dt.config.multiSelect = true;

						//--定义一个默认根,名称为：角色选择--
						dt.add(
								'1001',
								-1,
								'角色选择',
								'javascript:selectOne(\'1001;角色选择\');',
								'1001',
								'',
								'<%=contextPath%>/qm/images/app.gif',
								'',
								'false',
								'',
								'');

						//-----应用列表-----
						<c:forEach var="list" items="${applicationlist}">
						dt.add(
								'${list.id}',
								'1001',
								'${list.name}',
								'javascript:selectOne(\'${list.id};${list.name}\');',
								'${list.id}',
								'',
								'<%=contextPath%>/qm/images/app.gif',
								'<%=contextPath%>/qm/images/app.gif',
								'',
								'',
								'',
								'${list.activated}');
						selArray[selArray.length] = {id:'${list.id}',activated:'${list.activated}'};
						</c:forEach>

						//--------应用下的角色列表--------
						<c:forEach var="list" items="${rolelist}">
						dt.add(
								'${list.id}',
								'${list.applicationid}',
								'${list.name}',
								'javascript:selectOne(\'${list.id};${list.name}\');',
								'${list.id}|${list.name}',
								'',
								'<%=contextPath%>/qm/images/role.gif',
								'<%=contextPath%>/qm/images/role.gif',
								'',
								'',
								'');
						</c:forEach>


						document.write(dt);

						function displayroles(tempValue){
							if(tempValue == 'all'){
								jQuery(".dTreeNode").css("display","block");
								dt.openAll();
								km_dt.openAll();
							}else if(tempValue == 'selected'){
								showCheckRoles();
								dt.openAll();
								km_dt.openAll();
							}
							setHideRole();//隐藏禁用软件的角色
						}

						function showCheckRoles(){
							jQuery(".dTreeNode").css("display","block");
							jQuery("input[name=_roleSelectItem]").each(function(i){
								if(jQuery(this).attr("checked") != 'checked'){
									jQuery(this).parent().css("display","none");
								}
							});

							jQuery("a").each(function(i){
								jQuery(this).parent().css("display","block");
							});
						}

						function showSearchRoles(){
							dt.openAll();
							var textVal = document.getElementById("filterCondition").value;
							jQuery("input[name=_roleSelectItem]").each(function(i){
								var StrVal = jQuery(this).attr("text");
								if(StrVal.indexOf(textVal) == -1){
									jQuery(this).parent().css("display","none");
								}else{
									jQuery(this).parent().css("display","block");
								}
							});

							jQuery("a").each(function(i){
								jQuery(this).parent().css("display","block");
							});
							setHideRole();//隐藏禁用软件的角色
						}
                        if(document.getElementsByName("sm_name")[0].value){
                            dt.openAll();
                        }
					</script>
				</div></td>
			</tr>

		</table>
	</form>
	</body>
</o:MultiLanguage></html>
