//查询按钮
function groupTreeOnSelect(row) {
    loadRoles();
}
function add() {
    $("#dialog")
		.find(".dlg-content").panel({ href: "/SysRoleGroup/Edit/", queryParams: {} }).end()
		.find(".dlg-footer .dlg-btn-save").unbind("click").bind("click", save).end()
		.dialog({
		    title: lang.NewRole,
		    width: 350,
		    height: 550
		})
		.dialog("open");
}
function getSelectedItem() {
    var data = $("#rolelist tr.selected").data("role");
    if (data == undefined) {
        data = null;
    }
    return data;
}
function edit() {
    var row = getSelectedItem();
    if (row) {
        if (row.ID === 1) {//Name === "超级管理员") {
            $.tools.alert(lang.CNESAR);
            return;
        }
        $("#dialog")
			.find(".dlg-content").panel({ href: "/SysRoleGroup/Edit/", queryParams: { ID: row.ID} }).end()
			.find(".dlg-footer .dlg-btn-save").unbind("click").bind("click", save).end()
			.dialog({
			    title: lang.EditRole,
			    width: 350,
			    height: 550
			})
			.dialog("open");
    } else {
        $.tools.alert(lang.PleaseSelectTheData);
    }
}
//删除数据
function delData() {
    var selectedRow = getSelectedItem();
    if (selectedRow) {
        if (selectedRow.ID === 1) {//Name === "超级管理员") {
            $.tools.alert(lang.CNDTSAR);
            return;
        }
        $.tools.delPost("id=" + selectedRow.ID, "/SysRoleGroup/Delete/", function () {
            loadRoles();
        }, this);
    } else {
        $.tools.alert(lang.PleaseSelectTheData);
    }
}
function save() {
    var form = $("#dialog").find("form");
    if (form.form("enableValidation").form("validate")) {
        var params = form.serialize();
        var authorityNodes = $('#treeAuthority').tree('getChecked'); // $("#dialog .authorities").tree("getChecked", ["checked", "indeterminate"]);
        if (authorityNodes.length == 0) {
            $.tools.alert(lang.PleaseSelectTheData);
            return;
        }
        $.each(authorityNodes, function (i, n) { params += "&Authorities=" + n.id; });
        $.tools.post(params, "/SysRoleGroup/Save/", function () {
            loadRoles();
            $("#dialog").window('close');
        }, this);
    }
}
function itemClick() {
    $(".role-list tr").removeClass("selected");
    $(this).addClass("selected");
}
function itemDblClick() { edit(); }

function appendItem(obj) {
    var td1 = $("<td class='item-header'></td>").append("<img src='/images/role_32.png' alt='' />");
    var td2 = $("<td></td>").append("<h3></h3>").find("h3").text(obj.Name + " - (" + lang.Grouping + "：" + obj.GroupName + ")").end()
									.append("<span></span>").find("span").text(obj.Authorities).end();
    var tr = $("<tr></tr>").data("role", obj).append(td1, td2).bind("click", itemClick).bind("dblclick", itemDblClick);
    $("#rolelist").append(tr);
}
function loadData(data) {
    $("#rolelist tbody").remove();
    if (data instanceof Array) {
        for (var i in data) {
            appendItem(data[i]);
        }
    }
}
function loadRoles() {
    $.tools.post({}, "/SysRoleGroup/LoadAll2?groupId=" + $("#groupTree").treegrid('getSelected').ID, function (result) { loadData(result.data); }, this, true);
}
$(function () { });