<template>
  <view class="page-content">
    <mx-nav-bar title="关于" />
    <view class="fx-col fx-cen-cen py-80">
      <ie-image src="/static/logo.png" custom-class="w-160 h-160" mode="widthFix" />
    </view>
    <uv-cell-group class="bg-white !flex-none">
      <uv-cell v-for="s in settings" v-bind="s" @click="s.handler()" />
    </uv-cell-group>
  </view>
</template>

<script setup>
import { onMounted, reactive } from 'vue'
import { useTransfer } from "@/hooks/useTransfer";
import { useCacheStore } from "@/hooks/useCacheStore";
import { useEnvStore } from "@/hooks/useEnvStore";
import { confirmAsync } from "@/utils/uni-helper";
import { useUserStore } from "@/hooks/useUserStore";
import { sizeFormat } from "@/utils";
import { useUserStore as newUserStore } from '@/store/userStore';

const { transferToProtocolUser, transferToProtocolPrivacy, transferToIndex, cleanAllTransferCacheData } = useTransfer()
const { getSize: getCacheSize, gcCache } = useCacheStore()
const { systemInfo } = useEnvStore()
const { LogoutPhysical, GetInfo, isLogin } = useUserStore()
const { orgInfo } = newUserStore()
const cacheSize = ref(0)
const settings = computed(() => [
  {
    title: '服务协议',
    icon: '/static/personal/icon-yonghuxieyi@2x.png',
    isLink: true,
    handler: () => transferToProtocolUser()
  },
  {
    title: '隐私政策',
    icon: '/static/personal/icon-yinxixieyi@2x.png',
    isLink: true,
    handler: () => transferToProtocolPrivacy()
  },
  {
    title: '清除缓存', //icon_cache
    icon: '/static/personal/icon_cache.png',
    rightIcon: 'reload',
    isLink: true,
    value: cacheSize.value,
    handler: () => {
      cleanAllTransferCacheData() // 清理页面大对象
      if (isLogin.value) {
        GetInfo() // GetInfo也会清除缓存，借机也更新一下用户信息
      }
      calculateCacheSize()
    }
  },
  {
    title: '当前版本',
    icon: '/static/personal/icon-dangqianbanben@2x.png',
    value: systemInfo.value.appVersion,
    handler: () => {
    }
  },
  {
    title: '注销账号',
    icon: 'trash',
    isLink: true,
    handler: async () => {
      if (!isLogin.value) {
        const msg = '当前未登录'
        await confirmAsync(msg)
        return;
      }
      const msg = '是否注销账号，注销后卡号将不能再注册使用系统且永久失效。请谨慎操作！'
      await confirmAsync(msg)
      await LogoutPhysical()
      transferToIndex()
    }
  },
  {
    title: 'APP备案号',
    icon: 'empty-permission',
    value: '湘ICP备18012964号-8A',
    handler: () => {
    }
  },
  {
    title: '客服电话',
    icon: 'server-man',
    value: orgInfo.contactPhone,
    handler: () => {
    }
  }
])

const calculateCacheSize = function () {
  gcCache()
  const size = getCacheSize()
  cacheSize.value = sizeFormat(size)
}
onMounted(() => {
  calculateCacheSize()
});
</script>

<style scoped>
::v-deep .uv-cell__body {
  font-size: 16px;
  padding: 12px 15px;
}
</style>