import { Component, Input } from '@angular/core';
import { ModalController } from '@ionic/angular/standalone';
import { IonRow, IonCol, IonHeader, IonToolbar, IonTitle, IonButtons, IonButton, IonContent, IonCard, IonCardHeader, IonCardTitle, IonCardContent, IonItem, IonLabel, IonInput, IonToggle } from '@ionic/angular/standalone';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
@Component({
  selector: 'app-edit-plan-modal',
  templateUrl: './edit-plan-modal.component.html',
  styleUrls: ['./edit-plan-modal.component.scss'],
  standalone: true,
  imports: [
    IonRow,
    IonCol,
    IonHeader,
    IonToolbar,
    IonTitle,
    IonButtons,
    IonButton,
    IonContent,
    IonCard,
    IonCardHeader,
    IonCardTitle,
    IonCardContent,
    IonItem,
    IonLabel,
    IonInput,
    IonToggle,
    FormsModule,
    CommonModule
  ]
})
export class EditPlanModalComponent {
  @Input() plan: any;  // 接收传入的计划数据
  constructor(private modalCtrl: ModalController) { }
  // 更新数据字段
  onChange(field: string, event: any) {
    const value = event.target.value;
    console.log('Before update:', this.plan.data);
    this.plan.set({ [field]: value });  // 使用 set 来更新数据
  }
  // 处理任务项的变化
  onTaskChange(index: number, field: string, event: any) {
    const value = event.target.value;
    this.plan.data.trainingItems[index][field] = value;
    const updatedTrainingItems = this.plan.data.trainingItems;
    this.plan.set({ "trainingItems": updatedTrainingItems });
  }
  // 保存修改并更新数据库
  async saveChanges() {
    try {
      await this.plan.save();
      console.log('计划修改成功');
      this.modalCtrl.dismiss(this.plan);
    } catch (error) {
      console.error('保存失败:', error);
    }
  }
  dismiss() {
    this.modalCtrl.dismiss();
  }
}
