<template>
  <view class="container">
    <uni-section type="line" title="请求地址和租户ID配置" @click="handleClick"> </uni-section>
    <uni-forms label-position="top" label-width="100">
      <uni-forms-item label="系统请求地址">
        <uni-easyinput v-model="baseUrl" placeholder="请输入系统请求地址"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item v-if="showContent" label="租户ID">
        <uni-easyinput v-model="tenantId" placeholder="请输入租户ID"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item v-if="showContent" label="app更新地址">
        <uni-easyinput v-model="upgradeBaseUrl" placeholder="请输入"></uni-easyinput>
      </uni-forms-item>
    </uni-forms>
    <u-button type="primary" @click="submitForm">保存</u-button>
    <u-button v-if="showContent" type="primary" @click="clearCache">清除缓存</u-button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      baseUrl: '',
      tenantId: '',
      upgradeBaseUrl: '',
      clickCount: 0, // 初始化点击次数为0
      threshold: 5, //设置点击次数的阈值
      thresholdTime: 2, // 设置连续点击的时间阈值（秒）
      timer: null
    }
  },
  computed: {
    showContent() {
      return this.clickCount >= this.threshold
    }
  },

  mounted() {
    this.baseUrl = uni.getStorageSync('base_url')
    this.tenantId = uni.getStorageSync('tenant_id')
    this.upgradeBaseUrl = uni.getStorageSync('upgradeBaseUrl')
  },
  methods: {
    clearCache() {
      uni.showModal({
        title: '提示',
        content: '确定要清空缓存？',
        success: function (res) {
          if (res.confirm) {
            try {
              uni.clearStorageSync()
              plus.runtime.restart()
            } catch (e) {}
          }
        }
      })
    },
    submitForm() {
      if (!this.baseUrl) {
        uni.showToast({
          title: '请输入地址和租户',
          icon: 'none'
        })
        return
      }
      uni.setStorageSync('base_url', this.baseUrl)
      uni.setStorageSync('tenant_id', this.tenantId)
      uni.setStorageSync('upgradeBaseUrl', this.upgradeBaseUrl)
      uni.showToast({
        title: '保存成功',
        icon: 'none'
      })
      uni.navigateBack()
    },
    handleClick() {
      // 如果达到阈值，显示内容并重置计数
      if (this.showContent) {
        clearTimeout(this.timer)
        return
      }
      // 增加点击次数
      this.clickCount++
      uni.showToast({
        title: `再点击${this.threshold - this.clickCount}次可显示内容`,
        icon: 'none',
        duration: 1000
      })

      clearTimeout(this.timer)
      // 清除之前的定时器（如果有），设置新的定时器
      this.timer = setTimeout(() => {
        this.resetClickCount()
      }, this.thresholdTime * 1000)
    },
    resetClickCount() {
      if (!this.showContent) {
        this.clickCount = 0
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 30px;
}
</style>
