<template>
  <view class="user-center-page">
    <!-- 顶部用户信息栏 -->
    <view class="user-info-header">
      <!-- 渐变背景层 -->
      <view class="header-bg"></view>
      <!-- 用户信息主体 -->
      <view class="user-info-wrap">
        <!-- 头像 -->
        <up-avatar
            :src="'/static/imgs/default-avatar.png'"
            size="100rpx"
            shape="circle"
            class="user-avatar"
        ></up-avatar>

        <!-- 用户名+手机号+角色 -->
        <view class="user-info-content">
          <view class="user-name">{{ userInfo.username || '未登录' }}</view>
          <view class="user-phone">{{ userInfo.nickname || '--------' }}</view>
        </view>

        <!-- 登录/编辑入口 -->
        <up-button
            v-if="!userStore.isLogin"
            type="primary"
            size="mini"
            @click="handleLogin"
        >
          立即登录
        </up-button>
      </view>
    </view>


    <!-- 退出登录按钮 (仅登录状态显示) -->
    <view v-if="userStore.isLogin" class="logout-btn-wrap ">
      <up-button
          type="primary"
          size="large"
          @click="confirmLogout"
      >
        退出登录
      </up-button>
    </view>


  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useUserStore } from '@/pinia/user' // 匹配你的 Pinia 路径
import { onShow } from '@dcloudio/uni-app'

// 初始化Pinia仓库
const userStore = useUserStore()
const userInfo = userStore.userInfo.user
import { getDictList, getDictLabel, getDictSimpleList } from '@/common/utils/dict';
// 示例1：获取ai_image_status的完整字典列表
const aiImageStatusList = ref([]);
// 示例2：获取ai_image_status中value=10的label
const aiImageStatus10Label = ref('');

// 获取ai_image_status的完整列表（包含所有字段）
aiImageStatusList.value = getDictList('ai_image_status');
// 获取ai_image_status中value=10的label
aiImageStatus10Label.value = getDictLabel('ai_image_status', 10);
console.log('13')
console.log(aiImageStatusList.value)
console.log(aiImageStatus10Label.value)

// 可选：获取仅包含value和label的简化列表
const simpleList = getDictSimpleList('ai_image_status');
console.log('简化列表：', simpleList);
console.log('24')




// 页面显示时检查登录状态（同步缓存）
onShow(() => {
  // 触发登录状态检查，确保缓存和Pinia同步
  userStore.checkLogin()
})

// 处理登录（跳转到登录页）
const handleLogin = () => {
  navigateTo({
    url: '/pages/login/index' // 替换为你的实际登录页路径
  })
}



// 确认退出登录
const confirmLogout = async () => {
  try {
    // 调用Pinia自带的logout方法（你的仓库已实现清空状态+跳转）
    await userStore.logout()

  } catch (error) {
    console.error('退出登录失败：', error)
    uni.showToast({
      title: '退出登录失败，请重试',
      type: 'error'
    })

  }
}

</script>

<script lang="ts">
export default {
  name: 'UserCenter'
}
</script>

<style lang="scss" scoped>
.user-center-page {
  min-height: 100vh;
  background-color: #f5f7fa;
}

// 顶部用户信息栏
.user-info-header {
  position: relative;
  padding-bottom: 40rpx;

  .header-bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 320rpx;
    background: linear-gradient(135deg, #409eff, #67c23a);
    border-radius: 0 0 20rpx 20rpx;
    z-index: 1;
  }

  .user-info-wrap {
    position: relative;
    z-index: 2;
    display: flex;
    align-items: center;
    padding: 60rpx 30rpx 0;

    .user-avatar {
      border: 4rpx solid #ffffff;
      box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
    }

    .user-info-content {
      margin-left: 30rpx;
      flex: 1;

      .user-name {
        font-size: 32rpx;
        font-weight: 600;
        color: #ffffff;
        margin-bottom: 10rpx;
        text-shadow: 0 1rpx 3rpx rgba(0, 0, 0, 0.1);
      }

      .user-phone {
        font-size: 26rpx;
        color: rgba(255, 255, 255, 0.9);
        margin-bottom: 10rpx;
      }

      .user-role-tag {
        background-color: rgba(255, 255, 255, 0.2);
        color: #ffffff;
        border: none;
      }
    }
  }
}


// 退出登录按钮
.logout-btn-wrap {
  margin: 200rpx 20rpx 20rpx;
  padding: 20rpx;


}

</style>