var srvID = "";
var userID = "";
var aryau = "";
var ifrm;
var tbname = "TCWL_All_Dic_GongGaoType";
$(function () {
    $("[tag]").hide();
});
document.onkeydown = function (event) {
    var target, code, tag;
    if (!event) {
        event = window.event; 
        target = event.srcElement;
        code = event.keyCode;
        if (code == 13) {
            tag = target.tagName;
            if (tag == "TEXTAREA") { return true; }
            else { return false; }
        }
    }
    else {
        target = event.target; 
        code = event.keyCode;
        if (code == 13) {
            tag = target.tagName;
            if (tag == "INPUT") { return false; }
            else { return true; }
        }
    }
};
function resize_r() {
    var s = 0;
    var ss = $(window.parent.document).find('iframe[id=' + ifrm + ']');
    var l = $(window.parent.document).height() - ss.offset().top;
    var t = $(document).height();
    if (l > t) {
        s = l;
    }
    else {
        s = $(document).height();
    }
    $($(window.parent.document).find('iframe[id=' + ifrm + ']')).height(s);
    window.parent.sidebar_r();
}
function existRole(s, p) {
    var e = p.indexOf(s);
    if (e == -1) {
        return false;
    }
    else {
        return true;
    }
}
function SelRole() {
    $('[tag]').each(function (i, e) {
        var tag = $(e).attr('tag');
        if (existRole(tag, aryau)) {
            $(e).show();
        }
        else {
            $(e).hide();
        }
    });
}
$(function () {
    var coks = document.cookie.substring(document.cookie.indexOf('userinf_tcwl'));
    var coksau = document.cookie.substring(document.cookie.indexOf('TCWL_All'));
    var ary = coks.split(';')[0].split('=');
    aryau = coksau.split(';')[0].split('=')[2];
    if (!ary[2]) {
        $('html').empty();
        return;
    }
    userID = ary[2].split('&')[0];
    srvID = ary[3];
    $(document).on('click', 'th input:checkbox', function () {
        var that = this;
        $(this).closest('table').find('tr > td:first-child input:checkbox')
			.each(function () {
			    this.checked = that.checked;
			    $(this).closest('tr').toggleClass('selected');
			});
    });
});
function LoadData() {
    var s = window.location.href.split('=');
    if (s.length == 4) {
        ifrm = s[3];
    }
    resize_r();
    loadP();
}
function loadP() {
    var l = layer.load('正在加载数据......');
    $(".ace:checkbox").attr("checked", false);
    $.ajax({
        url: "../Handlers/system/DataDic.aspx?Method=Load&srvid=" + srvID + "&userid=" + userID + "&tbname=" + tbname + "",
        cache: false,
        dataType: "json",
        success: function (data) {
            createTable(data);
            layer.close(l);
            resize_r();
        },
        error: function () { layer.msg('网络错误！', 1, 3); }
    });
}
function createTable(data) {
    $('th input:checkbox').removeAttr("checked");
    var tb = document.getElementById("Management");
    for (var i = tb.rows.length - 1; i > 0; i--) {
        tb.deleteRow(i);
    }
    for (var i = 0; i < data.rows.length; i++) {
        var _tr1 = $('<tr></tr>');
        var _td0 = $('<td></td>');
        _td0.html("<input type='checkbox'  class='ace'><label class='lbl'></label>");
        var _td1 = $('<td></td>');
        _td1.append(data.rows[i].text);
        var _td2 = $('<td class="center"></td>');
        _td2.append('<div class="wjsygn"><a  href="javascript:void(0);" onclick="Edit_Gg(this)" title="修改" tag="M_119_E"><span class="tbqbgw">修改</span></i></a><a title="删除" tag="M_119_D" href="javascript:Del(\'' + data.rows[i].id + '\');"><span class="tbqbgw">删除</span></a></div>');
        var _td3 = $('<td style="display: none;"></td>');
        _td3.text(data.rows[i].id);
        _tr1.append(_td0);
        _tr1.append(_td1);
        _tr1.append(_td2);
        _tr1.append(_td3);
        $('#Management tbody').append(_tr1);
    }
    SelRole();
}
function Del(obj) {
    layer.confirm("确定删除吗？", function () {
        $.ajax({ url: "../Handlers/system/DataDic.aspx?Method=del&srvid=" + srvID + "&tbname=" + tbname + "&ID=" + obj,
            cache: false,
            type: 'post',
            success: function (data) {
                if (data == "success") {
                    loadP();
                    layer.msg('删除成功！', 1, 1);
                }
                else {
                    layer.msg('删除失败！', 1, 3);
                }
            },
            error: function () { layer.msg('删除错误！', 1, 3); }
        });
    }
            );
}
function Edit_Gg(obj) {
    var tds = $(obj).parent().parent().parent().find("td");
    var ID = tds.eq(3).text();
    var l = layer.load("正在加载...");
    $.ajax({ url: "../Handlers/system/DataDic.aspx?Method=GetByID&tbname=" + tbname + "&ID=" + ID,
        cache: false,
        type: 'get',
        success: function (data) {
            $("#eggTitle").val(data);
            $("#EditHid").val(ID);
            layer.close(l);
            $("#EditDialog").modal("show");
        },
        error: function () { layer.msg('网络错误！', 1, 3); }
    });
}
function Update() {
    var id = $("#EditHid").val();
    if ($("#eggTitle").val() == "") {
        $("#eggTitle").focus();
        return;
    }
    var l = layer.load("正在执行...");
    $.ajax({ url: "../Handlers/system/DataDic.aspx?Method=edit&srvid=" + srvID + "&tbname=" + tbname + "&ID=" + id,
        cache: false,
        type: 'post',
        data: $("#efmGonggao").serialize(),
        success: function (data) {
            if (data == "success") {
                layer.msg("修改成功！", 2, 1);
                $('#EditDialog').modal('hide');
                loadP();
            }
            else if (data == "exist") {
                layer.msg("已经存在此名称！", 2, 3);
            }
            else {
                layer.msg('修改失败！', 1, 3);
            }
        },
        error: function () { layer.msg('修改失败！', 1, 3); }
    });
}
function ADD_Gg() {
    $("#ggTitle").val("");
    $("#AddDialog").modal("show");
}
function Add() {
    if ($("#ggTitle").val() == "") {
        $("#ggTitle").focus();
        return;
    }
    var l = layer.load("正在添加...");
    $.ajax({ url: "../Handlers/system/DataDic.aspx?Method=add&srvid=" + srvID + "&tbname=" + tbname + "",
        cache: false,
        type: 'post',
        data: $("#fmGonggao").serialize(),
        success: function (data) {
            if (data == "success") {
                loadP();
                layer.msg("添加成功！", 1, 1);
                $('#AddDialog').modal('hide');
            }
            else {
                if (data == "exist") {
                    layer.msg('已经存在此名称！', 1, 3);
                }
                else {
                    layer.msg('添加信息失败！', 1, 3);
                }
            }
        },
        error: function () { layer.msg('添加角色信息错误！', 1, 3); }
    });
}