<template>
  <view class="edit-profile">
    <!-- 顶部导航栏 -->

	<view class="nav-bar">
	  <view class="back-icon" @click="goBack">
	    <uni-icons type="left" size="20"></uni-icons>
	  </view>
	  <view class="title">修改信息</view>
	</view>

    <!-- 个人信息表单 -->
    <view class="info-container">
      <view class="info-card">
        <text class="section-title">个人信息</text>
        
        <!-- 昵称 -->
        <view class="info-item">
          <text class="label">昵称</text>
          <input type="text" v-model="userInfo.nickname" placeholder="请输入昵称" />
        </view>

        <!-- 电话 -->
        <view class="info-item">
          <text class="label">电话</text>
          <view class="phone-input">
            <input type="number" v-model="userInfo.phone" placeholder="请输入手机号" />
            <uni-icons type="eye" size="20" color="#999"></uni-icons>
          </view>
        </view>

        <!-- 头像 -->
        <view class="info-item avatar-section">
          <text class="label">头像</text>
          <view class="avatar-container">
            <view class="avatar-box" @tap="chooseImage">
              <uni-icons v-if="!userInfo.avatar" type="reload" size="24" color="#999"></uni-icons>
              <image v-else :src="userInfo.avatar" mode="aspectFill"></image>
            </view>
            <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'

export default {
  mixins: [pullRefreshMixin],
  data() {
    return {
      userInfo: {
        nickname: '吴彦谋',
        phone: '15888977617',
        avatar: '',
        newAvatar: '/static/logo.png'
      },
      uploadStatus: {
        show: false,
        type: 'loading' // loading, success, error
      }
    }
  },
  methods: {
    async onRefresh() {
      // 模拟刷新数据
      await new Promise(resolve => setTimeout(resolve, 1000))
      uni.stopPullRefresh()
    },
    goBack() {
      uni.navigateBack()
    },
    async chooseImage() {
      try {
        const [tempFile] = await new Promise((resolve, reject) => {
          uni.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            success: (res) => resolve(res.tempFilePaths),
            fail: reject
          })
        })

        // 显示加载状态
        this.uploadStatus = {
          show: true,
          type: 'loading'
        }

        // 模拟上传过程
        await new Promise(resolve => setTimeout(resolve, 1500))

        // 随机模拟成功或失败
        const isSuccess = Math.random() > 0.3
        
        if (isSuccess) {
          this.userInfo.newAvatar = tempFile
          this.uploadStatus.type = 'success'
        } else {
          throw new Error('Upload failed')
        }

        // 成功状态显示1秒后关闭
        await new Promise(resolve => setTimeout(resolve, 1000))
        this.uploadStatus.show = false

      } catch (error) {
        console.error('Upload failed:', error)
        this.uploadStatus = {
          show: true,
          type: 'error'
        }
        // 失败状态显示1秒后关闭
        setTimeout(() => {
          this.uploadStatus.show = false
        }, 1000)
      }
    },
    saveProfile() {
      uni.showToast({
        title: '保存成功',
        icon: 'success'
      })
      setTimeout(() => {
        uni.navigateBack()
      }, 1500)
    }
  }
}
</script>

<style lang="scss" scoped>
.edit-profile {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: calc(env(safe-area-inset-bottom) + 120rpx);
}

.nav-bar {
  display: flex;
  align-items: center;
  height: 88rpx;
  padding-top: var(--status-bar-height);

  .title {
    font-family: PingFang SC;
    font-weight: 500;
    font-size: 16px;
    line-height: 140%;
    letter-spacing: 0%;
    text-align: center;
    vertical-align: middle;
    color: #333;
    margin-left: 30%;
  }

  .back-icon {
    width: 88rpx;
    height: 88rpx;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

.info-container {
  margin-top: calc(88rpx + env(safe-area-inset-top));
  padding: 30rpx;
}

.info-card {
  background: #fff;
  border-radius: 20rpx;
  padding: 30rpx;

  .section-title {
    font-size: 32rpx;
    color: #333;
    font-weight: 500;
    margin-bottom: 40rpx;
  }

  .info-item {
    margin-bottom: 40rpx;

    .label {
      font-size: 28rpx;
      color: #333;
      margin-bottom: 20rpx;
      display: block;
    }

    input {
      font-size: 32rpx;
      color: #333;
      width: 100%;
      padding: 20rpx 0;
      border-bottom: 2rpx solid #f5f5f5;
    }

    .phone-input {
      display: flex;
      align-items: center;
      border-bottom: 2rpx solid #f5f5f5;

      input {
        flex: 1;
        border-bottom: none;
      }
    }
  }

  .avatar-section {
    .avatar-container {
      display: flex;
      gap: 20rpx;
    }

    .avatar-box {
      width: 160rpx;
      height: 160rpx;
      background: #f5f5f5;
      border-radius: 10rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      overflow: hidden;

      image {
        width: 100%;
        height: 100%;
      }
    }

    .new-avatar {
      width: 160rpx;
      height: 160rpx;
      border-radius: 10rpx;
    }
  }
}

.save-button {
  position: fixed;
  left: 30rpx;
  right: 30rpx;
  bottom: calc(env(safe-area-inset-bottom) + 30rpx);
  height: 90rpx;
  background: linear-gradient(to right,#17f261,#13d755);
  border-radius: 45rpx;
  display: flex;
  align-items: center;
  justify-content: center;

  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);
  }
}
</style> 