import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
// 引用Parse JS SDK
import Parse from "parse";
Parse.initialize("dev"); // 设置applicationId
Parse.serverURL = "http://web2023.fmode.cn:9999/parse"; // 设置serverURL
@Component({
  selector: 'app-tab3',
  templateUrl: 'tab3.page.html',
  styleUrls: ['tab3.page.scss']
})
export class Tab3Page implements OnInit{
  //数据定义
  hasData: boolean = false;
  currentTab: string = 'unpublished';
  userImage: string = 'https://via.placeholder.com/150'; // 默认图片地址
    // 由于Parse.User.current()是随着localStorage变化的属性
  // 为了避免首次复制后用户状态变化，页面不同步，通过get方法实现实时获取
  user:Parse.User|undefined

  async ngOnInit() {
      this.user = await Parse.User.current()
      setInterval(async ()=>{
      this.user = await Parse.User.current()
    },1000)
    this.loadCreatContentsDate()
  }
  logout(){
    Parse.User.logOut();
  }


  constructor(private router: Router) {  this.checkData();}

  
  cards = [
    { title: '我的订单', content: '查看全部', icon: 'document-text-outline' },
    { title: '积分中心', content: '答题赢积分', icon: 'trophy-outline' },
    { title: '我的勋章', content: '2枚勋章', icon: 'medal-outline' }
  ];

  
  onFileSelected(event: Event) {
    const inputElement = event.target as HTMLInputElement;
    if (inputElement.files && inputElement.files.length > 0) {
    const file = inputElement.files[0];
    const reader = new FileReader();
    reader.onload = () => {
      if (reader.result) {
        this.userImage = reader.result as string;
      }
    };
    reader.readAsDataURL(file);
    }
  }


  //跳转到新页面
  GoToLoginPage(){
    this.router.navigate(['/user/login'])
  }
 navigateToCreatePage() {
  this.router.navigate(['/tab3/tab3-create']);
  }

   
    //数据库CreatContents中数据调入前端页面
    InputContentsList:Array<Parse.Object> = []
    async loadCreatContentsDate(){
    let query = new Parse.Query("CreatContents");
    let list = await query.find();
    this.InputContentsList = list
    }


    //查询数据库中是否有CreatContents的数据
    async checkData() {
      const CreatContents = Parse.Object.extend("CreatContents");
      const query = new Parse.Query(CreatContents);
      const results = await query.find();
  
      if (results.length > 0) {
        this.hasData = true;
      } else {
        this.hasData = false;
      }
    }
}
