plan-creation-modal.component.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { CommonModule } from '@angular/common';
  2. import { Component } from '@angular/core';
  3. import { FormsModule } from '@angular/forms';
  4. import { ModalController } from '@ionic/angular/standalone';
  5. import {
  6. IonHeader, IonToolbar, IonTitle, IonButtons,
  7. IonButton, IonIcon, IonContent, IonItem,
  8. IonInput, IonTextarea, IonSelect, IonSelectOption,
  9. IonLabel, IonCheckbox, IonDatetime
  10. } from '@ionic/angular/standalone';
  11. import { addIcons } from 'ionicons';
  12. import { close, save, barbell, body, walk, flame, bicycle, calendar } from 'ionicons/icons';
  13. // 定义新计划对象的接口
  14. interface NewPlan {
  15. name: string;
  16. description: string;
  17. duration: number;
  18. difficulty: string;
  19. goals: string[];
  20. icon: string;
  21. startDate: string; // 确保包含这个属性
  22. }
  23. @Component({
  24. selector: 'app-plan-creation-modal',
  25. templateUrl: './plan-creation-modal.component.html',
  26. styleUrls: ['./plan-creation-modal.component.scss'],
  27. standalone: true,
  28. imports: [
  29. CommonModule,
  30. FormsModule,
  31. IonHeader, IonToolbar, IonTitle, IonButtons, IonButton, IonIcon,
  32. IonContent, IonItem, IonInput, IonTextarea, IonSelect, IonSelectOption,
  33. IonLabel, IonCheckbox, IonDatetime
  34. ]
  35. })
  36. export class PlanCreationModalComponent {
  37. // 使用接口定义 newPlan
  38. newPlan: NewPlan = {
  39. name: '',
  40. description: '',
  41. duration: 4,
  42. difficulty: 'beginner',
  43. goals: [] as string[],
  44. icon: 'fitness',
  45. startDate: new Date().toISOString().split('T')[0] // 只保留日期部分
  46. };
  47. // 获取当前日期字符串 (YYYY-MM-DD)
  48. get todayDate(): string {
  49. return new Date().toISOString().split('T')[0];
  50. }
  51. // 日期变更处理方法
  52. dateChanged(event: any) {
  53. const value = event?.detail?.value;
  54. if (value) {
  55. this.newPlan.startDate = value.split('T')[0]; // 确保只保存日期部分
  56. }
  57. }
  58. fitnessGoals = [
  59. { name: '跑步', value: 'running', icon: 'walk' },
  60. { name: '减脂', value: 'fat_loss', icon: 'flame' },
  61. { name: '增肌', value: 'muscle_gain', icon: 'barbell' },
  62. { name: '耐力', value: 'endurance', icon: 'bicycle' },
  63. { name: '柔韧性', value: 'flexibility', icon: 'body' }
  64. ];
  65. constructor(private modalCtrl: ModalController) {
  66. addIcons({ close, save, barbell, body, walk, flame, bicycle, calendar });
  67. }
  68. cancel() {
  69. this.modalCtrl.dismiss(null, 'cancel');
  70. }
  71. create() {
  72. this.modalCtrl.dismiss(this.newPlan, 'confirm');
  73. }
  74. toggleGoal(goal: string) {
  75. const index = this.newPlan.goals.indexOf(goal);
  76. if (index > -1) {
  77. this.newPlan.goals.splice(index, 1);
  78. } else {
  79. this.newPlan.goals.push(goal);
  80. }
  81. // 自动选择图标
  82. if (this.newPlan.goals.includes('running')) {
  83. this.newPlan.icon = 'walk';
  84. } else if (this.newPlan.goals.includes('fat_loss')) {
  85. this.newPlan.icon = 'flame';
  86. } else if (this.newPlan.goals.includes('muscle_gain')) {
  87. this.newPlan.icon = 'barbell';
  88. } else {
  89. this.newPlan.icon = 'fitness';
  90. }
  91. }
  92. }