import axios from '@/common/services/axios-instance'
// eslint-disable-next-line no-restricted-imports
import orginAxios from 'axios'
import qs from 'qs'
import loginService from '@/login/login-service'
import { getUserInfo } from '@/common/store-mixin'
class PersonalCenterService {
  getAccountInfos(account) {
    // 获取指定用户信息
    return axios({
      url: `api/framework/v1/user-manager/users/${account}`,
    }).then((res) => {
      return res.data
    })
  }

  // 更新账户信息
  updateAccountInfo(FormData) {
    FormData.props = undefined
    return axios
      .put(`api/framework/v1/user-manager/users/${getUserInfo().account}/props`, FormData, {
        headers: {
          'Content-Type': 'application/json',
        },
      })
      .then(function(res) {
        return res.data
      })
  }

  // 修改密码
  changePassword(account, newpassword, oldpassword) {
    return orginAxios.get('api/ott').then((res) => {
      const ott = res.data
      return axios
        .post(
          `api/framework/v1/user-manager/users/${account}/changePassword`,
          qs.stringify({
            newPassword: loginService.encryptBySm4(ott.id, ott.token, newpassword),
            oldPassword: loginService.encryptBySm4(ott.id, ott.token, oldpassword),
          }),
          {
            headers: { 'content-type': 'application/x-www-form-urlencoded' },
          }
        )
        .then(function(res) {
          return res.data
        })
    })
  }

  // 上传头像
  uploadAvatar(param) {
    return axios
      .post(`api/framework/v1/user-avatar/Base64`, param, {
        headers: {
          'Content-Type': 'application/json',
        },
      })
      .then(function(res) {
        return res
      })
  }
}

export default new PersonalCenterService()
