import { Component, OnInit, ViewChild } from '@angular/core';
import { FormsModule } from '@angular/forms';
import {
  AlertController,
  IonicModule,
  LoadingController,
  ModalController,
  ToastController,
} from '@ionic/angular';
import { HttpService } from '../../../services/http.service';
import * as utils from '../../../services/utils';
import { AgreementComponent } from '../../login/agreement/agreement.component';
import { NavComponent } from '../../../app/components/nav/nav.component';
import { LiveService } from '../../../services/live.service';
import * as Parse from 'parse';
import { UploadComponent } from '../../../app/components/upload/upload.component';
import { Router } from '@angular/router';

@Component({
  selector: 'app-anchor',
  templateUrl: './anchor.component.html',
  styleUrls: ['./anchor.component.scss'],
  standalone: true,
  imports: [IonicModule, FormsModule, NavComponent,UploadComponent],
})
export class AnchorComponent implements OnInit {
  @ViewChild('upload') upload!: UploadComponent;
  profile?: Parse.Object; //身份信息
  formData: any = {
    avatar: '',
    name: '',
    nickname: '',
    sex: '',
    age: '',
    remark:''
  };
  mobile: string = '';
  loading: any;
  agreement: boolean = false;
  registerAgreement: any;
  company:any
  constructor(
    private modalController: ModalController,
    public loadingCtrl: LoadingController,
    public toastController: ToastController,
    private router: Router,
    private liveService: LiveService,
  ) {
    this.company = liveService.company;
  }
  ngOnInit() {
    this.getAgreement();
    this.getProfile();
  }
  async getProfile() {
    if(!this.liveService.profile){
      this.liveService.alertTips('请先完成实名认证','提示',()=>{
        this.router.navigate(['/user/certification'],{replaceUrl:true});
      })
      return
    }
    let uid = Parse.User.current()?.id;
    let query = new Parse.Query('Profile');
    query.equalTo('user', uid);
    query.notEqualTo('isDeleted', true);
    this.profile = await query.first();
    if (this.profile?.id) {}
  }
  getAgreement() {
    let Agreement = new Parse.Query('ContractAgreement');
    Agreement.equalTo('company', this.company);
    Agreement.equalTo('type', 'register');
    Agreement.first().then((res) => {
      console.log(res);
      this.registerAgreement = res;
    });
  }
  async showAgreement() {
    const modal = await this.modalController.create({
      component: AgreementComponent,
      cssClass: 'my-custom-class',
      componentProps: {
        agreement: this.registerAgreement,
      },
    });
    return await modal.present();
  }
}
