import { eLevel } from '@/types/level'
import { eNewRoleRelatedToBackEnd } from '@/types/role'
import { eUserStatusAction } from '@/types/user'
import { eUserType } from '@/types/userType'
import baseAxios from '../index'

export default class UserService {
  static instance: UserService
  static getInstance() {
    if (!UserService.instance) {
      UserService.instance = new UserService()
    }
    return UserService.instance
  }

  /**
   * 登录
   * @param data
   * @returns
   */
  login(data: {
    is_admin: boolean
    use_pwd: boolean
    phone?: string
    pwd?: string
    verify_code?: string
  }) {
    return baseAxios<{
      accessToken: string
      level: eLevel
      phone: string
      role: eNewRoleRelatedToBackEnd
      userType: eUserType
    }>({
      url: '/login',
      method: 'POST',
      data,
    })
  }

  /**
   * 登出
   * @returns
   */
  logout() {
    return baseAxios({
      url: '/logout',
      method: 'get',
    })
  }

  /**
   * 检查手机号是否已注册
   * @param phone
   * @returns
   */
  checkPhone(phone: string) {
    return baseAxios<boolean>({
      url: '/user/check/phone',
      method: 'get',
      params: {
        phone,
      },
    })
  }

 /**
   * 注册
   * @param data
   * @returns
   */
  register(data: { phone: string; verify_code: string }) {
    return baseAxios({
      method: 'POST',
      url: '/user/register',
      data,
    })
  }

  changePhone(data: { phone: string; pwd: string; verify_code: string }) {
    return baseAxios({
      method: 'post',
      url: '/change/phone',
      data,
    })
  }

  changePwd(data: { new_pwd: string; phone: string; verify_code: string }) {
    return baseAxios({
      method: 'post',
      url: '/change/pwd',
      data,
    })
  }

  adminAddPhone(data: { phone: string; pwd: string; verify_code: string }) {
    return baseAxios({
      method: 'post',
      url: '/admin/add/phone',
      data,
    })
  }

  adminCreateStaff(data: {
    bank_code: number
    password: string
    phone: string
    user_name: string
  }) {
    return baseAxios({
      method: 'post',
      url: '/admin/create/staff',
      data,
    })
  }

  adminQueryBankAdmins(data: {
    end: number
    firstBranch?: number
    firstSubBranch?: number
    level?: number
    location?: string
    page: number
    page_size: number
    phone: string
    secondBranch?: number
    secondSubBranch?: number
    start: number
    user_name: string
  }) {
    return baseAxios<{
      item: {
        bank_name: string
        created_at: number
        first_branch: string
        first_sub_branch: string
        level: number
        location: string
        operator_name: string
        operator_uuid: string
        phone: string
        role: string
        second_branch: string
        second_sub_branch: string
        status: eUserStatusAction
        user_name: string
        uuid: string
      }[]
      total: number
    }>({
      method: 'get',
      url: '/admin/query/bankadmins',
      params: data,
    })
  }

  adminQueryBanks() {
    return baseAxios<{
      item: { code: number; name: string }[]
      total: number
    }>({
      method: 'get',
      url: '/query/banks',
    })
  }
}

