<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="请输入昵称"
          :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">
          <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>
        </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: {
        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: {

    
    // 上传头像
    async uploadAvatar() {
      try {
        const result = await this.$utils.chooseAndUpload()
        if (result && result.success) {
          console.log(result);
          
          this.userInfo.avatar = result.url
        }
      } catch (error) {
        console.error('头像上传失败:', error)
        uni.showToast({
          title: '头像上传失败',
          icon: 'error'
        })
      }
      
    },
    
    // 保存资料
    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
      }
    }
  },
  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>
