invoiceModule.controller('invoiceManageVerifyController', ['$scope', '$log', '$document', 'SweetAlert', '$uibModal', '$translate', 'uiGridConstants', 'apiInterceptor', '$interval', 'region', 'InvoiceManageService',
        function ($scope, $log, $document, SweetAlert, $uibModal, $translate, uiGridConstants, apiInterceptor, $interval, region, InvoiceManageService) {
            'use strict';
            var initDatePicker = function () {
                $('#invoiceDatepicker').datepicker({
                    language: region,
                    format: $translate.instant('dateFormat4YearMonthDay')
                });
            };
            //页面加载初始化控件
            var initControl = function () {
                initDatePicker();
                $scope.verifyManagement = {
                    buyerNameOptions: {
                        bindingOptions: {
                            dataSource: 'searchFilterOptions.invoiceBuyerList'
                        },
                        displayExpr: "value",
                        valueExpr: "id",
                        placeholder: $translate.instant('ChoosePlaceholder'),
                        showClearButton: true,
                        height: '30px'
                    },
                    textBoxOptions: {
                        height: '30px',
                        placeholder: $translate.instant('InputPlaceholder'),
                    },
                    faceTaxOptions: {
                        //min: 0,
                        value: null,
                        height: '30px'
                    },
                    invoiceTypeOptions: {
                        displayExpr: "value",
                        valueExpr: "id",
                        bindingOptions: {
                            dataSource: 'searchFilterOptions.invoiceTypeList'
                        },
                        placeholder: $translate.instant('ChoosePlaceholder'),
                        showClearButton: true,
                        height: '30px'
                    },
                    buttonQueryOptions: {
                        text: $translate.instant('Query'),
                        //height: 34,
                        //width: 195,
                        onClick: function () {
                            $scope.eventService.refreshTable();
                        }
                    },
                    buttonGetVerifyListOptions: {
                        text: $translate.instant('GetNotVerifyInvoice'),
                        //height: 34,
                        //width: 195,
                        onClick: function () {
                            $scope.NotVerifyManage.getAllNotVerifyList();
                        }
                    },
                    buttonClearOptions: {
                        text: $translate.instant('CertificationClearning'),
                        //height: 34,
                        //width: 195,
                        onClick: function () {
                            $scope.NotVerifyManage.UpdateVerifyList();
                        },
                        bindingOptions: {
                            disabled: "disabled"
                        }
                    },
                    verifyDataGridOptions: {
                        bindingOptions: {
                            'dataSource': 'verifyList'
                        },
                        selection: {
                            mode: "multiple"
                        },
                        hoverStateEnabled: true,
                        noDataText: $translate.instant('NoDataAvailable'),
                        columnAutoWidth: true,
                        showBorders: true,
                        allowColumnResizing: true,
                        onSelectionChanged: function (data) {
                            $scope.selectedItems = data.selectedRowsData;
                            $scope.disabled = !$scope.selectedItems.length;
                        },
                        //pager: {
                        //    showPageSizeSelector: true,
                        //    allowedPageSizes: [5, 10, 20],
                        //    showInfo: false
                        //},
                        //paging: {
                        //    enabled: false,
                        //    pageSize: 10
                        //},
                        columns: [
                                {
                                    dataField: 'invoiceCode', caption: $translate.instant('InvoiceFPDM'),
                                    cellTemplate: 'invoiceCodeTemplate', alignment: 'left'
                                },
                                { dataField: 'invoiceNumber', caption: $translate.instant('InvoiceFPHM') },
                                { dataField: 'buyerName', caption: $translate.instant('BuyerName') },
                                { dataField: 'buyerTaxNumber', caption: $translate.instant('BuyerTaxNumber') },
                                { dataField: 'sellerName', caption: $translate.instant('SellerName') },
                                { dataField: 'sellerTaxNumber', caption: $translate.instant('SellerTaxNumber') },
                                { dataField: 'totalPriceLowerCase', caption: $translate.instant('FaceTaxAmount') },
                                { dataField: 'taxAmount', caption: $translate.instant('InvoiceSE') },
                                { dataField: 'invoiceDate', caption: $translate.instant('InvoiceDate') },
                                { dataField: 'invoiceEntityType', caption: $translate.instant('InvoiceEntity') },
                                { dataField: 'invoiceType', caption: $translate.instant('InvoiceFPLX') }
                        ],
                        showInvoiceTable: function (invoice) {
                            $scope.selectedInvoice = invoice.data;
                            $scope.verifyManagement.verifyDataGridOptions.thisModalService.open();
                        },
                        thisModalService: {
                            modalInstance: null,
                            open: function () {
                                var parentSelector = '.invoice-manage-verify';
                                var parentElem = parentSelector ? angular.element($document[0].querySelector(parentSelector)) : undefined;

                                var modalInstance = $uibModal.open({
                                    animation: true,
                                    ariaLabelledBy: 'modal-title',
                                    ariaDescribedBy: 'modal-body',
                                    templateUrl: 'invoiceTable.html',
                                    windowClass: 'invoice-table-dialog',
                                    scope: $scope,
                                    appendTo: parentElem,
                                });

                                $scope.verifyManagement.verifyDataGridOptions.thisModalService.modalInstance = modalInstance;
                            },
                            close: function (result) {
                                if ($scope.verifyManagement.verifyDataGridOptions.thisModalService.modalInstance) {
                                    $scope.verifyManagement.verifyDataGridOptions.thisModalService.modalInstance.close(result);
                                }
                            },
                            cancel: function () {
                                if ($scope.verifyManagement.verifyDataGridOptions.thisModalService.modalInstance) {
                                    $scope.verifyManagement.verifyDataGridOptions.thisModalService.modalInstance.dismiss('cancel');
                                }
                            }

                        }
                    }
                };

            };

            //页面加载初始化数据
            var initData = function () {
                $scope.searchFilterOptions = {};
                $scope.NotVerifyManage.getVerifyFilterBasicData();
              //  $scope.paging.populatePagingSelection();
                $scope.fullData = [
 {
     "invoiceCode": 1101432450,
     "invoiceNumber": 60118002,
     "buyerName": "嘉吉南京工厂",
     "buyerTaxNumber": 660200601108002,
     "sellerName": "XX玉米加工厂",
     "sellerTaxNumber": 660200601108002,
     "totalPriceLowerCase": 137310.07,
     "taxAmount": 23342.71,
     "invoiceDate": "2017-04-10",
     "invoiceEntityType": "电子票",
     "invoiceType": "普票",
     "uploadDate": "2017-5-6",
     "invoiceSource": "原料采购",
     "invoiceStatus": "已补录",
     "invoiceName": "上海增值税普通发票",
     "totalPriceUpperCase": "四百壹拾柒元整",
    
     "items": [
         {
             productionName: "餐饮费", specification: "", unit: "项", quantity: 1, unitPrice: 404.85437,
             amount: 404.85437, taxRate: 0.03, taxAmount: 12.15,
         },
         {
             productionName: "住宿费", specification: "", unit: "项", quantity: 2, unitPrice: 404.85437,
             amount: 404.85437, taxRate: 0.03, taxAmount: 12.15,
         },
         {
             productionName: "交通及电话费用", specification: "", unit: "项", quantity: 3, unitPrice: 404.85437,
             amount: 404.85437, taxRate: 0.03, taxAmount: 12.15,
         },
     ]
 },
 {
     "invoiceCode": 1101432450,
     "invoiceNumber": 60118002,
     "buyerName": "嘉吉南京工厂",
     "buyerTaxNumber": 660200601108002,
     "sellerName": "XX玉米加工厂",
     "sellerTaxNumber": 660200601108002,
     "totalPriceLowerCase": 137310.07,
     "taxAmount": 23342.71,
     "invoiceDate": "2017-4-10",
     "invoiceEntityType": "电子票",
     "invoiceType": "普票",
     "uploadDate": "2017-5-6",
     "invoiceSource": "原料采购",
     "invoiceStatus": "待匹配"
 },
 {
     "invoiceCode": 1101432450,
     "invoiceNumber": 60118002,
     "buyerName": "嘉吉南京工厂",
     "buyerTaxNumber": 660200601108002,
     "sellerName": "XX玉米加工厂",
     "sellerTaxNumber": 660200601108002,
     "totalPriceLowerCase": 137310.07,
     "taxAmount": 23342.71,
     "invoiceDate": "2017-4-10",
     "invoiceEntityType": "电子票",
     "invoiceType": "普票",
     "uploadDate": "2017-5-6",
     "invoiceSource": "原料采购",
     "invoiceStatus": "已匹配"
 },
 {
     "invoiceCode": 1101432450,
     "invoiceNumber": 60118002,
     "buyerName": "嘉吉南京工厂",
     "buyerTaxNumber": 660200601108002,
     "sellerName": "XX玉米加工厂",
     "sellerTaxNumber": 660200601108002,
     "totalPriceLowerCase": 49,
     "taxAmount": 8.33,
     "invoiceDate": "2016-4-10",
     "invoiceEntityType": "纸票",
     "invoiceType": "专票",
     "uploadDate": "2017-5-6",
     "invoiceSource": "非原料集中采购",
     "invoiceStatus": "识别成功"
 },
 {
     "invoiceCode": 1101432450,
     "invoiceNumber": 60118002,
     "buyerName": "嘉吉镇江工厂",
     "buyerTaxNumber": 660200601108002,
     "sellerName": "XX玉米加工厂",
     "sellerTaxNumber": 660200601108002,
     "totalPriceLowerCase": 25,
     "taxAmount": 4.25,
     "invoiceDate": "2016-4-10",
     "invoiceEntityType": "纸票",
     "invoiceType": "专票",
     "uploadDate": "2017-5-6",
     "invoiceSource": "非原料集中采购",
     "invoiceStatus": "已清理"
 },
 {
     "invoiceCode": 1101432450,
     "invoiceNumber": 60118002,
     "buyerName": "嘉吉镇江工厂",
     "buyerTaxNumber": 660200601108002,
     "sellerName": "XX玉米加工厂",
     "sellerTaxNumber": 660200601108002,
     "totalPriceLowerCase": 295,
     "taxAmount": 50.15,
     "invoiceDate": "2016-4-10",
     "invoiceEntityType": "纸票",
     "invoiceType": "专票",
     "uploadDate": "2017-5-6",
     "invoiceSource": "非原料集中材料",
     "invoiceStatus": "无法识别"
 },
 {
     "invoiceCode": 1101432450,
     "invoiceNumber": 60118002,
     "buyerName": "嘉吉镇江工厂",
     "buyerTaxNumber": 660200601108002,
     "sellerName": "嘉吉有限公司",
     "sellerTaxNumber": 660200601108002,
     "totalPriceLowerCase": 36,
     "taxAmount": 6.12,
     "invoiceDate": "2016-4-10",
     "invoiceEntityType": "纸票",
     "invoiceType": "普票",
     "uploadDate": "2017-5-6",
     "invoiceSource": "原料采购",
     "invoiceStatus": "待匹配"
 },
 {
     "invoiceCode": 1101432450,
     "invoiceNumber": 60118002,
     "buyerName": "嘉吉有限公司",
     "buyerTaxNumber": 660200601108002,
     "sellerName": "嘉吉南京工厂",
     "sellerTaxNumber": 660200601108002,
     "totalPriceLowerCase": 25,
     "taxAmount": 4.25,
     "invoiceDate": "2016-4-10",
     "invoiceEntityType": "纸票",
     "invoiceType": "普票",
     "uploadDate": "2017-5-6",
     "invoiceSource": "原料采购",
     "invoiceStatus": "验真失败"
 },
 {
     "invoiceCode": 1101432450,
     "invoiceNumber": 60118002,
     "buyerName": "嘉吉有限公司",
     "buyerTaxNumber": 660200601108002,
     "sellerName": "哈哈玉米加工厂",
     "sellerTaxNumber": 660200601108002,
     "totalPriceLowerCase": 4870,
     "taxAmount": 827.9,
     "invoiceDate": "2017-4-10",
     "invoiceEntityType": "纸票",
     "invoiceType": "普票",
     "uploadDate": "2017-5-6",
     "invoiceSource": "非原料集中采购",
     "invoiceStatus": "待匹配"
 },
 {
     "invoiceCode": 1101432450,
     "invoiceNumber": 60118002,
     "buyerName": "嘉吉有限公司",
     "buyerTaxNumber": 660200601108002,
     "sellerName": "uu玉米加工厂",
     "sellerTaxNumber": 660200601108002,
     "totalPriceLowerCase": 295,
     "taxAmount": 50.15,
     "invoiceDate": "2017-4-10",
     "invoiceEntityType": "纸票",
     "invoiceType": "专票",
     "uploadDate": "2017-5-6",
     "invoiceSource": "非原料集中材料",
     "invoiceStatus": "已认证"
 },
 {
     "invoiceCode": 1101432450,
     "invoiceNumber": 60118002,
     "buyerName": "嘉吉有限公司",
     "buyerTaxNumber": 660200601108002,
     "sellerName": "XX玉米加工厂",
     "sellerTaxNumber": 660260601108002,
     "totalPriceLowerCase": 18230.98,
     "taxAmount": 3099.27,
     "invoiceDate": "2017-4-10",
     "invoiceEntityType": "纸票",
     "invoiceType": "专票",
     "uploadDate": "2017-5-6",
     "invoiceSource": "原料采购",
     "invoiceStatus": "无法认证"
 },
 {
     "invoiceCode": 1101432450,
     "invoiceNumber": 60118002,
     "buyerName": "嘉吉有限公司",
     "buyerTaxNumber": 660200601108002,
     "sellerName": "XX玉米加工厂",
     "sellerTaxNumber": 660200601108002,
     "totalPriceLowerCase": 18230.98,
     "taxAmount": 3099.27,
     "invoiceDate": "2017-4-10",
     "invoiceEntityType": "纸票",
     "invoiceType": "专票",
     "uploadDate": "2017-5-6",
     "invoiceSource": "原料采购",
     "invoiceStatus": "待认证 "
 }
                ];

                $scope.verifyList = $scope.fullData;
                $scope.paging.pagingOptions.totalItems = $scope.verifyList.length;
            };
            //内部处理
            var memoryService = {
                translateDictionary: function (list) {
                    _.each(list, function (item) {
                        item.value = $translate.instant(item.value);

                    });
                },
                copyDataFromFull: function () {
                    return angular.copy($scope.fullData);
                }
            };
            //button的事件
            $scope.eventService = {
                refreshTable: function () {
                    //
                    //var grid = $('#verifyManagementGrid').dxDataGrid('instance');
                    //grid.clearSelection();
                    // grid.refresh(); 
                    var allData = $scope.NotVerifyManage.queryList();
                    $scope.paging.pagingOptions.totalItems = allData.length;
                    if (allData.length > 0) {
                        var pageSize = $scope.paging.pagingOptions.pageSize;
                        var pageIndex = $scope.paging.pagingOptions.pageIndex;
                        $scope.verifyList = _.filter(allData, function (item, index) {
                            return (pageIndex - 1) * pageSize <= index && index < pageIndex * pageSize;
                        });
                        
                    }else
                    {
                        $scope.verifyList = [];                       
                    }
                },


            };
            //分页的设置
            $scope.paging = {
                pagingOptions: {
                    pageIndex: 1,  //当前页码
                    totalItems: 100,  //总数据
                    totalPages: 10,//总页数
                    maxSize: 10, //分页数字的限制。
                    pageSize: constant.page.pageSizeArrary[1],  //每页多少条数据 
                    pageSizeString: constant.page.pageSizeArrary[1].toString(),
                },
            };
            //同一个控件的所有操作最好放在一个$scope函数里
            //例如工作流,有添加工作流,修改,删除,添加(删除)用户,添加(删除)角色
            $scope.NotVerifyManage = {
                getAllNotVerifyList: function () {

                },
                updateVerifyList: function () {
                    //$.each($scope.selectedItems, function () {
                    //    $scope.verifyList.remove(this);
                    //});
                    $("#verifyManagementGrid").dxDataGrid("instance").refresh();
                },               
                queryList: function (allData) {
                    var queryAvailableList = memoryService.copyDataFromFull();
                    _.each($scope.query, function (item, key) {
                        if (item) {                           
                            switch (key) {
                                case "buyerNameQuery":                                   
                                    queryAvailableList = _.where(queryAvailableList, { buyerName: item });
                                    break;
                                case "sellerNameQuery":                                   
                                    queryAvailableList = _.filter(queryAvailableList, function (record) {
                                        return record.sellerName.indexOf(item) > -1;
                                    });
                                    break;
                                case "invoiceDateQueryStart":
                                    queryAvailableList = _.filter(queryAvailableList, function (record) {
                                        return record.invoiceDate>=item;
                                    });
                                    break;
                                case "invoiceDateQueryEnd":
                                    queryAvailableList = _.filter(queryAvailableList, function (record) {
                                        return record.invoiceDate <= item;
                                    });
                                    break;
                                case "totalPriceLowerCaseStartQuery":                                   
                                    queryAvailableList = _.filter(queryAvailableList, function (record) {
                                        return record.totalPriceLowerCase >= item;
                                    });
                                    break;
                                case "totalPriceLowerCaseEndQuery":
                                    queryAvailableList = _.filter(queryAvailableList, function (record) {
                                        return record.totalPriceLowerCase <= item;
                                    });
                                    break;
                                case "invoiceNumberQuery":                                   
                                    queryAvailableList = _.filter(queryAvailableList, function (record) {
                                        return record.invoiceNumber.indexOf(item) > -1;
                                    });
                                    break;
                                case "invoiceTypeQuery":                                   
                                    queryAvailableList = _.where(queryAvailableList, { invoiceType: item });                                   
                                    break;

                            }
                           
                        }
                    });
                    return queryAvailableList;
                },
                getVerifyFilterBasicData: function () {
                    InvoiceManageService.getVerifyFilterBasicData().success(function (res) {
                        if (res.result) {
                            memoryService.translateDictionary(res.data.invoiceTypeList);
                            $scope.searchFilterOptions.invoiceTypeList = res.data.invoiceTypeList;
                        }
                    }).error(function () {
                        SweetAlert.error('', $translate.instant('CommonFail'));
                    });
                }
            };
            $scope.query = {
                buyerNameQuery: null,
                sellerNameQuery: null,
                invoiceDateQueryStart: null,
                invoiceDateQueryEnd: null,
                totalPriceLowerCaseStartQuery: null,
                totalPriceLowerCaseEndQuery: null,
                invoiceNumberQuery: null,
                invoiceTypeQuery: null
            };
            (function initialize() {
                $log.debug('invoiceManageVerifyController.ctor()...');
                $scope.selectedItems = [];
                $scope.disabled = true;
                $scope.selectedInvoice = null;
                $scope.fullData = [];
                initData();
                initControl();
            })();

        }
]);