<template>
  <div>
    <!-- 个人资料 -->
    <div class="base">
      <div class="profile">
        <h2>编辑简介</h2>
        <div class="profile-content">
          <!-- 头像 -->
          <div class="avatar-container">
            <img :src="user.avatar || defaultAvatar" alt="Avatar" class="avatar">
            <!-- 编辑头像 -->
            <label for="avatar-upload" class="edit-icon">
              <input type="file" id="avatar-upload" @change="uploadImage" accept="image/*" hidden>
              ✏️
            </label>
          </div>
          <!-- 个人信息 -->
          <div class="info">
            <div class="column">
              <label>账号</label>
              <input type="text" v-model="user.account">
              <label>邮箱</label>
              <input type="email" v-model="user.email">
              <label>生日</label>
              <input type="date" v-model="user.birthday">
              <label>职业</label>
              <input type="text" v-model="user.job">
            </div>
            <div class="column">
              <label>姓名</label>
              <input type="text" v-model="user.name">
              <label>密码</label>
              <input type="password" v-model="user.password">
              <label>居住地</label>
              <input type="text" v-model="user.location">
              <label>个人签名</label>
              <input type="text" v-model="user.signature">
              <!-- 保存按钮 -->
              <button @click="saveProfile">保存</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from '@/utils/axiosConfig';

export default {
  data() {
    return {
      user: {
        avatar: '',
        account: '用户',
        email: 'charlenereed@gmail.com',
        birthday: '1990-01-25',
        job: '程序员',
        name: 'Charlene Reed',
        password: '',
        location: 'San Jose, California, USA',
        signature: 'gali建帅 马思唯最好',
      },
      defaultAvatar: 'https://via.placeholder.com/150', // 默认头像占位符
    };
  },
  methods: {
    // 上传头像
    async uploadImage(event) {
      const file = event.target.files[0];
      if (file) {
        this.user.avatar = URL.createObjectURL(file);
      }
      // if (!file) return;

      // const formData = new FormData();
      // formData.append('file', file);

      // try {
      //   const response = await axios.post('/api/upload', formData, {
      //     headers: {
      //       'Content-Type': 'multipart/form-data'
      //     }
      //   });

      //   if (response.data.url) {
      //     this.user.avatar = response.data.url;
      //   } else {
      //     throw new Error('上传失败');
      //   }
      // } catch (error) {
      //   console.error('上传头像失败:', error);
      //   alert('上传头像失败，请重试');
      // }
    },
    // 加载默认的信息数据
    async fetchUserInfo() {
        try {
          // 调用后端接口获取用户信息
          const response = await axios.get("/api/getUserInfo", {
            params: {
              userId: "12345", // 假设传递用户 ID，需根据实际情况调整
            },
          });
          // 更新数据
          this.user.avatar = response.data.avatar;
          this.user.account = response.data.account;
          this.user.email = response.data.email;
          this.user.birthday = response.data.avatar;
          this.user.job = response.data.job;
          this.user.name = response.data.name;
          this.user.password = response.data.password;
          this.user.location = response.data.location;
          this.user.signature = response.data.signature;
        } catch (error) {
          console.error("获取用户信息失败:", error);
          // alert("无法加载用户信息，请稍后重试！");
        }
      },

    // 保存个人信息
    async saveProfile() {
      try {
        // 调用后端接口保存用户资料
        const response = await axios.post('',null, {
          params:{
            avatar: this.user.avatar,
            account: this.user.account,
            email: this.user.email,
            birthday: this.user.birthday,
            job: this.user.job,
            name: this.user.name,
            password: this.user.password,
            location: this.user.location,
            signature: this.user.signature,
          }
        });
        if (response.data.success) {
          alert('个人信息已保存！');
        } else {
          alert('保存失败，请重试！');
        }
      } catch (error) {
        console.error('保存时出错:', error);
        alert('保存失败，请检查网络或稍后重试！');
      }
    },
  },
};
</script>
  
  <style scoped>
  /* 个人资料 */
  .base{
    display: flex;
    background-image: url("../../assets/pictures/space/Inbase.png");
    justify-content: center;
    align-items: center;
    padding:12px 200px;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
  }
  .profile {
    display: flex;
    flex-direction: column;
    background-color:rgba(255,255,255,0.8);
    height: 600px;
    width:930px;
    border-radius: 50px;
    margin: 20px;
  }
  .profile h2 {
    position: relative;
    display: inline-block;
    margin-left: 48px;
    margin-top: 32px;
    font-size: 24px;
    color: #FE4E96;
    padding-bottom: 12px;
    border-bottom:#F4F5F7 2px solid;
  }
  .profile h2::before{
    content: '';
    position: absolute;
    bottom: 0;
    left: -12px;
    width: 124px;
    border-bottom: #FEB2D7 4px solid;
  }
  .profile-content {
    display: flex;
    gap: 20px;
  }
  .avatar-container {
  position: relative;
  display: inline-block;
  width: 140px;
  height: 140px;
}
.avatar {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  margin-left: 20px;
  object-fit: cover;
  background-color: #d3d3d3;
  border: 2px solid #ccc;
}
.avatar-container input[type="file"]{
  display: none;
}
.edit-icon {
  position: absolute;
  bottom: 0;
  right: 0;
  background-color: pink;
  border-radius: 50%;
  padding: 5px;
  cursor: pointer;
}
  .info {
    display: flex;
    margin-left: 70px;
    gap: 20px;
}
  .column {
    display: flex;
    flex-direction: column;
    width: 256px;
    gap: 20px;
    padding-right: 80px;
}
  .column input{
    width: 116%;
    height: 28px;
    padding: 8px;
    border-radius: 50px;
    border: 1px solid #DFEAF2;
}
  .column label {
  display: block; /* 使标签独占一行 */
  margin-bottom: -4px; /* 增加标签和输入框之间的间距 */
  font-size: 14px; /* 可以调整字体大小 */

}
  button {
    display: flex;
    flex-direction: row;
    justify-content: center;
    height: 40px;
    width: 150px;
    padding-top: 12px;
    margin-left: 150px;
    margin-top: 16px;
    background-color:#F177A8;
    color: #fff;
    border: none;
    border-radius: 50px;
    cursor: pointer;
  }
  </style>