<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.nickName" 
          placeholder="请输入昵称"
          :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>
        <view class="avatar-container" @click="uploadAvatar">
          <image 
            :src="userInfo.headImage || '/static/默认头像.png'" 
            class="avatar-image"
            mode="aspectFill"
          ></image>
          <!-- 遮罩层 -->
          <view class="avatar-mask" :class="{ 'fade-out': showMask }" v-if="showMask">
            <text>点击上传头像</text>
          </view>
        </view>
      </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: {
        headImage: '',
        nickName: '',
        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: {

    
    // 上传头像
    async uploadAvatar() {
      try {
        const result = await this.$utils.chooseAndUpload()
        if (result && result.success) {
          console.log(result);
          
          this.userInfo.headImage = result.url
        }
      } catch (error) {
        console.error('头像上传失败:', error)
        uni.showToast({
          title: '头像上传失败',
          icon: 'error'
        })
      }
      
    },
    
    // 保存资料
    async saveProfile() {
      if (!this.userInfo.nickName.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.user.updateUser({
        headImage: this.userInfo.headImage,
        nickName: this.userInfo.nickName,
        phone: this.userInfo.phone
      })
      if (res.code === 200) {
        uni.showToast({
          title: '保存成功',
          icon: 'success'
        })
        
        // 延迟返回上一页
        setTimeout(() => {
          uni.navigateBack()
        }, 1500)
      } 
    },

    // 获取个人信息
    async getProfile() {
      const res = await this.$api.user.queryUser()
      if (res.code === 200) {
        this.userInfo = res.result
      }
    }
  },
  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%;
      }
      
      .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>
