import { Component } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { CloudObject, CloudUser } from 'src/lib/ncloud';

@Component({
  selector: 'app-edit',
  templateUrl: './edit.page.html',
  styleUrls: ['./edit.page.scss'],
  standalone:false,
})
export class EditPage {
  diary: any = {
    Did: 0,
    date: this.getCurrentDate(),
    weekday: this.getCurrentWeekday(),
    time: this.getCurrentTime(),
    content: '',
    weather: '',
    mood: ''
  };

  constructor(private modalCtrl: ModalController) {}

  ngOnInit() {
    // 生成随机ID
    this.diary.Did = Math.floor(Math.random() * 100000);
  }

  // 获取当前日期
  private getCurrentDate(): string {
    return new Date().getDate().toString();
  }

  // 获取当前星期
  private getCurrentWeekday(): string {
    const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    return weekdays[new Date().getDay()];
  }

  // 获取当前时间
  private getCurrentTime(): string {
    const now = new Date();
    return `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`;
  }

  async saveDiary() {
    // 创建新的日记对象
    const newDiary = new CloudObject("Diary");
    //newDiary.equalTo('user',Parse.User.current()?.id);
    //const currentUser = Parse.User.current();
    newDiary.set(this.diary);
    
    try {
      await newDiary.save();
      // 保存成功，返回并刷新列表
      this.modalCtrl.dismiss({ saved: true });
    } catch (error) {
      console.error('保存日记失败:', error);
      this.modalCtrl.dismiss({ saved: false });
    }
  }

  // 关闭模态框
  dismiss() {
    this.modalCtrl.dismiss({ saved: false });
  }
}