var SysHotelGroup = function (groupTree) {
    this.groupTree = $(groupTree);
    $("#dialog").find(".dlg-footer .dlg-btn-save").bind("click", this.save);
};

SysHotelGroup.prototype = {
    add: function () {
        var selectedNode = this.groupTree.treegrid("getSelected");
        if (selectedNode) {
            $("#dialog")
				.find(".dlg-content").panel({ href: "/SysHotel/EditSysHotelGroup/", queryParams: { ParentID: selectedNode.ID} }).end()
				.dialog({
				    title: lang.NEW + " " + selectedNode.Name + " " + lang.SubGrouping,
				    width: 300,
				    height: 190
				})
				.dialog("open");
        } else {
            $.tools.alert(lang.PSAG);
        }
    },
    edit: function () {
        var selectedNode = this.groupTree.treegrid("getSelected");
        if (selectedNode) {
            $("#dialog")
				.find(".dlg-content").panel({ href: "/SysHotel/EditSysHotelGroup/", queryParams: { ID: selectedNode.ID} }).end()
				.dialog({
				    title: lang.EDIT + " " + selectedNode.Name + " " + lang.Grouping,
				    width: 300,
				    height: 190
				})
				.dialog("open");
        } else {
            $.tools.alert(lang.PSAG);
        }
    },
    del: function () {
        var node = this.groupTree.treegrid("getSelected");
        if (node) {
            var children = this.groupTree.treegrid("getChildren", node.ID);
            if (children.length > 0) {
                $.tools.alert(lang.PDTSGFTD + node.Name + " " + lang.Grouping);
                return;
            }
            var param = "id=" + node.ID;
            $.tools.delPost(param, "/SysHotel/DeleteSysHotelGroup/", function () {
                this.groupTree.treegrid("reload");
            }, this);
        } else {
            $.tools.alert(lang.PSAG);
        }
    },
    save: function () {
        var form = $("#dialog").find("form");
        if (form.form("enableValidation").form("validate")) {
            var entry = form.serializeJson();
            entry.Parent = { ID: entry.ParentID };
            var param = { jsonData: JSON.stringify(entry) };
            $.tools.post(param, "/SysHotel/SaveSysHotelGroup/", function () {
                $("#dialog").dialog("close");
                $("#groupTree").treegrid("reload");
            }, this);
        }
    }
};