// 定义用户信息数据类型
interface UserInfo {
  uid: string;
  telephone: string;
  password: string;
  avatar_url: string;
  gender: 0 | 1; // 0:女, 1:男
  birthdate_date: string;
  createtime: string;
  updatetime: string;
  bio: string;
  username: string;
  total_points: number;
}

Page({
  data: {
    userInfo: {} as UserInfo
  },

  onLoad() {
    // 从缓存或接口获取用户信息
    this.loadUserInfo();
  },

  // 加载用户信息
  loadUserInfo() {
    // 模拟数据，实际项目中应从接口获取
    const mockUserInfo: UserInfo = {
      uid: '10086',
      telephone: '138****8888',
      password: '',
      avatar_url: 'https://picsum.photos/200/200',
      gender: 1,
      birthdate_date: '1995-08-15',
      createtime: '2023-01-01',
      updatetime: '2023-06-10',
      bio: '热爱生活，喜欢探索',
      username: '张小明',
      total_points: 1250
    };

    this.setData({
      userInfo: mockUserInfo
    });
  },

  // 编辑昵称
  editNickname() {
    const { username } = this.data.userInfo;
    
    wx.showModal({
      title: '修改昵称',
      content: '请输入新的昵称',
      editable: true,
      placeholderText: username,
      success: (res) => {
        if (res.confirm && res.content) {
          // 调用接口修改昵称
          this.setData({
            'userInfo.username': res.content
          });
          
          wx.showToast({
            title: '昵称已更新',
            icon: 'success',
            duration: 1500
          });
        }
      }
    });
  },

  // 编辑性别
  editGender() {
    const { gender } = this.data.userInfo;
    
    wx.showActionSheet({
      itemList: ['男', '女'],
      itemColor: '#333',
      success: (res) => {
        const newGender = res.tapIndex === 0 ? 1 : 0;
        
        if (newGender !== gender) {
          this.setData({
            'userInfo.gender': newGender
          });
          
          wx.showToast({
            title: '性别已更新',
            icon: 'success',
            duration: 1500
          });
        }
      }
    });
  },

  // 编辑生日
  editBirthdate() {
    const { birthdate_date } = this.data.userInfo;
    
    wx.showDatePickerModal({
      startDate: '1900-01-01',
      endDate: '2023-12-31',
      currentDate: birthdate_date,
      success: (res) => {
        if (res.confirm) {
          this.setData({
            'userInfo.birthdate_date': res.date
          });
          
          wx.showToast({
            title: '生日已更新',
            icon: 'success',
            duration: 1500
          });
        }
      }
    });
  },

  // 编辑手机号
  editPhone() {
    wx.navigateTo({
      url: '/pages/editPhone/editPhone'
    });
  },

  // 修改密码
  changePassword() {
    wx.navigateTo({
      url: '/pages/changePassword/changePassword'
    });
  },

  // 更换头像
  changeAvatar() {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const tempFilePath = res.tempFiles[0].tempFilePath;
        
        // 模拟上传头像
        this.setData({
          'userInfo.avatar_url': tempFilePath
        });
        
        wx.showToast({
          title: '头像已更新',
          icon: 'success',
          duration: 1500
        });
      }
    });
  },

  // 隐私设置
  managePrivacy() {
    wx.navigateTo({
      url: '/pages/privacySettings/privacySettings'
    });
  },

  // 关于我们
  showAbout() {
    wx.navigateTo({
      url: '/pages/about/about'
    });
  },

  // 退出登录
  logout() {
    wx.showModal({
      title: '确认退出',
      content: '确定要退出当前账号吗？',
      confirmText: '退出',
      confirmColor: '#ff4d4f',
      cancelText: '取消',
      success: (res) => {
        if (res.confirm) {
          // 清除登录状态
          wx.removeStorageSync('token');
          wx.removeStorageSync('userInfo');
          
          // 跳转到登录页
          wx.reLaunch({
            url: '/pages/login/login'
          });
        }
      }
    });
  }
});
    