/**
 * 变压器的节点维护
 */
$(document).ready(function () {
    var tfIndex = parent.$("#tfIndex").val();
    var subIndex = parent.$("#subIndex").val();
    var ifTrue = false;
    var getSubvIndex = 0;//母线个数
    //查询当前变压器是否已绑定节点
    $.ajax({
        type: "POST",
        url: "/pqs9900/tflg/selectTwoName",
        data: {tfIndex: tfIndex},
        success: function (data) {
            $.each(data, function (i, item) {
                $("#tf_Index").val(item.tfName);
                $("#subIndex").val(item.subIndex);
                subIndex = item.subIndex;
                $.ajax({
                    type: "POST",
                    url: "/pqs9900/tflg/getSubvIndex",
                    data: {subIndex: subIndex},
                    success: function (data) {
                        if (data < 2) {
                            document.getElementById("myself").style.display = 'none';
                            document.getElementById("other").style.display = 'block';
                            document.getElementById("other").style = 'margin-left:5%;margin-top: 10px';
                            document.getElementById("divOther").style.display='block';
                            document.getElementById("divMyself").style.display='none';
                            $.ajax({
                                type: "POST",
                                url: "/pqs9900/tflg/selectByMe",
                                data: {tfIndex: tfIndex},
                                success: function (data) {
                                    $.each(data, function (i, item) {
                                        if (item != null) {
                                            var option = $("<option value='"
                                                + item.logicBefore + "'>"
                                                + item.logicBefore + "(" + item.logicBeforeName + ")"+ "</option>");
                                            $("#logicK_before").append(option);
                                            var option1 = $("<option value='"
                                                + item.logicNext + "'>"
                                                + item.logicNext + "(" + item.logicNextName + ")"+ "</option>");
                                            $("#logicK_next").append(option1);
                                        } else {
                                            var option = $("<option value='"
                                                + 0 + "'>"
                                                + "- - - 请选择- - - -" + "</option>");
                                            $("#logicK_before").append(option);
                                            $("#logicK_next").append(option);
                                        }
                                    })
                                }
                            })
                            ifTrue = true;
                            //selectIndexKAll(subIndex);
                        } else {
                            document.getElementById("divOther").style.display='none';
                            document.getElementById("divMyself").style.display='block';
                            $.ajax({
                                type: "POST",
                                url: "/pqs9900/tflg/selectByMe",
                                data: {tfIndex: tfIndex},
                                success: function (data) {
                                    $.each(data, function (i, item) {
                                        if (item != null) {
                                            var option = $("<option value='"
                                                + item.logicBefore + "'>"
                                                + item.logicBefore + "(" + item.logicBeforeName + ")"+ "</option>");
                                            $("#logic_before").append(option);
                                            var option1 = $("<option value='"
                                                + item.logicNext + "'>"
                                                + item.logicNext + "(" + item.logicNextName + ")"+ "</option>");
                                            $("#logic_next").append(option1);
                                        } else {
                                            var option = $("<option value='"
                                                + 0 + "'>"
                                                + "- - - 请选择- - - -" + "</option>");
                                            $("#logic_before").append(option);
                                            $("#logic_next").append(option);
                                        }
                                    })
                                }
                            })
                            ifTrue = false;
                            //  selectIndexAll(subIndex);
                        }
                    }
                })
                $("#subIndex").val(subIndex);
            })
        }
    })
    $.ajax({
        type: "POST",
        url: "/pqs9900/tflg/selectIdAll",
        data: {tfIndex: tfIndex},
        success: function (data) {
            $.each(data, function (i, item) {
                $("#logic_Before").val(item.logicBefore);
                $("#logic_Next").val(item.logicNext);
                $.ajax({
                    type: "POST",
                    url: "/pqs9900/tflg/selectIdName",
                    data: {userIndex: item.userIndex},
                    success: function (data) {
                        $("#userIndex").val(data);
                    }
                })
            })
        }
    })
    //切换
    $("#other").click(function () {
        document.getElementById("divOther").style.display = 'block';
        document.getElementById("divMyself").style.display = 'none';
        ifTrue = true;
        document.getElementById("logicK_before").options.length = 0
        document.getElementById("logicK_next").options.length = 0
        selectIndexKAll(subIndex);
    })
    $("#myself").click(function () {
        document.getElementById("divOther").style.display = 'none';
        document.getElementById("divMyself").style.display = 'block';
        ifTrue = false;
        document.getElementById("logic_before").options.length = 0
        document.getElementById("logic_next").options.length = 0
        selectIndexAll(subIndex);
    })

    function selectIndexAll(subIndex) {
        $.ajax({
            type: "POST",
            url: "/pqs9900/tflg/selectIndexAll",
            data: {
                subIndex: subIndex,
                tfIndex: tfIndex
            },
            success: function (data) {
                $.each(data.logicBefore, function (i, item) {
                    var option = $("<option value='"
                        + item.subvIndex + "'>"
                        + item.subvIndex + "(" + item.subvName + ")" + "</option>");
                    $("#logic_before").append(option);
                })
                $.each(data.logicNext, function (i, item) {
                    var option1 = $("<option value='"
                        + item.subvIndex + "'>"
                        + item.subvIndex + "(" + item.subvName + ")" + "</option>");
                    $("#logic_next").append(option1);
                })
            }
        })
    }

    function selectIndexKAll(subIndex) {
        $.ajax({
            type: "POST",
            url: "/pqs9900/tflg/selectIndexKAll",
            data: {
                subIndex: subIndex
            },
            success: function (data) {
                $.each(data.logicNext, function (i, item) {
                    var option = $("<option value='"
                        + item.subvIndex + "'>"
                        + item.subvIndex + "(" + item.subName + " " + item.subvName + ")" + "</option>");
                    $("#logicK_before").append(option);
                })
                $.each(data.logicBefore, function (i, item) {
                    var option1 = $("<option value='"
                        + item.subvIndex + "'>"
                        + item.subvIndex + "(" + item.subvName + ")" + "</option>");
                    $("#logicK_next").append(option1);
                })
            }
        })
    }

    //关闭
    $("#close").click(function () {
        parent.layer.closeAll();
    })

    $("#logic_before").change(function () {
        var subvIndex = $(this).val();
        var subIndex = $("#subIndex").val();
        document.getElementById("logic_next").options.length = 0
        $.ajax({
            type: "POST",
            url: "/pqs9900/tflg/selectExAll",
            data: {subvIndex: subvIndex, subIndex: subIndex},
            success: function (data) {

                $.each(data, function (i, item) {

                    var option = $("<option value='"
                        + item.subvIndex + "'>"
                        + item.subvIndex + "(" + item.subvName + ")" + "</option>");
                    $("#logic_next").append(option);
                })
            }
        })

    });

    $("#updateTfLgAss").click(function () {
        if (ifTrue == false) {
            var logicBefore = $("#logic_before").val();
            var logicNext = $("#logic_next").val();
            if (logicBefore != 0) {
                if (logicNext != 0) {
                    $.ajax({
                        type: "POST",
                        url: "/pqs9900/tflg/updateTfLgAss",
                        data: {
                            tfIndex: tfIndex,
                            logicBefore: logicBefore,
                            logicNext: logicNext
                        },
                        success: function (data) {
                            if (data == 0) {
                                layer.msg("逻辑节点维护失败", {
                                        icon: 5,
                                        time: 1000
                                    },
                                    function () {
                                        parent.layer
                                            .closeAll();
                                    })
                            } else if (data == 1) {
                                layer.msg("逻辑节点维护成功", {
                                        icon: 6,
                                        time: 1000
                                    },
                                    function () {
                                        parent.layer
                                            .closeAll();
                                    })
                            } else {
                                layer.msg("逻辑节点维护失败,请稍后再试......", {
                                        icon: 5,
                                        time: 1000
                                    },
                                    function () {
                                        parent.layer
                                            .closeAll();
                                    })
                            }
                        }
                    })
                } else {
                    layer.msg("请选择下节点", {
                        icon: 5,
                        time: 1000
                    })
                }
            } else {
                layer.msg("请选择上节点", {
                    icon: 5,
                    time: 1000
                })
            }
        } else {
            var logicBefore = $("#logicK_before").val();
            var logicNext = $("#logicK_next").val();
            if (logicBefore != 0) {
                if (logicNext != 0) {
                    $.ajax({
                        type: "POST",
                        url: "/pqs9900/tflg/updateTfLgAss",
                        data: {
                            tfIndex: tfIndex,
                            logicBefore: logicBefore,
                            logicNext: logicNext
                        },
                        success: function (data) {
                            if (data == 0) {
                                layer.msg("逻辑节点维护失败", {
                                        icon: 5,
                                        time: 1000
                                    },
                                    function () {
                                        parent.layer
                                            .closeAll();
                                    })
                            } else if (data == 1) {
                                layer.msg("逻辑节点维护成功", {
                                        icon: 6,
                                        time: 1000
                                    },
                                    function () {
                                        parent.layer
                                            .closeAll();
                                    })
                            } else {
                                layer.msg("逻辑节点维护失败,请稍后再试......", {
                                        icon: 5,
                                        time: 1000
                                    },
                                    function () {
                                        parent.layer
                                            .closeAll();
                                    })
                            }
                        }
                    })
                } else {
                    layer.msg("请选择下节点", {
                        icon: 5,
                        time: 1000
                    })
                }
            } else {
                layer.msg("请选择上节点", {
                    icon: 5,
                    time: 1000
                })
            }
        }

    })

});