import { Component, OnInit, ViewChild } from '@angular/core';
import {
  AlertController,
  IonicModule,
  LoadingController,
  ToastController,
} from '@ionic/angular';
import { NavComponent } from '../../../app/components/nav/nav.component';
import { LiveService } from '../../../services/live.service';
import { UploadComponent } from '../../../app/components/upload/upload.component';
import * as Parse from 'parse';
import { AuthService } from '../../../services/auth.service';

@Component({
  selector: 'app-room-manage',
  templateUrl: './room-manage.component.html',
  styleUrls: ['./room-manage.component.scss'],
  standalone: true,
  imports: [IonicModule, NavComponent, UploadComponent],
})
export class RoomManageComponent implements OnInit {
  @ViewChild('upload') upload!: UploadComponent;
  room?:Parse.Object
  formData: any = {
    title: '',
    cover: null,
    content: null,
  };
  loading: any;
  pid: string = localStorage.getItem('profile') ?? '';
  initLoad:boolean = true;
  constructor(
    public toastController: ToastController,
    private loadingCtrl: LoadingController,
    private liveService: LiveService,
    private alertController: AlertController,
    private authServ: AuthService,
  ) {}

  async ngOnInit() {
    await this.getProfile();
    this.getRoom()
  }
  // 获取用户信息
  async getProfile() {
    if (this.pid) return;
    let user = Parse.User.current();
    let query = new Parse.Query('Profile');
    query.equalTo('user', user?.id);
    query.notEqualTo('isDeleted', true);
    query.select('isCheck', 'isCross');
    let r = await query.first();
    if (r?.id) this.pid = r?.id;
  }
  /* 直播间 */
  async getRoom() {
    let query = new Parse.Query('Room');
    query.equalTo('user', Parse.User.current()?.id);
    query.equalTo('profile', this.pid);
    query.notEqualTo('isDeleted', true);
    this.room = await query.first();
    this.formData = {
      title: this.room?.get('title'),
      cover: this.room?.get('cover'),
      content: this.room?.get('content'),
    };
    console.log(this.formData);
    this.initLoad = false
  }
  async saveEdit(e: any) {
    let url = e[0]?.url
    console.log(url);
    if(!url || !this.formData.title){
      const toast = await this.toastController.create({
        message: '请填写完整',
        color: 'warning',
        duration: 1000,
      });
      toast.present()
      return
    }
    this.loading = await this.loadingCtrl.create({
      message: '正在保存修改',
    });
    this.loading.present();
    if (!this.room?.id) {
      let obj = Parse.Object.extend('Room');
      this.room = new obj();
      this.room?.set('user', Parse.User.current()?.toPointer());
      this.room?.set('company', {
        __type: 'Pointer',
        className: 'Company',
        objectId: this.authServ.company,
      });
      this.room?.set('profile', {
        __type: 'Pointer',
        className: 'Profile',
        objectId: this.pid,
      });
    }
    this.room?.set('roomid', this.pid);
    this.room?.set('title', this.formData.title);
    this.room?.set('cover', url);
    this.room?.set('content', this.formData.content);
    await this.room?.save();
    this.room && this.liveService.getToken(this.room)
    this.loading.dismiss();
    this.getRoom();
    this.presentAlert('修改成功');
  }
  async presentAlert(msg: string) {
    const alert = await this.alertController.create({
      header: '提示',
      message: msg,
      buttons: [
        {
          text: '好的',
          role: 'confirm',
          handler: () => {},
        },
      ],
    });
    await alert.present();
  }
}
