import { Component, OnInit } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { UserService } from '../services/user.service';

@Component({
  selector: 'app-new-post',
  templateUrl: './new-post.page.html',
  styleUrls: ['./new-post.page.scss'],
})
export class NewPostPage implements OnInit {

  title: string = ''; // 声明 title 属性
  content: string = ''; // 声明 content 属性
  user: any;
  posts = [];

  constructor(private modalCtrl: ModalController, private userService: UserService) {}

  ngOnInit() {
    this.userService.getUserInfo().subscribe(userInfo => {
      if (userInfo) {
        this.user = userInfo;
      }
    });
  }

  async submitPost() {
    if (this.title.trim() === '' || this.content.trim() === '') {
      console.warn('Title and content cannot be empty');
      return;
    }

    // 打印日志以确认数据是否正确
    console.log('Submit Post:', { title: this.title, content: this.content });

    // 关闭模态窗口并返回数据
    await this.modalCtrl.dismiss({ title: this.title, content: this.content });
  }

  async cancel() {
    // 关闭模态窗口而不返回数据
    await this.modalCtrl.dismiss();
  }
}