require.config({
    baseUrl: horizon.paths.pluginpath,
    paths: horizon.vars.requirePaths,
    shim: horizon.vars.requireShim
});
require([
	'jquery',
	'bootstrap',
	'horizonJqueryui',
	'horizonFlowchart',
	'horizonNote',
	'ace'
], function($) {
	var clickEvent = horizon.tools.clickEvent();
	var workflow = {
        showFlowchart: function(callback) {
            var _this = this,
                $container = $('.flowchart-container');
            var url = horizon.paths.apppath +'/workflow/module/designer/index_1.html?showtype=2&dataid='
                +horizon.tools.getQueryString('workId')
                +'&nodeid='+horizon.tools.getQueryString('nodeId')//当前节点
                +'&identifier='+horizon.tools.getQueryString('identifier')
                +'&tenantid='+horizon.tools.getQueryString('tenantCode');
            $container.append('<iframe src="'+url+'" style="width:100%;height:552px;border:0"></iframe>');
            /*
            if($container.data('flowchart-content-option')) return;
            $container.horizonFlowChart($.extend(true, {
                url: horizon.tools.formatUrl('/flowchart/data'),
                downloadUrl: horizon.tools.formatUrl('/flowchart/download'),
                dataParam: {
                    workId: horizon.tools.getQueryString('workId'),
                    nodeId: horizon.tools.getQueryString('nodeId')
                },
                iconType: 'font-awesome',
                tools: {
                    status: true,
                    exportImg: true,
                    dynamicDemo: true
                },
                fnNodeClick: function(node, historys) {
                    if(!historys) {
                        $('<div></div>').dialog({
                            title: horizon.lang.workflow['prompt'],
                            dialogText: horizon.lang.workflow['noRecordForNode'],
                            dialogTextType: 'alert-info',
                            closeText: horizon.lang.workflow['close']
                        });
                        return;
                    }
                    var nodeId = node['ID'];
                    var $history = $('#' + nodeId + '-history');
                    var showHistory = function() {
                        $history.dialog({
                            title: node['Name'],
                            width: $(window).width() > 500 ? 500 : $(window).width(),
                            maxWidth: $(window).width(),
                            maxHeight: $(window).height(),
                            closeText: horizon.lang.workflow['close']
                        });
                    };
                    if($history.length) {
                        showHistory();
                    }else {
                        $history = $('<div id="' + nodeId + '-history"></div>');
                        $container.append($history);
                        var option = {
                            local_data: historys,
                            fnAfterSuccess: function() {
                                showHistory();
                            }
                        };
                        $history.horizonNote($.extend(true, option, _this.noteOption));
                    }
                },
                fnFreeNodeClick: function(position) {
                    _this.action.setFreeNodePosition(position);
                },
                fnAfterSuccess: function(data) {
                    _this.operationNote(_this.formatHistory(data['Historys']));
                    if(callback) callback();
                }
            }, horizon.tools.getFlowchartOption()));
            //*/
        },
        formatHistory: function(historys) {
            /* 20191203 LIYS 屏蔽老版本的H5流程跟踪图显示时的数据处理,数据处理在新版本中已经处理
            if(historys && historys.length) {
                $.each(historys, function(i, history) {
                    if(history['action'].toLowerCase() === 'subflow') {
                        var memo = history['memo'],
                            subflows = memo.match(/\[[^\]]+\]/g);
                        for(var k = 0, iLen = subflows.length; k < iLen; k+=2) {
                            var _flowName = subflows[k].replace(/\[/g, '').replace(/\]/g, ''),
                                _workId = subflows[k+1].replace(/\[/g, '').replace(/\]/g, ''),
                                _url = horizon.paths.flowpath;
                            _url += '?workId=' + _workId;
                            var flowNameLink = ['<a tabindex="-1" target="_blank" href="', _url, '">', _flowName, '</a>'].join('');
                            var index = memo.indexOf(subflows[k]);
                            memo = memo.substring(0, index+1) + flowNameLink + memo.substring(index-1 + subflows[k].length);
                        }
                        memo = memo.replace(/实例ID\[[^\]]+\]/g, '');
                        history['memo'] = memo;
                    }
                });
            }
             */
            return historys;
        },
        setNoteLang: function() {
            var contentDom = horizon.lang.workflow['contentDom'];
            if(contentDom) {
                var contentTls = {
                    '_USERNAME_': '<span class="purple bolder"> [username] </span>',
                    '_NODENAME_': '<span class="bolder blue"> [nodename] </span>',
                    '_ACTIONNAME_': '<span class="green bolder"> [actionname] </span>',
                    '_MEMO_': '[memo]',
                    '_COMMENTS-ICON_': '<i class="ace-icon fa fa-pencil-square-o grey bigger-125"></i>',
                    '_COMMENTS_': '[comments]'
                };
                $.each(contentTls, function(key, value) {
                    contentDom = contentDom.replace(key, value);
                });
            }
            workflow.noteOption.lang = {
                empty_text: horizon.lang.workflow['noRecord'],
                header_text: horizon.lang.workflow['historyTableHead']
            };
            workflow.noteOption['timeline_content_dom'] = contentDom;
        },
        noteOption: {
            columns: [
                {
                    column_name: 'username',
                    head_icon: 'fa fa-user hidden-480',
                    column_class: 'nowrap',
                    head_class: 'nowrap'
                },
                {
                    column_name: 'actionname',
                    head_icon: 'glyphicon glyphicon-share hidden-480',
                    column_class: 'nowrap',
                    head_class: 'nowrap'
                },
                {
                    column_name: 'nodename',
                    head_icon: 'fa fa-circle-o hidden-480',
                    column_class: 'nowrap',
                    head_class: 'nowrap'
                },
                {
                    column_name: 'actiontime',
                    head_icon: 'fa fa-clock-o hidden-480',
                    column_class: 'nowrap',
                    head_class: 'nowrap'
                },
                {
                    column_name: 'dotime',
                    head_icon: 'fa fa-clock-o hidden-480',
                    column_class: 'nowrap',
                    head_class: 'nowrap'
                },
                {
                    column_name: 'memo',
                    head_icon: 'fa fa-pencil-square-o hidden-480',
                    column_class: 'nowrap',// for table
                    head_class: 'nowrap'// for tabl
                },
                {
                    column_name: 'comments',
                    head_icon: 'fa fa-pencil-square-o hidden-480',// for table
                    column_class: 'nowrap',// for table
                    head_class: 'nowrap'// for table
                }
            ],
            timeline_content_dom: '',
            group_column: 'actiontime',
            group_type: 'datetime'
        },
        operationNote: function(notes) {
            var $noteContainer = $('.note-container');
            if(!$noteContainer.length) return;
            var option;
            if(notes) {
                option = {
                    time: 'fast',
                    local_data: notes
                };
            }else {
                option = {
                    time: 'fast',
                    url: horizon.tools.formatUrl('/support/history'),
                    data: {
                        workId: horizon.tools.getQueryString('workId')
                    }
                }
            }
            $noteContainer.horizonNote($.extend(true, option, this.noteOption));
            $noteContainer.closest('.widget-box')
                .find('.widget-menu > .dropdown-menu a').on(clickEvent, function() {
                    var $this = $(this);
                    var action = $this.attr('data-action');
                    $noteContainer.horizonNote(action);
                    $this.parent().addClass('active').siblings('li').removeClass('active');
                    var $icon = $this.closest('.widget-header').find('.widget-title > .ace-icon');
                    $icon[0].className = $this.children('.ace-icon')[0].className;
                });
        }
    };
    //LIYS 20191017 增加跟流程图相关的两个方法供HTML5流程图调用
    horizon.tools.extend(horizon,{
        operationNote:function(data){
            var history = workflow.formatHistory(data['historys']);
            workflow.operationNote(history);
        }
    });
    horizon.language.getLanguage(['workflow', 'flowchart'], function() {
    	if(horizon.static) {
            horizon.language.handleFullPage();
            $('title').html(horizon.lang['workflow']['flowDesign']);
        }
        workflow.setNoteLang();
        //初始化流程图
        workflow.showFlowchart();
    })

});
