fengxiang
2018-01-10 3517e796f650b8aed52165c1a5905456f54033ef
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
import { Organization } from '@business/entity/data';
import { AreacodeService } from '@business/services/http/areacode.service';
import { Component, OnInit } from '@angular/core';
import { NzMessageService, NzModalSubject } from 'ng-zorro-antd';
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
import { CascaderOption } from 'ng-zorro-antd/src/cascader/nz-cascader.component';
 
@Component({
  selector: 'app-organization-edit',
  templateUrl: './organization-edit.component.html',
  styles: []
})
export class OrganizationEditComponent implements OnInit {
 
  rankOptions = [
    { value: 0, label: '企业' },
    { value: 1, label: '乡镇科级' },
    { value: 2, label: '县处级' },
    { value: 3, label: '司厅局级' },
    { value: 4, label: '省部级' },
    { value: 5, label: '国家级' }
  ];
 
  data: Organization;
  isSaving = false;
  validateForm: FormGroup;
  constructor(    
    private subject: NzModalSubject,
    private formBuilder: FormBuilder,
    private areacodeService: AreacodeService
    ) { }
 
  ngOnInit() {
    const data = this.data;
    const areaNames = data.areaNames;
    let _areas = null;
    if (areaNames != null) {
      _areas = {
       label: Object.values(areaNames).join('/'),
       value: data.areaCode
      };
    }
    if (data.createTime == null) {
      data.createTime = new Date().getTime();
    }
     const validates: Organization|object  = {
          name: [data.name, [Validators.required] ],
          rank: [data.rank],
          telephone: [data.telephone, [ Validators.pattern('^0\\d{2,3}-?\\d{7,8}$') ]],
          email: [data.email, [ Validators.pattern('^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+') ] ],
          address: [data.address ],
          _areas: [_areas,  [Validators.required]],
          createTime: [data.createTime, [Validators.required] ],
          expireTime: [data.expireTime, [Validators.required] ],
          description: [data.description ]
     };
     this.validateForm = this.formBuilder.group(
         validates
     );
  }
  close() {
       this.subject.destroy();
  }
  save($event, value, valid) {
      $event.preventDefault();
      if (valid) {
        for (const i in this.validateForm.controls) {
          this.validateForm.controls[ i ].disable();
        } 
        this.isSaving = true;
        Object.keys(value).forEach( (key: string) => {
              // '_'为前缀的为自定义属性
              if (!key.startsWith('_') && value[key] != null) {
                   this.data[key] = value[key];
              }
        } );
        this.subject.next( this );
      }else {
        this.validate(); 
      }
  }
  validate() {
     for (const i in this.validateForm.controls) {
       this.validateForm.controls[ i ].markAsDirty();
     }
  }
  areaLazyLoad(event: { option: CascaderOption, index: number, resolve: (children: CascaderOption[]) => void, reject: () => void }) {
    const index = event['index'];
    const option = event.option;  
    switch (index) {
       case -1:
       this.areacodeService.getProvinces().subscribe(
         (res: {label: string, value: string}[]) => {
             event.resolve( res );
         }
       ); break;
       case 0:
       this.areacodeService.getCities(option.value).subscribe(
         (res: {label: string, value: string}[]) => {
             event.resolve( res );
         }
       ); break;
       case 1:
       this.areacodeService.getAreas(option.value).subscribe(
         (res: {label: string, value: string}[]) => {
             event.resolve( res );
         }
       ); break;
    }
  }
  setAreaCodes(codes: string[]) {
      this.data.provinceCode = codes[0];
      this.data.cityCode = codes[1];
      this.data.areaCode = codes[2];
  }
}