/**
 * 流程管理域部门实例列表
 * @author pengys
 */
(function (factory) {
    if (typeof define === 'function' && define.amd) {
        define(['jquery', 'horizonFlowtree', 'horizonSelectuser',
            'horizonTable', 'horizonJqueryui', 'horizonFlowchart', 'gritter'], factory);
    }else {
        factory(jQuery);
    }
}(function($) {
    "use strict";
    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 flag = 0;
    var flowtree = {
        flowId:'',
        nodeId:'',
        group: '',
        flowStatus: '',
        init: function() {
            //初始化流程树
            $('#flow-tree').flowtree({
                defaultNode: [
                    {
                        id: 'allWorkList',
                        type: 'item',
                        text: parent.horizon.lang['base']['allflow'],
                        additionalParameters: {
                            'item-selected': true
                        }
                    }
                ],
                data:{isRun: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());
            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:parent.horizon.lang['flowarea']['tableTitle'],
            height: _height.outerHeight,
            checkbox: -1,
            columns: [
                {
                    dataProp : 'id',
                    visible:false
                },
                {
                    name : 'title',
                    title : parent.horizon.lang['flowarea']['columnsTitle'],
                    width : '120px',
                    orderable: false,
                    searchable : true,
                    fnClick :function(nTd, nTdData, rowData, iRow, i) {
                        var url = horizon.paths.syspath + '/module/designer/index.html?workid=' + rowData.workId +'&showtype=2&dataid=' + rowData.workId;
                        horizon.open({url:url});
                    }
                },
                {
                    name : 'flowName',
                    title :  parent.horizon.lang['flowarea']['columnsFlowName'],
                    width : '120px',
                    searchable : true,
                    orderable: false,
                },
                {
                    name : 'sendTime',
                    title :  parent.horizon.lang['flowarea']['columnsSendTime'],
                    width : '150px',
                    columnClass :'center'
                },
                {
                    name : 'status',
                    title :  parent.horizon.lang['flowarea']['columnsState'],
                    width : '100px',
                    columnClass :'center',
                    orderable: false,
                },
                {
                    name : 'nodeName',
                    title :  parent.horizon.lang['flowarea']['columnsNodeName'],
                    width : '120px',
                    columnClass :'center',
                    orderable: false,
                },
                {
                    name : 'authName',
                    title :  parent.horizon.lang['flowarea']['columnsAuthName'],
                    width : '120px',
                    columnClass :'center',
                    orderable: false,
                },
            ],
            pluginSettings: {
                "order":[[3,'desc']]
            }
        },
        
        init: function() {
            table.mytable = $('#myDatatable').horizonTable({
                settings: table.settings,
                ajaxDataSource: horizon.tools.formatUrl('/area/dept/instance/list'),
                ajaxDataParam: {
                    deptId: function() { return horizon.tools.getQueryString("deptId") },
                    status: function() { return horizon.tools.getQueryString("status"); }
                }
            });
        },
        getGroupData:function() {
            if(flag == 1){
                if(table.mytable) {
                    //清空数据
                    table.mytable.pluginTable.fnClearTable(true);
                    table.mytable.pluginTable.fnDestroy();
                }
                this.init();
            }else{
                //清空简单搜索项
                table.mytable.pluginTable._fnFilterComplete(table.mytable.pluginTable.fnSettings(), {
                    "sSearch": ''
                } );
                $('#instance-list').addClass('active').addClass('in');
                $('#instance-pic').removeClass('active').removeClass('in');
                table.mytable.pluginTable.fnDraw(false);
            }
        }
    };
    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);
            });
        }
    };
    return horizon.engine['flowareaMonitor'] = {
        init: function() {
            flowtree.init();
            table.init();
            operate.initCheckboxFun();
        },
        clickNode : instancePic.clickNode,
        authorOk: operate.authorOk
    };
}));