xufenglei
2018-06-21 9cd223e02abde85ba740ae8117e02f254c329167
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
import {NzModalSubject} from 'ng-zorro-antd';
import {Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {environment} from '../../../../../environments/environment';
import {FormGroup, FormBuilder, FormControl, Validators} from '@angular/forms';
import * as $ from 'jquery';
 
 
@Component({
  selector: 'app-account-edit',
  templateUrl: './account-edit.component.html',
  styles: []
})
export class AccountEditComponent implements OnInit {
 
  account: any;
  validateForm: FormGroup;
  searchOptions = [];
 
  constructor(
    private subject: NzModalSubject,
    public http: HttpClient,
    private formBuilder: FormBuilder
  ) {
 
  }
 
  ngOnInit() {
    const account = this.account;
    this.validateForm = this.formBuilder.group({
      accountName: [account.accountName],
      mobile: [account.mobile],
      email: [account.email],
      weixin: [account.weixin],
      organizationId: [account.organizationId],
      expireTime: [account.expireTime],
      id: [account.id]
    });
    this.searchOptions = account.organization ? [account.organization] : [];
    
    $(document).keydown(function(event) {
      switch (event.keyCode) {
        case 13: return false;
      }
    });
  }
 
  save() {
    const validateForm = this.validateForm;
    const controls = validateForm.controls;
    if (validateForm.valid) {
      for (const i in controls) {
        controls[i].disable();
      }
      this.http.post(environment.SERVER_BASH_URL + '/account/account', validateForm.value).subscribe(() => {
        this.subject.next('true');
        this.close();
      });
    } else {
      for (const i in controls) {
        controls[i].markAsDirty();
      }
    }
  }
 
  close() {
    this.subject.destroy();
  }
 
  check(accountName) {
    const controlsAccountName = this.validateForm.controls.accountName;
    if (accountName) {
      this.http.get(environment.SERVER_BASH_URL + '/account/' + accountName).subscribe((res: any) => {
        if (res.data > 0) {
          controlsAccountName.setErrors({unique: true});
        }
      });
    } else {
      controlsAccountName.setErrors({required: true});
    }
  }
 
  searchChange(searchText) {
    if (searchText) {
      const query = encodeURI(searchText);
      if (query) {
        this.http.get(environment.SERVER_BASH_URL + '/organization/list/' + query).subscribe((res: any) => {
          this.searchOptions = res.data;
        });
      }
    }
  }
 
}