//查询按钮
function groupTreeOnSelect(row) {
    $("#dg").datagrid("load", { query: $("#txtQueryContent").val(), groupId: $("#groupTree").treegrid('getSelected').ID });
}

function cbxProvinceOnSelect(r) {
    $("#selCity").combobox("clear").combobox("reload", '/SysHotel/GetCity?provinceCode=' + r.Code);
    $("#selCounty").combobox("clear")
}

function cbxCityOnSelect(r) {
    $("#selCounty").combobox("clear").combobox("reload", '/SysHotel/GetCounty?cityCode=' + r.Code);
}

function add() {
    $("#dialog")
		.find(".dlg-content").panel({ href: "/SysHotel/Edit/", queryParams: {} }).end()
		.find(".dlg-footer .dlg-btn-save").unbind("click").bind("click", save).end()
		.dialog({ title: lang.NewHotel, width: 600, height: 350 })
		.dialog("open");
}

function edit(row) {
    row = row || $('#dg').datagrid('getSelected');
    if (row) {
        $("#dialog")
			.find(".dlg-content").panel({ href: "/SysHotel/Edit/", queryParams: { ID: row.ID} }).end()
			.find(".dlg-footer .dlg-btn-save").unbind("click").bind("click", save).end()
			.dialog({ title: lang.EditHotel + "(ID:" + row.ID + ")", width: 600, height: 350 })
			.dialog("open");
    } else {
        $.tools.alert(lang.PleaseSelectTheData);
    }
}

function del() {
    var rows = $('#dg').datagrid('getSelections');
    if (!rows || rows.length == 0) {
        $.tools.alert(lang.PleaseSelectTheData);
        return;
    }
    var param;
    $.each(rows, function (i, n) {
        if (i == 0) {
            param = "idList=" + n.ID;
        } else {
            param += "&idList=" + n.ID;
        }
    });
    $.tools.delPost(param, "/SysHotel/Delete/", function () {
        $('#dg').datagrid("clearSelections");
        $('#dg').datagrid("reload");
    }, this);
}

function save() {
    var form = $("#dialog").find("form");
    if (form.form("enableValidation").form("validate")) {
        var entry = form.serializeJson();
        entry.SysHotelGroup = { ID: entry.SysHotelGroupID };
        var param = { jsonData: JSON.stringify(entry) };

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

function setLogo(row) {
    row = row || $('#dg').datagrid('getSelected');
    if (row) {
        $('#uploadForm').form('reset');
        $('#tftpsetting').window('open');
        $("#txtID").attr("value", row.ID);
    } else {
        $.tools.alert(lang.PleaseSelectTheData);
    }
}

//上传logo
function uploadFile() {
    $('#uploadForm').form('submit', {
        url: '/SysHotel/UploadLogo/',
        onSubmit: function () {
            var file = uploadForm.file.value;
            var mime = file.toLowerCase().substr(file.lastIndexOf('.'));
            if (mime !== '.png') {
                $.tools.alert(lang.LOGOTYPE);
                return false;
            }

            return true;
        },
        success: function (r) {
            r = $.parseJSON(r);
            if (r.IsSuccess) {
                $('#uploadFile').window('close');
                $('#dg').datagrid('reload');
            }
            $.tools.alert(r.Message);
        }
    });
}

$(function () {
    window.group = new SysHotelGroup('#groupTree');
});