//右边楼层选中事件
function groupTreeOnSelect(row) {
    $("#dg").datagrid("load", { groupId: $("#groupTree").treegrid('getSelected').ID });
}
//新增用户
function add() {
    $("#dialog")
		.find(".dlg-content").panel({ href: "/SysUser/Edit/", queryParams: {} }).end()
		.find(".dlg-footer .dlg-btn-save").unbind("click").bind("click", saveUser).end()
		.dialog({
		    title: lang.AddUser,
		    width: 470,
		    height: 400
		})
		.dialog("open");
}

//编辑用户
function edit(row) {
    row = row || $('#dg').datagrid('getSelected');
    if (row) {
        if (row.Account === 'admin') {
            $.tools.alert(lang.NoOperable);
            return;
        }
        $("#dialog")
			.find(".dlg-content").panel({ href: "/SysUser/Edit/", queryParams: { ID: row.ID} }).end()
			.find(".dlg-footer .dlg-btn-save").unbind("click").bind("click", saveUser).end()
			.dialog({
			    title: lang.EditUser + ":" + row.Account,
			    width: 470,
			    height: 400
			})
			.dialog("open");
    } else {
        $.tools.alert(lang.PleaseSelectTheData);
    }
}

//删除数据
function delData() {
    var rows = $('#dg').datagrid('getSelections');
    if (!rows || rows.length == 0) {
        $.tools.alert(lang.PleaseSelectTheData);
        return;
    }
    if (rows[0].Account === 'admin') {
        $.tools.alert(lang.NoOperable);
        return;
    }

    var param;
    $.each(rows, function (i, n) {
        if (i == 0) {
            param = "idList=" + n.ID + "&nameList=" + n.Account;
        } else {
            param += "&idList=" + n.ID + "&nameList=" + n.Account;
        }
    });

    $.tools.delPost(param, "/SysUser/Delete/", function () {
        $('#dg').datagrid("clearSelections");
        $('#dg').datagrid("reload");
    }, this);
}

//保存新增或编辑的用户信息
function saveUser() {
    var form = $("#dialog").find("form");
    if (form.form("enableValidation").form("validate")) {
        var entry = form.serializeJson();
        entry.Role = { ID: entry.RoleID };
        entry.Group = { ID: entry.GroupID };
        //entry.SysHotelGroup = { ID: entry.SysHotelGroupID };
        var param = { jsonData: JSON.stringify(entry) };

        $.tools.post(param, "/SysUser/Save/", function () {
            $('#dg').datagrid("reload");
            $("#dialog").window('close');
        }, this);
    }
}

//打开修改密码界面
function changePassword() {
    var row = $('#dg').datagrid('getSelected');
    if (row) {
        if (row.Account === 'admin') {
            $.tools.alert(lang.NoOperable);
            return;
        }
        $("#dialog")
			.find(".dlg-content").panel({ href: "/SysUser/ChangePassword/", queryParams: { ID: row.ID} }).end()
			.find(".dlg-footer .dlg-btn-save").unbind("click").bind("click", savePassword).end()
			.dialog({
			    title: lang.ResetPassword,
			    width: 300,
			    height: 190
			})
			.dialog("open");
    } else {
        $.tools.alert(lang.PleaseSelectTheData);
    }
}
//保存密码
function savePassword() {
    var form = $("#dialog").find("form");
    if (form.form("enableValidation").form("validate")) {
        var entry = form.serializeJson();
        if (entry.password != entry.password2) {
            $.tools.alert(lang.TPANCPR);
            return;
        }
        $.tools.post(entry, "/SysUser/ChangePassword/", function () {
            $("#dialog").dialog("close");
        }, this);
    }
}
//打开授权酒店界面
function authorityHotel(row) {
    row = row || $('#dg').datagrid('getSelected');
    if (row) {
        $("#dialog")
			.find(".dlg-content").panel({ href: "/SysUser/AuthorityHotel/", queryParams: { ID: row.ID} }).end()
			.find(".dlg-footer .dlg-btn-save").unbind("click").bind("click", saveAuthorityHotel).end()
			.dialog({
			    title: lang.EditUser + ":" + row.Account,
			    width: 470,
			    height: 500
			})
			.dialog("open");
    } else {
        $.tools.alert(lang.PleaseSelectTheData);
    }
}
//保存授权酒店
function saveAuthorityHotel() {
    var form = $("#dialog").find("form");
    if (form.form("enableValidation").form("validate")) {
        var params = form.serialize();
        var authorityHotels = $('#treeHotels').tree('getChecked');
        var hotelIDs = [];
        for (var i in authorityHotels) {
            if (authorityHotels[i].attributes.isHotel) {
                hotelIDs.push(authorityHotels[i].id);
            }
        }
        params += "&hotelIDs=" + JSON.stringify(hotelIDs);
        $.tools.post(params, "/SysUserGroup/SaveAuthorityHotel/", function () {
            $("#dialog").window('close');
        }, this);
    }
}

$(function () {
    window.group = new Group("#groupTree");
});