<route lang="json5" type="page">
{
  layout: 'default',
  style: {
    navigationBarTitleText: '%setting.title%',
    navigationBarBackgroundColor: '#fff',
  },
}
</route>

<script lang="ts" setup>
import { t } from '@/locale'
import i18n from '@/locale/index'
import { useUserStore } from '@/store'
import { toPage } from '@/utils/page'

defineOptions({
  name: 'Setting', // 设置
})
const userStore = useUserStore()
const columns = computed(() => {
  return [
    { label: 'English', value: 'en' },
    { label: 'বাংলা', value: 'bn' },
  ]
})
function normalizeLocale(locale: string | undefined | null) {
  const supportedLocales = ['en', 'zh-Hans', 'bn']
  if (supportedLocales.includes(locale as any))
    return locale as any
  return 'bn'
}

const language = ref(normalizeLocale(uni.getStorageSync('app_locale') || uni.getLocale()))
function changeLanguage(data: any) {
  console.log(data)
  // 下面2句缺一不可！！！
  uni.setLocale(data.value)
  uni.setStorageSync('app_locale', data.value)
  i18n.global.locale = data.value
}
function logout() {
  userStore.removeUserInfo()
  toPage({ url: '/pages/login/login', isReLaunch: true })
}
const appInfo = ref<any>({
  version: import.meta.env.VITE_APP_VERSION || '1.0.0',
})
function getAppInfo() {
  // #ifdef APP-PLUS
  plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
    console.log('云打包，安心打包方式获取设备信息:')
    console.log(wgtinfo)
    appInfo.value = wgtinfo
  })
  // #endif
}
onLoad(() => {
  getAppInfo()
})
</script>

<template>
  <view class="py-20rpx">
    <wd-cell-group custom-class="mb-20rpx" border>
      <wd-cell :title="$t('setting.changePassword')" custom-title-class="text-32rpx" is-link @click="toPage({ url: '/pages/forgotPassword/forgotPassword' })" />
      <wd-picker v-model="language" :columns="columns" use-default-slot @confirm="changeLanguage">
        <wd-cell :title="$t('setting.language')" custom-title-class="text-32rpx" custom-value-class="text-#838383! text-32rpx!" :value="columns.find(item => item.value === language)?.label" is-link />
      </wd-picker>
      <wd-cell :title="$t('setting.policies')" custom-title-class="text-32rpx" is-link @click="toPage({ url: '/pages/webLink/webLink', params: { title: t('setting.policies'), link: 'https://www.aisoco.net/privacy.html' } })" />
      <wd-cell :title="$t('setting.termsOfService')" custom-title-class="text-32rpx" is-link @click="toPage({ url: '/pages/webLink/webLink', params: { title: t('setting.termsOfService'), link: 'https://www.aisoco.net/terms.html' } })" />
      <wd-cell :title="$t('setting.refund')" custom-title-class="text-32rpx" title-width="400rpx" is-link @click="toPage({ url: '/pages/webLink/webLink', params: { title: t('setting.refund'), link: 'https://www.aisoco.net/refund.html' } })" />
    </wd-cell-group>

    <!-- Logout -->
    <view class="mb-20rpx bg-white p-24rpx text-center text-32rpx text-[var(--wot-color-theme)]" @click="logout">
      {{ $t('setting.logout') }}
    </view>
    <view class="text-center text-32rpx text-#909090">
      {{ t('setting.version', [appInfo.version]) }}
    </view>
  </view>
</template>

<style lang="scss" scoped>
//
</style>
