<template>
  <view class="edit-profile safe-area" :style="{paddingTop: navBarHeightRpx + 'rpx'}">
    <!-- 顶部导航栏 -->
    <view class="nav-bar" :style="{height: (statusBarHeight + 88) + 'rpx', paddingTop: statusBarHeight + 'px'}">
      <view class="back" @tap="goBack">
        <uni-icons type="left" size="20" color="#222"></uni-icons>
      </view>
      <text class="title">修改信息</text>
      <view class="nav-bar-right"></view>
    </view>

    <!-- 个人信息表单 -->
    <view class="info-container" :style="infoContainerStyle">
      <view class="info-card">
        <text class="section-title">个人信息</text>
        <view class="divider"></view>
        <!-- 昵称 -->
        <view class="info-item">
          <text class="label">昵称</text>
          <input type="text" v-model="userInfo.nickname" placeholder="请输入昵称" />
        </view>
        <view class="divider"></view>
        <!-- 电话 -->
        <view class="info-item">
          <text class="label">电话</text>
          <view class="phone-input">
            <input
              type="text"
              :value="showPhone ? userInfo.phone : maskedPhone"
              :readonly="!showPhone"
              placeholder="请输入手机号"
              @input="e => { userInfo.phone = e.detail.value }"
            />
            <uni-icons :type="showPhone ? 'eye' : 'eye-slash'" size="20" color="#999" @tap="showPhone = !showPhone"></uni-icons>
          </view>
        </view>
        <view class="divider"></view>
        <!-- 头像 -->
        <view class="info-item avatar-section">
          <text class="label">头像</text>
          <view class="avatar-container">
            <button class="avatar-box"  open-type="chooseAvatar" @chooseavatar="chooseImage"> 
              <uni-icons v-if="!userInfo.avatar" type="reload" size="24" color="#999"></uni-icons>
              <image v-else :src="userInfo.avatar" mode="aspectFill"></image>
            </button>
            <image v-if="userInfo.newAvatar" :src="userInfo.newAvatar" mode="aspectFill" class="new-avatar"></image>
          </view>
        </view>
      </view>
    </view>

    <!-- 头像上传状态提示 -->
    <view class="upload-status" v-if="uploadStatus.show">
      <view class="status-mask"></view>
      <view class="status-content">
        <!-- 加载中 -->
        <template v-if="uploadStatus.type === 'loading'">
          <view class="loading-icon"></view>
          <text>正在加载修改</text>
        </template>
        <!-- 成功 -->
        <template v-else-if="uploadStatus.type === 'success'">
          <uni-icons type="checkmark" size="24" color="#fff"></uni-icons>
          <text>修改成功</text>
        </template>
        <!-- 失败 -->
        <template v-else-if="uploadStatus.type === 'error'">
          <uni-icons type="closeempty" size="24" color="#fff"></uni-icons>
          <text>修改失败</text>
        </template>
      </view>
    </view>

    <!-- 保存按钮 -->
    <view class="save-button" @tap="saveProfile">
      <text>保存</text>
    </view>
  </view>
</template>

<script>
import pullRefreshMixin from '@/pages/mixins/pullRefreshMixin.js'
import OSS from '@/utils/oss-upload/oss/index.js'

export default {
  mixins: [pullRefreshMixin],
  data() {
    return {
      statusBarHeight: 0,
      navBarHeight: 44, // px
      navBarHeightRpx: 88, // rpx
      userInfo: {
        nickname: '',
        phone: '',
        avatar: '',
        newAvatar: ''
      },
      uploadStatus: {
        show: false,
        type: 'loading' // loading, success, error
      },
      showPhone: false
    }
  },
  computed: {
    maskedPhone() {
      // 返回和手机号等长的点点
      return this.userInfo.phone ? '●'.repeat(this.userInfo.phone.length) : ''
    },
    infoContainerStyle() {
      return {
        marginTop: this.navBarHeightRpx + 'rpx'
      }
    }
  },
  onLoad() {
    const sysInfo = uni.getSystemInfoSync()
    this.statusBarHeight = sysInfo.statusBarHeight
    let navBarHeight = 44
    try {
      const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
      navBarHeight = menuButtonInfo.bottom + menuButtonInfo.top - sysInfo.statusBarHeight
    } catch (e) {}
    this.navBarHeight = navBarHeight
    this.navBarHeightRpx = Math.round(navBarHeight * 750 / sysInfo.windowWidth)
    // 获取个人信息
    if (uni.getStorageSync('token')) {
      this.$api('getUserByToken', {}, (res) => {
        if (res.code === 200 && res.result) {
          this.userInfo.nickname = res.result.nickName || ''
          this.userInfo.phone = res.result.phone || ''
          this.userInfo.avatar = res.result.headImage || ''
        }
      })
    }
  },
  methods: {
    async onRefresh() {
      // 模拟刷新数据
      await new Promise(resolve => setTimeout(resolve, 1000))
      uni.stopPullRefresh()
    },
    goBack() {
      uni.navigateBack()
    },
    async chooseImage(res) {
      try {
        this.uploadStatus = { show: true, type: 'loading' }
        // 上传到OSS
        const url = await OSS.ossUpload(res.target.avatarUrl)
        console.log(url,'url');
        this.userInfo.avatar = url
        this.uploadStatus.type = 'success'
        setTimeout(() => { this.uploadStatus.show = false }, 1000)
      } catch (error) {
        this.uploadStatus = { show: true, type: 'error' }
        setTimeout(() => { this.uploadStatus.show = false }, 1000)
      }
    },
    saveProfile() {
      // 校验
      if (!this.userInfo.avatar) return uni.showToast({ title: '请上传头像', icon: 'none' })
      if (!this.userInfo.nickname) return uni.showToast({ title: '请填写昵称', icon: 'none' })
      if (!this.userInfo.phone) return uni.showToast({ title: '请填写手机号', icon: 'none' })
      this.uploadStatus = { show: true, type: 'loading' }
      console.log(this.userInfo,'this.userInfo');
      this.$api('updateInfo', {
        avatarUrl: this.userInfo.avatar,
        nickName: this.userInfo.nickname,
        phone: this.userInfo.phone
      }, res => {
        if (res.code === 200) {
          // this.uploadStatus.type = 'success'
          setTimeout(() => {
            // this.uploadStatus.show = false
            uni.$emit('refreshUserInfo')
            uni.showToast({ title: '保存成功', icon: 'success' })
            setTimeout(() => { uni.navigateBack() }, 1000)
          }, 1000)
        } else {
          this.uploadStatus.type = 'error'
          setTimeout(() => { this.uploadStatus.show = false }, 1000)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.edit-profile {
  min-height: 100vh;
  background-color: #f5f5f5;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  padding-bottom: 140rpx;
  box-sizing: border-box;
}

.nav-bar {
  display: flex;
  align-items: center;
  height: 88rpx;
  background: #fff;
  padding: 0 30rpx;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  width: 100vw;
  box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.03);
  .back {
    padding: 20rpx;
    margin-left: -20rpx;
    display: flex;
    align-items: center;
    height: 88rpx;
  }
  .title {
    flex: 1;
    text-align: center;
    font-size: 34rpx;
    font-weight: 500;
    color: #222;
    letter-spacing: 0.5px;
    line-height: 88rpx;
  }
  .nav-bar-right {
    width: 44px;
    height: 88rpx;
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
}

.info-container {
  flex: 1;
  overflow-y: auto;
  padding: 0 40rpx 0;
  box-sizing: border-box;
}

.info-card {
  background: linear-gradient(180deg, #fff7e6 0%, #fff 100%);
  border-radius: 24rpx;
  box-shadow: 0 8rpx 32rpx rgba(60, 167, 250, 0.08);
  padding: 48rpx 40rpx 32rpx 40rpx;
  width: 100%;
  max-width: 700rpx;
  margin: 0 auto;
  position: relative;
  box-sizing: border-box;
}
.section-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #222;
  margin-bottom: 24rpx;
}
.divider {
  border-bottom: 2rpx solid #f2f2f2;
  margin-bottom: 24rpx;
}
.info-item {
  margin-bottom: 24rpx;
  width: 100%;
  box-sizing: border-box;
  min-height: 60rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  .label {
    font-size: 28rpx;
    color: #333;
    margin-bottom: 12rpx;
    display: block;
  }
  input {
    font-size: 32rpx;
    color: #222;
    width: 100%;
    padding: 16rpx 0;
    border: none;
    background: none;
    outline: none;
    box-sizing: border-box;
    height: 80rpx;
    line-height: 48rpx;
    vertical-align: middle;
  }
  .phone-input {
    display: flex;
    align-items: center;
    width: 100%;
    box-sizing: border-box;
    input {
      flex: 1;
      width: 100%;
      box-sizing: border-box;
      height: 80rpx;
      line-height: 48rpx;
      vertical-align: middle;
    }
  }
}
.avatar-section {
  .avatar-container {
    display: flex;
    gap: 20rpx;
    width: 100%;
    box-sizing: border-box;
  }
  .avatar-box, .new-avatar {
    // max-width: 120rpx;
    padding: 0;
    margin: 0;
    width: 150rpx;
    height: 120rpx;
    border-radius: 18rpx;
    background: #f5f5f5;
    display: flex;
    align-items: center;
    justify-content: center;
    // overflow: hidden;
  }
  .avatar-box image, .new-avatar {
    width: 100%;
    height: 100%;
  }
}
.save-button {
  position: fixed;
  left: 30rpx;
  right: 30rpx;
  bottom: calc(env(safe-area-inset-bottom) + 30rpx);
  height: 90rpx;
  background: linear-gradient(90deg, #ffd01e 0%, #ffb400 100%);
  border-radius: 45rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4rpx 16rpx rgba(255, 149, 0, 0.08);
  z-index: 10;
  text {
    color: #fff;
    font-size: 32rpx;
    font-weight: 500;
  }
}
.upload-status {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  .status-mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
  }
  .status-content {
    position: relative;
    width: 240rpx;
    height: 240rpx;
    background: rgba(0, 0, 0, 0.8);
    border-radius: 20rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 20rpx;
    text {
      color: #fff;
      font-size: 28rpx;
    }
    .loading-icon {
      width: 60rpx;
      height: 60rpx;
      border: 4rpx solid #fff;
      border-top-color: transparent;
      border-radius: 50%;
      animation: spin 1s linear infinite;
    }
  }
}
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
.safe-area {
  padding-bottom: env(safe-area-inset-bottom);
}
</style> 