Joaquín Reñé
2026-03-27 4ee50e257b32f6ec0f72907305d1f2b1212808a4
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
import { Http } from '@angular/http';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import { PacksService, PACK_STATUS, PACK_ACTIONS } from '../resources/packs';
import { LicenseTypesService } from '../resources/license_types';
import { LocaleService } from '../common/i18n';
import { TdDataTableService, TdDataTableSortingOrder, ITdDataTableSortChangeEvent, ITdDataTableColumn } from '@covalent/core';
import { TdDialogService } 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[];
  pack_menu_options = PACK_ACTIONS;
  constructor(private http: Http,
              private licenseTypes: LicenseTypesService,
              private packs: PacksService,
              router: Router,
              toaster: ToastsManager,
              route: ActivatedRoute,
              $L: LocaleService,
              dialogs: TdDialogService) {
    super($L, router, route, toaster, packs, $L.get('pack'), dialogs);
  }
  loadCombos(): void {
      this.http.get('organization')
        .map(response => response.json().map((org : any) => <IComboOption>{id: org.id, label: `(${org.code}) ${org.name}`}))
        .subscribe(
          data => super.setViewData(() => 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 => super.setViewData(() => this.licensetypes = (<IComboOption[]>data).sort((e1, e2) => e1.label.localeCompare(e2.label))),
          err => console.error('Error loading license types')
        );
  }
  goLicenses(): void {
    this.router.navigate([`packs/${this.data.id}/licenses`]);
  }
  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 => super.setViewData(() => this.data.metadata = metadata),
          err => {
            console.error('Error loading license type metadata');
            console.error(err);
          }
        );
  }
  packAction(action: string) {
    return this.packs[action](this.data.id).subscribe(
      (actionResponse : any) => {
        this.toaster.success(this.$L.get('Action "{}" executed successfully', action));
        this.init();
      },
      (err : any) => this.toaster.error(err.message, this.$L.get('Action "{}" failed', action))
    );
  }
  changeOrg(event: any) {
    console.log(this.data.organization_id);
  } 
  canBeDeleted(event: any) : boolean{
    return !this.isNew && this.packs.isActionAvailable('delete', this.data);
  } 
  convertToEpoch(str: string) : number {
    return new Date(str).getTime();
  }
  ngAfterViewInit(): void {
    this.init();
  }
  init(): void {
    this.loadCombos();
    super.prepareInitialData('packId', {
      status: PACK_STATUS.CREATED,
      frozen: false
    });
  }
}