import { CommonModule } from '@angular/common';
import { Component, OnInit, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { 
  IonBackButton, 
  IonButton, 
  IonButtons,         // 导入 IonButtons
  IonCard, 
  IonCardContent, 
  IonCardHeader, 
  IonCardTitle, 
  IonContent, 
  IonHeader, 
  IonIcon, 
  IonItem, 
  IonLabel, 
  IonList, 
  IonRefresher, 
  IonRefresherContent, 
  IonTitle, 
  IonToolbar, 
  AlertController 
} from '@ionic/angular/standalone';
import { CloudObject, CloudQuery, CloudUser } from 'src/lib/ncloud';

@Component({
  selector: 'app-page-user-inquery',
  templateUrl: './page-user-inquery.component.html',
  styleUrls: ['./page-user-inquery.component.scss'],
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  standalone: true,
  imports: [
    CommonModule,
    IonTitle,
    IonContent,
    IonToolbar,
    IonHeader,
    IonList,
    IonLabel,
    IonItem,
    IonButton,
    IonButtons,             // 添加 IonButtons 到 imports
    IonRefresher,
    IonRefresherContent,
    IonBackButton,
    IonCard,
    IonCardContent,
    IonIcon
  ]
})
export class PageUserInqueryComponent implements OnInit {
  appointmentList: Array<CloudObject> = [];
  currentUser: CloudUser;
  constructor(private alertController: AlertController) {
    this.currentUser = new CloudUser();
    this.appointDisplay();
    // this.load()
  }

  ngOnInit() {}

  handleRefresh(event: any) {
    setTimeout(() => {
      // Any calls to load data go here
      this.appointDisplay();
      event.target.complete();
    }, 2000);
  }

  // 用来显示预约列表
  async appointDisplay() {
    console.log('appointDisplay');
    let query = new CloudQuery('appointment');
    // query.include("user")
    query.equalTo('doctor', this.currentUser?.id);
    let list = await query.find()
    this.appointmentList = list.reverse();
    console.log('我的appointmentList', this.appointmentList);
    console.log("this.currentUser", this.currentUser);
  }
  expandedAppointments: Set<string> = new Set();

// 切换折叠状态
toggleDetails(appointmentId: any) {
  if (this.expandedAppointments.has(appointmentId)) {
    this.expandedAppointments.delete(appointmentId);
  } else {
    this.expandedAppointments.add(appointmentId);
  }
}
// 检查是否展开
isExpanded(appointmentId: any): boolean {
  return this.expandedAppointments.has(appointmentId);
}
  // 用来认证医生
  async load() {
    let depart = new CloudQuery('Department');
    depart.equalTo("name", "妇产科");
    let object = await depart.find();
    console.log('object', object);
    // this.currentUser = new CloudUser();
    // console.log('currentUser',this.currentUser)
    let doctor = new CloudObject('Doctor1');
    doctor.set({
      avatar: 'https://app.fmode.cn/dev/jxnu/202226701019/doctor7.png',
      name: '李四',
      title: '主任医师',
      depart: {
        __type: "Pointer",
        className: "Department",
        objectId: object[0].id
      },
      hospital: '首都医科大学附属北京友谊..',
      hospitalLevel: '三甲',
      hospitalType: '百强医院',
      expertise: '擅长：结肠息肉和息肉病、胃息肉、幽门螺杆菌感染、慢性胃炎、胃食管反流、慢性萎缩性胃炎、糜烂性胃炎...',
      rating: 5.0,
      consultations: '1.1万',
      recommendations: 100,
      prices: {
        text: 100,
        voice: 150,
        video: 300
      },
      tags: ['百强医院', '可开处方', '从业24年', '擅长消化系统疾病', '可开处方'],
      isVerified: true,
      isExpert: true,
      user: {
        __type: "Pointer",
        className: "User",
        objectId: "Gx3N24u5gl"
      }
    });
    doctor.save();
    console.log('doctor', doctor);
  }

  // 显示预约信息的弹出框
  async presentAlert(username: string, userPhone: string) {
    const alert = await this.alertController.create({
      header: '预约确认',
      message: `患者${username}已向您预约，请拨打 ${userPhone} 号码联系患者。`,
      cssClass: 'custom-alert',
      buttons: [
        {
          text: '取消',
          role: 'cancel',
          cssClass: 'secondary',
          handler: () => {
            console.log('取消点击');
          }
        }, {
          text: '拨打电话',
          handler: () => {
            window.location.href = `tel:${userPhone}`;
          }
        }
      ]
    });
    await alert.present();
  }
  async deleteAppoint(appoint:CloudObject){
    let alert = await this.alertController.create({
      header: '温馨提示',
      subHeader: 'Tips',
      message: '删除成功！',
      buttons: ['好的'],
    });
    console.log("删除了",appoint)
    appoint.destroy();
    this.appointDisplay();
    await alert.present();
    
  }
}