commonModule.
controller('vatOperateLogController', ['$scope', '$log', '$translate', 'uiGridConstants', '$location', '$timeout', '$interval', 'vatOperationLogService', '$filter', 'enums',
    function ($scope, $log, $translate, uiGridConstants, $location, $timeout, $interval, vatOperationLogService, $filter, enums) {
        
        var commonTitle = {
            logType: "OperationContent",
            logOperateObject: "LogOperateObject",
            logOriginalState: "LogOriginalState",
            logUpdateState: "LogUpdateState",
            logOperationDescription: "LogOperationDescription",
            logOperationUser: "LogOperationUser",
            logOperationTime: "LogOperationTime",
            logOperationOID: "LogOperationOID",
            logOperationIP: "LogOperationIP",
            logComments: 'LogComments'
        };

        //点击任意一页事件
        var load_page = function(pageIndex) {
            getLogs(pageIndex);
        };

        //获取vat日志记录
        var getLogs = function(pageIndex) {
            $scope.InitPagination();
            $scope.curPage = pageIndex;
            $scope.paginInfo = {
                totalCount: $scope.queryResult.pageInfo.totalCount,
                pageIndex: pageIndex,
                pageSize: $scope.queryResult.pageInfo.pageSize
            };

            var model = {};
            model.pageInfo = $scope.paginInfo;
            model.period = $scope.period;
            model.queryValue = $scope.searchText;
            model.moduleID = $scope.moduleType;
            model.userId = $scope.userId;

            vatOperationLogService.getOperationLogList(model).success(function (data) {
                if (data != '') {
                    if (data && data.pageInfo && data.pageInfo.totalCount > 0) {
                        var i = model.pageInfo.pageSize * (model.pageInfo.pageIndex - 1) + 1;
                        data.list.forEach(function(row) {
                            row.index = i++;
                            row.createTime = $filter('date')(row.createTime, 'yyyy-MM-dd HH:mm');
                            row.operationObject = $translate.instant(row.operationObject);

                            row.comment = $translate.instant(row.comment);
                            row.operationContent = $translate.instant(row.operationContent);
                            //row.action = actionMap[row.action];
                            row.originalState = $translate.instant(row.originalState);
                            row.updateState = $translate.instant(row.updateState);
                           
                        });
                        $scope.operateLogGridOptions.data = data.list;
                        $scope.queryResult = data;
                        computeTotalPage();
                    } else {
                        $scope.operateLogGridOptions.data = data.list;
                        $scope.queryResult = data;
                        computeTotalPage();
                    }
                }
            });
        };

        var computeTotalPage = function() {

            if ($scope.queryResult.pageInfo && $scope.queryResult.pageInfo.totalCount > 0) {

                var totalPage = parseInt($scope.queryResult.pageInfo.totalCount / $scope.queryResult.pageInfo.pageSize);
                totalPage = $scope.queryResult.pageInfo.totalCount % $scope.queryResult.pageInfo.pageSize == 0 ? totalPage : totalPage + 1;

                $scope.queryResult.pageInfo.totalPage = totalPage;
                $scope.creatPage = $(".tcdPageCode").createPage({
                    pageCount: totalPage,
                    current: $scope.curPage,
                    backFn: function(p) {
                        //单击回调方法,p是当前页码
                        getLogs(p);
                    }
                });
                $('.tcdPageCode').css('display', 'inline-block');
            } else {
                $scope.creatPage = $(".tcdPageCode").createPage({
                    pageCount: 0,
                    current: $scope.curPage,
                    backFn: function(p) {
                        //单击回调方法,p是当前页码
                        getLogs(p);
                    }
                });
                $('.tcdPageCode').css('display', 'inline-block');
            }
        };

        var prePage = function() {
            if ($scope.curPage > 1) {
                $scope.curPage--;
            }
            getLogs($scope.curPage);
        };

        var nextPage = function() {
            if ($scope.curPage < $scope.queryResult.pageInfo.totalPage) {
                $scope.curPage++;
            }
            getLogs($scope.curPage); 
        };

        var normalDataGridOptions = function() { 
            $scope.operateLogGridOptions = {
                rowHeight: 45,
                selectionRowHeaderWidth: 45,
                enableFullRowSelection: true,
                enableRowSelection: true,
                enableColumnResizing: true,
                enableSorting: false,
                enableFiltering: false,
                enableColumnMenus: false,
                enableRowHeaderSelection: false,
                multiSelect: false,
                enableHorizontalScrollbar: uiGridConstants.scrollbars.NEVER,
                columnDefs: [{
                        
                        name: $translate.instant('LogIndex'),
                        width: '8%',
                        headerCellClass: 'header-cell-class',
                        cellTemplate: '<div class="ui-grid-cell-contents"><span title="{{row.entity.index}}">{{row.entity.index}}</span></div>'
                    }, {
                       
                        name: $translate.instant(commonTitle.logOperationTime),
                        width: '14%',
                        headerCellClass: 'header-cell-class',
                        cellTemplate: '<div class="ui-grid-cell-contents"><span>{{row.entity.createTime}}</span></div>'
                    }, {
                       
                        name: $translate.instant(commonTitle.logOperateObject),
                        width: '15%',
                        headerCellClass: 'header-cell-class',
                        cellTemplate: '<div class="ui-grid-cell-contents"><span title="{{row.entity.operationObject}}">{{row.entity.operationObject}}</span></div>'
                    },
                     {
                         name: $translate.instant(commonTitle.logOperationDescription),
                         width: '8%',
                         headerCellClass: 'header-cell-class',
                         cellTemplate: '<div class="ui-grid-cell-contents"><span title="{{row.entity.operationTypeName}}">{{row.entity.operationTypeName}}</span></div>'
                     },
                    {
                        name: $translate.instant(commonTitle.logType),
                        width: '13%',
                        headerCellClass: 'header-cell-class',
                        cellTemplate: '<div class="ui-grid-cell-contents"><span title="{{row.entity.operationContent}}">{{row.entity.operationContent}}</span></div>'
                    },                   
                    {
                        name: $translate.instant(commonTitle.logOriginalState),
                        width: '11%',
                        headerCellClass: 'header-cell-class',
                        cellTemplate: '<div class="ui-grid-cell-contents"><span title="{{row.entity.originalState}}">{{row.entity.originalState}}</span></div>'
                    }, { 
                        name: $translate.instant(commonTitle.logUpdateState),
                        width: '11%',
                        headerCellClass: 'header-cell-class',
                        cellTemplate: '<div class="ui-grid-cell-contents"><span title="{{row.entity.updateState}}">{{row.entity.updateState}}</span></div>'
                    },

                    { 
                        name: $translate.instant(commonTitle.logOperationUser),
                        width: '10%',
                        headerCellClass: 'header-cell-class',
                        cellTemplate: '<div class="ui-grid-cell-contents"><span title="{{row.entity.userName}}">{{row.entity.userName}}</span></div>'
                    }, { 
                        name: $translate.instant(commonTitle.logComments),
                        width: '10%',
                        headerCellClass: 'header-cell-class',
                        cellTemplate: '<div class="ui-grid-cell-contents"><span title="{{row.entity.comment}}">{{row.entity.comment}}</span></div>'
                    }

                ],
                onRegisterApi: function(gridApi) {
                    $scope.gridApi = gridApi;

                    // call resize every 500 ms for 5 s after modal finishes opening - usually only necessary on a bootstrap modal
                    $interval(function() {
                        $scope.gridApi.core.handleWindowResize();
                    }, 500, 60 * 60 * 8);
                }
            };
        };

        $scope.$watch('isShow', function(newValue, oldValue) {
            if (newValue) {
                load_page(1);
                $('#showOperatePop').modal('show');
            }
        });

        $scope.closeModal = function () {
            $scope.isShow = false;
        }; 

        $scope.InitData = function () {
            $scope.logTitle = $translate.instant('Logtitle') + $translate.instant(enums.vatModuleNameDic[$scope.moduleType]);
            $scope.searchText = '';
        }

        $scope.InitPagination = function () {          
            $scope.queryResult = {
                list: [],
                pageInfo: {
                    totalCount: -1,
                    pageIndex: 1,
                    pageSize: 100,
                    totalPage: 0,
                }
            }
            $scope.curPage = 1;
        };

        (function initialize() {
            $log.debug('operateLogController.ctor()...');
            $scope.InitData();
            $scope.InitPagination();
            normalDataGridOptions();

            $scope.load_page = load_page;

        })();
    }
]);