<template>
  <view class="profile-edit-page">
    <view class="custom-nav safe-area-top">
      <view class="nav-content">
        <view class="nav-back" @click="goBack">
          <uni-icons type="left" size="20" color="#1E293B"></uni-icons>
        </view>
        <text class="nav-title">编辑资料</text>
        <view class="nav-actions">
          <view class="nav-btn" @click="save">
            <text class="save-text">保存</text>
          </view>
        </view>
      </view>
    </view>

    <scroll-view scroll-y class="page-content">
      <view class="avatar-section">
        <view class="avatar-wrapper" @click="changeAvatar">
          <image v-if="form.avatar" :src="form.avatar" class="avatar-img"></image>
          <view v-else class="avatar-placeholder">
            <text>{{getAvatarText(form.nickname)}}</text>
          </view>
          <view class="avatar-edit">
            <uni-icons type="camera-filled" size="20" color="#FFFFFF"></uni-icons>
          </view>
        </view>
        <text class="avatar-tip">点击更换头像</text>
      </view>

      <view class="form-section">
        <view class="form-card">
          <view class="form-item">
            <text class="item-label">昵称</text>
            <input type="text" v-model="form.nickname" placeholder="请输入昵称" class="item-input"/>
          </view>
          <view class="form-item">
            <text class="item-label">手机号</text>
            <input type="number" v-model="form.phone" placeholder="请输入手机号" class="item-input" maxlength="11"/>
          </view>
          <view class="form-item">
            <text class="item-label">邮箱</text>
            <input type="text" v-model="form.email" placeholder="请输入邮箱" class="item-input"/>
          </view>
        </view>
      </view>

      <view class="safe-area-bottom" style="height: 40rpx;"></view>
    </scroll-view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        form: {
          avatar: '',
          nickname: '',
          phone: '',
          email: ''
        }
      }
    },
    onLoad() {
      const userInfo = uni.getStorageSync('userInfo') || {}
      this.form = {
        avatar: userInfo.avatar || '',
        nickname: userInfo.nickname || '',
        phone: userInfo.phone || '',
        email: userInfo.email || ''
      }
    },
    methods: {
      changeAvatar() {
        uni.chooseImage({
          count: 1,
          sourceType: ['album', 'camera'],
          success: (res) => {
            this.form.avatar = res.tempFilePaths[0]
          }
        })
      },
      save() {
        const userInfo = uni.getStorageSync('userInfo') || {}
        const updatedInfo = { ...userInfo, ...this.form }
        uni.setStorageSync('userInfo', updatedInfo)
        uni.showToast({ title: '保存成功', icon: 'success' })
        setTimeout(() => { uni.navigateBack() }, 1500)
      },
      goBack() { uni.navigateBack() },
      getAvatarText(nickname) {
        return nickname ? nickname.charAt(0).toUpperCase() : '用'
      }
    }
  }
</script>

<style scoped>
  .profile-edit-page { min-height: 100vh; background: #F8FAFC; display: flex; flex-direction: column; }
  .custom-nav { background: #FFFFFF; border-bottom: 2rpx solid #F1F5F9; }
  .nav-content { display: flex; align-items: center; justify-content: space-between; padding: 20rpx 32rpx; }
  .nav-back { width: 60rpx; height: 60rpx; display: flex; align-items: center; justify-content: center; }
  .nav-title { font-size: 36rpx; font-weight: 700; color: #1E293B; }
  .nav-actions { width: 80rpx; display: flex; align-items: center; justify-content: flex-end; }
  .save-text { font-size: 30rpx; color: #2563EB; font-weight: 600; }
  .page-content { flex: 1; padding: 48rpx 32rpx; }
  .avatar-section { display: flex; flex-direction: column; align-items: center; margin-bottom: 48rpx; }
  .avatar-wrapper { position: relative; width: 200rpx; height: 200rpx; border-radius: 50%; margin-bottom: 24rpx; }
  .avatar-img { width: 100%; height: 100%; border-radius: 50%; }
  .avatar-placeholder { width: 100%; height: 100%; border-radius: 50%; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); display: flex; align-items: center; justify-content: center; color: #FFFFFF; font-size: 80rpx; font-weight: 700; }
  .avatar-edit { position: absolute; right: 0; bottom: 0; width: 64rpx; height: 64rpx; background: #2563EB; border-radius: 50%; display: flex; align-items: center; justify-content: center; border: 4rpx solid #FFFFFF; }
  .avatar-tip { font-size: 26rpx; color: #64748B; }
  .form-section { margin-bottom: 32rpx; }
  .form-card { background: #FFFFFF; border-radius: 24rpx; overflow: hidden; box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.04); }
  .form-item { display: flex; align-items: center; padding: 28rpx 32rpx; border-bottom: 2rpx solid #F8FAFC; }
  .form-item:last-child { border-bottom: none; }
  .item-label { width: 140rpx; font-size: 30rpx; color: #1E293B; font-weight: 500; }
  .item-input { flex: 1; font-size: 30rpx; color: #1E293B; text-align: right; }
</style>
