addDeviceModelCtrl.js 7.38 KB


(function (angular) {
  function addDeviceModelCtrl($scope, $rootScope, customerService, $uibModalInstance) {
    // 3-轩宇
    $scope.channelType = $rootScope.loginUser.channelConfig.channelType
    $scope.required = ($scope.channelType == 3)

    // 非轩宇渠道,“设备安装时当前里程”、“下次保养里程”、“下次保养时间”、“上次保养里程”、“上次保养时间”为非必填项。
    // 但若只要有其中一项为非空。则此5个字段,必须全部填入。
    $scope.otherRequired = false
    $scope.phoneError = false
    $scope.otherAttr = ['installMileage', 'nextMaintainTime', 'nextMaintainMileage', 'lastMaintainTime', 'lastMaintainMileage']
    $scope.addData = {
      imei: '',
      name: '',
      phone: '',
      plateNum: '',
      standNo: '',
      installMileage: '',
      nextMaintainTime: '',
      nextMaintainMileage: '',
      lastMaintainTime: '',
      lastMaintainMileage: ''
    };

    $scope.form = {
      nextTime: '',
      lastTime: ''
    }
    // 下次保养时间
    $scope.nextMainTimeOpt = {
      singleDatePicker: true,
      format: 'YYYY-MM-DD',
      eventHandlers: {
        'apply.daterangepicker': function (ev, picker) {
          $scope.form.nextTime.startDate = picker.startDate
          $scope.form.nextTime.endDate = picker.endDate
          $scope.$apply()
        },
        'cancel.daterangepicker': function (ev, picker) {
          $scope.form.nextTime.startDate = ''
          $scope.form.nextTime.endDate = ''
          $scope.$apply()
        }
      }
    }
    // 上次保养时间
    $scope.lastMainTimeOpt = {
      singleDatePicker: true,
      format: 'YYYY-MM-DD',
      eventHandlers: {
        'apply.daterangepicker': function (ev, picker) {
          $scope.form.lastTime.startDate = picker.startDate
          $scope.form.lastTime.endDate = picker.endDate
          $scope.$apply()
        },
        'cancel.daterangepicker': function (ev, picker) {
          $scope.form.lastTime.startDate = ''
          $scope.form.lastTime.endDate = ''
          $scope.$apply()
        }
      }
    }
    $scope.today = moment()
    $scope.submitted = false;

    $scope.handleAddClose = function () {
      $uibModalInstance.close();
    }

    $scope.model = {};
    //获取品牌车型
    function getAllVehicleBrand() {
      return new Promise(function (resolve, reject) {
        customerService.getAllVehicleBrand(function (res) {
          $scope.carBrandOptions = res.data;
          resolve();
        });
      })
    }

    //车辆品牌值改变事件
    $scope.changeCarBrand = function (id) {
      for (var i = 0; i < $scope.carBrandOptions.length; i++) {
        if ($scope.carBrandOptions[i].id == id) {
          $scope.carSmallBrandOptions = JSON.parse(JSON.stringify($scope.carBrandOptions[i].childs));
          break;
        }
      }
      $scope.carSeriesOptions = [];
      $scope.carTypeOptions = [];
      $scope.model.carSmallBrand = '';
      $scope.model.carSeries = '';
      $scope.model.carType = '';
    };

    //车辆品牌值改变事件
    $scope.changeCarSmallBrand = function (id) {
      for (var i = 0; i < $scope.carSmallBrandOptions.length; i++) {
        if ($scope.carSmallBrandOptions[i].id == id) {
          $scope.carSeriesOptions = JSON.parse(JSON.stringify($scope.carSmallBrandOptions[i].childs));
          break;
        }
      }
      $scope.carTypeOptions = [];
      $scope.model.carSeries = '';
      $scope.model.carType = '';
    };

    //车系值改变事件
    $scope.changeCarSeries = function (id) {
      customerService.getCardType({
        carSeriesId: id
      }, function (res) {
        if (res.data) {
          $scope.carTypeOptions = res.data;
        }
      });
      $scope.model.carType = '';
    };

    // 必填项校验
    function checkColumn() {
      var flag = true;
      var requiredAttr = [
        'name',
        'phone',
        'standNo',
      ]

      for (var i = 0; i < requiredAttr.length; i++) {
        if (!$scope.addData[requiredAttr[i]]) {
          flag = false;
          return flag;
        }
      }

      /*var reg = new RegExp("^[1][3,4,5,6,7,8,9][0-9]{9}$");
      console.log(reg.test($scope.addData.phone))
      $scope.phoneError = false;
      if (!reg.test($scope.addData.phone)) {
        $scope.phoneError = true;
      }*/
      if ($scope.otherRequired) {
        for (var i = 0; i < $scope.otherAttr.length; i++) {
          if (!$scope.addData[$scope.otherAttr[i]]) {
            flag = false;
            return flag
          }
        }
      }
      var requiredXuanyuAttr = [
        'plateNum',
        'installMileage',
        'nextMaintainTime',
        'nextMaintainMileage',
        'lastMaintainTime',
        'lastMaintainMileage'
      ]
      if ($scope.required) {
        for (var i = 0; i < requiredXuanyuAttr.length; i++) {
          if (!$scope.addData[requiredXuanyuAttr[i]]) {
            flag = false;
            return flag;
          }
        }
      }
      return flag;
    };

    /**
     * 下拉框绑定的值是一个对象,需要获取值
     * @param fieldName
     * @returns {*}
     */
    function getSelectValue(objName, id, tempObjName) {
      var obj = tempObjName ? $scope.tempModel[tempObjName] : $scope.model[objName];
      return (obj && obj[id]) ? obj[id] : '';
    }

    /**
     * 获取参数(所有查询都从此获取)
     */
    function getParams() {
      $scope.addData.vehicleBrandId = getSelectValue('carBrand', 'id');
      $scope.addData.vehicleBrand = getSelectValue('carBrand', 'name');
      $scope.addData.vehicleChildsBrandId = getSelectValue('carSmallBrand', 'id');
      $scope.addData.vehicleChildsBrandName = getSelectValue('carSmallBrand', 'name');
      $scope.addData.vehicleSeriesId = getSelectValue('carSeries', 'id');
      $scope.addData.vehicleSeriesName = getSelectValue('carSeries', 'name');
      $scope.addData.vehicleTypeId = getSelectValue('carType', 'id');
      $scope.addData.vehicleTypeName = getSelectValue('carType', 'name');
      // return params;
    }
    $scope.handleAddConfirm = function () {
      $scope.submitted = true;
      ($scope.form.nextTime.startDate) && ($scope.addData.nextMaintainTime = $scope.form.nextTime.startDate.format('YYYY-MM-DD'));
      ($scope.form.lastTime.startDate) && ($scope.addData.lastMaintainTime = $scope.form.lastTime.startDate.format('YYYY-MM-DD'));
      for (var i = 0; i < $scope.otherAttr.length; i++) {
        if ($scope.addData[$scope.otherAttr[i]]) {
          $scope.otherRequired = true;
          break;
        }
      }
      getParams();
      var flag = checkColumn();
      // console.log(flag)
      if (!flag) {
        return;
      }
      var params = JSON.parse(JSON.stringify($scope.addData));
      console.log(params)
      customerService.addDevice(params, function (res) {
        if (res.returnCode == '0') {
          $uibModalInstance.close(true);
          swal({
            title: "添加成功",
            text: "",
            type: "success",
            timer: 2000,
            showConfirmButton: false
          });
        } else {
          swal({
            title: res.message,
            text: "",
            type: 'error',
            confirmButtonText: '确定',
            showConfirmButton: true
          });
        }
      })
    };

    (function () {
      getAllVehicleBrand();
    })();

  }
  angular.module('customer').controller('addDeviceModelCtrl', addDeviceModelCtrl);
})(angular);