// components/tail_tip/index.js
import {
  getDate,
  getWaterDrop,
  formatTime
} from '../../utils/index/index'
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {},
  props: {
    onFinish: {
      type: Function,
      value: () => {}
    },
    showTitText: true, //是否显示“操作时间"
    countdown: 0, // 倒计时时间
    urlPath: '../../', //图片路径
    titleText: '', // 右侧展示页面小标题
    isHomePage: false, // 是否是首页
  },

  /**
   * 组件的初始数据
   */
  data: {
    newDate: '', //现在时间
    remainingTime: '', //倒计时剩余时间
    timer: null,
    timer2: null,
    count: 0,
    hasNetworkType: false,
    cusModalObj: {
      visible: false,
      showCloseBtn: true
    },
  },
  didMount() {
    let showTitText = this.props.showTitText
    let remainingTime = showTitText ? '操作时间 ' + this.props.countdown + ' 秒' : this.props.countdown + ' 秒'
    this.setData({
      newDate: getDate(),
      remainingTime,
    })
    this.data.timer2 && clearInterval(this.data.timer2)
    let _this = this
    this.data.timer2 = setInterval(() => {
      // let expirationDate = app.globalData.snDisposition.expirationDate;
      let expirationDate = '2024-12-15';
      // let expirationDate = '2025-01-15';
      let visible = false
      let showCloseBtn = false
      if (expirationDate) {
        const now = new Date();
        const currentStr = new Date(formatTime(now,'YYYY-MM-DD 00:00:00'));
        const expirationDateStr = new Date(formatTime(expirationDate,'YYYY-MM-DD 00:00:00'));
        const expirationDateStr1 = new Date(expirationDateStr.getFullYear(), expirationDateStr.getMonth(), expirationDateStr.getDate() - 30);
        console.log(app.globalData.expirationDateOpenNum)
        if (currentStr.getTime() == expirationDateStr1.getTime() && app.globalData.expirationDateOpenNum == 0 && !this.data.cusModalObj.visible) {
          app.globalData.expirationDateOpenNum = app.globalData.expirationDateOpenNum + 1;
          _this.setData({
            'cusModalObj.visible': true,
            'cusModalObj.showCloseBtn': true,
          })
          console.log(currentStr.getTime() , expirationDateStr1.getTime(),currentStr.getTime() == expirationDateStr1.getTime())
          return
        }
        if(currentStr.getTime() >= expirationDateStr.getTime() && !this.data.cusModalObj.visible){
          console.log(currentStr.getTime() , expirationDateStr.getTime(),currentStr.getTime() >= expirationDateStr.getTime())
          _this.setData({
            'cusModalObj.visible': true,
            'cusModalObj.showCloseBtn': false,
          })
        }
      }
      this.setData({
        newDate: getDate()
      })
      this.getNetworkType()
    }, 1000);
    this.getNetworkType()
    this.startCountdown()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onOk() {
      this.setData({
        'cusModalObj.visible': false
      })
    },
    // 倒计时
    startCountdown() {
      let that = this
      let {
        count
      } = that.data
      count = that.props.countdown
      let showTitText = that.props.showTitText
      if (count && count > 0) {
        that.data.timer && clearInterval(that.data.timer)
        that.data.timer = setInterval(() => {
          count--
          let remainingTime = showTitText ? '操作时间 ' + count + ' 秒' : count + ' 秒'
          that.setData({
            remainingTime
          })
          if (count <= 0) {
            that.data.timer && clearInterval(that.data.timer)
            that.props.onFinish('finish')
            // 倒计时结束，清除定时器
          }
        }, 1000) // 每秒更新一次倒计时数字
      }
    },
    // 网络状态
    getNetworkType() {
      my.getNetworkType({
        success: (res) => {
          if (res.networkType == 'NOTREACHABLE') {
            this.setData({
              hasNetworkType: false,
            })
            my.showToast({
              content: '网络状态异常',
              duration: 2000
            });
          } else {
            this.setData({
              hasNetworkType: true,
            })
          }
        },
        fail: (error) => {
          console.log(error)
          this.setData({
            hasNetworkType: false,
          })
        },
      })
    },
    // 长按logo进入设置
    goto() {
      this.data.timer && clearInterval(this.data.timer)
      getWaterDrop()
      if (this.props.isHomePage) {
        this.clearIntervalAll()
        my.reLaunch({
          url: '/pages/settings/login/index'
        });
      }
    },
    clearIntervalAll() {
      this.data.timer && clearInterval(this.data.timer)
      this.data.timer2 && clearInterval(this.data.timer2)
    },
    onClose() {
      console.log('onclose')
      this.clearIntervalAll();
      my.reLaunch({
        url: '/pages/home/index'
      });
    }
  },
  didUnmount() {
    this.clearIntervalAll();
  },
  onUnload() {
    this.clearIntervalAll();
  },
  observers: {}
})
