import { Component, OnInit, OnDestroy } from '@angular/core';
import { IonicModule } from '@ionic/angular'; // 导入 IonicModule

@Component({
  selector: 'app-discount-banner',
  templateUrl: './discount-banner.component.html',
  styleUrls: ['./discount-banner.component.scss'],
  standalone: true,  // 确保组件是独立组件
  imports: [IonicModule],  // 引入 Ionic 模块
})
export class DiscountBannerComponent implements OnInit, OnDestroy {
  countdown: string = '';  // 倒计时字符串
  private countdownInterval: any;  // 保存计时器引用

  ngOnInit() {
    const endTime = new Date('2024-12-01T00:00:00');  // 活动结束时间
    this.countdownInterval = setInterval(() => {
      const now = new Date();
      const diff = endTime.getTime() - now.getTime();
      const days = Math.floor(diff / (1000 * 60 * 60 * 24));
      const hours = Math.floor((diff / (1000 * 60 * 60)) % 24);
      const minutes = Math.floor((diff / (1000 * 60)) % 60);
      const seconds = Math.floor((diff / 1000) % 60);

      // 更新倒计时字符串
      this.countdown = `${days}天 ${hours}小时 ${minutes}分 ${seconds}秒`;
    }, 1000);
  }

  ngOnDestroy() {
    // 清除计时器
    if (this.countdownInterval) {
      clearInterval(this.countdownInterval);
    }
  }
}
