import { Component,OnInit } from '@angular/core';
import { AlertController } from '@ionic/angular';
@Component({
  selector: 'app-tab2',
  templateUrl: './tab2.page.html',
  styleUrls: ['./tab2.page.scss'],
})
export class Tab2Page implements OnInit {
  segment: string = 'memo';
  memos: string[] = [];
  newMemo: string = '';
  messages: { sender: string, text: string }[] = [];
  newMessage: string = '';

  constructor(private alertController: AlertController) {}

  ngOnInit() {
    this.loadMemos();
  }
  //加载备忘录
  loadMemos() {
    const memos = localStorage.getItem('memos');
    if (memos) {
      this.memos = JSON.parse(memos);
    }
  }
  //保存备忘录
  saveMemos() {
    localStorage.setItem('memos', JSON.stringify(this.memos));
  }

  //获取时间戳
  getCurrentTimestamp(): string {
    const now = new Date();
    const year = now.getFullYear();
    const month = (now.getMonth() + 1).toString().padStart(2, '0');
    const day = now.getDate().toString().padStart(2, '0');
    const hours = now.getHours().toString().padStart(2, '0');
    const minutes = now.getMinutes().toString().padStart(2, '0');
    return `${year}年${month}月${day}日${hours}:${minutes}`;
  }

  //异步函数,添加备忘录
  async addMemo() {
    if (this.newMemo.trim().length > 0) {
      this.memos.push(this.newMemo);
      this.newMemo = '';
      this.saveMemos();
      const alert = await this.alertController.create({
        header: '提示',
        message: '添加成功',
        buttons: ['确定']
      });
      await alert.present();
    }
  }
  //异步函数,确认删除
  async confirmDeleteMemo(memo: string) {
    const alert = await this.alertController.create({
      header: '确认删除',
      message: `你确定要删除 "${memo}" 吗?`,
      buttons: [
        {
          text: '取消',
          role: 'cancel'
        },
        {
          text: '删除',
          handler: () => {
            this.memos = this.memos.filter(m => m !== memo);
            this.saveMemos();
          }
        }
      ]
    });
    await alert.present();
  }

  //删除备忘录
  deleteMemo(memo: string) {
    this.memos = this.memos.filter(m => m !== memo);
    this.saveMemos();
  }

  //
  sendMessage() {
    if (this.newMessage.trim().length > 0) {
      this.messages.push({ sender: 'user', text: this.newMessage });
      this.newMessage = '';
      
      // 模拟AI回复
      setTimeout(() => {
        this.messages.push({ sender: 'ai', text: 'AI的回复' });
      }, 1000);
    }
  }

}