import {
  Component,
  OnInit,
  ViewChild,
} from '@angular/core';
import * as Parse from 'parse';
import { IonicModule } from '@ionic/angular';
import { Router } from '@angular/router';
import { AuthService } from '../../../services/auth.service';
import { Swiper } from 'swiper';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss'],
  standalone: true,
  imports: [IonicModule],
  // schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class HomeComponent implements OnInit {
  @ViewChild('modal') modal!: any;
  options: Array<any> = [
    {
      label: '关注',
      value: 'follow',
      icon: 'home-outline',
      color: 'primary',
    },
    {
      label: '推荐',
      value: 'recommend',
      icon: 'home-outline',
      color: 'primary',
    },
    {
      label: '新人',
      value: 'news',
      icon: 'videocam-outline',
      color: 'danger',
    },
    {
      label: '三星',
      value: 'star3',
      icon: 'videocam-outline',
      color: 'danger',
    },
    {
      label: '四星',
      value: 'star4',
      icon: 'videocam-outline',
      color: 'danger',
    },
    {
      label: '五星',
      value: 'star5',
      icon: 'people-outline',
    },
  ];
  currentValue: string = 'recommend';
  oldCurrentValue:string = 'recommend';
  isOpen: boolean = false; //显示选择弹窗
  banner: Array<Parse.Object> = [];
  roomList: Array<Parse.Object> = [];
  pageSwiper: Swiper | undefined | any;

  constructor(private router: Router, public authServ: AuthService) {}

  ngOnInit() {
    this.refresh();
  }
  async refresh() {
    await this.getBanner();
    await this.getRoom();
    setTimeout(() => {
      this.initSwiperTimeEvent();
    }, 0);
  }
  async getBanner() {
    let query = new Parse.Query('Banner');
    query.equalTo('company', this.authServ.company);
    query.descending('index');
    query.equalTo('isEnabled', true);
    query.notEqualTo('isDeleted', true);
    let banner = await query.find();
    this.banner = banner;
  }
  initSwiperTimeEvent() {
    // 初始化轮播图
    let swiper = new Swiper('.mySwiper', {
      loop: true, // 循环模式选项
      observer: false, //修改swiper自己或子元素时，自动初始化swiper
      observeParents: true, //修改swiper的父元素时，自动初始化swiper
      autoplay: {
        delay: 1500,
      },
      pagination: {
        el: '.swiper-pagination',
      },
    });
    swiper.on('slideChange', function (event: any) {
      // console.log(event);
    });
  }
  async getRoom() {
    let query = new Parse.Query('Room');
    query.equalTo('company', this.authServ.company);
    query.equalTo('state', true);
    query.notEqualTo('isDeleted', true);
    let r = await query.find();
    this.roomList = r;
  }
  segmentChanged(e: any) {
    let { value } = e.detail;
    this.currentValue = value;
  }
  onIonChange(event: CustomEvent) {
    this.currentValue = event.detail.value;
  }
  /* 关闭弹窗回调 */
  onDidDismiss(event: CustomEvent) {
    this.isOpen = false;
    console.log(this.currentValue);
  }
  cancel(type: string, value?: string) {
    console.log(type, value);
    if(type == 'cancel'){
      this.currentValue = this.oldCurrentValue;
    }else{
      this.oldCurrentValue = this.currentValue;
    }
    this.isOpen = false;
    this.modal.dismiss();
  }
  search() {
    this.router.navigate(['live/search']);
  }
  toUrl(url: string){
    this.router.navigate([url]);
  }
}
