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]; 
 | 
  } 
 | 
} 
 |