import { Component } from '@angular/core';
import { Router } from '@angular/router';
import * as Parse from "parse";

@Component({
  selector: 'app-page-add',
  templateUrl: './page-add.component.html',
  styleUrls: ['./page-add.component.scss']
})
export class PageAddComponent {
  userAdd: any = {
    name: '',
    tag: '',
    desc: ''
  };
  selectedImage: string | ArrayBuffer | null = null;

  constructor(
    private router: Router
  ) {}

  async saveAdd() {
    const HrmContact = Parse.Object.extend('HrmContact');
    const newContact = new HrmContact();

    newContact.set('name', this.userAdd.name);
    newContact.set('tag', this.userAdd.tag);
    newContact.set('desc', this.userAdd.desc);
    newContact.set('img', this.userAdd.img);
    // 设置其他联系人信息...

    try {
      await newContact.save();
      console.log('New contact saved successfully');
      this.router.navigate(['/lesson/me/userFollow']); // 导航到联系人列表页面
    } catch (error) {
      console.error('Error while saving new contact', error);
    }
  }

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