// 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(() => {
      _this.checkExpiryDate()
      this.setData({
        newDate: getDate()
      })
      this.getNetworkType()
    }, 1000);
    this.getNetworkType()
    this.startCountdown()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    checkExpiryDate() {
      let expiryDate = app.globalData.snDisposition.expirationDate;
      // let expiryDate = '2025-01-25';
      // let expiryDate = '2024-12-26';
      if (expiryDate && expiryDate !== '') {
        console.log(`弹窗次数：${app.globalData.expirationDateOpenNum}`);
        let { cusModalObj } = this.data
        const now = new Date();
        // 计算当前日期
        const currentDate = new Date(formatTime(now,'YYYY-MM-DD 00:00:00'));
        // 计算到期日期
        const expiry = new Date(formatTime(expiryDate,'YYYY-MM-DD 00:00:00'));
        // 计算到期日期30天前
        const expiryThirtyDate = new Date(expiry.getFullYear(), expiry.getMonth(), expiry.getDate() - 30);

        // 检查是否在30天之前
        if (app.globalData.expirationDateOpenNum == 0 && !cusModalObj.visible && currentDate.getTime() == expiryThirtyDate.getTime()) {
          console.log(`您的FKJ账户将在 30 天后到期：${expiry.toLocaleDateString()}`);
          app.globalData.expirationDateOpenNum = app.globalData.expirationDateOpenNum + 1;
          this.setData({
            'cusModalObj.visible': true,
            'cusModalObj.showCloseBtn': true,
          })
          return;
        }

        // 如果当前日期超过到期日期
        if (!cusModalObj.visible && currentDate.getTime() >= expiry.getTime()) {
          console.log(`您的FKJ账户已到期。${expiry.toLocaleDateString()}`);
          this.setData({
            'cusModalObj.visible': true,
            'cusModalObj.showCloseBtn': false,
          })
        }
      }
    },
    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: {}
})
