<route lang="json5" type="page">
{
  layout: 'default',
  style: {
    navigationBarTitleText: '我的',
  },
}
</route>

<template>
  <view class="main overflow-hidden pt-2 px4" :style="{ marginTop: safeAreaInsets?.top + 'px' }">
    <view class="head">
      <wd-img
        v-if="userInfo.avatar"
        custom-class="img-btn"
        :width="40"
        :height="40"
        :src="userInfo.avatar"
      />
      <view class="head-name">{{ userInfo.name }}</view>
    </view>
    <view class="main-content mt6">
      <view class="content-item line-b" @click="toAddUserHub">
        <view>添加打印助手</view>
        <wd-icon name="arrow-right" size="20px" color="#808080"></wd-icon>
      </view>
      <view v-if="isLogined" class="content-item" @click="handleLogout">
        <view>退出登录</view>
        <wd-icon name="arrow-right" size="20px" color="#808080"></wd-icon>
      </view>
      <view v-else class="content-item" @click="toLogin">
        <view>前往登录</view>
        <wd-icon name="arrow-right" size="20px" color="#808080"></wd-icon>
      </view>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { logout } from '@/service/api/index'
import { useUserStore } from '@/store'
import { useToast } from 'wot-design-uni'

// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()
const userInfo = reactive({
  avatar: '',
  name: '',
})
const toast = useToast()
const isLogined = computed(() => !!useUserStore().token)

function toAddUserHub() {
  if (isLogined.value) {
    uni.navigateTo({
      url: `/pages/assistant/detail?type=add`,
    })
  } else {
    toast.warning('请先前往登录')
  }
}

function handleLogout() {
  // logout().then((res) => {
  //   if (res.code === 0) {
  //     useUserStore().setToken('')
  //     useUserStore().setAvatar('')
  //     useUserStore().setUserInfo({})
  //     toast.success('退出登录成功')
  //     toLogin()
  //   }
  // })
  useUserStore().clearUserInfo()
  toast.success('退出登录成功')
  toLogin()
}

function toLogin() {
  uni.reLaunch({
    url: `/pages/login/index?isLogout=1`,
  })
}

function initFn() {
  // userInfo.name = useUserStore().userInfo.name
  // userInfo.avatar = useUserStore().userInfo.avatar
  userInfo.name = "微信用户"
  userInfo.avatar = "/static/images/avatar.png"
}

onLoad(() => {
  initFn()
})
</script>

<style lang="scss" scoped>
.head {
  display: flex;
  align-items: center;
  .head-name {
    @apply pl-3;
  }
}
.main-content {
  padding: 20rpx;
  background-color: white;
  border-radius: 16rpx;
}
.content-item {
  display: flex;
  align-items: start;
  justify-content: space-between;
  padding: 30rpx 0;
}
.line-b {
  border-bottom: 1px solid #eee;
}
:deep(.head .wd-upload__evoke) {
  width: 80rpx;
  height: 80rpx;
  margin-bottom: 0;
  border-radius: 100%;
}
:deep(.wd-upload) {
  display: flex;
}
:deep(.wd-upload__evoke-num) {
  display: none;
}
:deep(.wd-icon) {
  font-size: 50rpx;
}
:deep(.img-btn .wd-img__image) {
  width: 80rpx;
  height: 80rpx;
  border-radius: 40rpx;
}
:deep(.wd-upload__preview) {
  display: none !important;
}
:deep(.gray-btn.wd-button) {
  width: 80rpx;
  height: 80rpx;
  background-color: #ddd;
  border-radius: 40rpx;
}
:deep(.form-ipt) {
  padding-right: 0 !important;
  padding-left: 0 !important;
}
</style>

