import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; import {Product} from "../../model/product"; import {verticleSlide} from "../../animations"; import {ProductsService} from "../../services/products.service"; import {AlertService} from "../../services/alert.service"; @Component({ selector: 'app-product', templateUrl: './product.component.html', styleUrls: ['./product.component.scss'], animations: [verticleSlide] }) export class ProductComponent implements OnInit { @Input() product: Product; @Input() canDeleteProducts: boolean; @Output() refreshProducts = new EventEmitter<boolean>(); productStatuses: Array<string> = ["WAITING", "READY", "COMPLETED", "PROCESSING", "CACHED"]; detailsExposed: boolean = false; constructor(private productService: ProductsService, private alertService: AlertService) { } ngOnInit() { } getName(): string { if (this.product.typeName == 'schedblock') { return this.product.name + ' (' + this.product.optId + ')'; } else { return this.product.name; } } getStatusBadgeClass(): string { switch (this.product.status) { case 'COMPLETED': return 'btn-success'; case 'READY': return 'btn-info'; case 'WAITING': return 'btn-warning'; case 'PROCESSING': return 'btn-primary'; default: return 'btn-light'; } } showImageDetails(): boolean { let showTypes = [ 'quicklook', 'se_cont', 'se_coarse_cube_image', 'se_coarse_plane_image', 'se_coarse_plane_I_image', 'se_coarse_plane_Q_image', 'se_coarse_plane_U_image' ]; return showTypes.indexOf(this.product.typeName) > -1; } getPhaseCenterRA(): string { return parseFloat(this.product.phaseCenterRA).toFixed(4); } getPhaseCenterDec(): string { return parseFloat(this.product.phaseCenterDec).toFixed(4); } getImageSizeX(): string { return parseFloat(this.product.imageSizeX).toFixed(4); } getImageSizeY(): string { return parseFloat(this.product.imageSizeY).toFixed(4); } updateStatus(status: string) { if (status.toLowerCase() !== this.product.status.toLowerCase()) { this.productService.updateProductStatus(this.product.id, status).subscribe(response => { this.product.status = response; }); } } deleteProduct(): void { if (this.canDeleteProducts) { let confirmed = confirm('Are you sure you want to delete ' + this.product.id); if (confirmed) { this.alertService.info('Deleting Product'); this.productService.deleteProduct(this.product.id).subscribe(response => { this.alertService.success('Product Deleted'); this.refreshProducts.emit(true); }, error => { this.alertService.error('Product deletion failed'); }); } } } }