| .. | .. |
|---|
| 1 | 1 | import { Http } from '@angular/http'; |
|---|
| 2 | +import { ToastsManager } from 'ng2-toastr/ng2-toastr'; |
|---|
| 3 | + |
|---|
| 2 | 4 | import { PacksService } from '../resources/packs'; |
|---|
| 5 | +import { LicenseTypesService } from '../resources/license_types'; |
|---|
| 3 | 6 | import { LocaleService } from '../common/i18n'; |
|---|
| 4 | 7 | import { TdDataTableService, TdDataTableSortingOrder, ITdDataTableSortChangeEvent, ITdDataTableColumn } from '@covalent/core'; |
|---|
| 5 | 8 | import { IPageChangeEvent } from '@covalent/core'; |
|---|
| 6 | 9 | import { Component, AfterViewInit } from '@angular/core'; |
|---|
| 7 | 10 | import { TdMediaService } from '@covalent/core'; |
|---|
| 8 | 11 | import { IComboOption } from './base'; |
|---|
| 12 | + |
|---|
| 13 | + |
|---|
| 9 | 14 | |
|---|
| 10 | 15 | @Component({ |
|---|
| 11 | 16 | selector: 'pack-form', |
|---|
| .. | .. |
|---|
| 19 | 24 | licensetypes : IComboOption[]; |
|---|
| 20 | 25 | data: any = {}; |
|---|
| 21 | 26 | isNew : boolean = true; |
|---|
| 22 | | - |
|---|
| 27 | + fields: any = { |
|---|
| 28 | + 'code': '', |
|---|
| 29 | + 'num_licenses': '', |
|---|
| 30 | + 'init_valid_date': '', |
|---|
| 31 | + 'end_valid_date': '', |
|---|
| 32 | + 'license_preactivation': '', |
|---|
| 33 | + 'license_type_id': '', |
|---|
| 34 | + 'organization_id': '', |
|---|
| 35 | + 'licensetype_code': '', |
|---|
| 36 | + 'organization_name': '', |
|---|
| 37 | + 'preactivation_valid_period': '', |
|---|
| 38 | + 'renew_valid_period': '', |
|---|
| 39 | + 'application_name': '', |
|---|
| 40 | + 'status': '', |
|---|
| 41 | + 'metadata': '', |
|---|
| 42 | + 'comments': '', |
|---|
| 43 | + 'key': '', |
|---|
| 44 | + 'value': '', |
|---|
| 45 | + } |
|---|
| 23 | 46 | constructor(private http: Http, |
|---|
| 47 | + private toaster: ToastsManager, |
|---|
| 48 | + private licenseTypes: LicenseTypesService, |
|---|
| 24 | 49 | private packs: PacksService, |
|---|
| 25 | 50 | private $L: LocaleService) { |
|---|
| 26 | | - |
|---|
| 51 | + Object.keys(this.fields).forEach(k => this.fields[k] = $L.get(`field.${k}`)); |
|---|
| 52 | + } |
|---|
| 53 | + |
|---|
| 54 | + public getFieldName(fieldId: string) : string { |
|---|
| 55 | + return this.fields[fieldId]; |
|---|
| 27 | 56 | } |
|---|
| 28 | 57 | |
|---|
| 29 | 58 | private loadCombos(): void { |
|---|
| .. | .. |
|---|
| 33 | 62 | data => this.organizations = (<IComboOption[]>data).sort((e1, e2) => e1.label.localeCompare(e2.label)), |
|---|
| 34 | 63 | err => console.error('Error loading orgs') |
|---|
| 35 | 64 | ); |
|---|
| 36 | | - this.http.get('licensetype') |
|---|
| 37 | | - .map(response => response.json().map((lt : any) => <IComboOption>{id: lt.id, label: `(${lt.code}) ${lt.name}`})) |
|---|
| 65 | + this.licenseTypes.get() |
|---|
| 66 | + .map(list => list.map((lt : any) => <IComboOption>{id: lt.id, label: `(${lt.code}) ${lt.name}`})) |
|---|
| 38 | 67 | .subscribe( |
|---|
| 39 | 68 | data => this.licensetypes = (<IComboOption[]>data).sort((e1, e2) => e1.label.localeCompare(e2.label)), |
|---|
| 40 | 69 | err => console.error('Error loading license types') |
|---|
| 41 | 70 | ); |
|---|
| 42 | 71 | } |
|---|
| 43 | 72 | |
|---|
| 44 | | - log(obj: any) { |
|---|
| 45 | | - console.log(obj) |
|---|
| 73 | + save() { |
|---|
| 74 | + var command = this.isNew ? this.packs.create(this.data) : this.packs.modify(this.data.id, this.data); |
|---|
| 75 | + command.subscribe( |
|---|
| 76 | + data => this.toaster.success(this.$L.get('Pack saved sucessfully')), |
|---|
| 77 | + err => { |
|---|
| 78 | + console.error(err); |
|---|
| 79 | + this.toaster.success(this.$L.get('Error saving pack')); |
|---|
| 80 | + } |
|---|
| 81 | + ); |
|---|
| 46 | 82 | } |
|---|
| 83 | + |
|---|
| 84 | + changeLicType(event) { |
|---|
| 85 | + this.licenseTypes.get(this.data.license_type_id) |
|---|
| 86 | + .map(lt_data => lt_data.metadata) |
|---|
| 87 | + .subscribe( |
|---|
| 88 | + metadata => this.data.metadata = metadata, |
|---|
| 89 | + err => { |
|---|
| 90 | + console.error('Error loading license type metadata'); |
|---|
| 91 | + console.error(err); |
|---|
| 92 | + } |
|---|
| 93 | + ); |
|---|
| 94 | + } |
|---|
| 95 | + |
|---|
| 96 | + changeOrg(event) { |
|---|
| 97 | + console.log(event); |
|---|
| 98 | + console.log(this.data.organization_id); |
|---|
| 99 | + } |
|---|
| 47 | 100 | |
|---|
| 48 | 101 | ngOnInit(): void { |
|---|
| 49 | 102 | this.loadCombos(); |
|---|
| 50 | | - this.data = {}; |
|---|
| 103 | + // this.data = {}; |
|---|
| 51 | 104 | this.form_title = this.$L.get('Pack data'); |
|---|
| 52 | | - this.form_subtitle = this.$L.get('Create a new licenses pack'); |
|---|
| 105 | + this.form_subtitle = this.$L.get(this.isNew ? 'Create a new licenses pack': 'Modify the licenses pack data') ; |
|---|
| 53 | 106 | } |
|---|
| 54 | 107 | |
|---|
| 55 | 108 | |
|---|