addPackageCtrl.js
35.5 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
(function (angular) {
function addPackageCtrl($scope, DTOptionsBuilder, DTColumnBuilder, addCarPackageService, $state, $uibModal,$stateParams,$rootScope) {
/**
* 常量定义
*/
//自定义标签
$scope.labels = [[]];
//默认标签
$scope.defaultLabels = [{labelName:'基础包'},{labelName:'增值包'},{labelName:'单品热销'}];
//选中的标签
$scope.activeTarget = '';
//展示-左侧菜单
$scope.menus = [];
$scope.menuActive = '';
//礼包名称列表
$scope.packageList = [
{
name: '礼包1',
showAll:1,
carBoutiqueCarTypes: [
{
carBrand: '全部适用',
carChildsBrandName: '全部适用',
carSeriesName: '全部适用',
carBrandId: '-1',
carChildsBrandId: '-1',
carSeriesId: '-1'
}
]
}
];
$scope.packageIndex = 0;
//是否新增标签
$scope.showAddInput = false;
//绑定标签值
$scope.giftLabel = '';
//树对象
$scope.treeObject = '';
//车辆数据
$scope.carBrandOptions = [];
$scope.carBrandOptions_copy = [];
//适用车型
//$scope.showAll = 1;
//标签选中集合
$scope.labelList = [];
//标签错误提示语
$scope.labelTips = {show: false};
//礼包提示语
$scope.giftNameTips = {show: false};
//车系提示语
$scope.carTips = {show:false};
//提交loading
$scope.loading = false;
//门店下的车系列表
$scope.seriesList = [];
//选择的商品列表
$scope.productList = [];
//商品提示语
$scope.productTips = {show: false};
//商品零售价提示语
$scope.retailPriceTips = {show: false};
//商品折扣价提示语
$scope.discountPriceTips = {show: false};
/**
* 标签点击事件
*/
$scope.tagEvent = function (item, index) {
var list = $scope.labelList || [];
var labels = $scope.labels || [];
var package = $scope.packageList;
var _index = list.findIndex(function (k) {
return k.labelName == item.labelName;
});
$scope.labelTips = {show:false,message:'请选择标签'};
/*if (_index >= 0) {
list.splice(_index, 1);
labels[index].checked = false;
} else {
labels[index].checked = true;
list.push(item);
};*/
var o = {};
o.labelName = item.labelName;
if($scope.activeTarget == item.labelName){
$scope.activeTarget = '';
package[$scope.packageIndex].carBoutiqueLabels = [];
}else{
$scope.activeTarget = item.labelName;
package[$scope.packageIndex].carBoutiqueLabels = [o];
}
$scope.labelList = [o];
$scope.labels = labels;
};
/**
* 菜单点击事件
*/
$scope.menuEvent = function (id) {
$scope.menuActive = id;
};
/**
* 礼包菜单点击事件
*/
$scope.menusEvent = function (index) {
$scope.packageIndex = index;
$scope.activeTarget = '';
};
/**
* 监听获取焦点
*/
$scope.changePackageName = function (name) {
$scope[name] = {show:false};
//$scope.giftNameTips = {show:false};
};
/**
* 新增礼包
*/
$scope.addEvent = function () {
var m = $scope.packageList || [];
var n = {};
var k = {};
k.carBrand = '全部适用';
k.carBrandId = '-1';
k.carChildsBrandId = '-1';
k.carChildsBrandName = '全部适用';
k.carSeriesId = '-1';
k.carSeriesName = '全部适用';
n.name = '礼包' + (m.length + 1);
n.carBoutiqueCarTypes = k;
n.showAll = 1;
m.push(n);
//$scope.packageList = m;
};
/**
* 删除礼包
*/
$scope.deletePackage = function (index) {
swal({
title: '',
text: '你确定要删除礼包吗?',
html: true,
showCancelButton: true,
confirmButtonText: "确定",
cancelButtonText: "取消"
}, function (confirm) {
if (confirm) {
var package = $scope.packageList;
package.splice(index, 1);
$scope.packageList = package;
$scope.packageIndex = 0;
$scope.$apply();
}
});
};
/**
* 新增礼包标签
*/
$scope.addLabels = function () {
$scope.showAddInput = true;
setTimeout(function () {
$('#giftLableInput')[0].focus();
}, 100)
}
/**
* 隐藏新增礼包标签
*/
$scope.hideAddInput = function () {
$scope.showAddInput = false;
$scope.giftLabel = '';
};
/**
* 保存标签
*/
$scope.saveLables = function (e) {
var m = $scope.labels[$scope.packageIndex] || [];
var dn = $scope.defaultLabels || [];
// 监听回车键
if (e.keyCode == 13) {
var n = {};
n.labelName = $scope.giftLabel;
// n.id = parseFloat(Math.random()*1000);
$scope.labelTips = {show: false, message: '标签已存在'};
// 自定义标签重复校验
var reNodes = m.find(function (k) {
return k.labelName == $scope.giftLabel;
});
// 默认标签重复校验
var dreNodes = dn.find(function(k){
return k.labelName == $scope.giftLabel;
});
if (!$scope.giftLabel) {
$scope.labelTips = {show: true, message: '标签不能为空'};
return false;
}
if (reNodes||dreNodes) {
$scope.labelTips = {show: true, message: '标签已存在'};
return false;
}
if($scope.activeTarget){
$scope.labelTips = {show:true,message:'礼包标签只能添加一个'};
}else{
//m.push(n);
$scope.labels[$scope.packageIndex] = [n];
$scope.packageList[$scope.packageIndex].carBoutiqueLabels = [n];
$scope.activeTarget = $scope.giftLabel;
$scope.showAddInput = false;
$scope.giftLabel = '';
}
}
};
/**
* 选择商品
*/
$scope.choseProduct = function () {
var _id = $stateParams.id;
var packageInfo = $scope.packageList[$scope.packageIndex];
var _list = packageInfo.boutiqueList || [];
console.log(_list);
var modalInstance = $uibModal.open({
templateUrl: 'biz/newCarPackage/view/product.html',
controller: 'productCtrl',
size: 'lg',
resolve: {
parentData : function() {
return {
id: _id,
list:_list
};
}
}
});
};
/**
* 保存数据
*/
$scope.saveInfo = function (event) {
var labels = $scope.labelList;
var list = $scope.packageList;
var current = list[$scope.packageIndex];
var oParams = current;
//var m = $scope.getCheckedCarBrand();
console.log(current);
/*if(!$scope.labels || $scope.labels.length<=0){
$scope.labelTips = {show:true,message:'请先创建标签'};
return false;
}*/
if(!$scope.activeTarget){
$scope.labelTips = {show:true,message:'请选择标签'};
return false;
};
if(!current.name){
$scope.giftNameTips = {show: true,message:'请输入礼包名称'};
return false;
};
if(current.retailPrice){
if(Number(current.retailPrice) > 999999){
$scope.retailPriceTips = {show: true,message:'最大输入999999'};
return false;
}
if(!checkNumber(current.retailPrice)){
$scope.retailPriceTips = {show: true,message:'请输入有效的数字'};
return false;
}
};
if(current.discountPrice){
if(Number(current.discountPrice) > 999999){
$scope.discountPriceTips = {show: true,message:'最大输入999999'};
return false;
}
if(!checkNumber(current.discountPrice)){
$scope.discountPriceTips = {show: true,message:'请输入有效的数字'};
return false;
}
if(Number(current.discountPrice) > Number(current.retailPrice)){
$scope.discountPriceTips = {show: true,message:'礼包折扣价不能大于礼包零售价'};
return false;
}
};
if(!current.boutiqueList || current.boutiqueList.length<=0){
$scope.productTips = {show: true,message:'请选择商品'};
return false;
}
if(current.showAll == 2 && current.carBoutiqueCarTypes.length<=0){
$scope.carTips = {show:true,message:'请选择车系'};
return false;
};
$scope.loading = true;
//判断是新增还是编辑
if($stateParams.id){
// 编辑礼包
addCarPackageService.updateGiftInfo(oParams,function(res){
$scope.loading = false;
if(res.returnCode == 0){
var _type = 'error';
var msg = '编辑失败';
if(res.data){
_type = 'success';
msg = '编辑成功';
}
swal({
type:_type,
title: '',
text: msg,
html: true,
showCancelButton: false,
confirmButtonText: "确定"
},function(confirm){
$scope.goBack();
});
}
});
return false;
}
//新增礼包
addCarPackageService.saveGiftInfo(oParams,function(res){
$scope.loading = false;
if(res.returnCode == 0){
var _type = 'error';
var msg = '新增失败';
if(res.data){
_type = 'success';
msg = '新增成功';
}
swal({
type:_type,
title: '',
text: msg,
html: true,
showCancelButton: false,
confirmButtonText: "确定"
},function(confirm){
$scope.goBack();
});
}
});
};
/**
* 全部适用
*/
$scope.checkAll = function (id) {
var _index = $scope.packageIndex;
var carList = $scope.packageList;
var list = carList[_index].carBoutiqueCarTypes;
var m = {};
m.carBrand = '全部适用';
m.carBrandId = '-1';
m.carChildsBrandId = '-1';
m.carChildsBrandName = '全部适用';
m.carSeriesId = '-1';
m.carSeriesName = '全部适用';
if(id == 2){
var nIndex = list.findIndex(function(k){
return k.carBrandId == '-1';
});
if(nIndex >= 0){
list.splice(nIndex,1);
};
}else{
list = [m];
};
carList[_index].carBoutiqueCarTypes = list;
};
/**
* 勾选一级菜单获取所有选中的车型车系
*/
$scope.getFirstCheckedNodes = function(index){
var list = $scope.carBrandOptions_copy || [];
var firstNodes = list[index]|| {};
var firstChilds = firstNodes.childs || [];
var package = $scope.packageList;
var _index = $scope.packageIndex;
var arr = package[_index].carBoutiqueCarTypes;
//判断是勾选还是取消勾选,如果勾选,则循环吧子集菜单加入进数组,如果是取消,则循环把数据从数组中删除
if(firstNodes.checked){
//循环加入数据
angular.forEach(firstChilds,function(item){
var childs = item.childs || [];
angular.forEach(childs,function(sItem){
var m = {};
m.carBrand = firstNodes.name;
m.carBrandId = firstNodes.id;
m.carChildsBrandId = item.id;
m.carChildsBrandName = item.name;
m.carSeriesId = sItem.id;
m.carSeriesName = sItem.name;
arr.push(m);
})
})
}else{
//取消勾选,从数组中删除
console.log(firstNodes);
splitFromArray(firstNodes.id,'carBrandId');
}
$scope.carTips = {show:false};
};
/**
* 勾选二级菜单,获取所有选择的车型
*/
$scope.getSecondCheckedNodes = function(index,sIndex){
var list = $scope.carBrandOptions_copy || [];
var firstNodes = list[index]|| {};
var firstChilds = firstNodes.childs || [];
var package = $scope.packageList;
var _index = $scope.packageIndex;
var arr = package[_index].carBoutiqueCarTypes;
var secondNodes = firstChilds[sIndex]||{};
var secondChilds = secondNodes.childs || [];
if(secondNodes.checked){
angular.forEach(secondChilds,function(item){
var m = {};
console.log('进入此处');
m.carBrand = firstNodes.name;
m.carBrandId = firstNodes.id;
m.carChildsBrandId = secondNodes.id;
m.carChildsBrandName = secondNodes.name;
m.carSeriesId = item.id;
m.carSeriesName = item.name;
arr.push(m);
})
}else{
//取消勾选,从数组中删除
splitFromArray(secondNodes.id,'carChildsBrandId');
}
$scope.carTips = {show:false};
};
/**
* 勾选三级级菜单,获取所有选择的车型
*/
$scope.getThirdCheckedNodes = function(index,sIndex,tIndex){
var list = $scope.carBrandOptions_copy || [];
var firstNodes = list[index]|| {};
var firstChilds = firstNodes.childs || [];
var package = $scope.packageList;
var _index = $scope.packageIndex;
var arr = package[_index].carBoutiqueCarTypes;
var secondNodes = firstChilds[sIndex]||{};
var secondChilds = secondNodes.childs || [];
var threeNodes = secondChilds[tIndex];
if(threeNodes.checked){
var m = {};
m.carBrand = firstNodes.name;
m.carBrandId = firstNodes.id;
m.carChildsBrandId = secondNodes.id;
m.carChildsBrandName = secondNodes.name;
m.carSeriesId = threeNodes.id;
m.carSeriesName = threeNodes.name;
arr.push(m);
}else{
//取消勾选,从数组中删除
console.log(threeNodes);
splitFromArray(threeNodes.id,'carSeriesId');
}
$scope.carTips = {show:false};
};
/*$scope.getCheckedCarBrand = function(index,sIndex,tIndex){
var list = $scope.carBrandOptions_copy || [];
var firstNodes = list[index]|| {};
var firstChilds = firstNodes.childs || [];
var package = $scope.packageList;
var _index = $scope.packageIndex;
var arr = package[_index].carBoutiqueCarTypes;
//如果是单独勾选一级菜单
if(sIndex == undefined){
angular.forEach(firstChilds,function(item){
var childs = item.childs || [];
console.log(item);
if(item.checked){
angular.forEach(childs,function(sItem){
var m = {};
if(sItem.checked) {
m.carBrand = firstNodes.name;
m.carBrandId = firstNodes.id;
m.carChildsBrandId = item.id;
m.carChildsBrandName = item.name;
m.carSeriesId = sItem.id;
m.carSeriesName = sItem.name;
arr.push(m);
}
})
}
})
};
//如果单独勾选二级菜单
if(tIndex == undefined){
var secondNodes = firstChilds[sIndex]||{};
var secondChilds = secondNodes.childs || [];
angular.forEach(secondChilds,function(item){
if(item.checked){
var m = {};
console.log('进入此处');
m.carBrand = firstNodes.name;
m.carBrandId = firstNodes.id;
m.carChildsBrandId = secondNodes.id;
m.carChildsBrandName = secondNodes.name;
m.carSeriesId = item.id;
m.carSeriesName = item.name;
arr.push(m);
}else{
//取消勾选,从数组中删除
splitFromArray(arr,secondNodes.carSeriesId,'carSeriesId');
}
})
};
//如果单独勾选三级菜单
if(tIndex != undefined){
var secondNodes = firstChilds[sIndex]||{};
var secondChilds = secondNodes.childs || [];
var threeNodes = secondChilds[tIndex];
var m = {};
if(threeNodes.checked){
m.carBrand = firstNodes.name;
m.carBrandId = firstNodes.id;
m.carChildsBrandId = secondNodes.id;
m.carChildsBrandName = secondNodes.name;
m.carSeriesId = threeNodes.id;
m.carSeriesName = threeNodes.name;
arr.push(m);
}else{
//取消勾选,从数组中删除
var oIndex = arr.findIndex(function(k){
return k.carSeriesId == threeNodes.id;
});
arr.splice(oIndex,1);
}
};
console.log(arr);
package[_index].carBoutiqueCarTypes = arr;
$scope.packageList = package;
return arr;
};*/
/**
* 初始化函数
*/
$scope.init = function () {
$scope.getCarBrand().then(function(){
$scope.getSeriesList();
if($stateParams.id){
$scope.getPackageInfo($stateParams.id);
};
//$scope.getDefaultLabels();
});
//监听选择的产品列表
$rootScope.$on('selectProduct', function(event,data){
var _index = $scope.packageIndex;
var package = $scope.packageList;
package[_index].boutiqueList = data || [];
$scope.productTips = {show:false};
});
//判断入口,如果是从标准配置添加商品跳转过来,则默认打开弹框
};
/**
* 获取车型数据
*/
$scope.getCarBrand = function () {
return new Promise(function(resolve,reject){
addCarPackageService.getCarBrand(function (res) {
var list = res.data || [];
$scope.carBrandOptions = list;
$scope.carBrandOptions_copy = list;
resolve();
});
});
};
/**
* 三级菜单勾选复选框
*/
$scope.threeCheckdBrand = function (fIndex, sIndex, index, checked) {
var list = $scope.carBrandOptions;
var f = list[fIndex];
var s = (f.childs)[sIndex];
var t = (s.childs)[index];
var secondHasChecked = $scope.judgeChecked(f.childs, sIndex);
var threeHasChecked = $scope.judgeChecked(s.childs, index);
t.checked = checked;
if (!secondHasChecked && !threeHasChecked) {
f.checked = !f.checked;
};
if (!threeHasChecked) {
s.checked = !s.checked;
};
//勾选,则创建数据
if (checked) {
}
$scope.carBrandOptions = list;
//$scope.getCheckedCarBrand(fIndex,sIndex,index);
$scope.getThirdCheckedNodes(fIndex,sIndex,index);
};
/**
* 二级菜单勾选复选框
*/
$scope.secondCheckdBrand = function (fIndex, sIndex, checked) {
var list = $scope.carBrandOptions;
var f = list[fIndex];
var s = (f.childs)[sIndex];
var t = s.childs;
var hasChecked = $scope.judgeChecked(f.childs, sIndex);
s.checked = checked;
//判断同级是否有选中状态的
if (!hasChecked) {
f.checked = !f.checked;
}
if (t && t.length > 0) {
for (var i = 0; i < t.length; i++) {
var k = t[i];
k.checked = checked;
}
}
$scope.carBrandOptions = list;
//$scope.getCheckedCarBrand(fIndex,sIndex);
$scope.getSecondCheckedNodes(fIndex,sIndex);
};
/**
* 一级菜单勾选复选框
*/
$scope.firstCheckedBrand = function (fIndex, checked) {
var list = $scope.carBrandOptions;
var f = list[fIndex];
var t = f.childs;
f.checked = checked;
// 勾选该一级菜单下所有的二级菜单
if (t && t.length > 0) {
for (var i = 0; i < t.length; i++) {
var k = t[i];
var n = k.childs;
k.checked = checked;
// 勾选该一级菜单下所有二级菜单下所有三级菜单
if (!n || n.length <= 0) {
return false;
}
for (var j = 0; j < n.length; j++) {
var m = n[j];
m.checked = checked;
}
}
}
$scope.carBrandOptions = list;
//$scope.getCheckedCarBrand(fIndex);
$scope.getFirstCheckedNodes(fIndex);
};
/**
* 判断同级元素是否有选中的
*/
$scope.judgeChecked = function (list, index) {
if (!list || list.length <= 0) {
return false;
}
;
var copyArr = JSON.parse(JSON.stringify(list));
copyArr.splice(index, 1);
var m = copyArr.find(function (k) {
return k.checked == true;
});
if (m) {
return true;
} else {
return false;
}
};
/**
* 返回
*/
$scope.goBack = function () {
$state.go('newCarPackage.configuration');
};
/**
* 编辑礼包,获取礼包信息
*/
$scope.getPackageInfo = function(id){
var oParams = {};
oParams.id = id;
addCarPackageService.getGiftInfoById(oParams,function(res){
if(res.returnCode == 0){
var package = res.data || {};
var oInfo = {};
var m = package.carBoutiqueLabels ||[];
var n = [];
var checkList = package.carBoutiqueCarTypes || [];
var carBrandList = $scope.carBrandOptions_copy || [];
oInfo.carBoutiqueCarTypes = checkList;
oInfo.description = package.description;
oInfo.discountPrice = package.discountPrice;
oInfo.name = package.name;
oInfo.retailPrice = package.retailPrice;
oInfo.showAll = (package.carBoutiqueCarTypes)[0].carSeriesId == '-1' ? 1 : 2;
oInfo.id = package.id;
oInfo.boutiqueList = package.boutiqueList || [];
//循环设置标签选中
/*angular.forEach(m,function(item){
item.checked = true;
n.push(item);
});*/
//设置车系勾选数据
angular.forEach(checkList,function(item){
var firstNodes = carBrandList.find(function(k){
return k.id == item.carBrandId;
});
if(firstNodes){
firstNodes.checked = true;
var firstChilds = firstNodes.childs || [];
var secondNodes = firstChilds.find(function(k){
return k.id == item.carChildsBrandId;
});
if(secondNodes){
secondNodes.checked = true;
var secondChilds = secondNodes.childs || [];
var threeNodes = secondChilds.find(function(k){
return k.id == item.carSeriesId
});
if(threeNodes){
threeNodes.checked = true;
};
};
}
});
// 判断标签是否是默认标签
var isDefault = $scope.defaultLabels.find(function(k){
return k.labelName === m[0].labelName;
});
//设置选中标签
$scope.activeTarget = m[0].labelName;
if(!isDefault){
$scope.labels[$scope.packageIndex] = package.carBoutiqueLabels;
$scope.labelList = m;
}
oInfo.carBoutiqueLabels = m;
$scope.packageList = [oInfo];
console.log(oInfo);
}
})
};
/**
* 获取门店车系列表
*/
$scope.getSeriesList = function(){
addCarPackageService.getMerchantSeries({},function(res){
if(res.returnCode == 0){
$scope.seriesList = res.data || [];
}
})
};
/**
* 展开子菜单
*/
$scope.showChildsBox = function(index,sIndex){
var list = $scope.carBrandOptions_copy;
var firstNodes = list[index];
firstNodes.newChilds = firstNodes.childs;
//二级菜单点击
if(sIndex != undefined){
var secondNodes = firstNodes.childs;
secondNodes[sIndex].newChilds = secondNodes[sIndex].childs||[];
};
$scope.carBrandOptions_copy = list;
//$scope.$apply();
};
/**
* 处理数据
*/
$scope.dealData = function(list,name,max){
if(!list || list.length<=0){
return '';
};
var _html = [];
var len = 10;
if(max){
len = max;
}
for(var i=0;i<list.length;i++){
var m = list[i];
_html.push(m[name]);
};
var str = _html.toString();
var newStr = '';
if(str.length> len){
newStr = str.slice(0,len) + '...';
}else{
newStr = str;
}
return {text:str,sort:newStr};
};
/**
* 从数组中删除元素
*/
function splitFromArray(id,type){
var package = $scope.packageList;
var _index = $scope.packageIndex;
var list = package[_index].carBoutiqueCarTypes;
var arr = [];
if(!type){
return false;
}
console.log(list);
for(var i=0;i<list.length;i++){
var m = list[i];
if(m[type] != id){
console.log(i);
//list.splice(i,1);
arr.push(m);
}
};
//list = arr;
$scope.packageList[$scope.packageIndex].carBoutiqueCarTypes = arr;
console.log(list);
};
/**
* 校验数字是否为有效的数字
*/
function checkNumber(txt){
// 1、不包含小数点,第一位不能为0
// 2、包含小数点,小数前面如果超过两位,第一位不能为0,
console.log(txt)
var str = txt.split('');
var n = str.find(function(k){
return k == '.';
});
if(n){
var str2 = txt.split('.');
if(str2[0].length>1){
var str3 = str2[0].split('');
if(str3[0] == 0){
return false;
}else{
return true;
}
}else{
return true;
}
}else{
if(str[0] == 0){
return false
}else{
return true;
}
}
};
/**
* 输入框校验
*/
function checkInput(txt,num){
var newText = txt;
var m = txt.toString();
var b = /^(\-)*(\d+)\.(\d\d).*$/;
if(num){
b = /^(\-)*(\d+)\.(\d).*$/;
};
if(txt){
//先把非数字的都替换掉,除了数字和.
m = m.replace(/[^\d.]/g,"");
//必须保证第一个为数字而不是.
m = m.replace(/\.{2,}/g,".");
//保证.只出现一次,而不能出现两次以上
m = m.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
//只能输入两位小数
m = m.replace(b,'$1$2.$3');
newText = m;
};
return newText
};
/**
* 监听零售价输入
*/
$scope.$watch('packageList[packageIndex].retailPrice',function(newValue,oldValue){
if(newValue){
newValue = checkInput(newValue);
$scope.packageList[$scope.packageIndex].retailPrice = newValue;
}
});
/**
* 监听折扣价输入
*/
$scope.$watch('packageList[packageIndex].discountPrice',function(newValue,oldValue){
if(newValue){
newValue = checkInput(newValue);
$scope.packageList[$scope.packageIndex].discountPrice = newValue;
}
});
/**
* 删除选中的产品
*/
$scope.deleteProduct = function(index){
var list = $scope.packageList[$scope.packageIndex].boutiqueList || [];
swal({
title: '',
text: '你确定要删除选中产品吗?',
html: true,
showCancelButton: true,
confirmButtonText: "确定",
cancelButtonText: "取消"
}, function (confirm) {
if (confirm) {
list.splice(index, 1);
/*$scope.packageList[$scope.packageIndex].boutiqueList = list;*/
$scope.$apply();
}
});
};
/**
* 获取默认标签
*/
$scope.getDefaultLabels = function(){
addCarPackageService.getDefaultLabels(function(res){
console.log(res);
if(res.returnCode == 0){
var list = res.data||[];
var arr = [];
angular.forEach(list,function(item){
var m = {};
console.log(item);
m.labelName = item;
arr.push(m);
});
$scope.labels[$scope.packageIndex] = arr;
console.log($scope.labels);
}
})
};
/**
* 删除标签
*/
$scope.deleteLabels = function(index,$event){
console.log(index);
var list = $scope.labels[$scope.packageIndex];
swal({
title: '',
text: '你确定要删除该标签吗?',
html: true,
showCancelButton: true,
confirmButtonText: "确定",
cancelButtonText: "取消"
}, function (confirm) {
if (confirm) {
list.splice(index, 1);
/*$scope.packageList[$scope.packageIndex].boutiqueList = list;*/
$scope.$apply();
$scope.activeTarget = '';
}
});
$event.stopPropagation();
};
$scope.init();
}
angular.module('newCarPackage').controller('addPackageCtrl', addPackageCtrl)
})(angular);