import { request } from '@/utils/request'
import { BASE_URL } from '@/utils/config'

export function getInfo() {
  return request({
    url: '/system/user/getInfo',
    method: 'get'
  })
}

// 用户密码重置
export function updateUserPwd(oldPassword, newPassword) {
  const data = {
    oldPassword,
    newPassword
  }
  return request({
    url: '/system/user/profile/updatePwd',
    method: 'put',
    data: data
  })
}

// 修改用户个人信息
export function updateUserProfile(data) {
  return request({
    url: '/system/user/profile',
    method: 'put',
    data: data
  })
}

/**
 * 账号注销
 * @Author: Antigravity
 */
export function cancelUser() {
  return request({
    url: '/system/user/cancel',
    method: 'delete'
  })
}

// 上传头像
// @Author: Antigravity
export function uploadAvatar(filePath) {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('token') || ''
    uni.uploadFile({
      url: BASE_URL + '/system/user/profile/avatar',
      filePath: filePath,
      name: 'avatarfile',
      header: {
        'Authorization': token ? `Bearer ${token}` : '',
        'clientid': 'fe63fea7be31b0200b496d08bc6b517d',
        'X-Platform-Code': 'MfJkMNMW2JKXBuPcbP2rxkD3ynXmReAZZFm4fN7cAGwGJdKCmd'
      },
      success: (res) => {
        const resData = JSON.parse(res.data)
        if (resData.code === 200) {
          resolve(resData.data)
        } else {
          uni.showToast({ title: resData.msg || '上传失败', icon: 'none' })
          reject(resData.msg)
        }
      },
      fail: (err) => {
        uni.showToast({ title: '网络异常', icon: 'none' })
        reject(err)
      }
    })
  })
}
