<template>
  <view class="profile-container">
    <!-- 个人信息表单 -->
    <view class="form-container">
      <view class="form-title">个人信息</view>
      
      <!-- 昵称 -->
      <view class="form-item">
        <view class="label">
          <text class="required">*</text>
          <text>昵称</text>
        </view>
        <uv-input 
          v-model="userInfo.name" 
          placeholder="请输入昵称"
          type="nickname"
          :customStyle="inputStyle"
          border="bottom"
        ></uv-input>
      </view>
      
      <!-- 电话 -->
      <view class="form-item">
        <view class="label">
          <text class="required">*</text>
          <text>电话</text>
        </view>
        <uv-input 
          v-model="userInfo.phone" 
          placeholder="请输入手机号"
          :customStyle="inputStyle"
          border="bottom"
          type="number"
        ></uv-input>
      </view>
      
      <!-- 头像 -->
      <view class="form-item">
        <view class="label">
          <text class="required">*</text>
          <text>头像</text>
        </view>
        <button class="avatar-container" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
          <view 
            v-if="userInfo.avatar === 'undefined'"
            class="avatar-image"
          >
              <uv-icon name="camera" size="40" color="white"></uv-icon>
          </view>
          <image 
            v-else
            :src="userInfo.avatar || '/static/默认头像.png'" 
            class="avatar-image"
            mode="aspectFill"
          ></image>
          <!-- 遮罩层 -->
          <view class="avatar-mask" :class="{ 'fade-out': showMask }" v-if="showMask">
            <text>点击上传头像</text>
          </view>
        </button>
      </view>
    </view>
    
    <!-- 固定底部保存按钮 -->
    <view class="save-button-container">
      <uv-button 
        @click="saveProfile"
        :customStyle="saveButtonStyle"
        shape="circle"
      >
        保存
      </uv-button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      userInfo: {
        avatar: 'undefined',
        name: '', 
        phone: ''
      },
      showMask: true,
      saveButtonStyle: {
        backgroundColor: '#06DADC',
        borderRadius: '41rpx',
        height: '94rpx',
        width: '594rpx',
        border: 'none',
        color: '#fff',
        fontSize: '32rpx',
        fontWeight: '500'
      },
      inputStyle: {
        backgroundColor: '#fff',
        borderRadius: '12rpx',
        padding: '0 -20rpx',
        fontSize: '28rpx'
      }
    }
  },
  methods: {
    // // 微信昵称审核回调
    // onNicknameReview(e) {
    //   console.log('微信昵称审核回调', e);
    //   if (e.detail.pass) {
    //     // 审核通过，使用微信昵称
    //     this.userInfo.name = e.detail.value;
    //     console.log('微信昵称审核通过:', e.detail.value);
    //     uni.showToast({
    //       title: '昵称获取成功',
    //       icon: 'success'
    //     });
    //   } else {
    //     // 审核不通过
    //     console.log('微信昵称审核不通过');
    //     uni.showToast({
    //       title: '昵称包含敏感词，请重新输入',
    //       icon: 'none'
    //     });
    //   }
    // },
    
    
    // 选择头像并上传到OSS
    async onChooseAvatar(e) {
      console.log('选择头像回调', e);
      if (e.detail.avatarUrl) {
        try {
          // 显示上传中提示
          uni.showLoading({ title: '上传头像中...' });
          
          // 构造文件对象
          const file = {
            path: e.detail.avatarUrl,
            tempFilePath: e.detail.avatarUrl
          };
          
          // 上传到OSS
          const uploadResult = await this.$utils.uploadImage(file);
          
          uni.hideLoading();
          
          if (uploadResult.success) {
            // 上传成功，更新头像URL
            this.userInfo.avatar = uploadResult.url;
            console.log('头像上传成功', uploadResult.url);
            uni.showToast({
              title: '头像上传成功',
              icon: 'success'
            });
          } else {

          }
        } catch (error) {
          uni.hideLoading();
          console.error('头像上传异常:', error);
          // 异常情况下使用本地头像
          this.userInfo.avatar = e.detail.avatarUrl;
          uni.showToast({
            title: '头像处理异常，使用本地头像',
            icon: 'none'
          });
        }
      } else {
        uni.showToast({
          title: '头像选择失败',
          icon: 'none'
        });
      }
    },
    
    // 保存资料
    async saveProfile() {
      if (!this.userInfo.name.trim()) {
        uni.showToast({
          title: '请输入昵称',
          icon: 'none'
        })
        return
      }
      
      if (!this.userInfo.phone.trim()) {
        uni.showToast({
          title: '请输入手机号',
          icon: 'none'
        })
        return
      }
      
      // 简单的手机号验证
      const phoneReg = /^1[3-9]\d{9}$/
      if (!phoneReg.test(this.userInfo.phone)) {
        uni.showToast({
          title: '请输入正确的手机号',
          icon: 'none'
        })
        return
      }
      
      // TODO: 调用API保存用户信息
      const res = await this.$api.login.updateUserInfo({
        avatar: this.userInfo.avatar,
        name: this.userInfo.name,
        phone: this.userInfo.phone
      })
      if (res.code === 200) {
        uni.showToast({
          title: '保存成功',
          icon: 'success'
        })
        
        // 延迟返回上一页
        setTimeout(() => {
          uni.navigateBack()
        }, 1500)
      } 
    },

    // 获取个人信息
    async getProfile() {
      const res = await this.$api.login.getUserInfo()
      if (res.code === 200) {
        this.userInfo = res.result
        this.$store.dispatch('updateUserInfo', this.userInfo)
      }
    }
  },
  onLoad() {
    this.getProfile()
    // 3秒后隐藏遮罩层
    setTimeout(() => {
      this.showMask = false
    }, 3000)
  }
}
</script>

<style lang="scss" scoped>
.profile-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 40rpx 32rpx 200rpx;
  
  .form-container {
    background: #fff;
    border-radius: 20rpx;
    padding: 40rpx 32rpx;
    
    .form-title {
      font-size: 36rpx;
      font-weight: 600;
      color: #333;
      margin-bottom: 60rpx;
    }
    
    .form-item {
      margin-bottom: 60rpx;
      
      &:last-child {
        margin-bottom: 0;
      }
      
      .label {
        display: flex;
        align-items: center;
        margin-bottom: 20rpx;
        font-size: 28rpx;
        color: #333;
        
        .required {
          color: #ff4757;
          margin-right: 8rpx;
        }
      }
    }
    
    .avatar-container {
      position: relative;
      width: 200rpx;
      height: 200rpx;
      border-radius: 16rpx;
      overflow: hidden;
      
      .avatar-image {
        width: 100%;
        height: 100%;
        background: #00000080;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      
      .avatar-mask {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.6);
        display: flex;
        align-items: center;
        justify-content: center;
        color: #fff;
        font-size: 24rpx;
        transition: opacity 1s ease-out;
        
        &.fade-out {
          opacity: 0;
        }
      }
    }
  }
  
  .save-button-container {
    position: fixed;
    bottom: 60rpx;
    left: 50%;
    transform: translateX(-50%);
    z-index: 999;
  }
}
</style>
