selectBrandModel.js 6.27 KB
(function (angular) {
  function selectBrandModel($scope, $rootScope, merchant, $uibModalInstance, aiNicheService, $filter) {
    $scope.isSuper = $rootScope.loginUser.isSuper
    $scope.merchant = merchant

    //主营品牌操作
    $scope.searchCar = '';
    $scope.carBrandOptions_copy = []
    $scope.$watch('searchCar', function (newValue) {
      $scope.carBrandOptions = [];
      $scope.carBrandOptions_copy.forEach(function (el) {
        if (el.name.indexOf(newValue) !== -1) {
          $scope.carBrandOptions.push(el)
        }
      })
    })
    $scope.toggleCarBrand = function (index, type) {
      // console.log("传入的参数:" + "index : " + index + " type : " + type);
      if (type == 2) {
        $scope.carBrandOptions[index].toggle = !$scope.carBrandOptions[index].toggle;
      }
      // console.log($scope.carBrandOptions[index].carSeresList_copy)
      if (!$scope.carBrandOptions[index].carSeresList_copy) {
        $scope.carBrandOptions[index].childs1 = $scope.carBrandOptions[index].childs;
        $scope.carBrandOptions[index].carSeresList_copy = $scope.carBrandOptions[index].childs;
        angular.element(`#search-${index}`)[0].addEventListener('input', function (val) {
          var value = val.target.value;
          $scope.carBrandOptions[index].childs1 = [];
          $scope.carBrandOptions[index].carSeresList_copy.forEach(function (el) {
            if (el.name.indexOf(value) !== -1) {
              $scope.carBrandOptions[index].childs1.push(el)
            }
            $scope.$apply();
          })
        })
        if (type == 1) {
          angular.forEach($scope.carBrandOptions[index].childs1, function (k) {
            k.checked = $scope.carBrandOptions[index].checked;
            angular.forEach(k.childs, function (e) {
              e.checked = k.checked;
            });
          });
        }
      } else {
        if (type == 1) {
          angular.forEach($scope.carBrandOptions[index].childs1, function (k) {
            k.checked = $scope.carBrandOptions[index].checked;
            angular.forEach(k.childs, function (e) {
              e.checked = k.checked;
            });
          });
        }
        if (type == 3) {
          var ifOneChecked = false;
          for (var i = 0; i < $scope.carBrandOptions[index].childs1.length; i++) {
            if ($scope.carBrandOptions[index].childs1[i].checked) {
              ifOneChecked = true;
              angular.forEach($scope.carBrandOptions[index].childs1[i].childs, function (k) {
                k.checked = $scope.carBrandOptions[index].childs1[i].checked;
              });
              break;
            } else {
              angular.forEach($scope.carBrandOptions[index].childs1[i].childs, function (k) {
                k.checked = $scope.carBrandOptions[index].childs1[i].checked;
              });
            }
          }
          if (ifOneChecked) {
            $scope.carBrandOptions[index].checked = true;
          } else {
            $scope.carBrandOptions[index].checked = false;
          }
        }
      }
    };
    $scope.selectCarSmallBrandArr = [];
    $scope.getCheckedCarBrand = function () {
      var selectCarSmallBrandArr = [];
      var carBrandSelectItems = $filter('filter')($scope.carBrandOptions, {
        checked: true
      }, true);
      angular.forEach(carBrandSelectItems, function (k) {
        var carSmallBrandSelectItems = $filter('filter')(k.childs1, {
          checked: true
        }, true);
        angular.forEach(carSmallBrandSelectItems, function (e) {
          if (!e.disabled) {
            selectCarSmallBrandArr.push({
              'brandId': k.id,
              'brandName': k.name,
              'brandLogo': k.logoUrl,
              'childsBrandId': e.id,
              'childsBrandName': e.name
            });
          }
        });
      });
      $scope.selectCarSmallBrandArr = selectCarSmallBrandArr;
    };

    /**
     * 关闭弹框方法
     * */
    $scope.handleClose = function () {
      $uibModalInstance.dismiss(0);
    };

    $scope.handleConfirm = function () {
      //主营品牌数据           
      $scope.getCheckedCarBrand();
      // if ($scope.selectCarSmallBrandArr.length > 0) {
      //   params.carBrandList = $scope.selectCarSmallBrandArr;
      // }
      // console.log($scope.selectCarSmallBrandArr)
      aiNicheService.addCarRules($scope.selectCarSmallBrandArr, function (res) {
        swal({
          type: res.returnCode == 0 ? 'success' : 'error',
          title: '',
          html: true,
          text: res.returnCode == 0 ? '添加成功' : res.message,
          showConfirmButton: false,
          timer: 1000
        });
        if (res.returnCode == 0) {
          $uibModalInstance.close(true);
        }
      })
    }

    $scope.selectedBrandList = []
    $scope.init = function () {
      apiName = $scope.isSuper ? 'getAllCarBrandList' : 'getMerchantCarBrand'
      aiNicheService.getAllVehicleBrand(function (res) {
    	if(res.data){
    		res.data.forEach(function (item, index) {
    	          item.childs.forEach(function (subItem, subIndex) {
    	            $scope.selectedBrandList.push({
    	              brandId: item.id,
    	              childsBrandId: subItem.id
    	            })
    	          })
    	     })
    	}
        
        //获取车品牌
        aiNicheService[apiName](function (subRes) {
          $scope.carBrandOptions = subRes.data;
          $scope.carBrandOptions_copy = subRes.data

          angular.forEach($scope.selectedBrandList, function (i) {
            angular.forEach($scope.carBrandOptions, function (j, index) {
              if (i.brandId == j.id) {
                j.checked = true;
                j.disabled = true;
                $scope.carBrandOptions[index].childs1 = $scope.carBrandOptions[index].childs;
                $scope.carBrandOptions[index].carSeresList_copy = $scope.carBrandOptions[index].childs;

                angular.forEach($scope.carBrandOptions[index].childs1, function (k, index1) {
                  if (!i.childsBrandId || i.childsBrandId == k.id) {
                    k.checked = true;
                    k.disabled = true;
                  }
                });
              }
            });
          });
        });
      });
    }
    $scope.init();

  }
  angular.module('AIniche').controller('selectBrandModel', selectBrandModel);
})(angular);