rsanchez
2017-03-14 ce9bc0c6ad58e4117d26a204ffc56bbcdb7fe916
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
import { Http } from '@angular/http';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import { PacksService, PACK_STATUS } from '../resources/packs';
import { LicenseTypesService } from '../resources/license_types';
import { LocaleService } from '../common/i18n';
import { TdDataTableService, TdDataTableSortingOrder, ITdDataTableSortChangeEvent, ITdDataTableColumn } from '@covalent/core';
import { IPageChangeEvent } from '@covalent/core';
import { Component, AfterViewInit } from '@angular/core';
import { TdMediaService } from '@covalent/core';
import { IComboOption } from './base';
@Component({
  selector: 'pack-form',
  templateUrl: 'src/app/forms/pack.form.html'
})
export class PackFormComponent implements AfterViewInit {
  form_title: string = 'Title';
  form_subtitle: string = '';
  organizations : IComboOption[];
  licensetypes : IComboOption[];
  data: any = {};
  isNew : boolean = true;
  fields: any = {
    'code': '',
    'num_licenses': '',
    'init_valid_date': '',
    'end_valid_date': '',
    'license_preactivation': '',
    'license_type_id': '',
    'organization_id': '',
    'licensetype_code': '',
    'organization_name': '',
    'preactivation_valid_period': '',
    'renew_valid_period': '',
    'application_name': '',
    'status': '',
    'metadata': '',
    'comments': '',
    'key': '',
    'value': '',
  }
  constructor(private http: Http,
              private toaster: ToastsManager,
              private licenseTypes: LicenseTypesService,
              private packs: PacksService,
              private $L: LocaleService) {
      Object.keys(this.fields).forEach(k => this.fields[k] = $L.get(`field.${k}`));
  }
  public getFieldName(fieldId: string) : string {
    return this.fields[fieldId];
  }
  private loadCombos(): void {
      this.http.get('organization')
        .map(response => response.json().map((org : any) => <IComboOption>{id: org.id, label: `(${org.code}) ${org.name}`}))
        .subscribe(
          data => this.organizations = (<IComboOption[]>data).sort((e1, e2) => e1.label.localeCompare(e2.label)),
          err => console.error('Error loading orgs')
        );
      this.licenseTypes.get()
        .map(list => list.map((lt : any) => <IComboOption>{id: lt.id, label: `(${lt.code}) ${lt.name}`}))
        .subscribe(
          data => this.licensetypes = (<IComboOption[]>data).sort((e1, e2) => e1.label.localeCompare(e2.label)),
          err => console.error('Error loading license types')
        );
  }
  save() {
      var command = this.isNew ? this.packs.create(this.data) : this.packs.modify(this.data.id, this.data);
      command.subscribe(
          data => this.toaster.success(this.$L.get('Pack saved sucessfully')),
          err => {
            console.error(err);
            this.toaster.success(this.$L.get('Error saving pack'));
          }
        );
  }
  changeLicType(event: any) {
    this.licenseTypes.get(this.data.license_type_id)
        .map(lt_data => lt_data.metadata)
        .subscribe(
          metadata => this.data.metadata = metadata,
          err => {
            console.error('Error loading license type metadata');
            console.error(err);
          }
        );
  }
  changeOrg(event: any) {
    console.log(event);
    console.log(this.data.organization_id);
  } 
  
  ngOnInit(): void {
    this.loadCombos();
    if (this.isNew) {
      this.data.status = PACK_STATUS.CREATED;
    }
    this.form_title = this.$L.get('Pack data');
    this.form_subtitle = this.$L.get(this.isNew ? 'Create a new licenses pack': 'Modify the licenses pack data') ;
  }
  ngAfterViewInit(): void {
  }
}