import { OnInit, Component, Input } from '@angular/core'; import { StpConfigureService } from '../stp-configure.service'; import { PartListConnectorModel } from '../../../models/parts/part-list-connector.model'; import { ShowViewerArgs } from '../../../models/viewer/ShowViewerArgs.model'; import { ExtensionInterface } from '../../../models/viewer/ExtensionInterface.model'; @Component({ selector: 'part-list', templateUrl: './part-list.component.html', styleUrls: ['./part-list.component.scss'] }) export class PartListComponent implements OnInit { @Input() args: ShowViewerArgs; private extensionInterface: ExtensionInterface; public headers = ['Pos', 'Article No', 'Description', 'Unit', 'Qty', 'Actions']; public partListGridData = []; public templateConnector = new PartListConnectorModel(); public unitCollection = ['pc / m', 'pc', 'm']; public currentEdited = null; public currentViewerTypeId; constructor(private stpConfigureService: StpConfigureService) { } ngOnInit(): void { this.stpConfigureService.loadAllPartListConnectors(this.args.itemId).subscribe((res) => { this.partListGridData = res; }, (error: any) => {}); this.setViewerTypeId(); } save() { if (this.args.variantId !== null) { this.templateConnector.variantId = this.args.variantId; } if (this.currentEdited !== null) { this.updatePartListElement(); }else { this.saveNewPartListConnector(); } } saveNewPartListConnector() { this.templateConnector.connectorId = this.args.itemId; this.stpConfigureService.addNewPartListConnectorIntoDb(JSON.stringify(this.templateConnector)).subscribe((response) => { this.templateConnector._id = response; this.partListGridData.push(this.templateConnector); this.templateConnector = new PartListConnectorModel(); }); } deletePartListElement(elementIndex) { this.stpConfigureService.removePartListConnector(this.partListGridData[elementIndex]._id).subscribe((res) => { }, (error: any) => {}); this.partListGridData.splice(elementIndex, 1); } isPartListConnectorValidate() { return this.templateConnector.articleNumber !== null && this.templateConnector.description.length > 2 && this.templateConnector.qty !== null && this.templateConnector.unit.length > 0; } updatePartListElement() { Object.assign(this.partListGridData[this.currentEdited], this.templateConnector); this.stpConfigureService.updatePartListConnector(JSON.stringify (this.partListGridData[this.currentEdited])).subscribe((response) => { this.templateConnector = new PartListConnectorModel(); }); this.currentEdited = null; } setCurrentEdited(index) { this.currentEdited = index; Object.assign(this.templateConnector, this.partListGridData[index]); } clearInputs() { this.templateConnector = new PartListConnectorModel(); } setViewerTypeId() { if (this.args.variantId === null) { this.currentViewerTypeId = this.args.itemId; }else { this.currentViewerTypeId = this.args.variantId; } } checkTypeOfPartListElement(variantId) { return (this.currentViewerTypeId === variantId && this.args.variantId !== null) || (!(variantId.length > 0) && this.args.variantId === null); } }