import { openSecondaryScreen, openCycleSecondaryScreen,closeSecondaryScreen, viceBroadcast, stopVoiceBroadcast } from "../../utils/index/callAmpe";

const app = getApp()
Page({
  data: {
    timer: 60,
    count: 0,
    reminderCount: 0,
    remainingTime: '',
    intervalId1: null,
    img: '',
    confirmCountdownTime: 15,
    isDisabled: true,
    confirmCountdownSwitch: true,

    isBack: false,// 是否点击退出按钮
  },
  onLoad() {
    my.hideBackHome();
    let remainingTime = '操作时间 ' + this.data.timer + ' 秒'
    console.log(app.globalData.snDisposition, app.globalData.snDisposition.confirmCountdownTime)
    let confirmCountSwitch = true
    if (app.globalData.snDisposition.confirmCountdownSwitch != null) {
      confirmCountSwitch = app.globalData.snDisposition.confirmCountdownSwitch
    }
    this.setData({
      remainingTime,
      img: app.globalData.snDisposition.reminder,
      confirmCountdownSwitch: confirmCountSwitch,
      confirmCountdownTime: app.globalData.snDisposition.confirmCountdownTime || 15,
      count: this.data.timer,
      reminderCount: app.globalData.snDisposition.confirmCountdownTime || 15
    })
    setTimeout(()=>{
      !this.data.isBack && viceBroadcast(app.globalData.snDisposition.reminderPlayText)
    }, 1500)
    this.startCountdown()
  },
  // 倒计时
  startCountdown() {
    let that = this
    let {
      count,
      reminderCount
    } = that.data
    if (count) {
      that.data.intervalId1 && clearInterval(that.data.intervalId1);
      that.data.intervalId1 = setInterval(() => {
        count--
        if(reminderCount>0){
          reminderCount--
        }
        console.log()
        let remainingTime = '操作时间 ' + count + ' 秒'
        that.setData({
          remainingTime,
          reminderCount,
          isDisabled: that.data.confirmCountdownSwitch && (reminderCount > 0)
        })
        if (count <= 0) {
          // 倒计时结束，清除定时器
          clearInterval(that.data.intervalId1)
          my.reLaunch({
            url: '/pages/home/index',
          })

        }
      }, 1000) // 每秒更新一次倒计时数字
    }
  },
  sure() {
    if (this.data.isDisabled) {
      return
    }
    stopVoiceBroadcast()
    this.setData({
      isBack: true
    })
    clearInterval(this.data.intervalId1)
    // 是否启用副屏处理中页面
    let processingPageSwitch = app.globalData.snDisposition.processingPageSwitch;
    if (app.data.isSecondaryScreenState && processingPageSwitch) {
      console.log('打开副屏', app.data.isSecondaryScreenState);
      openSecondaryScreen(app.globalData.snDisposition.processingPageImage || '')
    }
    my.reLaunch({
      url: '/pages/interviewee/index'
    })
  },
  logOut() {
    stopVoiceBroadcast()
    this.setData({
      isBack: true
    })
    clearInterval(this.data.intervalId1)
    my.reLaunch({
      url: '/pages/home/index'
    });
  },
});
