/**
 * 权限管理
 */
$(function(){
	
	AppAuthorization = {
        Events : {'click .btn': function (e, value, row, index) {
			var event = $(e.target).data('event');
			if(event == 'view'){
                AppAuthorization.view(value);
			}else if(event == 'edit'){
                AppAuthorization.edit(value);
			}else if(event == 'del'){
                AppAuthorization.del(value);
			}
		}},
		add : function(){
			MsgBox.window({
				title	: '角色管理',
				url		: Config.ROOT+'/sys/role/edit',
				callback: function (r) {
					$table.reload(1);
				}
			});
		},
		edit: function(oid){
			MsgBox.window({
				title	: '角色管理',
				url		: Config.ROOT+'/sys/role/edit?oid='+oid,
				callback: function (r) {
					$table.reload();
				}
			});
		},
        view: function(oid){
			MsgBox.window({
				title	: '角色管理',
				url		: Config.ROOT+'/sys/role/edit?editType=view&oid='+oid
			});
        },
		del : function(oids){
			if(!oids)return MsgBox.warnDelete();
			App.MsgBox.confirm('删除后将无法恢复，确认要删除吗？',function(){
				App.Ajax.request({
					url		: Config.ROOT+'/authoriz/deleteRole',
					params	: {
						'roleId': oids
					},
					success: function(resp){
                        $table.reload(1);
					}
				});
			});
		},
		saveRoleModule: function(){
			var rd = $table.getSelected();
			if(!rd || !rd.oid)return MsgBox.warn('请选择角色');
			var nodes = moduletree.getCheckedNodes(), moduleIds = [];
			$(nodes).each(function(node){
				moduleIds.push(this.oid);
			});
			App.Ajax.request({
				url		: Config.ROOT+'/authoriz/saveRoleModuleRelation',
				params	: {
					'roleId': rd.oid,
					'moduleIds': moduleIds.join(',')
				}
			});
		}
	};
	
	//初始化角色表格
    var $table = new App.Table({
        url: Config.ROOT + '/sys/role/list',
        singleSelect: true,
        showRefresh: false,
        showToggle: false,
        showColumns: false,
        search: true,
        searchParamName: 'name',
        searchPlaceholder: '角色名称',
        columns	:[
            {checkbox: true},
            {field:'name', title:'名称'},
            {field:'state', title:'状态', tpl: 'tpl_state'},
            {field:'oid', title:'操作', width: 150, toolbar: 'tableRowToolbar', events: AppAuthorization.Events}
        ],
        onClickRow: function(row, $element){
            $table.clickRow = row;
            moduletree.reAsyncChildNodes(null, "refresh");
        }
    });

    var moduletree = $('#authorization_module_tree').zTreeInit({
        hasCheckbox	: true,
        getAsyncUrl	: function(){
            var roleId = '';
            if($table.clickRow && $table.clickRow.oid){
                roleId = $table.clickRow.oid;
            }
            return Config.ROOT + '/authoriz/loadModuleTree?roleId='+roleId;
        }
    });

});