import { ChangeDetectorRef, Component, OnInit } from "@angular/core"; import { API, EAction, EText, PREFIX } from "../../../../@config/app"; import { AppService } from "../../../../app.service"; import { lastValueFrom } from "rxjs"; import { IProductMeasurement } from "../../../../app.interface"; import { ActivatedRoute, Router } from "@angular/router"; import { BaseFormComponent } from "../../../../@common/base/base-form.component"; @Component({ selector: "app-promotion-do", templateUrl: "./promotion-do.component.html", styleUrls: [] }) export class PromotionDoComponent extends BaseFormComponent implements OnInit { title = "โปรโมชั่น"; api: any = API; addItemNumber: number = 1; masterProductUnit: any = []; unitData: any = ["%"]; promotionData: any = []; promotionTypeData: any = ["งวด"]; constructor( public activatedRoute: ActivatedRoute, public router: Router, public changeDetectorRef: ChangeDetectorRef, public appService: AppService ) { super(); } async ngOnInit() { this.activatedRoute.params.subscribe(async params => { this.ids = params["id"]; this.action = params["action"]; this.dataForm.type = "งวด"; this.defaultData(); if (this.ids) await this.getData(); }); } defaultData() { const promotionDetail: any[] = []; for (let i = 0; i < 12; i++) { const item : any = { code : i + 1, status : false, name : `งวดที่ ${i + 1}`, value : 0, unit: '%' } promotionDetail.push(item) } this.dataForm.promotionDetail = promotionDetail; } async onAction(action: string) { if (action === "back") return this.router.navigate(["/pages/setting/promotion"]); return; } async getData() { if (!this.ids) this.appService.message(EAction.INFO, EText.NO_DATA); try { this.dataForm = await lastValueFrom(this.appService.get(`${this.api.promotion}/getById/${this.ids}`)); this.changeDetectorRef.detectChanges(); } catch (err) { this.appService.message(EAction.ERROR, EText.ERROR); } } async onSubmit(form: any) { if (!form.valid) return false; if (this.action === EAction.CREATE) return await this.onCreate(); if (this.action === EAction.UPDATE) return await this.onUpdate(); return; } async onCreate() { try { await lastValueFrom(this.appService.post(this.api.promotion, this.dataForm)); await this.appService.message(EAction.SUCCESS, EText.CREATE); await this.onAction("back"); } catch (err) { this.appService.message(EAction.ERROR, EText.ERROR); } } async onUpdate() { try { await lastValueFrom(this.appService.post(`${this.api.promotion}/update/${this.ids}`, this.dataForm)); await this.appService.message(EAction.SUCCESS, EText.UPDATE); await this.onAction("back"); } catch (err) { this.appService.message(EAction.ERROR, EText.ERROR); } } onAddItem(form: any) { if (!form.valid) return false; for (let i = 0; i < this.addItemNumber; i++) { this.dataForm.productMeasurement?.push({}); } return; } onRemoveItem(i: number) { console.log(this.dataForm.productMeasurement?.[i]); this.dataForm.productMeasurement?.splice(i, 1); this.changeDetectorRef.detectChanges(); } protected readonly PREFIX = PREFIX; }