<template>
  <view class="user-container">
    <!-- 顶部背景区域 -->
    <view class="header-bg" @click="goLogin">
      <!-- 用户信息区域 -->
      <view class="user-info">
        <view class="avatar-section">
          <image class="avatar" :src="userInfo.headImage || '/static/默认头像.png'" mode="aspectFill"></image>
        </view>
        <view class="info-section">
          <text class="username">{{userInfo.nickName}}</text>
          <text class="user-id">ID:{{userInfo.id}}</text>
        </view>
      </view>
    </view>
    
    <!-- 常用功能模块 -->
    <view class="function-card">
      <view class="card-title">
        <text class="title-text" >常用功能</text>
      </view>
      
      <view class="function-list">
        <!-- 基本信息 -->
        <view class="function-item" @click="goToBasicInfo">
          <view class="item-left">
            <uv-icon name="account" size="28" color="#C70019"></uv-icon>
            <text class="item-text">基本信息</text>
          </view>
          <uv-icon name="arrow-right" size="24" color="#000"></uv-icon>
        </view>
        
        <!-- 用户协议和隐私政策 -->
        <view class="function-item" @click="showPrivacyPolicy">
          <view class="item-left">
            <uv-icon name="file-text" size="28" color="#C70019"></uv-icon>
            <text class="item-text">用户协议和隐私政策</text>
          </view>
          <uv-icon name="arrow-right" size="24" color="#000"></uv-icon>
        </view>
        
        
        <!-- 退出登录 -->
        <view class="function-item" @click="logout" v-if="isLogin">
          <view class="item-left">
            <uv-icon name="minus-circle" size="24" color="#C70019"></uv-icon>
            <text class="item-text">退出登录</text>
          </view>
          <uv-icon name="arrow-right" size="24" color="#000"></uv-icon>
        </view>
      </view>
    </view>
    
    <!-- 用户协议和隐私政策弹窗 -->
    <uv-modal ref="privacyModal" title="用户协议和隐私政策" :show-cancel-button="false" confirm-text="我知道了" confirm-color="#C70019"> 
      <view class="privacy-content">
        <!-- 如果是富文本 -->
        <rich-text :nodes="configParamTextarea('app_agreement')">
        </rich-text>
      </view>
    </uv-modal>
  </view>
</template>

<script>

export default {
  data() {
    return {
      userInfo: {
        headImage: '/static/默认头像.png',
        nickName: '请先登录',
        id: 'XXXXX'
      },
      isLogin: uni.getStorageSync('token')
    }
  },
  computed: {
    // 检查是否已登录
    
  },
  onShow() {
    this.isLogin = uni.getStorageSync('token')
    // 如果没登陆过 就别登了
    if (uni.getStorageSync('token')) {
      
      this.getUserInfo();
    }
  },
  methods: {
    // 跳转登录页面
    goLogin() {
      uni.navigateTo({
        url: '/subPages/login/login'
      })
    },

    // 跳转到基本信息页面
    goToBasicInfo() {
      uni.navigateTo({
        url: '/subPages/user/profile'
      })
    },
    
    // 获取用户信息
    async getUserInfo() {
      const res = await this.$api.user.queryUser();
      if (res.code === 200) {
        this.userInfo = res.result;
      }
    },
    
    // 显示隐私政策弹窗
    showPrivacyPolicy() {
      this.$refs.privacyModal.open()
    },
    
    // 退出登录
    logout() {
      uni.showModal({
        title: '提示',
        content: '确定要退出登录吗？',
        confirmColor: '#C70019',
        success: (res) => {
          if (res.confirm) {
            // 清除本地存储的用户信息
            uni.removeStorageSync('token')
            
            // 重置用户信息
            this.userInfo = {
              headImage: '/static/默认头像.png',
              nickName: '请先登录',
              id: 'XXXXX'
            }
            
            this.isLogin = false;

            // 显示退出成功提示
            uni.showToast({
              title: '退出成功',
              icon: 'success'
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.privacy-content {
  padding: 20rpx;
  
  .privacy-section {
    margin-bottom: 30rpx;
    
    .section-title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333;
      display: block;
      margin-bottom: 20rpx;
    }
    
    .section-text {
      font-size: 28rpx;
      color: #666;
      line-height: 1.6;
      display: block;
      margin-bottom: 15rpx;
    }
  }
}

.user-container {
  
  min-height: 100vh;
  background: #f5f5f5;
  position: relative;
}

.header-bg {
  height: 513rpx;
  background: linear-gradient(156deg, #f56073 15%, #c70019 61%, #ffacb6 100%);
  position: relative;
  padding: 0 46rpx;
  
  .user-info {
    display: flex;
    align-items: center;
    padding-top: 177rpx;
    
    .avatar-section {
      margin-right: 22rpx;
      
      .avatar {
        width: 120rpx;
        height: 120rpx;
        border-radius: 60rpx;
        // border: 4rpx solid rgba(255, 255, 255, 0.3);
      }
    }
    
    .info-section {
      flex: 1;
      
      .username {
        display: block;
        font-size: 30rpx;
        // font-weight: bold;
        color: #fff;
        margin-bottom: 12rpx;
      }
      
      .user-id {
        font-size: 28rpx;
        color: #fff;
      }
    }
  }
}

.function-card {
  width: 88%;
  height: 709rpx;
  background: #ffffff;
  border-radius: 16rpx;
  margin: -152rpx auto 0;
  position: relative;
  z-index: 2;
  padding: 57rpx 28rpx;
  
  .card-title {
    // margin-bottom: 40rpx;
    
    .title-text {
      font-size: 32rpx;
      font-weight: bold;
      color: $primary-text-color;
    }
  }
  
  .function-list {
    .function-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-top:44rpx ;
      border-bottom: 1rpx solid #f0f0f0;
      
      .item-left {
        display: flex;
        align-items: center;
        
        .item-text {
          font-size: 28rpx;
          color: $primary-text-color;
          margin-left: 14rpx;
        }
      }
    }
  }
}
</style>