import { Component } from '@angular/core';
import { ModalController } from '@ionic/angular/standalone';
import { FormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';
import { CommonModule } from '@angular/common';
@Component({
  selector: 'app-add-data-modal',
  templateUrl: './add-data-modal.component.html',
  styleUrls: ['./add-data-modal.component.scss'],
  standalone: true,
  imports: [IonicModule, FormsModule,CommonModule]
})
export class AddDataModalComponent {
  selectedType: 'bloodGlucose' | 'bloodPressure' | 'heartRate' = 'bloodGlucose';
  formData: any = {
    value: '',
    measurementType: '空腹',
    pulse: '',
    measurementMethod: '手动测量',
    notes: ''
  };

  measurementTypes = ['空腹', '餐后', '随机'];
  measurementMethods = ['手动测量', '设备测量'];

  constructor(private modalCtrl: ModalController) {}

  cancel() {
    this.modalCtrl.dismiss();
  }

  save() {
    if (!this.validateForm()) {
      return;
    }

    this.modalCtrl.dismiss({
      type: this.selectedType,
      formData: this.formData
    });
  }

  validateForm(): boolean {
    if (!this.formData.value) {
      return false;
    }

    if (this.selectedType === 'bloodPressure') {
      const values = this.formData.value.split('/');
      if (values.length !== 2 || isNaN(values[0]) || isNaN(values[1])) {
        return false;
      }
    } else {
      if (isNaN(this.formData.value)) {
        return false;
      }
    }

    return true;
  }
}