let relatedIntentValues = {
    oprMngCode: "",
    scenarioGroupName: "",
    relatedIntents: new Array(),
    checkIntent: new Array(),
    flagIntent: new Array(),
    flag: "N"
}

$(document).ready(function () {
    getOpr();
    // 시나리오 관리 modal 배포 datatable 초기화
    PagingDataTableVersion();
    modalUi();

    // 권한별 컨트롤 설정
    // "rcpconversationscenariomanageRead" : 시나리오 관리 GUI 읽기
    // "rcpconversationscenariomanageFull" " 시나리오 관리 GUI 읽기 수정
    // "rcpconversationscenariomanageRelease" " 시나리오 관리 GUI 배포
    if (!checkPermissionPolicy("rcpconversationscenariomanageFull")) {
        $("#btnScenarioRegistration").hide();
        $("#ipbScenarioName input").attr("readonly",true);

        $("#btnScenarioNameApply").hide();
        $("#btnScenarioEdit").hide();
        $("#btnScenarioDelete").hide();
        $("#btnSRelatedIntentSave").hide();

    } else {
        $("#ipbScenarioName input").attr("readonly",false);
        $("#btnScenarioView").hide();
    }

    if (!checkPermissionPolicy("rcpconversationscenariomanageFull")) {
        $("#btnScenarioRelease").hide();
    }

    // 버튼 클릭 이벤트 처리
    $('#btnSearchScenario').on('click', function () {
        fnScenarioSearchBtn();
    });

    $('#btnScenarioNameApply').on('click', function () {
        fnUpdateScenarioName();
    });

    $('#btnScenarioView').on('click', function () {
        let url = commonUtil.getContextPath() + "/admin/rcp/conversation/scenarioGUINodeEdit.do" + "?flag=read&seq=" + $("#managerScenario").attr("scSeq") + "&custCode=" + $('#serviceGroup option:selected').attr("custCode") + "&serviceGroup=" + $("#serviceGroup option:selected").val();
        $.utils.redirect(url);
    });

    $('#btnScenarioEdit').on('click', function () {
        let url = commonUtil.getContextPath() + "/admin/rcp/conversation/scenarioGUINodeEdit.do" + "?flag=edit&seq=" + $("#managerScenario").attr("scSeq") + "&custCode=" + $('#serviceGroup option:selected').attr("custCode") + "&serviceGroup=" + $("#serviceGroup option:selected").val();
        $.utils.redirect(url);
    });

    $('#btnScenarioRelease').on('click', function () {
        fnReleaseScenario($("#managerScenario").attr("scSeq"));
    });

    $('#btnScenarioDelete').on('click', function () {
        $.utils.confirm("확인 버튼을 누르면 시나리오가 삭제됩니다.", confirmDelete);
        function confirmDelete() {
            fnDeleteScenario($("#managerScenario").attr("scSeq"));
        }
    });

    $("#manageScenario").on("keyup", function (key) {
        if (key.keyCode == 13) {
            fnScenarioSearchBtn();
        }
    });

    //$("#scenarioListTable tbody").on('click', 'tr td:first-child', function () {
    $('#scenarioListTable tbody').on('dblclick', 'tr', function () {
        let data = $("#scenarioListTable").DataTable().row(this).data();
        if (data != undefined) {
            getScenarioManagerInfo(data.seq)
            relatedIntentValues.flag = "N";

            $.modal.modalLoad($("#managerScenario"));
        }
    });

    $("#scenarioListTable tbody").on('click', 'a[name="btnRelatedIntentModify"]', function () {
        let data = $("#scenarioListTable").DataTable().row($(this).parents('tr:first')).data();

        $("#relatedIntent").empty();
        if (data != undefined) {
            fnScenarioRelatedIntent(data.seq);
        }
    });

    $("#scenarioReleaseTable tbody").on('click', 'a[name="btnRollback"]', function () {
        let data = $("#scenarioReleaseTable").DataTable().row($(this).parents('tr:first')).data();

        if(relatedIntentValues.flag === "Y") {
            return;
        } else {
            relatedIntentValues.flag = "Y";
        }

        if (data != undefined) {
            // console.log("롤백 시나리오 seq: " + data.seq);
            $.utils.confirmElse("확인 버튼을 누르면 롤백됩니다.\n" +
                "수정중인 시나리오가 있다면 사라집니다.", confirmRollBack, ()=> {relatedIntentValues.flag = "N"});

            function confirmRollBack() {
                fnRollbackScenario($("#managerScenario").attr("scSeq"), data.seq)
            }
        }
    });

    $("#scenarioReleaseTable tbody").on('click', 'a[name="btnReload"]', function () {
        let data = $("#scenarioReleaseTable").DataTable().row($(this).parents('tr:first')).data();

        if(relatedIntentValues.flag === "Y") {
            return;
        } else {
            relatedIntentValues.flag = "Y";
        }

        if (data != undefined) {
            // console.log("불러오기 시나리오 seq: " + data.seq);
            $.utils.confirmElse("확인 버튼을 누르면 불러오기됩니다.\n" +
                "수정중인 시나리오가 있다면 사라집니다.", confirmReload, ()=>{ relatedIntentValues.flag = "N" });

            function confirmReload() {
                fnReloadScenario($("#managerScenario").attr("scSeq"), data.seq)
            }
        }
    });

    let popupScenarioSeq = Number($("#manageScenario").attr("popupSeq"));
    if (popupScenarioSeq != -1) {
        getScenarioManagerInfo(popupScenarioSeq);
        $.modal.modalLoad($("#managerScenario"));
    }
});

function getOpr() {
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "JSON",
        url: commonUtil.getContextPath() + "/adm/menu/oprmng?menuval=rcpconversationscenariomanage",
        success: function (data) {
            if (data.length == 0 ){
                $.utils.warning("조회 가능한 시나리오 그룹이 없습니다.");
            } else {
                let i = 0;
                data.forEach(function (e, f) {
                    // console.log(e);
                    if (i == 0) {
                        $('#serviceGroup').append("<option value=" + e.serviceGroup + " custCode="+e.custCode+" selected>" + e.serviceGroupName + "</option>");
                    } else {
                        $('#serviceGroup').append("<option value=" + e.serviceGroup + " custCode="+e.custCode+">" + e.serviceGroupName + "</option>");
                    }
                    ++i;
                });

                $('#serviceGroup').niceSelect('update');

                // 시나리오 그룹 가져온 경우에만 데이터 테이블 생성
                PagingDataTable();
                
                // oprMngCode에 따라 연관 인텐트 초기화
                fnReLoadRelatedIntent();
            }
        }
    })
};


var PagingDataTable = function () {
    let paging = JSON.stringify({'page': 0, 'size': 10});
    $('#scenarioListTable').DataTable(
        {
            ajax: {
                url: commonUtil.getContextPath() + "/admin/rcp/conversation/scenario/list/search",
                contentType: "application/json; charset=utf-8",
                type: "POST",
                dataType: "JSON",
                data: function (data) {
                    let json = {};
                    if (data) {
                        json.page = data.start / data.length;
                        json.size = 10
                        json.serviceGroup = $("#serviceGroup option:selected").val();
                        json.custCode = $('#serviceGroup option:selected').attr("custCode");
                        json.scenarioName = $('#scenarionName').val();
                    } else {
                        json = JSON.stringify({'page': 0, 'size': 10});
                    }
                    return JSON.stringify(json);
                }
                ,
                dataFilter: function (data) {
                    let jsons = jQuery.parseJSON(data);
                    //console.log(JSON.stringify(data));
                    let json = {};
                    json.recordsTotal = jsons.getTotal;
                    json.recordsFiltered = jsons.getTotal;
                    json.data = jsons.getList;
                    //console.log(json);
                    return JSON.stringify(json);
                }
            },
            columns: [
                {data: "seq"},
                {data: "scenarioName"},
                {data: "relatedIntents"},
                {data: "updateDate"},
                {data: "updateName"},
                {data: "version"},
            ],
            order: [0, "asc"],
            scrollX: false,
            scrollY: false,
            autoWidth: false,
            searching: false,
            bProcessing: false,
            sProcessing: false,
            bServerSide: true,
            sDom: '<"row view-filter"<"col-sm-12"<"pull-left"><"pull-right"><"clearfix">>>t<"row view-pager"<"col-sm-12"<"text-center"pi>>>',

            columnDefs: [
                {
                    targets: 0,
                    visible: false,
                    searchable: false,
                    render: function (data) {
                        return data;
                    },
                },
                {
                    targets: 1,
                    orderable: false,
                    render: function (data) {
                        return '<b>'+data+'</b>';
                    },
                },
                {
                    targets: 2,
                    orderable: false,
                    // onclick: "event.cancleBubble=true",
                    render: function (data) {
                        let icoPath = commonUtil.getContextPath() + "/lib/assets/images/btnico-modify.png";

                        if (data == null) { data = "없음"; }

                        let viewText;
                        if (data.length > 15) {
                            viewText = data.slice(0, 15) + "...";
                        } else {
                            viewText = data;
                        }

                        return viewText + '<a href="#" rel="intentModfy" class="btnico modalLoad" name="btnRelatedIntentModify"><img src="'+ icoPath +'" alt="연관 인텐트 설정" title="'+ data +'"></a>';
                    },
                },
                {
                    targets: 3,
                    orderable: false,
                    render: function (data) {
                        return $.utils.sliceDateTimeFormat(data);
                    },
                },
                {
                    targets: 4,
                    orderable: false,
                    render: function (data) {
                        return data;
                    },
                },
                {
                    targets: 5,
                    orderable: false,
                    render: function (data) {
                        return $.utils.scenarioVersionString(data);
                    },
                },
            ],
            createdRow: function(row, data, dataIndex) {
                $(row).addClass('modalLoad');
            }
        }
    )
}

var PagingDataTableVersion = function () {
    let paging = JSON.stringify({'page': 0, 'size': 5});

    let isReleasePolicy = checkPermissionPolicy("rcpconversationscenariomanageRelease");
    let isFullPolicy = checkPermissionPolicy("rcpconversationscenariomanageFull");

    $('#scenarioReleaseTable').DataTable(
        {
            ajax: {
                url: commonUtil.getContextPath() + "/admin/rcp/conversation/scenario/release/list/search",
                contentType: "application/json; charset=utf-8",
                type: "POST",
                dataType: "JSON",
                data: function (data) {
                    let json = {};
                    if (data) {
                        json.page = data.start / data.length;
                        json.size = 5;
                        json.scenarioMasterSeq = $("#managerScenario").attr("scSeq");
                    } else {
                        json = JSON.stringify({'page': 0, 'size': 5});
                    }
                    return JSON.stringify(json);
                }
                ,
                dataFilter: function (data) {
                    let jsons = jQuery.parseJSON(data);
                    let json = {};
                    json.recordsTotal = jsons.getTotal;
                    json.recordsFiltered = jsons.getTotal;
                    json.data = jsons.getList;
                    return JSON.stringify(json);
                }
            },
            columns: [
                {data: "seq"},
                {data: "version"},
                {data: "releaseYn"},
                {data: "releaseYn"},
                {data: "registDate"},
                {data: "registName"},
            ],
            order: [0, "asc"],
            scrollX: false,
            autoWidth: true,
            searching: false,
            bProcessing: false,
            sProcessing: false,
            bServerSide: true,
            sDom: '<"row view-filter"<"col-sm-12"<"pull-left"><"pull-right"><"clearfix">>>t<"row view-pager"<"col-sm-12"<"text-center"p>>>',
            pageLength: 5,

            columnDefs: [
                {
                    targets: 0,
                    visible: false,
                    searchable: false,
                    render: function (data) {
                        return data;
                    },
                },
                {
                    targets: 1,
                    orderable: false,
                    render: function (data) {
                        return $.utils.scenarioVersionString(data);
                    },
                },
                {
                    targets: 2,
                    orderable: false,
                    render: function (data) {
                        if (data == "Y") {
                            return "배포";
                        } else {
                            if (isReleasePolicy) {
                                return '<div class="btn_wrap fl sm float-none"> \
    <a href="#" class="btn red" name="btnRollback">롤백</a> \
</div>';
                            } else {
                                return "";
                            }

                        }
                    },
                },
                {
                    targets: 3,
                    orderable: false,
                    render: function (data) {
                        // console.log(data);
                        if (isFullPolicy) {
                            return '<div class="btn_wrap fl sm float-none"> \
    <a href="#" class="btn line" name="btnReload" title="해당 버전 시나리오를 불러와 저장합니다">불러오기</a> \
</div>'
                        } else {
                            return ""
                        }
                    },
                },
                {
                    targets: 4,
                    orderable: false,
                    render: function (data) {
                        return $.utils.sliceDateTimeFormat(data);;
                    },
                },
                {
                    targets: 5,
                    orderable: false,
                    render: function (data) {
                        return data;
                    },
                },
            ],
            createdRow: function(row, data, dataIndex) {
                $(row).addClass('modalLoad');
            }
        }
    )
}

function getScenarioManagerInfo(scenarioSeq) {
    $.modal.modalLoad($("#managerScenario"));
    commonUtil.doActionGet({
        "url" : commonUtil.getContextPath() + "/admin/rcp/conversation/scenario/manager/info",
        "data" : { scenarioSeq : scenarioSeq},
        "callBack" : function (data) {
            // 가져온 데이터이 팝업
            $("#managerScenario").attr("scSeq", data.seq);  // 시퀀스 값 저장

            // console.log("popup scenairo seq: " + $("#managerScenario").attr("scSeq"));
            $("#managerScenarioHeader h3").text("시나리오관리("+ data.scenarioName + ")");
            $("#ipbScenarioName input").val(data.scenarioName);

            let versionString = $.utils.scenarioVersionString(data.releaseVersion);
            $("#managerScenarioReleaseVersion dd").text(versionString);

            $("#managerScenarioReleaseVersion").attr("modifyType", data.modifyType);

            switch (data.modifyType) {
                case 0:
                    $("#managerScenarioModifyType dd span").attr("class", "state sm fatal");
                    $("#managerScenarioModifyType dd span").text("생성");
                    break;
                case 1:
                    $("#managerScenarioModifyType dd span").attr("class", "state sm normal");
                    $("#managerScenarioModifyType dd span").text("배포");
                    break;
                case 2:
                    $("#managerScenarioModifyType dd span").attr("class", "state sm warning");
                    $("#managerScenarioModifyType dd span").text("수정중");
                    break;
                default:

            }
            $("#managerScenarioLastSave dd em").text($.utils.sliceDateTimeFormat(data.lastSaveDateTime) + " (" + data.updateName + ")");

            // PagingDataTableVersion();
            fnScenarioReleaseReload();
            getScenarioManagerHistory(scenarioSeq);

        }
    })
}

function getScenarioManagerHistory(scenarioSeq) {
    commonUtil.doAction({
        "url" : commonUtil.getContextPath() + "/admin/rcp/conversation/scenario/history/log",
        "data" : {
            seq : scenarioSeq
        },
        "callBack" : function (data) {
            let scHistory = "";

            data.results.reverse();
            data.results.forEach(function(rowHistory) {
                // console.log(rowHistory);
                let rowLine = $.utils.sliceDateTimeFormat(rowHistory.registDate) + " " + rowHistory.userName + " " +rowHistory.remark + "\n";
                scHistory += rowLine;
            })
            $("#txScenarioHistory").text(scHistory);
        }
    });
}

function fnInsertScenario() {
    // 시나리오 이름 확인
    if ($.utils.isBlank($('#insertScenarioName').val())) {
        $.utils.error("시나리오 명이 입력되지 않았습니다.");
        return;
    }

    commonUtil.doAction({
        "url" : commonUtil.getContextPath() + "/admin/rcp/conversation/scenario/register",
        "data" : {
            custCode : $('#serviceGroup option:selected').attr("custCode"),
            serviceGroup : $('#serviceGroup option:selected').val(),
            scenarioName : $('#insertScenarioName').val()
        },
        "callBack" : function (data) {
            // Todo: alert 공통 모달 확인 필요
            $.utils.success("정상적으로 저장되었습니다.");
            $("#createScenario").hide();
            removeDim();
            fnScenarioSearchBtn();
            $('#insertScenarioName').val('');
        }
    });

}

function fnReleaseScenario(scSeq) {
    // 수정사항이 수정 중일 때 서버에 배포 요청
    if ($("#managerScenarioReleaseVersion").attr("modifyType") != 2) {  // 2일 때 수정중
        $.utils.error("배포할 내용이 없습니다.");
        return;
    }

    if(relatedIntentValues.flag === "Y") {
        return;
    } else {
        relatedIntentValues.flag = "Y";
    }

    $.utils.confirmElse("배포하시겠습니까?", confirmRelease, ()=> {relatedIntentValues.flag = "N"});
    function confirmRelease() {
        commonUtil.doAction({
            "url": commonUtil.getContextPath() + "/admin/rcp/conversation/scenario/release",
            "data": {
                seq: scSeq,
                custCode: $('#serviceGroup option:selected').attr("custCode"),
                serviceGroup: $("#serviceGroup option:selected").val()
            },
            "callBack": function (data) {
                switch(data) {
                    case 1:
                        $.utils.success("정상적으로 배포되었습니다.\n" +
                            "배포 인텐트와 연관인텐트가 동일하여 삭제하였습니다.");
                        break;
                    default:
                        $.utils.success("정상적으로 배포되었습니다.");
                }
                relatedIntentValues.flag = "N";
                getScenarioManagerInfo(scSeq);
            }
        });
    }

}

function fnRollbackScenario(masterSeq, releaseSeq) {
    commonUtil.doAction({
        "url" : commonUtil.getContextPath() + "/admin/rcp/conversation/scenario/rollback",
        "data" : {
            seq : masterSeq,
            releaseSeq: releaseSeq,
            custCode: $('#serviceGroup option:selected').attr("custCode"),
            serviceGroup: $("#serviceGroup option:selected").val()
        },
        "callBack" : function (data) {
            switch(data) {
                case 1:
                    $.utils.success("정상적으로 롤백 되었습니다.\n" +
                        "롤백 인텐트와 연관인텐트가 동일하여 삭제하였습니다.");
                    break;
                default:
                    $.utils.success("정상적으로 롤백 되었습니다.");
            }
            relatedIntentValues.flag = "N";
            getScenarioManagerInfo(masterSeq);
        }
    });

}

function fnReloadScenario(masterSeq, releaseSeq) {
    commonUtil.doAction({
        "url" : commonUtil.getContextPath() + "/admin/rcp/conversation/scenario/reload",
        "data" : {
            seq : masterSeq,
            releaseSeq: releaseSeq,
            custCode: $('#serviceGroup option:selected').attr("custCode"),
            serviceGroup: $("#serviceGroup option:selected").val()
        },
        "callBack" : function (data) {
            $.utils.success("정상적으로 불러오기 되었습니다.");
            relatedIntentValues.flag = "N";
            getScenarioManagerInfo(masterSeq);
        }
    });
}

function fnDeleteScenario(masterSeq){
    commonUtil.doAction({
        "url" : commonUtil.getContextPath() + "/admin/rcp/conversation/scenario/delete",
        "data" : {
            seq : masterSeq,
            custCode: $('#serviceGroup option:selected').attr("custCode"),
            serviceGroup: $("#serviceGroup option:selected").val()
        },
        "callBack" : function (data) {
            $.utils.success("정상적으로 삭제 되었습니다.");
            $("#managerScenario").hide();
            removeDim();
            fnScenarioSearchBtn();
        }
    });
}

function fnUpdateScenarioName() {
    let seq = $("#managerScenario").attr("scSeq");
    let scName = $("#ipbScenarioName input").val();

    if ($.utils.isBlank(scName)) {
        $.utils.error("시나리오 명이 비었습니다.");
        return;
    }

    if(relatedIntentValues.flag === "Y") {
        return;
    } else {
        relatedIntentValues.flag = "Y";
    }

    commonUtil.doAction({
        "url": commonUtil.getContextPath() + "/admin/rcp/conversation/scenario/name/update",
        "data": {
            seq: seq,
            scenarioName: scName,
            custCode: $('#serviceGroup option:selected').attr("custCode"),
            serviceGroup: $("#serviceGroup option:selected").val()
        },
        "callBack": function (data) {
            $.utils.success("정상적으로 저장되었습니다.");
            $("#managerScenarioHeader h3").text("시나리오관리("+ scName + ")");
            relatedIntentValues.flag = "N";
        }
    });
}

function fnInitInsertScenario() {
    // 입력 값 초기화
    $('#insertScenarioName').val('');
}

function fnScenarioSearchBtn() {
    $('#scenarioListTable').DataTable().ajax.reload();
    // setTimeout(modalUi , 100);

    fnReLoadRelatedIntent();
}

function fnScenarioReleaseReload(){
    $('#scenarioReleaseTable').DataTable().ajax.reload();
}

function fnReLoadRelatedIntent() {
    let serviceGroup = $("#serviceGroup option:selected").val();
    let custCode = $('#serviceGroup option:selected').attr("custCode");
    if (isChangeOprMngCode(serviceGroup, custCode)) {
        relatedIntentValues.oprMngCode = custCode + "_RCP_" + serviceGroup;
        relatedIntentValues.scenarioGroupName = $('#serviceGroup option:selected').text();
        commonUtil.doActionText({
            "url": commonUtil.getContextPath()+ "/admin/rcp/conversation/intent/search.do",
            "data" : {
                custCode : custCode,
                serviceGroup : serviceGroup,
            },
            "callBack" : function (data) {
                let json = JSON.parse(data);
                relatedIntentValues.relatedIntents.length = 0;
                for (let i = 0; i < json.list.length; i++) {
                    relatedIntentValues.relatedIntents.push({key: json.list[i].intentName, value: json.list[i].seq})
                }
            }
        });
    }
}

function fnScenarioRelatedIntent(scenarioSeq) {
    commonUtil.doAction({
        "url": commonUtil.getContextPath()+ "/admin/rcp/conversation/scenario/related/intent",
        "data" : {
            seq: scenarioSeq,
        },
        "callBack" : function (data) {
            fnInitScenarioRelatedIntent();

            for(let i=0; i < data.results.length; i++) {
                // console.log(data.results[i]);
                relatedIntentValues.checkIntent.push(data.results[i].intentSeq);
            }

            let intentHtml = ``;
            for (let i=0; i < relatedIntentValues.relatedIntents.length; i++) {
                if (relatedIntentValues.checkIntent.includes(relatedIntentValues.relatedIntents[i].value)) {
                    relatedIntentValues.flagIntent.push(relatedIntentValues.relatedIntents[i].value);
                    intentHtml += `<div class="chk_box">
    <input type="checkbox" name="checkbox" id="chk`+ i +`" value="`+ relatedIntentValues.relatedIntents[i].value + `" checked>
    <label for="chk`+i+`">`+relatedIntentValues.relatedIntents[i].key+`</label>
</div>`
                } else {
                    intentHtml += `<div class="chk_box">
    <input type="checkbox" name="checkbox" id="chk`+ i +`" value="`+ relatedIntentValues.relatedIntents[i].value + `">
    <label for="chk`+i+`">`+relatedIntentValues.relatedIntents[i].key+`</label>
</div>`
                }
            }

            $("#relatedIntent").append(intentHtml);
            $("#relatedIntentModify h2").text("인텐트 목록(" + relatedIntentValues.scenarioGroupName + ")");

            $("#relatedIntentModify").attr("scSeq", scenarioSeq);
            $.modal.modalLoad($("#relatedIntentModify"));

            // 연관 인텐트 불러오고 권한 체크 후 권한이 없으면 클릭 이벤트 제거
            if (!checkPermissionPolicy("rcpconversationscenariomanageFull")) {
                $("#relatedIntentModify input").attr("onClick", 'return false;');
            }
        }
    });
}

function fnScenarioRelatedIntentSave() {
    let scSeq = $("#relatedIntentModify").attr("scSeq");

    relatedIntentValues.checkIntent = [];
    $("#relatedIntent input:checked").each(function(index) {
        // relatedIntentValues.checkIntent.push({ scenarioSeq: scSeq, intentSeq: $(this).val() } );
        relatedIntentValues.checkIntent.push( Number($(this).val()) );
    })

    if( (relatedIntentValues.flagIntent).toString() === (relatedIntentValues.checkIntent).toString()) {
        $.utils.warning("수정된 데이터가 없습니다.");
        return;
    }

    if(relatedIntentValues.flag === "Y") {
        return;
    } else {
        relatedIntentValues.flag = "Y";
    }

    commonUtil.doAction({
        "url" : commonUtil.getContextPath() + "/admin/rcp/conversation/scenario/related/intent/save",
        "data" :  {
            scenarioSeq: scSeq,
            custCode: $('#serviceGroup option:selected').attr("custCode"),
            serviceGroup: $("#serviceGroup option:selected").val(),
            checkIntents: relatedIntentValues.checkIntent,
        },
        "callBack" : function (data) {
            switch (data) {
                case 1:
                    $.utils.success("정상적으로 저장 되었습니다.\n" +
                        "시나리오 인텐트와 동일한 연관인텐트는 저장되지 않습니다.");
                    break;
                default:
                    $.utils.success("정상적으로 저장 되었습니다.");
            }
            relatedIntentValues.flag = "N";
            $("#relatedIntentModify").hide();
            removeDim();

            // 연관 인텐트 선택 변수 초기화
            fnInitScenarioRelatedIntent();
            fnScenarioSearchBtn();
        }
    });
}

function fnInitScenarioRelatedIntent() {
    relatedIntentValues.checkIntent = [];
}

function isChangeOprMngCode(serviceGroup, custCode) {
    if (relatedIntentValues.oprMngCode == custCode + "_RCP_" + serviceGroup) {
        return false;
    } else {
        return true;
    }
}
