<template>
  <view class="update-version-root">
    <image
      class="img"
      src="https://image.jxc4.com/image/70143fcf48aefe74537533f35a0a8153.jpg"
      mode="scaleToFill"
    />
    <view class="name">京西到家商家版管理</view>
    <!-- cid -->
    <view class="cid-number" v-show="isShowCid" @tap.stop="copyInfo(getStorage('cid'), 'CID复制成功')">
      <text class="cid">cid:{{ getStorage('cid') }}</text>
      <text class="copy">复制</text>
    </view>
    <view class="center-root">
      <view class="version-text" @tap="aboutJx">
        <text>关于京西</text>
        <jx-icon icon="gengduo" color="rgb(220, 220, 220)"></jx-icon>
      </view>
      <view class="version" @tap="moreTap">
        <text>当前版本</text>
        <text class="version-num">𝑽 {{ oldVersion }}</text>
      </view>
      <view class="version-text" @tap="popup.open()">
        <text>版本说明</text>
        <jx-icon icon="gengduo" color="rgb(220, 220, 220)"></jx-icon>
      </view>
      <!-- ios不展示 -->
      <view class="version-text" @tap="isUpdate(1)" v-if="isHuawei">
        <text>检查更新</text>
        <view>
          <jx-icon icon="gengduo" color="rgb(220, 220, 220)"></jx-icon>
          <text v-if="isVarsion" class="newVarsion">新</text>
        </view>
      </view>
    </view>
  </view>

  <uni-popup type="center" ref="popup">
    <view class="version-info-root">
      <view class="title">版本说明V{{ oldVersion }}</view>
      <text class="text">{{ versionInfos.description }}</text>
      <view class="btn" @tap="popup.close()">确定</view>
    </view>
  </uni-popup>
</template>

<script lang="ts" setup >
import toast from '@/utils/toast'
import { onShow } from '@dcloudio/uni-app'
import { computed, ref } from 'vue'
import useGlobalFunc from '@/composables/useGlobalFunc'
import { getStorage } from "@/utils/storage";
import useOrderInfo from '@/composables/useOrderInfo'
onShow(() => {
  isUpdate(2)
})
const { watchVersion } = useGlobalFunc()

const oldVersion = ref<string>(uni.getSystemInfoSync().appVersion)

// 华为不展示
const isHuawei = computed(() => {
  let brand = uni.getSystemInfoSync().brand
  if (brand == 'huawei' || brand == 'honor') {
    return false
  } else {
    return true
  }
})

/*************************************************
 * 检查版本
 */
const isVarsion = ref<boolean>(false)
const versionInfos = ref<AnyObject>({})
function isUpdate(type: number) {
  watchVersion((res: boolean, msg: AnyObject) => {
    versionInfos.value = msg
    if (res) {
      if (type == 1) {
        uni.globalAlert({
          data: {
            type: 3,
          },
        })
      } else {
        isVarsion.value = true
      }
    } else {
      isVarsion.value = false
      if (type == 1) toast('已经是最新版')
    }
  })
}

function aboutJx() {
  uni.navigateTo({
    url: '/subPages/agreement/jxcs?url=https://www.jxc4.com/managerApp/jxdjAbout.html',
  })
}

/*************************************************
 * 版本说明
 */
const popup = ref<any>(null)

/**
 * 敲击五次，获取cid号
 */
const clickCount = ref(0)
const timeoutId = ref<any>(null)
const isShowCid = ref(false)
const {
  copyInfo, // 复制内容
} = useOrderInfo()
function moreTap() {
  clickCount.value++; // 每次点击增加次数
  if (clickCount.value === 5) { // 如果点击次数达到5次
        // 触发事件
        console.log('连续点击了5次！');
        isShowCid.value = true
        // 可选：重置点击次数
        clickCount.value = 0;

        // 清除超时
        clearTimeout(timeoutId.value);
  }else{
      // 设置超时，超过一定时间后重置点击计数
      timeoutId.value = setTimeout(() => {
          clickCount.value = 0;
          clearTimeout(timeoutId.value)
      }, 3000); // 假设超时时间为3秒
  }
}
</script>

<style>
page {
  background-color: #f4f5f9;
}
</style>

<style lang="scss" scoped>
.update-version-root {
  display: flex;
  width: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  .img {
    width: 249rpx;
    height: 249rpx;
    border-radius: 40rpx;
    border: 2rpx solid rgb(211, 211, 211);
    margin-top: 70rpx;
  }

  .name {
    margin-top: 20rpx;
    font-size: 40rpx;
    font-weight: bold;
  }

  .center-root {
    background-color: rgb(255, 255, 255);
    width: 100%;
    margin-top: 100rpx;
    border-top: 1rpx solid rgb(231, 231, 231);
    .version {
      display: flex;
      justify-content: space-between;
      padding: 25rpx;
      border-bottom: 1rpx solid rgb(206, 206, 206);
      font-size: 34rpx;
      font-weight: bold;

      .version-num {
        color: rgb(218, 218, 218);
      }
    }

    .version-text {
      position: relative;
      display: flex;
      justify-content: space-between;
      padding: 40rpx 25rpx;
      border-bottom: 1rpx solid rgb(231, 231, 231);
      font-size: 34rpx;
      font-weight: bold;
    }
  }
}

// 发现新版本
.newVarsion {
  position: absolute;
  right: 50rpx;
  display: inline-block;
  background-color: $jx-warring;
  width: 30rpx;
  height: 30rpx;
  border-radius: 50%;
  font-size: 24rpx;
  line-height: 30rpx;
  text-align: center;
  color: #fff;
  padding: 5rpx;
  animation: bill-new-animation 0.5s ease-in-out infinite alternate;
}

@keyframes bill-new-animation {
  0% {
    transform: scale(0.95);
  }

  100% {
    transform: scale(1.05);
  }
}

.version-info-root {
  box-sizing: border-box;
  width: 500rpx;
  padding: 20rpx 30rpx;
  background-color: #fff;
  border-radius: 15rpx;

  .title {
    text-align: center;
    color: #929292;
  }

  .text {
    display: block;
    margin-top: 20rpx;
    text-decoration: none;
  }

  .btn {
    margin-top: 30rpx;
    border-radius: 10rpx;
    padding: 20rpx;
    text-align: center;
    color: #fff;
    background-color: $jx-primary;
  }
}

.cid-number {
  display: flex;
  justify-content: space-between;
  align-items: center;
  white-space: nowrap;
  padding: 15rpx;
  color: #999999;

  .copy {
    margin-left: 20rpx;
    font-size: 26rpx;
    padding: 4rpx 12rpx;
    border-radius: 5rpx;
    border: 2rpx solid #999999;
    color: #999999;
  }
}
</style>