import { http } from './http'
import { isWebEvn } from '@/utils'
import { post } from './request'

interface accountLoginType {
  account: string
  password: string
  simSerialNumber?: string
  model?: string
}

interface smsLoginType {
  phone: string
  verifyCode: string
}
/**
 * 密码登录
 * @param account 手机号
 * @param password 密码
 * @param clientType 客户端类型 默认客服登录
 */
const psdLogin = async (data: accountLoginType) => {
  return await post('/login', {
    clientType: isWebEvn ? 'PC' : 'PAD',
    ...data,
  })
}

// 退出登录
const logout = () => {
  return http.request({
    url: '/logout',
  })
}

// 获取是否首次绑定
const getFirstBind = (sn: string) => {
  return http.request({
    url: `/sysVisLog/getBindRelationByDeviceAndAccount/${sn}`,
  })
}

// 获取机器绑定码
const getBindQrCode = (data: anyObj) => {
  return http.request({
    url: '/sysFileInfo/generateQrCodeAndUpload',
    method: 'post',
    data,
  })
}

// 获取作业TOKEN
const workLogin = (data: anyObj) => {
  return http.request({
    baseURL: '/api/campus',
    url: '/sc/stu/stuLogin',
    headers: {
      Authorization: '',
    },
    method: 'POST',
    data,
  })
}

// 作业服务退出登录
const workLogout = () => {
  return http.request({
    baseURL: '/api/campus',
    url: '/logout',
    method: 'POST',
  })
}

const loginApi = {
  psdLogin,
  logout,
  getFirstBind,
  getBindQrCode,
  workLogin,
  workLogout,
}

export default loginApi
export type { accountLoginType, smsLoginType }
