<template>
  <view class="container">
    <!-- 用户信息卡片 -->
    <view class="user-card">
      <view class="avatar">
        <image :src="userInfo.avatar || '/static/images/default-avatar.png'" mode="aspectFill"></image>
      </view>
      <view class="user-info">
        <text class="nickname">{{ userInfo.nickName || '未登录' }}</text>
        <text class="phone">{{ userInfo.phone || '未绑定手机号' }}</text>
      </view>
    </view>

    <!-- 余额卡片 -->
    <view class="balance-card">
      <view class="balance-title">余额</view>
      <view class="balance-amount">￥{{ userInfo.balanceAmount || '0.00' }}</view>
      <view class="balance-desc">可用于租借设备</view>
    </view>

    <!-- 功能菜单 -->
    <view class="menu-list">
      <view class="menu-item" @click="navigateTo('/pages/order/list')">
        <text class="menu-icon">📋</text>
        <text class="menu-text">我的订单</text>
        <text class="menu-arrow">></text>
      </view>
      <view class="menu-item" @click="navigateTo('/pages/user/feedback')">
        <text class="menu-icon">💬</text>
        <text class="menu-text">意见反馈</text>
        <text class="menu-arrow">></text>
      </view>
      <view class="menu-item" @click="navigateTo('/pages/user/about')">
        <text class="menu-icon">ℹ️</text>
        <text class="menu-text">关于我们</text>
        <text class="menu-arrow">></text>
      </view>
    </view>

    <!-- 退出登录按钮 -->
    <view class="logout-btn" @click="handleLogout" v-if="isLogin">
      <text>退出登录</text>
    </view>
  </view>
</template>

<script>
import { getUserInfo } from '@/api/user'
import { URL } from '@/config/url'

export default {
  data() {
    return {
      userInfo: {},
      isLogin: false
    }
  },
  onShow() {
    this.checkLoginStatus()
    this.loadUserInfo()
  },
  methods: {
    async loadUserInfo() {
      try {
        const res = await getUserInfo()
        if (res.code === 200) {
          this.userInfo = res.data
          this.isLogin = true
        } else {
          this.isLogin = false
          uni.showToast({
            title: '获取用户信息失败',
            icon: 'none'
          })
        }
      } catch (error) {
        console.error('加载用户信息失败:', error)
        this.isLogin = false
      }
    },
    checkLoginStatus() {
      const token = uni.getStorageSync('token')
      this.isLogin = !!token
      if (!this.isLogin) {
        uni.redirectTo({
          url: '/pages/login/index'
        })
      }
    },
    navigateTo(url) {
      uni.navigateTo({ url })
    },
    handleLogout() {
      uni.showModal({
        title: '提示',
        content: '确定要退出登录吗？',
        success: (res) => {
          if (res.confirm) {
            uni.removeStorageSync('token')
            uni.removeStorageSync('userInfo')
            this.isLogin = false
            uni.redirectTo({
              url: '/pages/login/index'
            })
          }
        }
      })
    }
  }
}
</script>

<style>
.container {
  padding: 20rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.user-card {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  display: flex;
  align-items: center;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.avatar {
  width: 120rpx;
  height: 120rpx;
  border-radius: 60rpx;
  overflow: hidden;
  margin-right: 30rpx;
}

.avatar image {
  width: 100%;
  height: 100%;
}

.user-info {
  flex: 1;
}

.nickname {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 10rpx;
  display: block;
}

.phone {
  font-size: 28rpx;
  color: #666;
}

.balance-card {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.balance-title {
  font-size: 28rpx;
  color: #666;
  margin-bottom: 20rpx;
}

.balance-amount {
  font-size: 48rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 10rpx;
}

.balance-desc {
  font-size: 24rpx;
  color: #999;
}

.menu-list {
  background-color: #fff;
  border-radius: 16rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #f5f5f5;
}

.menu-item:last-child {
  border-bottom: none;
}

.menu-icon {
  font-size: 36rpx;
  margin-right: 20rpx;
}

.menu-text {
  flex: 1;
  font-size: 28rpx;
  color: #333;
}

.menu-arrow {
  font-size: 28rpx;
  color: #999;
}

.logout-btn {
  margin-top: 40rpx;
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  text-align: center;
  color: #ff4d4f;
  font-size: 28rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}
</style> 