<script setup lang="ts">
import { logout } from '@/api/login'
import { pageUrl } from '@/utils/constants'
import { useMemberStore, useSettingStore } from '@/stores'

const memberStore = useMemberStore()
const settngStore = useSettingStore()

//退出登录
const onLogout = () => {
  uni.showModal({
    content: '确定要退出登录吗',
    cancelText: '保持登录',
    confirmText: '确定退出',
    success: (res) => {
      if (res.confirm) {
        logout().then(() => {
          //清空信息 回首页
          useMemberStore().clearProfile()
          uni.showToast({
            title: '退出成功',
            icon: 'success',
          })
          setTimeout(() => {
            uni.reLaunch({ url: `${pageUrl['index']}` })
          }, 1500)
        })
      }
    },
  })
}

// 跳转富文本
const toRichTextPage = (type: string) => {
  const agreements = settngStore.data.agreement

  const content = encodeURIComponent(agreements[type as keyof typeof agreements])

  uni.navigateTo({
    url: `${pageUrl['rich-text']}?content=${content}`,
  })
}
</script>

<template>
  <view class="viewport">
    <view class="list">
      <button hover-class="none" class="item arrow content">
        当前版本
        <text class="content-text">{{ settngStore.data.version }}</text>
      </button>
      <button hover-class="none" class="item arrow" @tap="toRichTextPage('user_agreement')">
        用户协议
      </button>
      <button hover-class="none" class="item arrow" @tap="toRichTextPage('privacy_agreement')">
        隐私政策
      </button>
      <button hover-class="none" class="item arrow" @tap="toRichTextPage('about_us_agreement')">
        关于我们
      </button>
      <!-- #ifdef MP-WEIXIN -->
      <button hover-class="none" class="item arrow" open-type="openSetting">授权管理</button>
      <button hover-class="none" class="item arrow" open-type="feedback">问题反馈</button>
      <button hover-class="none" class="item arrow" open-type="contact">联系我们</button>
      <!-- #endif -->
    </view>
    <!-- 操作按钮 -->
    <view class="action" v-if="memberStore.profile">
      <view class="button" @tap="onLogout">退出登录</view>
    </view>
  </view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f8;
}

.viewport {
  padding: 20rpx;
}

/* 列表 */
.list {
  padding: 0 20rpx;
  background-color: #fff;
  margin-bottom: 20rpx;
  border-radius: 10rpx;
  .item {
    line-height: 90rpx;
    padding-left: 10rpx;
    font-size: 30rpx;
    color: #333;
    border-top: 1rpx solid #ddd;
    position: relative;
    text-align: left;
    border-radius: 0;
    background-color: #fff;
    &::after {
      width: auto;
      height: auto;
      left: auto;
      border: none;
    }
    &:first-child {
      border: none;
    }
    &::after {
      right: 5rpx;
    }
  }
  .content {
    display: flex;
    justify-content: space-between;

    .content-text {
      margin-right: 25rpx;
      color: #aaa;
    }
  }

  .arrow::after {
    content: '\e6c2';
    position: absolute;
    top: 50%;
    color: #ccc;
    font-family: 'iconfont' !important;
    font-size: 32rpx;
    transform: translateY(-50%);
  }
}

/* 操作按钮 */
.action {
  text-align: center;
  line-height: 90rpx;
  margin-top: 40rpx;
  font-size: 32rpx;
  color: #333;
  .button {
    background-color: #fff;
    margin-bottom: 20rpx;
    border-radius: 10rpx;
  }
}
</style>
