import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { NavController } from '@ionic/angular';

// 定义接口
interface HealthReport {
  title: string;
  date: Date;
  details: string;
  type: 'annual' | 'eye'; // 添加类型字段
}

@Component({
  selector: 'app-health-record',
  templateUrl: './health-record.page.html',
  styleUrls: ['./health-record.page.scss'],
})
export class HealthRecordPage implements OnInit {
  healthRecord = {
    name: '',
    gender: '',
    age: null as number | null,
    contact: '',
    medicalHistory: '',
    reports: [] as HealthReport[] // 使用 HealthReport 接口定义 reports 数组的类型
  };

  constructor(private route: ActivatedRoute, private router: Router, private navCtrl: NavController) {}

  ngOnInit() {
    // 初始化健康档案数据
    this.loadHealthRecord();
  }

  loadHealthRecord() {
    // 这里可以加载用户健康档案数据
    // 例如从服务中获取或从本地存储读取
    // 示例数据
    this.healthRecord = {
      name: '张三',
      gender: 'male',
      age: 30,
      contact: '12345678901',
      medicalHistory: '高血压',
      reports: [
        { title: '年度体检', date: new Date(), details: '血压正常，血糖略高', type: 'annual' },
        { title: '眼科检查', date: new Date('2024-01-01'), details: '视力良好，无异常', type: 'eye' }
      ] as HealthReport[] // 显式指定类型
    };
  }

  editBasicInfo() {
    // 导航到编辑基本信息页面
    this.navCtrl.navigateForward('/edit-profile');
  }

  addNewReport() {
    // 导航到添加新体检报告页面
    this.navCtrl.navigateForward('/add-checkup');
  }

  viewReport(index: number) {
    // 查看具体的体检报告
    const report = this.healthRecord.reports[index];
    console.log('Viewing report:', report);

    // 根据报告类型导航到相应的页面
    if (report.type === 'annual') {
      this.navCtrl.navigateForward(`/annual-checkup/${index}`);
    } else if (report.type === 'eye') {
      this.navCtrl.navigateForward(`/eye-exam/${index}`);
    } else {
      console.error('Unknown report type:', report.type);
    }
  }
}