import { Component, ViewChild, Input } from '@angular/core';
import { ChangeDetectorRef } from '@angular/core';
import { Router, RouterOutlet } from '@angular/router';
import * as Parse from 'parse';
import { IonicModule } from '@ionic/angular';

// 全局消息服务和JIM插件
import { OnInit } from '@angular/core';
import { AlertController } from '@ionic/angular';
// import { MessageService } from '../../../services/message.service';
import { ConnectTaskService } from '../../../services/connectTask.service';

@Component({
  selector: 'live-tabs',
  templateUrl: 'tabs.component.html',
  styleUrls: ['tabs.component.scss'],
  standalone: true,
  imports: [IonicModule],
})
export class TabsComponent implements OnInit {
  @Input('tabsOption') tabsOption: any = {};
  company: any = window.localStorage.getItem('company');
  // @ViewChild('tabs',{static:true}) tabs;
  constructor(
    private router: Router,
    private alertController: AlertController,
    private connectTask: ConnectTaskService
  ) {
    this.active = Number(localStorage.getItem('active')) || 0
    this.presentAlert();
  }

  option: any = [
    {
      src: '/img/首页.png',
      active: '/img/首页_active.png',
      url: 'tabs/home',
      name: '首页',
    },
    {
      src: '/img/消息.png',
      active: '/img/消息_active.png',
      url: 'tabs/notice',
      name: '消息',
    },
    {
      src: '/img/live.png',
      active: '/img/live.png',
      url: 'tabs/live-review',
      name: '直播',
    },
    {
      src: '/img/排名.png',
      active: '/img/排名_active.png',
      url: 'tabs/ranking',
      name: '排名',
    },
    {
      src: '/img/我的.png',
      active: '/img/我的_active.png',
      url: 'tabs/my',
      name: '我的',
    },
  ];
  show: any;
  active: number = 0;

  ngOnInit() {

  }

  async presentAlert() {
    let profile = localStorage.getItem('profile');
    if (profile) {
      return;
    }
    let uid = Parse.User.current()?.id;
    let query = new Parse.Query('Profile');
    query.equalTo('user', uid);
    query.equalTo('isCross', true);
    query.equalTo('company', this.company);
    query.notEqualTo('isDeleted', true);
    let res = await query.first();
    if (res?.id) {
      localStorage.setItem('profile', JSON.stringify(res.toJSON()));
      return;
    }
    // const alert = await this.alertController.create({
    //   cssClass: 'my-custom-class',
    //   header: '实名认证',
    //   message: '根据相关法律法规要求,请先完成实名！',
    //   buttons: [
    //     {
    //       text: '取消',
    //       role: 'cancel',
    //       cssClass: 'secondary',
    //       handler: (blah) => {
    //         console.log('Confirm Cancel: blah');
    //       },
    //     },
    //     {
    //       text: '去认证',
    //       handler: () => {
    //         this.router.navigate(['real']);
    //       },
    //     },
    //   ],
    // });
    // await alert.present();
  }

  allowNavi: boolean = true;
  goTabPage(index: number, route: string) {
    if (this.allowNavi == false) {
      return;
    }
    this.active = index;
    localStorage.setItem('active',String(this.active))
    if (('temp' + route).indexOf('http') > 0) {
      window.location.href = route;
      console.log('temp' + route);
    } else {
      let r = route;
      let params: any = {};
      if (r.split(';').length > 1) {
        // 若存在;参数，则拼接参数对象params
        for (let index = 1; index < r.split(';').length; index++) {
          let key = r.split(';')[index].split('=')[0];
          let val = r.split(';')[index].split('=')[1];
          params[key] = val;
        }
        // 此时路由为数组第一个
        route = r.split(';')[0];
      }
      this.router.navigate([route, params]);
    }
    this.allowNavi = false;
    setTimeout(() => {
      this.allowNavi = true;
    }, 1000);
  }
}
