import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AlertController } from '@ionic/angular';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { IonicModule } from '@ionic/angular';

@Component({
  selector: 'app-page-ytb',
  templateUrl: './page-ytb.component.html',
  styleUrls: ['./page-ytb.component.scss'],
  standalone: true,
  imports: [FormsModule, CommonModule, IonicModule],
})
export class PageYtbComponent implements OnInit {
  height: number | null = null; // 身高
  waist: number | null = null;  // 腰围
  hip: number | null = null;    // 臀围
  whr: number | null = null;    // 腰臀比
  whrStatus: string | null = null; // 腰臀比状态信息

  constructor(private router: Router, private alertController: AlertController) {}

  ngOnInit() {
    this.resetForm(); // 页面加载时清空数据
  }

  // 计算腰臀比
  async calculateWhr() {
    if (this.height && this.waist && this.hip) {
      if (this.height > 0 && this.waist > 0 && this.hip > 0) {
        // 计算腰臀比
        const whr = this.waist / this.hip;
        this.whr = whr;

        // 检查腰臀比是否正常
        this.checkWhrStatus(whr);
      } else {
        const alert = await this.alertController.create({
          header: '身高、腰围和臀围不能为零！',
          buttons: ['确定'],
        });
        await alert.present(); // 显示弹出框
      }
    } else {
      const alert = await this.alertController.create({
        header: '请确保输入完整的身高、腰围和臀围！',
        buttons: ['确定'],
      });
      await alert.present(); // 显示弹出框
    }
  }

  // 检查腰臀比是否正常
  checkWhrStatus(whr: number) {
    if (whr <= 0) {
      this.whrStatus = '腰臀比不正常，请检查输入数据';
    } else if (whr > 0 && whr <= 0.9) {
      this.whrStatus = '腰臀比正常';
    } else if (whr > 0.9 && whr <= 1.0) {
      this.whrStatus = '腰臀比偏高，需注意';
    } else {
      this.whrStatus = '腰臀比不正常，建议调整';
    }
  }

  // 清空数据
  resetForm() {
    this.height = null;
    this.waist = null;
    this.hip = null;
    this.whr = null;
    this.whrStatus = null; // 清空状态信息
  }

  // 返回上一个页面
  goBack() {
    this.router.navigate(['/tabs/tab2'], { replaceUrl: true });
  }
}
