/**
 * 添加通用字典
 */
var leavenum = 0;
var tyIndex = "";
$(document).ready(
    function () {
        tyIndex = parent.typeId;
        $.ajax({
            method: "post",
            url: "/pqs9900/dic/selectAll",
            data: "",
            success: function (data) {

                $.each(data, function (i, item) {
                    var option = $("<option value='" + item.dicTypeIndex
                        + "'>" + item.dicTypeName + "</option>");
                    $("#dicType").append(option);

                });
                if (tyIndex == "0" || tyIndex == "") {
                    $("#dicType").val("---请选择---");
                } else {
                    $("#dicType").val(tyIndex);
                }
            }
        });
        $("#close").click(function () {
            parent.layer.closeAll();
        });
        var dicType = $("#dicType").val();
        if (tyIndex == "0" || tyIndex == "") {
            dicType = $("#dicType").val("---请选择---");
        } else {
            dicType = tyIndex;
        }
        $("#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;
            }
        });
        $("#insertAddData").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 {
                if (dicType == "" || dicType == "---请选择---") {
                    layer.tips('字典类型未选择', '#dicType', {
                        tips: [1, 'red']
                        // 还可配置颜色
                    });
                    $("#dicType").prop("placeholder", "请选择字典类型");
                } else {
                    if (leavenum == 1) {
                        leave = $("#proleave").val();
                    } else {
                        leave = $("#leave").val();
                    }
                    var triphase = $("#triphase").val();
                    triphase = filterXSS(triphase);
                    if (triphase == "") {
                        triphase = triphase.replace(/(^\s*)|(\s*$)/g, '');
                        var re = numberz;// 只能输入数字
                        if (!re.test(triphase)) {
                            layer.tips('三相非法', '#triphase', {
                                tips: [1, 'red']
                                // 还可配置颜色
                            });
                            $("#triphase").val("");
                            $("#triphase").prop("placeholder", "只能由1-3位数字组成");
                        } else {
                            $.ajax({
                                url: '/pqs9900/dic/saveData',
                                type: 'POST',
                                dataType: "text",
                                data: {
                                    dicName: dicName,
                                    dicType: dicType,
                                    leave: leave,
                                    triphase: triphase
                                },
                                success: function (data) {
                                    // 没有权限的请求
                                    if (data == 300) {
                                        location.href = '/pqs9900/auth/unauthorized';
                                        // 没有登陆的请求
                                    } else if (data == 301) {
                                        location.href = '/pqs9900';
                                    } else {
                                        var info = $.parseJSON(data);
                                        if (info.code != 200) {
                                            layer.msg(info.message, {
                                                icon: 5
                                            });
                                        } else {
                                            layer.msg(info.message, {
                                                icon: 6,
                                                time: 1000
                                            }, function () {
                                                parent.cc(dicType);
                                                parent.layer.closeAll();
                                            });
                                        }
                                    }
                                }
                            });
                        }
                    } else {
                        $.ajax({
                            url: '/pqs9900/dic/saveData',
                            type: 'POST',
                            dataType: "text",
                            data: {
                                dicName: dicName,
                                dicType: dicType,
                                leave: leave,
                                triphase: triphase
                            },
                            success: function (data) {
                                // 没有权限的请求
                                if (data == 300) {
                                    location.href = '/pqs9900/auth/unauthorized';
                                    // 没有登陆的请求
                                } else if (data == 301) {
                                    location.href = '/pqs9900';
                                } else {
                                    var info = $.parseJSON(data);
                                    if (info.code != 200) {
                                        layer.msg(info.message, {
                                            icon: 5
                                        });
                                    } else {
                                        layer.msg(info.message, {
                                            icon: 6,
                                            time: 1000
                                        }, function () {
                                            parent.cc(dicType);
                                            parent.layer.closeAll();
                                        });
                                    }
                                }
                            }
                        });
                    }
                }
            }
        });
    })
