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) => {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') ); } 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 { } }