import {
  getToken
} from '@/utils/auth'
import request from '@/utils/request'

export function login(data) {
  console.log('login post data ', data)
  return request({
    url: '/login',
    method: 'post',
    data
  })
}

// 获取验证码
export function getCodeImg() {
  return request({
    url: '/captchaImage',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}

export function getInfo(username) {
  return request({
    url: '/users/' + username,
    method: 'get',
    params: {
      token: getToken()
    }
  })
}

export function deleteUser(username) {
  return request({
    url: '/users/' + username,
    method: 'delete'
  })
}

export function updateUser(data) {
  return request({
    url: `/users/${data.username}`,
    method: 'put',
    data
  })
}

export function updateUserPassword(data) {
  return request({
    url: `/users/${data.username}` + '/password',
    method: 'put',
    data
  })
}

export function getAllUsers() {
  return request({
    url: '/users',
    method: 'get'
  })
}

export function createUser(user) {
  return request({
    url: '/users',
    method: 'post',
    data: user
  })
}

export function logout() {
  console.log('start to logout 3')
  return request({
    url: '/user/logout',
    method: 'post'
  })
}
