import { Component, OnInit } from '@angular/core';
import { IonicModule } from '@ionic/angular';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-attendance',
  templateUrl: './attendance.component.html',
  styleUrls: ['./attendance.component.scss'],
  standalone: true,
  imports: [IonicModule, CommonModule],
})
export class AttendanceComponent implements OnInit {
  currentDate = new Date();
  currentStreak = 3; // 当前连续签到天数
  totalDays = 15;    // 总签到天数
  todaySigned = false; // 今日是否已签到
  rewards = [
    { days: 1, coins: 5, claimed: true },
    { days: 3, coins: 15, claimed: true },
    { days: 7, coins: 40, claimed: false },
    { days: 15, coins: 100, claimed: false },
    { days: 30, coins: 250, claimed: false }
  ];

  constructor() { }

  ngOnInit() {
    // 这里可以从服务器获取用户签到数据
  }

  signIn() {
    if (!this.todaySigned) {
      this.todaySigned = true;
      this.currentStreak++;
      this.totalDays++;
      // 这里添加签到成功的提示
      this.showToast('签到成功！获得5个积分');
    }
  }

  async showToast(message: string) {
    const toast = document.createElement('ion-toast');
    toast.message = message;
    toast.position = 'top';
    toast.color = 'success';
    document.body.appendChild(toast);
    await toast.present();
  }

  claimReward(reward: any) {
    if (!reward.claimed && this.currentStreak >= reward.days) {
      reward.claimed = true;
      this.showToast(`领取成功！获得${reward.coins}个积分`);
    }
  }

  formatDate(date: Date): string {
    return date.toLocaleDateString('zh-CN', {
      month: 'long',
      day: 'numeric',
      weekday: 'long'
    });
  }
}
