import { reactive } from 'vue'
import axios from 'axios'

const userInfo = reactive({
  isLogin: false,
  userId: '',
})

export const userApi = {
  loginProcess: async function (inputUsername, inputUserPassword) {
    const data = { username: inputUsername, password: inputUserPassword }
    axios.post('/public-api/loginProcess', data).then((response) => {
      if (response.status == 200 && response.data && response.data.success == true) {
        userInfo.isLogin = true
        userInfo.userId = response.data.username
        return true
      } else {
        return false
      }
    })
  },
  logoutProcess: async function () {
    axios.post('/public-api/logoutProcess').then((response) => {
      if (response.status == 200 && response.data && response.data.success == true) {
        userInfo.isLogin = false
        userInfo.userId = ''
        return true
      } else {
        return false
      }
    })
  },
  loginCheck: async function () {
    axios.get('/public-api/sessionCheck').then((response) => {
      if (response.status == 200 && response.data && response.data.success == true) {
        userInfo.isLogin = true
        userInfo.userId = response.data.username
        return true
      } else {
        userInfo.isLogin = false
        userInfo.userId = ''
        return false
      }
    })
  },
}
window.userApi = userApi
export default userInfo
