import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AlertController, IonButton, IonButtons, IonContent, IonHeader, IonIcon, IonInput, IonItem, IonLabel, IonTitle, IonToolbar } from '@ionic/angular/standalone';
import { FormsModule } from '@angular/forms'; // 导入 FormsModule
import { CommonModule } from '@angular/common'; // 导入 CommonModule
import { chevronBackOutline } from 'ionicons/icons';
import { addIcons } from 'ionicons';
addIcons({chevronBackOutline});
@Component({
  selector: 'app-page-bmi',
  templateUrl: './page-bmi.component.html',
  styleUrls: ['./page-bmi.component.scss'],
  standalone: true,
  imports:[IonHeader,IonToolbar,IonTitle,IonButton,IonButtons,IonContent,IonItem,IonLabel,IonInput,FormsModule,CommonModule,IonIcon]
})
export class PageBmiComponent  implements OnInit {

  ngOnInit() {this.resetForm();} // 页面加载时清空数据
  height: number|null =null; // 身高
  weight: number|null =null; // 体重
  bmi: number|null=null; // BMI 值

  constructor(private router: Router,private alertController: AlertController) {this.resetForm();}

  // 计算 BMI
  async calculateBMI() {
    if (this.height !=null && this.weight !=null) {
      if(this.height >0 && this.weight >0) {
      const heightInMeters = this.height / 100; // 将身高转换为米
      this.bmi = this.weight / (heightInMeters * heightInMeters); // 计算 BMI
      } else {
        const alert = await this.alertController.create({
          header: '身高体重不能为0！',
          buttons: ['确定']
        });
        await alert.present(); // 显示弹出框
      }
    } else {
      const alert = await this.alertController.create({
        header: '身高体重不能为空！',
        buttons: ['确定']
      });
      await alert.present(); // 显示弹出框
      
    }
  }

  //清空数据
    // 清空输入数据
    resetForm() {
      this.height = null;
      this.weight = null;
      this.bmi = null;
    }

  // 返回上一个页面
  goBack() {
    this.router.navigate(['/tabs/tab3'], { replaceUrl: true }); // 使用 replaceUrl 强制重载页面
  }
}

