<template>
  <view class="userInfo-container">
    <view class="header">
      <div class="header-left">
        <u-icon
          class="header-left-icon"
          name="arrow-left"
          @click="handleLeftClick"
        ></u-icon>
      </div>
      <text class="header-title">个人信息</text>
      <div></div>
    </view>

    <view class="user-info-content">
      <!-- 第一个卡片：头像 -->
      <view class="info-card">
        <view class="info-item avatar-item">
          <text class="item-label">头像</text>
          <view class="item-content avatar-content">
            <image
              class="avatar-image"
              :src="
                userInfo.avatar || '/static/common/images/avatar_default.png'
              "
              mode="aspectFill"
              @click="chooseAvatar"
            ></image>
          </view>
        </view>
      </view>

      <!-- 第二个卡片：姓名和性别 -->
      <view class="info-card">
        <view class="info-item">
          <text class="item-label">姓名</text>
          <view class="item-content">
            <text>{{ userInfo.name }}</text>
          </view>
        </view>

        <view class="info-item">
          <text class="item-label">性别</text>
          <view class="item-content" @click="openPicker('gender')">
            <text class="placeholder-text" v-if="!userInfo.gender"
              >请选择性别</text
            >
            <text v-else>{{ userInfo.gender }}</text>
            <text class="arrow-icon">></text>
          </view>
        </view>
      </view>

      <!-- 第三个卡片：归属地、高考年份、生源高中、意向学院专业 -->
      <view class="info-card">
        <view class="info-item">
          <text class="item-label">归属地</text>
          <view class="item-content" @click="openPicker('region')">
            <text class="placeholder-text" v-if="!userInfo.region"
              >请选择归属地</text
            >
            <text v-else>{{ userInfo.region }}</text>
            <text class="arrow-icon">></text>
          </view>
        </view>

        <view class="info-item">
          <text class="item-label">高考年份</text>
          <view class="item-content" @click="openPicker('examYear')">
            <text class="placeholder-text" v-if="!userInfo.examYear"
              >请选择高考年份</text
            >
            <text v-else>{{ userInfo.examYear }}</text>
            <text class="arrow-icon">></text>
          </view>
        </view>

        <view class="info-item">
          <text class="item-label">生源高中</text>
          <view class="item-content">
            <input
              class="item-input"
              v-model="userInfo.highSchool"
              placeholder="请输入生源高中"
              placeholder-style="color: #CCCCCC;"
            />
          </view>
        </view>

        <view class="info-item">
          <text class="item-label">意向学院专业</text>
          <view class="item-content" @click="openPicker('major')">
            <text class="placeholder-text" v-if="!userInfo.major"
              >请选择意向学院专业</text
            >
            <text v-else>{{ userInfo.major }}</text>
            <text class="arrow-icon">></text>
          </view>
        </view>
      </view>
    </view>

    <div class="user-info-footer">
      <u-button class="user-info-footer-button" @click="handleSave">
        保存
      </u-button>
    </div>

    <!-- 选择器弹窗 -->
    <u-picker
      v-model="showPicker"
      :columns="pickerColumns"
      @confirm="onPickerConfirm"
      @cancel="showPicker = false"
    ></u-picker>
  </view>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      userInfo: {
        avatar: "/static/common/images/avatar_default.png",
        name: "杨翼",
        gender: "",
        region: "",
        examYear: "",
        highSchool: "",
        major: "",
      },
      showPicker: false,
      currentPicker: "",
      pickerColumns: [],
      pickerData: {
        gender: ["男", "女"],
        region: ["北京", "上海", "广州", "深圳", "江西", "湖南"],
        examYear: ["2023", "2024", "2025", "2026"],
        major: [
          "计算机科学与技术",
          "软件工程",
          "人工智能",
          "电子信息工程",
          "机械工程",
        ],
      },
    };
  },
  mounted() {},
  onLoad(options) {
    if (options.userId) {
      this.userId = options.userId;
      this.getUserInfo();
    }
  },
  methods: {
    handleLeftClick() {
      uni.navigateBack();
    },
    getUserInfo() {
      // 模拟获取用户信息的接口
      console.log("获取用户ID为", this.userId, "的信息");
      // 实际开发中这里应该调用接口获取用户信息
    },
    chooseAvatar() {
      uni.chooseImage({
        count: 1,
        sizeType: ["compressed"],
        sourceType: ["album", "camera"],
        success: (res) => {
          this.userInfo.avatar = res.tempFilePaths[0];
          // 实际开发中这里应该上传头像到服务器
        },
      });
    },
    openPicker(type) {
      this.currentPicker = type;
      this.pickerColumns = [this.pickerData[type]];
      this.showPicker = true;
    },
    onPickerConfirm(e) {
      const value = e.value[0];
      switch (this.currentPicker) {
        case "gender":
          this.userInfo.gender = value;
          break;
        case "region":
          this.userInfo.region = value;
          break;
        case "examYear":
          this.userInfo.examYear = value;
          break;
        case "major":
          this.userInfo.major = value;
          break;
      }
      this.showPicker = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.userInfo-container {
  min-height: 100vh;
  padding-bottom: calc(112rpx + env(safe-area-inset-bottom));
  padding: 32rpx;
  padding-top: calc(88rpx + 40rpx);

  background-image: url(/static//common/images/images_bg.png);
  background-repeat: no-repeat;
  background-size: 100% 100%;
  background-position: center;

  .header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    // background-color: #ffffff;
    height: 88rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 30rpx;
    z-index: 99;

    .header-left {
      font-size: 36rpx;
    }

    .header-title {
      font-size: 36rpx;
      font-weight: 500;
      color: #333333;
    }
  }

  .user-info-content {
    .info-card {
      background-color: #ffffff;
      border-radius: 20rpx;
      padding: 0 32rpx;
      margin-bottom: 32rpx;

      .info-item {
        height: 110rpx;
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1rpx solid #f2f2f2;

        &:last-child {
          border-bottom: none;
        }

        .item-label {
          font-family: PingFang SC;
          font-size: 28rpx;
          color: #333333;
        }

        .item-content {
          flex: 1;
          display: flex;
          justify-content: flex-end;
          align-items: center;
          font-size: 28rpx;
          color: #333333;

          .item-input {
            text-align: right;
            width: 100%;
            font-size: 28rpx;
          }

          .placeholder-text {
            color: #cccccc;
          }

          .arrow-icon {
            margin-left: 10rpx;
            color: #cccccc;
          }
        }

        &.avatar-item {
          .avatar-content {
            display: flex;
            align-items: center;
          }

          .avatar-image {
            width: 68rpx;
            height: 68rpx;
            border-radius: 50%;
          }
        }
      }
    }
  }

  .user-info-footer {
    position: fixed;
    bottom: 32rpx;
    left: 0;
    right: 0;
    padding: 0 32rpx;
    .user-info-footer-button {
      width: 100%;
      height: 85rpx;
      background: #4f6aff;
      color: #ffffff;
      border-radius: 16rpx;
      opacity: 0.9;
    }
  }
}

/* 响应式布局 - PC端样式 */
@media screen and (min-width: 768px) {
  .userInfo-container {
    .user-info-content {
      max-width: 1200rpx;
      margin: 0 auto;
    }
  }
}
</style>
