<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="org.wdk.WDK"%>
<%@page import="org.wdk.WDKConstant"%>
<%@page import="org.wdk.WDKCore"%>
<%@page import="com.noblelift.imp.platform.module.portal.source.SessionUser"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
String WDK_ROOT = WDKCore.WDK_ROOT;
SessionUser su = (SessionUser)WDK.getSessionUser(request, response);
WDK.writeSessionUser(out, su);
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>用户管理-应用授权</title>
<base href="<%=basePath%>" target="_self">

<script type="text/javascript" src="<%=WDK_ROOT%>/theme/je/core/js/wdk.js"></script>
<style>
   	/*双树要修改的样式*/
   	.layout_twotree * {
   		box-sizing: border-box;
   	}
   	.table-col {
   		float: left;
   	}
   	.action-btns .btn {
   		display: block;
   	}
   </style>
</head>

<body class="easyui-layout win" data-options="fit:true">
	<div data-options="region:'north',border:false,collapsed:false" class="page-title" style="overflow:hidden">
		<div class="page-toolbar" style="border:0;">
			<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fa fa-save'" id="save">保存</a>
    		<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fa fa-close'" id="cancel">取消</a>
		</div>
	</div>
	<div data-options="region:'center',border:false,collapsed:false">
		<div id="layout_twotree" class="layout_twotree" style="height:100%;"></div>
	</div>
</body>

<script type="text/javascript">

//获取参数数据
var param = $.getURLParam('param');
var jparam = $.str2json($.urlparam_decode(param));
var _account_uuid = jparam.queryParams.account_uuid;
var wid = $.getURLParam("wid");

$(function(){ 
	//注册双树控件
	$.twotree({
		id:'layout_twotree'
		,srcurl:'wdk?action=wdk.pub&method=call_service&ajaxparam='+new Date().getTime()
		,srcQueryParams:{
		    	_SRVNAME:'service.usr.authorization'
	   	   	  	,_SRVMETHOD:'queryUnAuthedAppListByAccID'
	   	   		,_DATA:$.json2str({
					op_account_uuid:$.getSessionUser().account_id
	   	 			,account_uuid:_account_uuid
	   	 			,resultfields:''
	   	 			,orderfields:''
	   	   		})
	   	   		,_RSTYPE:'tree'
	   	   		,_RSFIELD:'resultset'
	   	   		,_RSPARAM:$.json2str({
	   	   			idField:'resource_uuid'
	   	   			,textField:'resource_name'
	   	   			,parentField:'resource_parent_id'
	   	   		})
		}
		,desturl:'wdk?action=wdk.pub&method=call_service&ajaxparam='+new Date().getTime()
		,destQueryParams:{
				_SRVNAME:'service.usr.authorization'
	   	   	  	,_SRVMETHOD:'queryAuthedAppListByAccID'
	   	   		,_DATA:$.json2str({
					op_account_uuid:$.getSessionUser().account_id
	   	 			,account_uuid:_account_uuid
	   	 			,resultfields:''
	   	 			,orderfields:''
	   	   		})
	   	   		,_RSTYPE:'tree'
	   	   		,_RSFIELD:'resultset'
	   	   		,_RSPARAM:$.json2str({
	   	   				idField:'resource_uuid'
		   	   			,textField:'resource_name'
		   	   			,parentField:'resource_parent_id'
	   	   		})
		}
		,onBeforeLoad:function(){
			var rtreeObj = $.fn.zTree.getZTreeObj("layout_twotree_ltree");
			if(rtreeObj){
				$.wait_open();
			}
		}
		,onLoadSuccess:function(){
			var rtreeObj = $.fn.zTree.getZTreeObj("layout_twotree_rtree");
			if(rtreeObj){
				$.wait_close();
				rtreeObj.expandAll(true);
			}
		}
	});
	
 
});
  

$('#save').on("click", function(e){
    var nodes = $.twotree_getNode({id:'layout_twotree',srcordest:'dest'});
    if(!nodes){
    	nodes = new Array();
    }
    var infos = new Array();
    nodes.forEach(function(node){
    	if(node.parentTId){
	    	var info = new Object();
	    	info.app_uuid = node.id;
	    	infos.push(info);
    	}
    });
    
    $.wait_open();
	$.cuajax({
		url:'wdk?action=wdk.pub&method=call_service&ajaxparam='+new Date().getTime(),
 		method:"post", 
  		timeout:WDK_Timeout,
  		data:{
	        _SRVNAME:'service.usr.authorization'
	        ,_SRVMETHOD:'setAppToAcc'
	        ,_DATA:$.json2str({
	        		op_account_uuid:$.getSessionUser().account_id
		        	,account_uuid:_account_uuid
		        	,accountapp_info:infos
	        })
		},
  		success:function(result) {
  			$.wait_close();
  			//返回值字符串转json
    		var jres = $.str2json(result);
        	if(jres.code=="1"){  //操作成功
        		alert(jres.desc);
        		$.closeWin(wid);
        	}else{
        		alert(jres.desc);
        	}
  		},
  		error:function(result){
  			$.wait_close();
    		alert('网络错误!result='+result);
  		}
    });
});
$('#cancel').on("click", function(e){
	$.closeWin(wid);
});

</script>
</html>