var ReqEvent = {


    // 시나리오 그룹 매핑
    reqGetOpr : function(){
        commonUtil.doActionGet({
            "url" : commonUtil.getContextPath()+ "/adm/menu/oprmng?menuval=rcpconversationcmmNodemanage",
            "callBack" : ResEvent.resGetOpr
        })
    },

    // Common 노드 삭제
    reqDeleteCmmNode : function(seq, serviceGroup){
        commonUtil.doAction({
            "url" : commonUtil.getContextPath()+ "/admin/rcp/conversation/deleteCmmNode",
            "data" : {
                seq : seq
                , serviceGroup : serviceGroup
            },
            "callBack" : ResEvent.resDeleteCmmNode
        })
    },

};

var ResEvent = {

    resGetOpr : function(data) {
        if (data.length == 0 ){
            alert("조회 가능한 시나리오 그룹이 없습니다.");
        } else {
            data.forEach(function (e) {
                if (e.serviceGroup === e.serviceGroupUpper) {
                    $('#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>");
                }
            });
            $('#serviceGroup').niceSelect('update');
        }
        ViewEvent.tableEvent();
    },

    resDeleteCmmNode : function(data) {
        swal("success", "삭제되었습니다.", "success").then(function () {
            ViewEvent.tableReload();
        })
    }
};

var ViewEvent = {
    tableEvent : function(){

        var paging = JSON.stringify({'page': 0, 'size': 10});
        $('#resultTable').DataTable(
            {
                ajax: {
                    url: commonUtil.getContextPath()+ "/admin/rcp/conversation/selectCmmNodeList",
                    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.nodeName = $("#nodeName").val();
                            json.containUpper = false;
                        } else {
                            json = JSON.stringify({'page': 0, 'size': 10});
                        }
                        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" : "nodeName"},
                    {"data" : "updateDate"},
                    {"data" : "updateId"},
                    {"data" : ""},
                ],
                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,
                        render: function (data) {
                            return '<b>'+data+'</b>';
                        },
                    },
                    {
                        targets: 2,
                        render: function (data) {
                            return data;
                        },
                    },
                    {
                        targets: 3,
                        render: function (data) {
                            return data;
                        },
                    },
                    {
                        targets: 4,
                        render: function (data) {
                            let deleteBtn;
                            if (!checkPermissionPolicy("rcpconversationcmmNodemanageFull")) {
                                deleteBtn = "";
                            } else {
                                deleteBtn = '<a href="#" class="btn btn-sm red btnDeleteCmm">삭제</a>';
                            }
                            return deleteBtn;
                        },
                    },
                ],
                createdRow: function(row, data, dataIndex) {

                }
            }
        )

        $("#resultTable tbody").on('dblclick', 'tr', function () {
            let data = $('#resultTable').DataTable().row(this).data();
            let url = commonUtil.getContextPath() + "/admin/rcp/conversation/cmmNode/cmmNodeEdit.do?seq="+data.seq+"&serviceGroup="+$("#serviceGroup").val();
            $.utils.redirect(url);

        });

        $("#resultTable tbody").on("click", 'a.btnDeleteCmm' , function () {
            let data = $("#resultTable").DataTable().row( $(this).parents("tr")).data();
            let serviceGroup = $('#serviceGroup option:selected').val();
            swal("삭제요청","삭제하시겠습니까?","info", {
                buttons: ["취소", "확인"],
            }).then(function(willDelete) {
                if (willDelete) {
                    swal("Success","삭제하였습니다.", {
                        icon: "success",
                        buttons:"확인"
                    });
                    ReqEvent.reqDeleteCmmNode(data.seq, serviceGroup);
                } else {
                    swal("Success","취소되었습니다.", {
                        icon: "error",
                        buttons:"확인"
                    });
                }
            });
        })

    },

    btnEvent : function(){

        $("#btnSearchCmm").click(function() {
            $('#resultTable').DataTable().ajax.reload();
        })

        $("#nodeName").on("keyup", function (key) {
            if (key.keyCode == 13) {
                ViewEvent.tableReload();
            }
        });

        $("#createCmm").click(function () {
            let url = commonUtil.getContextPath() + "/admin/rcp/conversation/cmmNode/cmmNodeEdit.do?serviceGroup="+$("#serviceGroup").val();
            $.utils.redirect(url);
        });
    },

    tableReload : function(){
        $('#resultTable').DataTable().ajax.reload();
    }
};
$(document).ready(function(){
    // 버튼 이벤트 실행
    ViewEvent.btnEvent();
    ReqEvent.reqGetOpr();
    modalUi();

    if(!checkPermissionPolicy("rcpconversationcmmNodemanageFull")) {
        $("#createCmm").hide();
    }

});

