/**
 * 修改数据字典
 */
var leavenum = 0;
$(document)
    .ready(
        function () {
            var triphase;
            var dicIndex = parent.$("#dicTypeIndex").val();
            var typeId = "";
            $.ajax({
                type: "POST",
                url: "/pqs9900/dic/selectId",
                data: {"dicIndex": dicIndex},
                success: function (data) {
                    console.log(data)
                    triphase = data.triphase;
                    $("#dicName").val(data.dicName);
                    $("#leave").val(data.dicLeave)
                    typeId = data.dicType;
                    selectTypeName(typeId,data.dicLeave);
                }
            })

            function selectTypeName(typeId,dicLeave) {
                $.ajax({
                    type: "POST",
                    url: "/pqs9900/dic/selectTypeName",
                    data: {typeId: typeId},
                    success: function (data) {
                        if(data.dicTypeName == '行政区域'){
                            $("#proleave").val(dicLeave)
                        }

                        var option = $("<option value='" + data.dicTypeIndex + "'>" + data.dicTypeName + "</option>");
                        $("#dicType").append(option);
                        $.ajax({
                            method: "post",
                            url: "/pqs9900/dic/All",
                            data: {typeId: typeId},
                            success: function (data) {
                                $.each(data, function (i, item) {
                                    var option = $("<option value='" + item.dicTypeIndex + "'>" + item.dicTypeName + "</option>");
                                    $("#dicType").append(option);
                                });
                                var dicType = $("#dicType").find("option:selected").text();
                                $("#triphase").val(-1);
                                dicType = filterXSS(dicType);
                                if ($("#dicType option:checked").text() == '暂降类型' || $("#dicType option:checked").text() == '暂降原因' || $("#dicType option:checked").text() == '变压器接线方式') {
                                    $("#ok").show();
                                    $("#triphase").val(triphase);
                                } else {
                                    $("#ok").hide();
                                    $("#triphase").val(-1);
                                }
                                if ($("#dicType option:checked").text() == '行政区域') {
                                    $("#isPro").show();
                                    $("#noPro").hide();
                                    leavenum = 1;
                                } else {
                                    $("#isPro").hide();
                                    $("#noPro").show();
                                    leavenum = 0;
                                }
                                if ($("#dicType option:checked").text() == '告警类型' || $("#dicType option:checked").text() == '系统事件类型'
                                    || $("#dicType option:checked").text() == '业务事件类型') {
                                    $("#noPro").show();
                                    leavenum = 0;
                                } else {
                                    $("#noPro").hide();
                                    leavenum = 1;
                                }
                            }
                        });
                    }
                })
            }

            $("#close").click(function () {
                parent.layer.closeAll();
            });
            var dicType = $("#dicType").find("option:selected").text();
            $("#triphase").val(-1);
            $("#dicType").click(function () {
                dicType = $(this).val();
                dicType = filterXSS(dicType);
                if ($("#dicType option:checked").text() == '暂降类型' || $("#dicType option:checked").text() == '暂降原因' || $("#dicType option:checked").text() == '变压器接线方式') {
                    $("#ok").show();
                    $("#triphase").val("");
                } else {
                    $("#ok").hide();
                    $("#triphase").val(-1);
                }
                if ($("#dicType option:checked").text() == '行政区域') {
                    $("#isPro").show();
                    $("#noPro").hide();
                    leavenum = 1;
                } else {
                    $("#isPro").hide();
                    $("#noPro").show();
                    leavenum = 0;
                }
                if ($("#dicType option:checked").text() == '告警类型' || $("#dicType option:checked").text() == '系统事件类型'
                    || $("#dicType option:checked").text() == '业务事件类型') {
                    $("#noPro").show();
                    leavenum = 0;
                } else {
                    $("#noPro").hide();
                    leavenum = 1;
                }
            });
            $("#updateData").click(function () {
                var dicName = $("#dicName")
                    .val();
                dicName = dicName.replace(
                    /(^\s*)|(\s*$)/g, '');
                dicName = filterXSS(dicName);
                var re = censz;//只能输入汉字、数字、英文字母、.、（）
                if (!re.test(dicName)) {
                    layer.tips('数据字典名非法',
                        '#dicName', {
                            tips: [1, 'red']
                            //还可配置颜色
                        });
                    $("#dicName").val("");
                    $("#dicName").prop(
                        "placeholder",
                        "数据字典名由汉字、数字、字母组成");
                } else {
                    var dicType = $(
                        "#dicType").val();
                    dicType = filterXSS(dicType);
                    var leave = null;
                    if (leavenum == 1) {
                        leave = $("#proleave").val();
                    } else {
                        leave = $("#leave").val();
                    }
                    $.ajax({
                        url: '/pqs9900/dic/updateData',
                        type: 'POST',
                        dataType: "text",
                        data: {
                            dicIndex: dicIndex,
                            dicName: dicName,
                            dicType: dicType,
                            dicLeave: leave
                        },
                        success: function (
                            data) {
                            //没有权限的请求
                            if (data == 300) {
                                location.href = '/pqs9900/auth/unauthorized';
                                //没有登陆的请求
                            } else if (data == 301) {
                                location.href = '/pqs9900';
                            } else {
                                if (data == 1) {
                                    layer.msg("修改字典成功", {
                                        icon: 6,
                                        time: 1000
                                    }, function () {
                                        parent.cc(dicType)
                                        parent.layer.closeAll()
                                    });
                                } else if (data == 0) {
                                    layer.msg("字典名已存在", {
                                        icon: 5,
                                        time: 1000
                                    })
                                } else if (data == 2) {
                                    layer.msg("字典名称为空", {
                                        icon: 5,
                                        time: 1000
                                    })
                                } else if (data == 3) {
                                    layer.msg("字典名称非法", {
                                        icon: 5,
                                        time: 1000
                                    })
                                } else {
                                    layer.msg("修改字典失败", {
                                        icon: 5,
                                        time: 1000
                                    })
                                }
                            }
                        }
                    });
                }
            });
        })
