import { Component, Input, OnChanges, SimpleChanges, TemplateRef } from '@angular/core' // import { MealDishInterface } from "../ingredient-meals/ingredient-meals.component"; import { NzModalService } from 'ng-zorro-antd/modal' import { Augmented, OptionItemInterface } from '@cdk/types' import { NzMessageService } from 'ng-zorro-antd/message' import { forkJoin } from 'rxjs' import { ApiService } from '@cdk/services' import { NzDrawerService } from 'ng-zorro-antd/drawer' import { IngredientAnalysisComponent } from '../ingredient-analysis/ingredient-analysis.component' import { weekdayMap } from '../ingredient-form-basic/ingredient-form-basic.component' // export interface MenuObjectInterface { // [Day: number]: { // [MealIndex: number]: MealDishInterface[]; // }; // } export type DishInterface = Augmented<{ dish: number day: number meal: string mark: string items: Array< Augmented<{ key: string isMain: boolean value: Record }> > }> @Component({ selector: 'app-ingredient-dish', templateUrl: './ingredient-dish.component.html', styleUrls: ['./ingredient-dish.component.less'], }) export class IngredientDishComponent implements OnChanges { constructor( private modal: NzModalService, private msg: NzMessageService, private api: ApiService, private drawer: NzDrawerService, ) {} @Input() menuBaisc: any | null @Input() client = false @Input() menuDishFormServer: any | null expanded = new Set() days: number[] = [] selectDay: OptionItemInterface[] = [] weekdayMap = weekdayMap mealCurrentIndex: Record = {} // d = { // 1:{ // zaocan:[ // {dishId:1...} // ] // } // } // menuObject!: MenuObjectInterface; mealDishList: DishInterface[] = [] ngOnChanges(changes: SimpleChanges): void { if (changes['menuBaisc']?.currentValue) { this.initMenuBasic() } if (changes['menuDishFormServer']?.currentValue) { this.initMenuDish() } } initMenuDish() { this.mealDishList = this.menuDishFormServer // const foodIds = new Set(); // this.menuDishFormServer.forEach((i: any) => { // i.ingredient.map((food: any) => { // foodIds.add(food.key); // }); // }); // forkJoin([this.api.getFoodList({ keys: Array.from(foodIds) })]).subscribe(([res]) => { // this.mealDishList = this.menuDishFormServer.map((i: any) => { // return { // ...i, // dishName: i.name, // mealIndex: this.menuBaisc.meals.findIndex((m: string) => m === i.meal), // items: i.ingredient.map((food: any) => { // const fd = res.body.find((f) => f.key === food.key); // food.foodName = fd.name; // return food; // }), // }; // }); // }); } initMenuBasic() { if (this.menuBaisc) { const meals = this.menuBaisc.meals as string[] console.log('this.menuBaisc', this.menuBaisc) const day = this.menuBaisc.day as number[] this.days = day.map((i) => { const d = weekdayMap[i] this.selectDay.push({ label: d, value: String(i), }) this.mealCurrentIndex[i] = 0 this.expanded.add(i) // if (!this.menuObject) { // this.menuObject = {}; // } // this.menuObject[i + 1] = meals.reduce((a, c, idx) => { // return { // ...a, // [idx]: [], // }; // }, {} as Record); return i }) } } expandChange(i: number) { if (this.expanded.has(i)) { this.expanded.delete(i) } else { this.expanded.add(i) } } onSaveDish(v: DishInterface[], day: number, mealIndex: number) { this.mealDishList = JSON.parse(JSON.stringify(v)) // this.menuObject[day][mealIndex] = JSON.parse(JSON.stringify(v)); } reuse(day: number, nzContent: TemplateRef<{}>) { const thisDayDishs = this.mealDishList.filter((f) => f.day === day) console.log('dayDishs', day, this.mealDishList, thisDayDishs, this.selectDay) this.modal.create({ nzTitle: '请选择应用到的日期', nzContent, nzOnOk: () => { this.selectDay.forEach((i) => { if (i['checked']) { thisDayDishs.forEach((reused) => { this.mealDishList.push(JSON.parse(JSON.stringify({ ...reused, day: Number(i.value) }))) }) } i['checked'] = false }) this.mealDishList = JSON.parse(JSON.stringify(this.mealDishList)) this.msg.success('操作成功') }, }) } analysis(day: number) { this.drawer.create({ nzWidth: 720, nzWrapClassName: 'analysis-drawer', nzContent: IngredientAnalysisComponent, nzContentParams: { menu: { ...this.menuBaisc, days: this.days, }, current: { day, }, }, }) } }