/**
 * 实例监控
 * @author yaodd
 */
(function (factory) {
    if (typeof define === 'function' && define.amd) {
        define(['jquery', 'horizonFlowtree', 'horizonSelectuser',
            'horizonTable', 'horizonJqueryui', 'horizonFlowchart', 'gritter'], factory);
    }else {
        factory(jQuery);
    }
}(function($) {
    "use strict";
    var $dialog = $('#instance-dialog');
    var _height = {
        outerHeight: function() {
            var _height = horizon.tools.getPageContentHeight(),
                $pageHeader = $('.page-header');
            if($pageHeader.css('display') != 'none') {
                _height -= $pageHeader.outerHeight(true);
            }
            return _height;
        },
        treeHeight: function() {
            var outerHeight = this.outerHeight();
            var $container = $('.modal-dialog');
            return outerHeight - parseInt($container.css('borderTopWidth'))*2
                - parseInt($container.find('.modal-body').css('paddingTop'))*2
                - $container.find('.widget-header').outerHeight(true);
        }
    };
    var flowtree = {
        flowId:'',
        nodeId:'',
        group: '',
        flowStatus: '',
        init: function() {
            //初始化流程树
            $('#flow-tree').flowtree({
                defaultNode: [
                    {
                        id: 'allWorkList',
                        type: 'item',
                        text: horizon.lang['base']['allflow'],
                        additionalParameters: {
                            'item-selected': true
                        }
                    }
                ],
                data:{isEnd:true,type:'flow'},
                defaultNodePosition: 'top',
                selected: flowtree.selected,
                deselected: flowtree.deselected,
                opened: flowtree.selGroup,
                closed: flowtree.selGroup
            });
        },
        selected: function() {
            flowtree.flowId=arguments[1].target.flowId;
            flowtree.group='';
            if(arguments[1].target.id == 'allWorkList') {//查询所有流程
                flowtree.flowId = '';
                flowtree.nodeId = '';
                flowtree.flowStatus = '';
                table.getGroupData();
                $('#picture').addClass('hidden');
                $('#instance-pic').addClass('hidden');
                $('#instance-list').removeClass('hidden');
                table.mytable.setTitle(arguments[1].text);
            }else{
                instancePic.init();
                table.mytable.setTitle(arguments[1].target.text);
            }
        },
        deselected: function() {
            $(arguments[1].el).addClass('tree-selected');
        },
        selGroup: function() {
            $('#picture').addClass('hidden');
            $('#instance-pic').addClass('hidden');
            $('#instance-list').removeClass('hidden');
            $(arguments[0].target).find('.tree-item.tree-selected').removeClass('tree-selected');
            flowtree.group = arguments[1].group;
            flowtree.flowId = '';
            flowtree.nodeId = '';
            flowtree.flowStatus = '';
            table.getGroupData();
            table.mytable.setTitle(arguments[1].text);
        }
    };

    var instancePic = {
        init:function() {
            var _this = this;
            $('#picture').removeClass('hidden');
            $('#instance-list').addClass('hidden');
            var $instancePic = $('#instance-pic');
            $instancePic.removeClass('hidden');
            $('input[name="listCheckbox"]').prop('checked', false).prev().removeClass('blue');
            var $container = $('#flowchart-' + flowtree.flowId);
            if($container.length) {
                $container.remove();
            }
            $container = $('<div id="flowchart-' + flowtree.flowId + '"></div>');

            //20191203 LIYS 切换新版本HTML5
            var url = horizon.paths.syspath + '/module/designer/index.html?showtype=3&dataid='+flowtree.flowId;
            $container.append('<iframe src="'+url+'" style="width:100%;height:800px;border: 0"></iframe>');

            $instancePic.children().hide().end().append($container);
            $container.children(0).css('height', _height.outerHeight());

            /* 20191203 LIYS 屏蔽老版本的H5显示
            $container.horizonFlowChart($.extend(true, {
                height: _height.outerHeight(),
                url: horizon.tools.formatUrl('/flowchart/data'),
                downloadUrl: horizon.tools.formatUrl('/flowchart/download'),
                dataParam: {
                    flowId: flowtree.flowId,
                    instanceNum: true
                },
                iconType: 'font-awesome',
                tools: {
                    exportImg: true
                },
                fnNodeClick: function(node) {
                    if(node['_nodeNum'] || node['sumnum']) {
                        _this.clickNode(flowtree.flowId, node['ID'] || node['nodeid']);
                    }
                }
            }, horizon.tools.getFlowchartOption()));
            //*/
            if(!horizon.vars.touch) {
                $(window)
                    .off('resize.flowchart-container')
                    .on('resize.flowchart-container', function() {
                        $('.flowchart-container').css('height', _height.outerHeight());
                    });
            }
        },
        //单击节点
        clickNode:function(flowid, nodeid) {
            flowtree.flowId = flowid;
            flowtree.nodeId = nodeid;
            $('#instance-list').removeClass('hidden');
            $('#instance-pic').addClass('hidden');
            $('input[name="listCheckbox"]').prop('checked', true).prev().removeClass('blue');
            var aoServerParams = table.mytable.pluginTable.fnSettings().aoServerParams;
            aoServerParams.push({
                "fn": function(aoData){
                    aoData.push({
                        'name': 'flowId',
                        'value': flowtree.flowId
                    });
                    aoData.push({
                        'name': 'nodeId',
                        'value': flowtree.nodeId
                    });
                    aoData.push({
                        'name': 'flowStatus',
                        'value': flowtree.flowStatus
                    });
                }
            });
            table.mytable.pluginTable.fnDraw(false);
        }
    };
    var table = {
        //初始化视图数据
        settings: {
            title:horizon.lang['instance-monitor']['tableTitle'],
            height: _height.outerHeight,
            checkbox: 0,
            columns: [
                {
                    dataProp : 'id'
                },
                {
                    name : 'title',
                    title : horizon.lang['instance-monitor']['columnsTitle'],
                    width : '150px',
                    searchable : true,
                    fnClick :function(nTd, nTdData, rowData, iRow, i) {
                        table.openWorkFlowInfo(rowData);
                    }
                },
                {
                    name : 'flowName',
                    title :  horizon.lang['instance-monitor']['columnsFlowName'],
                    width : '150px',
                    searchable : true
                },
                {
                    name : 'created',
                    title :  horizon.lang['instance-monitor']['columnsCreateTime'],
                    width : '150px',
                    columnClass :'center'
                },
                {
                    name : 'nodeName',
                    title :  horizon.lang['instance-monitor']['columnsNodeName'],
                    width : '120px',
                    columnClass :'center'
                },
                {
                    name : 'status',
                    title :  horizon.lang['instance-monitor']['columnsState'],
                    width : '50px',
                    columnClass :'center'
                },
                {
                    name : 'id',
                    title :  horizon.lang['instance-monitor']['columnsGraphical'],
                    width : '50px',
                    orderable : false,
                    columnClass :'center'
                },
                /*{
                    name : 'id',
                    title : horizon.lang['instance-monitor']['columnsExample'],
                    width : '50px',
                    orderable : false,
                    columnClass :'center'
                },*/
                {
                    name : 'ver',
                    title :  horizon.lang['instance-monitor']['columnsVersion'],
                    width : '35px',
                    columnClass :'center'
                },
                {
                    name : 'typeName',
                    title :  horizon.lang['instance-monitor']['columnsTypeName'],
                    width : '100px',
                    visible : false,
                    searchable : false
                }
            ],
            buttons: [
                {
                    id: 'recoverRunWork',
                    text: horizon.lang['instance-monitor']['endToRunRecovery'],
                    icon: 'glyphicon glyphicon-repeat green',
                    fnClick: function() {
                        var ids = table.mytable.checkRowKeyArray;
                        if(!ids.length) {
                            horizon.notice.error(horizon.lang['instance-monitor']['selectEndToRun']);
                            return;
                        }else if(ids.length >1) {
                            horizon.notice.error(horizon.lang['instance-monitor']['recoveryEndOnePerTime']);
                            return;
                        }else{
                            $dialog.dialog({
                                closeText:horizon.lang['base']['cancel'],
                                title:horizon.lang['message']['title'],
                                dialogText:horizon.lang['instance-monitor']['confirmRecoverOperate'],
                                dialogTextType:'alert-danger',
                                buttons: [
                                    {
                                        html: horizon.lang['base']['ok'],
                                        "class" : "btn btn-primary btn-xs",
                                        click: function() {
                                            operate.recoverEndToRun(ids[0]);
                                            $( this ).dialog( "close" );
                                        }
                                    }
                                ]
                            });
                        }
                    }
                },
                {
                    id: 'delWork',
                    text: horizon.lang['base']['delete'],
                    icon: 'fa fa-times red2',
                    fnClick: function() {
                        var ids = table.mytable.checkRowKeyArray;
                        if(!ids.length) {
                            horizon.notice.error(horizon.lang['instance-monitor']['selectDelete']);
                            return;
                        }else {
                            $dialog.dialog({
                                closeText:horizon.lang['base']['cancel'],
                                title:horizon.lang['message']['title'],
                                dialogText:horizon.lang['instance-monitor']['confirmDeleteOperate'],
                                dialogTextType:'alert-danger',
                                buttons: [
                                    {
                                        html: horizon.lang['base']['ok'],
                                        "class" : "btn btn-primary btn-xs",
                                        click: function() {
                                            operate.deleteWork(ids.join(';'));
                                            $( this ).dialog( "close" );
                                        }
                                    }
                                ]
                            });
                        }
                    }
                }
            ],
            fnCreateCell: function(nTd, nTdData, rowData, iRow, i){
                var _html = '';
                if(i == 6) {//操作列
                    _html = '<label class="blue"  data-action="flowpic" title="'+horizon.lang['instance-monitor']['columnsGraphical']+'" style="cursor:pointer">'+horizon.lang['instance-monitor']['columnsGraphical']+'</label>';
                }else if(i == 7) {
                    //_html = '<label class="blue"  data-action="flowInstance" title="'+horizon.lang['instance-monitor']['columnsExample']+'" style="cursor:pointer">'+horizon.lang['instance-monitor']['columnsExample']+'</label>';
                }
                if(_html != '') {
                    var $ntd = $(nTd);
                    $ntd.html(_html);
                    /*if(i == 7) {
                        $ntd.find('label[data-action="flowInstance"]').on(horizon.tools.clickEvent(), function() {
                            var url = horizon.static ? (horizon.paths.syspath + '/module/flash/HorizonInstance.html')
                                : (horizon.paths.apppath + '/horizon/module/flash/flow/instance'+ horizon.paths.urlsuffix);
                            url += '?workid=' + nTdData;
                            //20191203 LIYS 切换新版本HTML5
                            url = horizon.paths.syspath + '/module/designer/index.html';
                            url += '?workid=' + nTdData +'&showtype=5&dataid='+ nTdData;
                            horizon.open({url:url});
                        });
                    }else */if(i == 6){
                        $ntd.find('label[data-action="flowpic"]').on(horizon.tools.clickEvent(), function() {
                            //20191203 LIYS 切换新版本HTML5
                            var url = horizon.paths.syspath + '/module/designer/index.html';
                            url += '?workid=' + nTdData +'&showtype=2&dataid='+ nTdData;
                            horizon.open({url:url});
                            /*
                            var id = '#flowchart-' + nTdData + '-' + rowData.nodeId,
                                $container = $(id);
                            var showFlowchart = function() {
                                $container.dialog({
                                    title: rowData['flowName'],
                                    width: '100%',
                                    height: $(window).height(),
                                    maxWidth: $(window).width(),
                                    maxHeight: $(window).height(),
                                    closeText: horizon.lang['base']['close']
                                });
                            };
                            if($container.length) {
                                showFlowchart();
                            }else {
                                $container = $('<div id="' + id + '"></div>');
                                $('.page-header').after($container);
                                $container.horizonFlowChart($.extend(true, {
                                    url: horizon.tools.formatUrl('/flowchart/data'),
                                    downloadUrl: horizon.tools.formatUrl('/flowchart/download'),
                                    dataParam: {
                                        workId: nTdData,
                                        nodeId: rowData.nodeId
                                    },
                                    iconType: 'font-awesome',
                                    tools: {
                                        status: true,
                                        exportImg: true,
                                        dynamicDemo: true
                                    },
                                    fnAfterSuccess: function() {
                                        showFlowchart();
                                    }
                                }, horizon.tools.getFlowchartOption()));
                            }
                            //*/
                        });
                    }
                }
            },
            pluginSettings: {
                "order":[[3,'desc']]
            }
        },
        init: function() {
            table.mytable = $('#myDatatable').horizonTable({
                settings: table.settings,
                ajaxDataSource: horizon.tools.formatUrl('/monitor/instance/end/list'),
                ajaxDataParam: {
                    flowStatus: function() { return flowtree.flowStatus; },
                    group: function() { return flowtree.group; }
                }
            });
        },
        getGroupData:function() {
             //清空简单搜索项
             table.mytable.pluginTable._fnFilterComplete(table.mytable.pluginTable.fnSettings(), {
                 "sSearch": ''
             } );
             $('#list').parent().addClass('active');
             $('#pic').parent().removeClass('active');
             $('#instance-list').addClass('active').addClass('in');
             $('#instance-pic').removeClass('active').removeClass('in');
             table.mytable.pluginTable.fnDraw(false);
        },
        openWorkFlowInfo: function() {
            var workid = arguments[0].id;
            var trackid = arguments[0].trackId;
            var url = horizon.paths.flowpath + '?workId=' + workid + '&trackId=' + trackid;
            horizon.open({url:url});
        }
    };
    var operate = {
        id:'',
        trackid:'',
        nodeid:'',
        status:'',
        initCheckboxFun: function() {
            $('input[name="listCheckbox"]').on(horizon.tools.clickEvent(), function(){
                $(this).prev().toggleClass('blue');
                $('#instance-list').toggleClass('hidden');
                $('#instance-pic').toggleClass('hidden');
                flowtree.nodeId = '';
                flowtree.flowStatus = '';
                var aoServerParams = table.mytable.pluginTable.fnSettings().aoServerParams;
                aoServerParams.push({
                    "fn": function(aoData){
                        aoData.push({
                            'name': 'flowId',
                            'value': flowtree.flowId
                        });
                        aoData.push({
                            'name': 'nodeId',
                            'value': flowtree.nodeId
                        });
                        aoData.push({
                            'name': 'flowStatus',
                            'value': flowtree.flowStatus
                        });
                    }
                });
                table.mytable.pluginTable.fnDraw(false);
            });
        },
        //删除
        deleteWork:function(ids) {
            table.mytable.showProcessing();
            $.ajax({
                url : horizon.tools.formatUrl('/monitor/instance/delete'),
                data : {workid: ids},
                dataType:'json',
                cache : false,
                error : function(){
                    table.mytable.hideProcessing();
                    horizon.notice.error(horizon.lang['message']['operateError']);
                },
                success : function(data) {
                    table.mytable.hideProcessing();
                    if(data.restype == 'success') {
                        horizon.notice.success(horizon.lang['message']['deleteSuccess']);
                        $('#flow-tree').flowtree('reload');
                        table.mytable.reload();
                    }else{
                        horizon.notice.error(horizon.lang['message']['deleteFail']);
                    }
                }
            });
        },
        //删除
        recoverEndToRun:function(id) {
            table.mytable.showProcessing();
            $.ajax({
                url : horizon.tools.formatUrl('/monitor/instance/end/recover'),
                data : {workId: id},
                dataType:'json',
                cache : false,
                error : function(){
                    table.mytable.hideProcessing();
                    horizon.notice.error(horizon.lang['message']['operateError']);
                },
                success : function(data) {
                    table.mytable.hideProcessing();
                    if(data.restype == 'success') {
                        horizon.notice.success(horizon.lang['message']['recoveryEndSuccess']);
                        $('#flow-tree').flowtree('reload');
                        table.mytable.reload();
                    }else{
                        horizon.notice.error(horizon.lang['message']['recoveryEndFailed']);
                    }
                }
            });
        }
    };
    return horizon.engine['instancemonitorEnd'] = {
        init: function() {
            flowtree.init();
            table.init();
            operate.initCheckboxFun();
        },
        clickNode : instancePic.clickNode,
    };
}));