import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { AlertController } from '@ionic/angular';
import { ChangeDetectorRef } from '@angular/core';
// import { AuthService } from './AuthService';
import Parse from "parse"

@Component({
  selector: 'app-tab3',
  templateUrl: 'tab3.page.html',
  styleUrls: ['tab3.page.scss']
})
export class Tab3Page {
  isLoggedIn: boolean = false; 
  // isLoggedIn: boolean = false;
  user = {
    nickname: 'John Doe',
    avatarUrl: 'assets/美津未.jpg',
    campus: 'Main Campus',
    building: 'A',
    gender: 'male'
  };
  // isLoggedin: boolean = false;
  editedUser = { ...this.user }; // Clone of user for editing
  //editing = false;

  constructor(
    private router: Router,
    private alertController: AlertController,private cdr: ChangeDetectorRef
  ) {
    const navigation = this.router.getCurrentNavigation();
    const state = navigation?.extras.state;
    if (state && state['isLoggedIn']) {
      this.isLoggedIn = true;
    };
    
  }
  
  refreshView() {
    // 执行变化检测并刷新视图
    this.cdr.detectChanges();
  };
  currentUser:Parse.User|undefined

  async ngOnInit() {
    this.currentUser = await Parse.User.current();
    setInterval(()=>{
      this.refreshPage()
    },1000)
    if (this.currentUser) {
      // 如果有当前用户信息，则更新user对象
      this.user.nickname = this.currentUser.get("nickname");
      this.user.gender=this.currentUser.get("gender");
      // 根据你的数据库字段来更新其他用户信息
      this.user.avatarUrl = this.currentUser.get("avatarUrl");
      this.user.campus = this.currentUser.get("campus");
      this.user.building = this.currentUser.get("building");
      // this.user.gender = this.username.get("gender");
      console.log(this.user.nickname);
      //detectChanges();
    }
  }
  // async ngOnInit() {
  //   this.username = await Parse.User.current()
  //   setInterval(async ()=>{
  //   this.username = await Parse.User.current()
  // },1000)

  async refreshPage() {
    this.currentUser = await Parse.User.current();
    if (this.currentUser) {
      this.user.nickname = this.currentUser.get("nickname");
      this.user.gender = this.currentUser.get("gender");
      this.user.campus = this.currentUser.get("campus");
      this.user.building = this.currentUser.get("building");
      // detectChanges( );
      // 可根据需要更新其他用户信息
    }
  }
  onFileSelected(event: any) {
    const file: File = event.target.files[0];
    const reader = new FileReader();
    reader.onload = () => {
      this.editedUser.avatarUrl = reader.result as string;
    };
    reader.readAsDataURL(file);
    this.refreshPage();
  }

  startEditing() {
    //this.editing = true;
  }

  saveChanges() {
    // Save changes to user object and exit editing mode
    this.user = { ...this.editedUser };
    //this.editing = false;
    // Optionally, you can add logic here to save changes to backend
  }

  cancelEditing() {
    // Exit editing mode without saving changes
    this.editedUser = { ...this.user }; // Reset editedUser to original user
    //this.editing = false;
  }

  // login() {
  //   // Handle login logic here, for example navigate to login page
  //   this.router.navigate(['/user/login']);
  // }

  logout() {
    // Handle logout logic here
     Parse.User.logOut();
     this.refreshPage();
    //  detectChanges();
     console.log("退出登入！");
     
  }
}


