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, FormBase } from './base'; import { ActivatedRoute, Router } from '@angular/router'; @Component({ selector: 'pack-form', templateUrl: 'src/app/forms/pack.form.html' }) export class PackFormComponent extends FormBase implements AfterViewInit { 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, toaster: ToastsManager, private licenseTypes: LicenseTypesService, route: ActivatedRoute, private router: Router, private packs: PacksService, $L: LocaleService) { super($L, route, toaster, packs, $L.get('pack')); } public getFieldName(fieldId: string) : string { return this.fields[fieldId]; } loadCombos(): void { this.http.get('organization') .map(response => response.json().map((org : any) => {id: org.id, label: `(${org.code}) ${org.name}`})) .subscribe( data => this.organizations = (data).sort((e1, e2) => e1.label.localeCompare(e2.label)), err => console.error('Error loading orgs') ); this.licenseTypes.get() .map(list => list.map((lt : any) => {id: lt.id, label: `(${lt.code}) ${lt.name}`})) .subscribe( data => this.licensetypes = (data).sort((e1, e2) => e1.label.localeCompare(e2.label)), err => console.error('Error loading license types') ); } goBack(): void { this.router.navigate([`packs`]); } 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 { } ngAfterViewInit(): void { this.loadCombos(); super.prepareData('packId', { status: PACK_STATUS.CREATED }); } }