vatModule.controller('vatUnbilledInvoiceController', ['$scope', '$log', '$timeout', '$q', '$translate','$interval', 'loginContext', 'apiInterceptor', 'SweetAlert', 'vatSessionService', 'uiGridConstants', 'enums', 'vatReductionService',
function ($scope, $log, $timeout, $q, $translate, $interval, loginContext, apiInterceptor, SweetAlert, vatSessionService, uiGridConstants, enums, vatReductionService) {
    'use strict';
    $log.debug('vatUnbilledInvoiceController.ctor()...');

    $scope.period = vatSessionService.month;
    $scope.ImportErrorTag = true;
    $scope.TotalDebitAmount = 0;
    $scope.TotalCreditAmount = 0;
    $scope.TotalAmount = 0;
    $scope.TotalTaxAmount = 0;
    $scope.MapTotalDebitAmount = 0;
    $scope.MapTotalCreditAmount = 0;
    $scope.MapTotalAmount = 0;
    $scope.MapTotalTaxAmount = 0;

    $scope.voucherSearchTypeList = [
        { id: 10, name: $translate.instant('Enum_UnBilled') },
        { id: 20, name: $translate.instant('Enum_PartBilled') },
       { id: 30, name: $translate.instant('Enum_Billed') },
    ];
    $scope.invoiceSearchTypeList = [
       { id: 40, name: $translate.instant('Enum_NotAccounting') },
       { id: 50, name: $translate.instant('Enum_PartAccounting') },
        { id: 60, name: $translate.instant('Enum_Accounting') },
    ];
    $scope.voucherSearchType = {};
    $scope.invoiceSearchType = {};
    $scope.tranCode = "";
    $scope.IsVoucher = true;

    //发票类型
    var invoiceTypeEnum = {
        Normal: $translate.instant('Normal'),
        Special: $translate.instant('Special')
    };
    //发票类型转换
    $scope.typeToString = function (strType) {
        var type = invoiceTypeEnum.Normal;
        switch (strType) {
            case 0:
                type = invoiceTypeEnum.Normal;
                break;
            case 1:
                type = invoiceTypeEnum.Special;
                break;
            default:
                type = invoiceTypeEnum.Normal;
        }
        return type;
    }

    var voucherColumns = [
            { name: $translate.instant('period'), width: '10%', cellTemplate: '<div class="ui-grid-cell-contents"><span>{{row.entity.period}}<span></div>' },
            { name: $translate.instant('VoucherDate'), width: '15%', cellTemplate: '<div class="ui-grid-cell-contents" ><span>{{row.entity.date | date:"yyyy-MM-dd"}}<span></div>' },
            { name: $translate.instant('VoucherID'), width: '15%', cellTemplate: '<div class="ui-grid-cell-contents"><span>{{row.entity.vid}}<span></div>' },
             { name: $translate.instant('VoucherAccountCode'), width: '15%', cellTemplate: '<div class="ui-grid-cell-contents"><span>{{row.entity.acctCode}}<span></div>' },
            { name: $translate.instant('VoucherSummary'), width: '15%', cellTemplate: '<div class="ui-grid-cell-contents"><span>{{row.entity.summary}}<span></div>' },           
            { name: $translate.instant('VoucherDebitAmount'), width: '15%', headerCellClass: 'right', cellTemplate: '<div class="ui-grid-cell-contents right"><span>{{row.entity.debit}}<span></div>' },
            { name: $translate.instant('VoucherCreditAmount'), width: '15%', headerCellClass: 'right', cellTemplate: '<div class="ui-grid-cell-contents right"><span>{{row.entity.credit}}<span></div>' },
    ];
    var invoiceColumns = [
            { name: $translate.instant('period'), width: '8%', cellTemplate: '<div class="ui-grid-cell-contents"><span>{{row.entity.periodID}}<span></div>' },
            { name: $translate.instant('InvoiceDate'), width: '12%', cellTemplate: '<div class="ui-grid-cell-contents"><span>{{row.entity.invoiceDate | date:"yyyy-MM-dd"}}<span></div>' },
            { name: $translate.instant('InvoiceType'), width: '15%', cellTemplate: '<div class="ui-grid-cell-contents"><span>{{grid.appScope.typeToString(row.entity.invoiceType)}}<span></div>' },
            { name: $translate.instant('ClassCode'), width: '15%', cellTemplate: '<div class="ui-grid-cell-contents"><span>{{row.entity.classCode}}<span></div>' },
            { name: $translate.instant('InvoiceNumber'), width: '15%', cellTemplate: '<div class="ui-grid-cell-contents"><span>{{row.entity.invoiceNumber}}<span></div>' },
            { name: $translate.instant('BuyerName'), width: '15%', cellTemplate: '<div class="ui-grid-cell-contents"><span>{{row.entity.buyerName}}<span></div>' },
            { name: $translate.instant('BuyerTaxNumber'), width: '15%', cellTemplate: '<div class="ui-grid-cell-contents"><span>{{row.entity.buyerTaxNumber}}<span></div>' },
            { name: $translate.instant('Amount'), width: '10%', cellTemplate: '<div class="ui-grid-cell-contents"><span>{{row.entity.amount}}<span></div>' },
              { name: $translate.instant('TaxRate'), width: '10%', cellTemplate: '<div class="ui-grid-cell-contents"><span>{{((row.entity.taxRate) * 100).toFixed(0) + "%"}}<span></div>' },
            { name: $translate.instant('TaxAmount'), width: '10%', cellTemplate: '<div class="ui-grid-cell-contents"><span>{{row.entity.taxAmount}}<span></div>' },
    ];
    //Voucher数据源
    $scope.gridOptionsVoucher = {
        rowHeight: constant.UIGrid.rowHeight,
        selectionRowHeaderWidth: constant.UIGrid.selectionRowHeaderWidth,
        enableColumnResizing: true,
        enableRowSelection: true,
        enableRowHeaderSelection: false,
        multiSelect: false,
        modifierKeysToMultiSelect: false,
        noUnselect: true,
        enableSorting: false,
        enableColumnMenus: false,
        enableHorizontalScrollbar: uiGridConstants.scrollbars.enableHorizontalScrollbar,
        enableVerticalScrollbar: uiGridConstants.scrollbars.enableVerticalScrollbar,
        columnDefs: voucherColumns,
        onRegisterApi: function (gridApi) {
            $scope.gridVoucherApi = gridApi;

            $interval(function () {
                $scope.gridVoucherApi.core.handleWindowResize();
            }, 500, 60 * 60 * 8);

            if (gridApi.selection) {
                gridApi.selection.on.rowSelectionChanged($scope, function (row) {
                    voucherRowSelectedEvent(row);
                });
            }

        }
    };


    //Voucher历史数据源
    $scope.gridOptionsHistoryVoucher = {
        rowHeight: constant.UIGrid.rowHeight,
        selectionRowHeaderWidth: constant.UIGrid.selectionRowHeaderWidth,
        enableColumnResizing: true,
        enableRowSelection: true,
        enableRowHeaderSelection: false,
        multiSelect: false,
        modifierKeysToMultiSelect: false,
        noUnselect: true,
        enableSorting: false,
        enableColumnMenus: false,
        enableHorizontalScrollbar: uiGridConstants.scrollbars.enableHorizontalScrollbar,
        enableVerticalScrollbar: uiGridConstants.scrollbars.enableVerticalScrollbar,
        columnDefs: voucherColumns,
        onRegisterApi: function (gridApi) {
            $scope.gridHistoryVoucherApi = gridApi;

            $interval(function () {
                $scope.gridHistoryVoucherApi.core.handleWindowResize();
            }, 500, 60 * 60 * 8);

            if (gridApi.selection) {
                gridApi.selection.on.rowSelectionChanged($scope, function (row) {
                    voucherRowSelectedEvent(row);
                });
            }

        }
    };

    //MapVoucher数据源
    $scope.gridOptionsMapVoucher = {
        rowHeight: constant.UIGrid.rowHeight,
        selectionRowHeaderWidth: constant.UIGrid.selectionRowHeaderWidth,
        enableColumnResizing: true,
        enableSorting: false,
        enableColumnMenus: false,
        enableHorizontalScrollbar: uiGridConstants.scrollbars.enableHorizontalScrollbar,
        enableVerticalScrollbar: uiGridConstants.scrollbars.enableVerticalScrollbar,
        columnDefs: voucherColumns,
        onRegisterApi: function (gridApi) {
            $scope.gridMapVoucherApi = gridApi;

            $interval(function () {
                $scope.gridMapVoucherApi.core.handleWindowResize();
            }, 500, 60 * 60 * 8);
        }
    };

    //Invoice数据源
    $scope.gridOptionsInvoice = {
        rowHeight: constant.UIGrid.rowHeight,
        selectionRowHeaderWidth: constant.UIGrid.selectionRowHeaderWidth,
        enableColumnResizing: true,
        enableRowSelection: true,
        enableRowHeaderSelection: false,
        multiSelect: false,
        modifierKeysToMultiSelect: false,
        noUnselect: true,
        enableSorting: false,
        enableColumnMenus: false,
        enableHorizontalScrollbar: uiGridConstants.scrollbars.enableHorizontalScrollbar,
        enableVerticalScrollbar: uiGridConstants.scrollbars.enableVerticalScrollbar,
        columnDefs: invoiceColumns,
        onRegisterApi: function (gridApi) {
            $scope.gridInvoiceApi = gridApi;

            $interval(function () {
                $scope.gridInvoiceApi.core.handleWindowResize();
            }, 500, 60 * 60 * 8);

            if (gridApi.selection) {
                gridApi.selection.on.rowSelectionChanged($scope, function (row) {
                    invoiceRowSelectedEvent(row);
                });
            }
        }
    };

    //Invoice历史数据源
    $scope.gridOptionsHistoryInvoice = {
        rowHeight: constant.UIGrid.rowHeight,
        selectionRowHeaderWidth: constant.UIGrid.selectionRowHeaderWidth,
        enableColumnResizing: true,
        enableRowSelection: true,
        enableRowHeaderSelection: false,
        multiSelect: false,
        modifierKeysToMultiSelect: false,
        noUnselect: true,
        enableSorting: false,
        enableColumnMenus: false,
        enableHorizontalScrollbar: uiGridConstants.scrollbars.enableHorizontalScrollbar,
        enableVerticalScrollbar: uiGridConstants.scrollbars.enableVerticalScrollbar,
        columnDefs: invoiceColumns,
        onRegisterApi: function (gridApi) {
            $scope.gridHistoryInvoiceApi = gridApi;

            $interval(function () {
                $scope.gridHistoryInvoiceApi.core.handleWindowResize();
            }, 500, 60 * 60 * 8);

            if (gridApi.selection) {
                gridApi.selection.on.rowSelectionChanged($scope, function (row) {
                    invoiceRowSelectedEvent(row);
                });
            }
        }
    };

    //MapInvoice数据源
    $scope.gridOptionsMapInvoice = {
        rowHeight: constant.UIGrid.rowHeight,
        selectionRowHeaderWidth: constant.UIGrid.selectionRowHeaderWidth,
        enableColumnResizing: true,
        enableSorting: false,
        enableColumnMenus: false,
        enableHorizontalScrollbar: uiGridConstants.scrollbars.enableHorizontalScrollbar,
        enableVerticalScrollbar: uiGridConstants.scrollbars.enableVerticalScrollbar,
        columnDefs: invoiceColumns,
        onRegisterApi: function (gridApi) {
            $scope.gridMapInvoiceApi = gridApi;

            $interval(function () {
                $scope.gridMapInvoiceApi.core.handleWindowResize();
            }, 500, 60 * 60 * 8);
        }
    };

    //切换VoucherTab
    $scope.switchVoucherTable = function (event) {
        $('.voucher-tab span').each(function () {
            $(this).removeClass('active');
        });

        $(event.target).addClass('active');

        if (event.target.innerText == $translate.instant('CurrentPeriod')) {
            $('#gridVoucher').css('display', 'block');
            $('#gridHistoryVoucher').css('display', 'none');
            $scope.isVoucherHistory = false;
        }
        else {
            $('#gridHistoryVoucher').css('display', 'block');
            $('#gridVoucher').css('display', 'none');
            $scope.isVoucherHistory = true;
        }
        setErrorWrapCssDefault();
        getVoucherListByPage(1, $scope.isVoucherHistory);
    };

    //切换InvoiceTab
    $scope.switchInvoiceTable = function (event) {
        $('.invoice-tab span').each(function () {
            $(this).removeClass('active');
        });

        $(event.target).addClass('active');

        if (event.target.innerText == $translate.instant('CurrentPeriod')) {
            $('#gridInvoice').css('display', 'block');
            $('#gridHistoryInvoice').css('display', 'none');
            $scope.isInvoiceHistory = false;
        }
        else {
            $('#gridHistoryInvoice').css('display', 'block');
            $('#gridInvoice').css('display', 'none');
            $scope.isInvoiceHistory = true;
        }
        setErrorWrapCssDefault();
        getInvoiceListByPage(1, $scope.isInvoiceHistory);
    };

    var voucherRowSelectedEvent = function (row) {
        $scope.IsVoucher = true;
        setErrorWrapCssDispalyHeight();
        setMapTableFloat(true);
        $scope.gridInvoiceApi.selection.clearSelectedRows();
        $scope.gridHistoryInvoiceApi.selection.clearSelectedRows();
        var queryParams = {
            VID: row.entity.vid,
            Period: row.entity.period,
            Group: row.entity.group,
            ItemID: row.entity.itemID,
            CurPeriod: $scope.period,
        }
        vatReductionService.getVoucherMapInvoice(queryParams).success(function (data) {
            if (data && data.tranCode) {
                setMapGridData(data);
            }
            else {
                setMapGridDataNull();
            }
        }).error(function () {
            SweetAlert.error($translate.instant('PleaseContactAdministrator'));
        });
    }

    var invoiceRowSelectedEvent = function (row) {
        $scope.IsVoucher = false;
        setMapTableFloat(false);
        setErrorWrapCssDispalyHeight();
        $scope.gridVoucherApi.selection.clearSelectedRows();
        $scope.gridHistoryVoucherApi.selection.clearSelectedRows();
        var queryParams = {
            InvoiceCode: row.entity.classCode,
            CurPeriod: $scope.period,
            InvoiceNumber: row.entity.invoiceNumber,
        }
        vatReductionService.getInvoiceMapVoucher(queryParams).success(function (data) {
            if (data && data.tranCode) {
                setMapGridData(data);
            }
            else {
                setMapGridDataNull();
            }
        }).error(function () {
            SweetAlert.error($translate.instant('PleaseContactAdministrator'));
        });
    }

    var setMapGridData = function (data) {
        var mapDebit = 0;
        var mapCredit = 0;
        if (data.voucherList !== null) {
            data.voucherList.forEach(function (v) {
                mapDebit += v.debit;
                mapCredit += v.credit;
                v.debit = PWC.round(v.debit, 2);
                v.credit = PWC.round(v.credit, 2);
            });
        }
        var mapAmount = 0;
        var mapTaxAmount = 0;
        if (data.invoiceList !== null) {
            data.invoiceList.forEach(function (v) {
                mapAmount += v.amount;
                mapTaxAmount += v.taxAmount;
                v.amount = PWC.round(v.amount, 2);
                v.taxAmount = PWC.round(v.taxAmount, 2);
            });
        }

        $scope.tranCode = data.tranCode;
        $scope.MapTotalDebitAmount = PWC.round(mapDebit, 2);
        $scope.MapTotalCreditAmount = PWC.round(mapCredit, 2);
        $scope.MapTotalAmount = PWC.round(mapAmount, 2);
        $scope.MapTotalTaxAmount = PWC.round(mapTaxAmount, 2);

        $scope.gridOptionsMapVoucher.data = data.voucherList;
        $scope.gridOptionsMapInvoice.data = data.invoiceList;
    }

    var setMapGridDataNull = function () {
        $scope.tranCode = '';
        $scope.MapTotalDebitAmount = 0;
        $scope.MapTotalCreditAmount = 0;
        $scope.MapTotalAmount = 0;
        $scope.MapTotalTaxAmount = 0;
        $scope.gridOptionsMapVoucher.data = [];
        $scope.gridOptionsMapInvoice.data = [];
    }

    //初始化Voucher分页信息
    var initVoucherPagination = function () {
        $scope.queryVoucherResult = {
            list: [],
            pageInfo: {
                totalCount: -1,
                pageIndex: 1,
                pageSize: 10,
                totalPage: 0,
            }
        }
        $scope.curVoucherPage = 1;
    };
    //初始化Invoice分页信息
    var initInvoicePagination = function () {
        $scope.queryInvoiceResult = {
            list: [],
            pageInfo: {
                totalCount: -1,
                pageIndex: 1,
                pageSize: 10,
                totalPage: 0,
            }
        }
        $scope.curInvoicePage = 1;
    };
    //切换账票显示方向
    var setMapTableFloat = function (isvoucher) {
        if (isvoucher) {
            $('.map-voucher-table').css('float', 'left');
            $('.map-voucher-table').css('padding-left', '20px');
            $('.map-voucher-table').css('padding-right', '0px');

            $('.map-invoice-table').css('float', 'right');
            $('.map-invoice-table').css('padding-left', '0px');
            $('.map-invoice-table').css('padding-right', '20px');
        }
        else {
            $('.map-voucher-table').css('float', 'right');
            $('.map-voucher-table').css('padding-left', '0px');
            $('.map-voucher-table').css('padding-right', '20px');

            $('.map-invoice-table').css('float', 'left');
            $('.map-invoice-table').css('padding-left', '20px');
            $('.map-invoice-table').css('padding-right', '0px');
        }
    }
    //账票对应关系grid的高度
    var getMapGridHeight = function () {
        if ($scope.isLoadComplete) {
            var y = $("#map-item-wrapper").height();

            // Enough space
            if (y > constant.UIGrid.gapHeight) {
                y = y - constant.UIGrid.gapHeight;
                return {
                    height: (y - 40 - 67 - 30) + "px"
                };
            } else {
                return {
                    height: '0px'
                };
            }
        }
        return {
        };
    };

    //对应关系中间图片的高度
    var getImgGridHeight = function () {
        if ($scope.isLoadComplete) {
            var y = $('.map-detail').height();
            // Enough space
            if (y > 140) {
                return {
                    'padding-top': (y - 140) / 2 + "px"
                };
            } else {
                return {
                    'padding-top': '0px'
                };
            }
        }
        return {
        };
    };

    //对应关系中间交易号内容的高度
    var getTranCodeHeight = function () {
        if ($scope.isLoadComplete) {
            var y = $('.map-detail').height();
            // Enough space
            if (y > 140) {
                return {
                    'top': (y - 140) / 2 + "px"
                };
            } else {
                return {
                    'top': '25px'
                };
            }
        }
        return {
        };
    };

    //显示对应关系grid明细
    var toggleMapGridTab = function () {
        $scope.ImportErrorTag = !$scope.ImportErrorTag;
        // topIcon and content-resize gapBottom 15px
        if (!$scope.ImportErrorTag) {
            setErrorWrapCssDefault();
        } else {
            setErrorWrapCssDispalyHeight();
        }
    };

    var setErrorWrapCssDispalyHeight = function () {
        if (parseInt($('#content-resizer').css('bottom')) < 100) {
            $('#content-resizer').css('bottom', '300px');
            $('.map-container').css('height', '300px');
        }
    }

    var setErrorWrapCssDefault = function () {
        $('#content-resizer').css('bottom', '10px');
        $('.map-container').css('height', '10px');
        $('#detail-table-wrapper').css('bottom', '5px');
        var y = ($(".total-container").height() - 5) + "px";
        $('.detail-table').css('height', y);
    };

    //获取记账信息
    var getVoucherListByPage = function (pageIndex, isVoucherHistory) {
        $scope.curVoucherPage = pageIndex;
        //初始化查询信息
        var pageInfo = {
            totalCount: $scope.queryVoucherResult.pageInfo.totalCount,
            pageIndex: pageIndex,
            pageSize: $scope.queryVoucherResult.pageInfo.pageSize,
            totalPage: 0,
        }
        var queryParams = {
            pageInfo: pageInfo,
            status: $scope.voucherSearchType.selected.id,
            period: $scope.period,
        };
        if (isVoucherHistory) {
            vatReductionService.getVoucherHistoryList(queryParams).success(function (data) {
                if (data) {
                    var index = 1;
                    data.list.forEach(function (v) {
                        v.index = index++;
                        v.debit = PWC.round(v.debit, 2);
                        v.credit = PWC.round(v.credit, 2);
                    });
                    $scope.TotalDebitAmount = PWC.round(data.debitTotalAmount, 2);
                    $scope.TotalCreditAmount = PWC.round(data.creditTotalAmount, 2);

                    $scope.gridOptionsHistoryVoucher.data = data.list;
                    $scope.queryVoucherResult.pageInfo = data.pageInfo;
                    computeVoucherPage(isVoucherHistory);
                }
            }).error(function () {
                SweetAlert.error($translate.instant('PleaseContactAdministrator'));
            });
        }
        else {
            vatReductionService.getVoucherList(queryParams).success(function (data) {
                if (data) {
                    var index = 1;
                    data.list.forEach(function (v) {
                        v.index = index++;
                        v.debit = PWC.round(v.debit, 2);
                        v.credit = PWC.round(v.credit, 2);
                    });
                    $scope.TotalDebitAmount = PWC.round(data.debitTotalAmount, 2);
                    $scope.TotalCreditAmount = PWC.round(data.creditTotalAmount, 2);

                    $scope.gridOptionsVoucher.data = data.list;
                    $scope.queryVoucherResult.pageInfo = data.pageInfo;
                    computeVoucherPage(isVoucherHistory);
                }
            }).error(function () {
                SweetAlert.error($translate.instant('PleaseContactAdministrator'));
            });
        }
    }

    //获取记账信息
    var getInvoiceListByPage = function (pageIndex, isInvoiceHistory) {
        $scope.curInvoicePage = pageIndex;
        //初始化查询信息
        var pageInfo = {
            totalCount: $scope.queryInvoiceResult.pageInfo.totalCount,
            pageIndex: pageIndex,
            pageSize: $scope.queryInvoiceResult.pageInfo.pageSize,
            totalPage: 0,
        }
        var queryParams = {
            pageInfo: pageInfo,
            status: $scope.invoiceSearchType.selected.id,
            period: $scope.period,
        };
        if (isInvoiceHistory) {
            vatReductionService.getOuputInvoiceHistoryList(queryParams).success(function (data) {
                if (data) {
                    var index = 1;
                    data.list.forEach(function (v) {
                        v.index = index++;
                        v.amount = PWC.round(v.amount, 2);
                        v.taxAmount = PWC.round(v.taxAmount, 2);
                    });
                    $scope.TotalAmount = PWC.round(data.amountTotal, 2);
                    $scope.TotalTaxAmount = PWC.round(data.taxAmountTotal, 2);  

                    $scope.gridOptionsHistoryInvoice.data = data.list;
                    $scope.queryInvoiceResult.pageInfo = data.pageInfo;
                    computeInvoicePage(isInvoiceHistory);
                }
            }).error(function () {
                SweetAlert.error($translate.instant('PleaseContactAdministrator'));
            });
        }
        else {
            vatReductionService.getOuputInvoiceList(queryParams).success(function (data) {
                if (data) {
                    var index = 1;
                    data.list.forEach(function (v) {
                        v.index = index++;
                        v.amount = PWC.round(v.amount, 2);
                        v.taxAmount = PWC.round(v.taxAmount, 2);
                    }); 
                    $scope.TotalAmount = PWC.round(data.amountTotal, 2);
                    $scope.TotalTaxAmount = PWC.round(data.taxAmountTotal, 2);

                    $scope.gridOptionsInvoice.data = data.list;
                    $scope.queryInvoiceResult.pageInfo = data.pageInfo;
                    computeInvoicePage(isInvoiceHistory);
                }
            }).error(function () {
                SweetAlert.error($translate.instant('PleaseContactAdministrator'));
            });
        }

    }

    //计算Voucher页数,创建分页栏
    var computeVoucherPage = function (isVoucherHistory) {

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

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

            var createVoucherPage = $("#totalVoucherPage").createPage({
                pageCount: totalPage,
                current: $scope.curVoucherPage,
                backFn: function (p) {
                    //单击回调方法,p是当前页码
                    getVoucherListByPage(p, isVoucherHistory);
                }
            });
            $('#totalVoucherPage').css('display', 'inline-block');
        } else {
            var createPage = $("#totalVoucherPage").createPage({
                pageCount: 0,
                current: $scope.curVoucherPage,
                backFn: function (p) {
                    //单击回调方法,p是当前页码
                    getVoucherListByPage(p, isVoucherHistory);
                }
            });
            $('#totalVoucherPage').css('display', 'inline-block');
        }
    };
    //计算Invoice页数,创建分页栏
    var computeInvoicePage = function (isInvoiceHistory) {

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

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

            var createInvoicePage = $("#totalInvoicePage").createPage({
                pageCount: totalPage,
                current: $scope.curInvoicePage,
                backFn: function (p) {
                    //单击回调方法,p是当前页码
                    getInvoiceListByPage(p, isInvoiceHistory);
                }
            });
            $('#totalInvoicePage').css('display', 'inline-block');
        } else {
            var createPage = $("#totalInvoicePage").createPage({
                pageCount: 0,
                current: $scope.curInvoicePage,
                backFn: function (p) {
                    //单击回调方法,p是当前页码
                    getInvoiceListByPage(p, isInvoiceHistory);
                }
            });
            $('#totalInvoicePage').css('display', 'inline-block');
        }
    };
    //初始获取数据
    var InitDataFromDB = function () {
        $scope.voucherSearchType.selected = $scope.voucherSearchTypeList[0];
        $scope.invoiceSearchType.selected = $scope.invoiceSearchTypeList[0];
        $scope.isVoucherHistory = false;
        $scope.isInvoiceHistory = false;
        getVoucherListByPage(1, $scope.isVoucherHistory);
        getInvoiceListByPage(1, $scope.isInvoiceHistory);
    }
    //过滤Voucher选择事件
    function changeVoucherSearchType(selectVoucherType) {
        setErrorWrapCssDefault();
        getVoucherListByPage(1, $scope.isVoucherHistory);       
    }
    //过滤Invoice选择事件
    function changeInvoiceSearchType() {
        setErrorWrapCssDefault();
        getInvoiceListByPage(1, $scope.isInvoiceHistory);       
    }

    var InitTabUI = function () {
        $('#gridVoucher').css('display', 'block');
        $('#gridHistoryVoucher').css('display', 'none');

        $('#gridInvoice').css('display', 'block');
        $('#gridHistoryInvoice').css('display', 'none');
    };

    (function initialize() {
        $log.debug('vatUnbilledInvoiceController.ctor()...');

        $scope.changeVoucherSearchType = changeVoucherSearchType;
        $scope.changeInvoiceSearchType = changeInvoiceSearchType;
        $scope.toggleMapGridTab = toggleMapGridTab;
        $scope.getMapGridHeight = getMapGridHeight;
        $scope.getImgGridHeight = getImgGridHeight;
        $scope.getTranCodeHeight = getTranCodeHeight;

        initVoucherPagination();
        initInvoicePagination();
        InitDataFromDB();

        $timeout(function () {
            $scope.isLoadComplete = true;

            setErrorWrapCssDefault();
            InitTabUI();
        }, 500);

    })();
}
]);