/**
 * 流程日志
 *
 * @author pengys
 * @date 2022-04-08
 */
(function(factory) {
    if (typeof define === 'function' && define.amd) {
        define(['jquery','horizonFlowtree', 'horizonTable'], factory);
    }else {
        factory();
    }
}(function() {
    "use strict";
    var $dialog = $('#dialog-default');
    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 flowDefLog = {
        tableObj: null,
        tree: {
            nodeId: null,
            groupId: null,
            init: function() {
                $('#flow-tree').flowtree({
                    data: {
                        type: 'flow'
                    },
                    defaultNode: [ {
                        id: 'allList',
                        type: 'item',
                        text: horizon.lang['base']['allflow'],
                        additionalParameters: {
                            'item-selected': true
                        }
                    } ],
                    defaultNodePosition: 'top',
                    selected: flowDefLog.tree.selNode,
                    deselected: flowDefLog.tree.deselected,
                    opened: flowDefLog.tree.selGroup,
                    closed: flowDefLog.tree.selGroup
                });
            },
            selNode: function() {
                if (arguments[1].target.id == 'allList') {
                    flowDefLog.tree.groupId = null;
                    flowDefLog.tree.nodeId = null;
                    flowDefLog.tableObj.setTitle(arguments[1].text);
                } else {
                    flowDefLog.tree.nodeId = arguments[1].target.flowId;
                    flowDefLog.tree.groupId = arguments[1].group;
                    flowDefLog.tableObj.setTitle(arguments[1].target.text);
                }
                flowDefLog.table.groupData();
            },
            selGroup: function() {
                $(arguments[0].target).find('.tree-item.tree-selected').removeClass('tree-selected');
                flowDefLog.tree.nodeId = null;
                flowDefLog.tree.groupId = arguments[1].group;
                flowDefLog.table.groupData();
                flowDefLog.tableObj.setTitle(arguments[1].text);
            },
            deselected: function() {
                $(arguments[1].el).addClass('tree-selected');
            }
        },
        table: {
            init: function() {
                flowDefLog.tableObj = $('#flowDefLogTable').horizonTable({
                    settings: {
                        title: horizon.lang['base']['allflow'],
                        multipleSearchable: true,
                        height: _height.outerHeight,
                        checkbox: 0,
                        columns: [
                            {
                            dataProp : 'id'
                            }, {
                                name: 'flowId',
                                title: horizon.lang['flow-log']['formFlowID'],
                                width: '100px',
                                fnClick: flowDefLog.info
                            }, {
                                name: 'flowName',
                                title: horizon.lang['flow-log']['formFlowName'],
                                width: '100px',
                                searchable: true,
                                multipleSearchable: true
                            }, {
                                name: 'type',
                                title: horizon.lang['flow-log']['flowType'],
                                width: '80px',
                                columnClass: 'center'
                            }, {
                                name: 'userName',
                                title: horizon.lang['flow-log']['formOperator'],
                                width: '100px',
                                searchable: true,
                                multipleSearchable: true,
                                columnClass: 'center'
                            },
                                {
                                name: 'version',
                                title: horizon.lang['flow-log']['operatorVersion'],
                                width: '60px',
                                columnClass: 'center'
                            }, {
                                name: 'actionName',
                                title: horizon.lang['flow-log']['actionName'],
                                width: '100px',
                                searchable: true,
                                columnClass: 'center'
                            }, {
                                name: 'actionTime',
                                title: horizon.lang['flow-log']['formOperatingTime'],
                                width: '150px',
                                columnClass: 'center'
                            }
                        ],
                        buttons:[
                            {
                                id: 'delFlowLog',
                                text: horizon.lang['base']['delete'],
                                icon: 'fa fa-times red2',
                                fnClick: function() {
                                    var ids = flowDefLog.tableObj.checkRowKeyArray;
                                    if(!ids.length) {
                                        horizon.notice.error(horizon.lang.message["operateHelp"]);
                                        return;
                                    }else {
                                        $dialog.dialog({
                                            title: horizon.lang.message['title'],
                                            dialogText: horizon.lang.message["deleteConfirm"],
                                            dialogTextType: 'alert-danger',
                                            closeText: horizon.lang.base["close"],
                                            buttons: [
                                                {
                                                    html: horizon.lang['base']['ok'],
                                                    "class" : "btn btn-primary btn-xs",
                                                    click: function() {
                                                        flowDefLog.delFlowDefLog(ids.join(';'));
                                                        $( this ).dialog( "close" );
                                                    }
                                                }
                                            ]
                                        });
                                    }
                                }
                            }
                        ],
                        fnCreateCell: function(nTd, nTdData, rowData, iRow, i){
                            if (i== 5 && nTdData == 0){
                                $(nTd).text('全部版本')
                            }
                        },
                        pluginSettings: {
                            "order":[[7,'desc']]
                        }
                    },
                    ajaxDataSource: horizon.tools.formatUrl('/manager/flowdef/log/page')
                });
            },
            groupData: function() {
                // 清空简单搜索项
                flowDefLog.tableObj.pluginTable._fnFilterComplete(flowDefLog.tableObj.pluginTable.fnSettings(), {
                    "sSearch": ''
                });
                var aoServerParams = flowDefLog.tableObj.pluginTable.fnSettings().aoServerParams;
                aoServerParams.push({
                    "fn": function(aoData) {
                        aoData.push({
                            'name': 'flowId',
                            'value': flowDefLog.tree.nodeId
                        });
                        aoData.push({
                            'name': 'type',
                            'value': flowDefLog.tree.groupId
                        });
                    }
                });
                flowDefLog.tableObj.pluginTable.fnDraw(false);
            }
        },
        info: function() {
            var $infoFormContainer = $('#infoFormContainer');
            var $formData = $('#flowLogForm');
            var rowData = arguments[2];
            $.each(rowData, function(key, val) {
                if ( key =='version' && val == 0){
                    $formData.find('input[name="' + key + '"]').val("全部版本");
                } else {
                    $formData.find('input[name="' + key + '"]').val(val);
                }
            });
            $infoFormContainer.dialog({
                width: $(window).width() > 700 ? '700' : 'auto',
                height: 'auto',
                maxHeight: $(window).height(),
                closeText:  horizon.lang['base']['close'],
                title: horizon.lang['flow-log']['flowUpdateLog']
            });
        },
        delFlowDefLog:function(ids) {
            flowDefLog.tableObj.showProcessing();
            $.ajax({
                url : horizon.tools.formatUrl('/manager/flowdef/log/delete'),
                data : {ids: ids},
                dataType:'json',
                cache : false,
                error : function(){
                    flowDefLog.tableObj.hideProcessing();
                    horizon.notice.error(horizon.lang['message']['operateError']);
                },
                success : function(data) {
                    flowDefLog.tableObj.hideProcessing();
                    if(data.restype == 'success') {
                        horizon.notice.success(horizon.lang['message']['deleteSuccess']);
                        $('#flow-tree').flowtree('reload');
                        flowDefLog.tableObj.reload();
                    }else{
                        horizon.notice.error(horizon.lang['message']['deleteFail']);
                    }
                }
            });
        }
    };
    return horizon.engine['flowDefLog'] = {
        initFlowTree: flowDefLog.tree.init,
        initTable: flowDefLog.table.init
    };
}));
