model-mapping-add-edit.ctrl.js 3.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
invoiceModule.controller('modelMappingAddEditController', ['$scope', '$log', '$q', '$translate', 'messagebox',
    'vatOutputInvoiceManageService', 'refreshFunc', 'model',
function ($scope, $log, $q,$translate, messagebox, vatOutputInvoiceManageService, refreshFunc, model) {
    var isEdit = !_.isEmpty(model);
    var pleaseSelect = $translate.instant("ChoosePlaceholder");
    var pleaseInput = $translate.instant('InputPlaceholder');
    var initSelectBoxes = function () {
        return $q.all([
            vatOutputInvoiceManageService.getModelSeries(),
            vatOutputInvoiceManageService.getCabinConfiguration()
        ]).then(function (result) {
            if (result && result[0] && result[0].data) {
                $scope.modelSeriesList = result[0].data;
            }
            else {
                $scope.modelSeriesList = [];
            }

            if (result && result[1] && result[1].data) {
                //$scope.cabinConfigurationList = _.map(result[1].data, function (x) {
                //    return _.isEmpty(x) ? 'NA' : x;
                //});
                //fix bug13211
                $scope.cabinConfigurationList = _.filter(result[1].data, function (x) {
                    return !_.isEmpty(x);
                });
               
            }
            else {
                $scope.cabinConfigurationList = [];
            }

            $scope.modelSeriesOption = {
                items: $scope.modelSeriesList,
                bindingOptions: {
                    value: 'model.modelSeries'
                },
                placeholder: pleaseSelect
            };

            $scope.cabinConfigurationOption = {
                items: $scope.cabinConfigurationList,
                bindingOptions: {
                    value: 'model.cabinConfiguration'
                },
                placeholder: pleaseSelect
            };
        });
    };

    //关闭弹窗口
    $scope.hidePopPanel = function () {
        $scope.$dismiss({ $value: 'cancel' });
    };

    $scope.saveModel = function () {
        if (_.isEmpty($scope.model.modelSeries)) {
            messagebox.warning('ModelSeriesRequired');
            return;
        }

        if (_.isEmpty($scope.model.cabinConfiguration)) {
            messagebox.warning('CabinConfigurationRequired');
            return;
        }

        if (_.isEmpty($scope.model.makeAndModel)) {
            messagebox.warning('MakeAndModelRequired');
            return;
        }

        var promise;
        if (isEdit) {
            promise = vatOutputInvoiceManageService.updateModelMappingConfig($scope.model);
        }
        else {
            promise = vatOutputInvoiceManageService.addModelMappingConfig($scope.model);
        }

        return promise.then(function (data) {
            if (!data || !data.data) {
                messagebox.warning('UnknownError');
            }
            else if (!data.data.result) {
                messagebox.warning(_.isEmpty(data.data.resultMsg) ? 'UnknownError' : data.data.resultMsg);
            }
            else {
                $scope.hidePopPanel();
                refreshFunc();
            }
        });
    };

    $scope.$watch('model.makeAndModel', function (newVal, oldVal) {
        var rtn = '';
        if (!_.isEmpty(newVal)) {
            var strs = newVal.split(' ');
            $scope.model.category = strs[0];
        }
        else {
            $scope.model.category = '';
        }
    });

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

        $scope.title = isEdit ? 'EditMapping' : 'AddNewMapping';
        var localModel = model ? angular.copy(model) : {};
        if (_.isEmpty(localModel.cabinConfiguration)) {
            localModel.cabinConfiguration = '';
        }

        $scope.model = localModel;
        initSelectBoxes();
        $log.debug($scope.model);
    })();
}]);