import {
  get,
  post,
} from '@/common/utils/http-util.ts'
import ClientUtil from '@/common/utils/client-util.ts'

const Clients = [ {
  txt: '电脑端',
  val: 0,
}, {
  txt: '微信小程序',
  val: 1,
} ]
export default {
  paging(data: UserTypes.SearchUserParam) {
    return get<G.PageResult<UserTypes.SearchUserResult>>('/user/paging', data)
  },
  detail(id: string) {
    return get<UserTypes.SearchUserResult>('/user/detail', {id})
  },
  disable(id: string, disable: boolean) {
    return get('/user/disable', {id, disable})
  },
  add(data: UserTypes.AddUserParam) {
    return post('/user/add', data)
  },
  bindRole(data: UserTypes.BindRoleParam) {
    return post('/user/bind_role', data)
  },
  modify(data: UserTypes.ModifyUserParam) {
    return post('/user/modify', data)
  },
  modifyInfo(data: { nickname?: string; avatar?: string; phone?: string }) {
    return post('/user/modify_info', data)
  },
  modifyPasswd(data: { oldPasswd: string; newPasswd: string }) {
    return post('/user/modify_passwd', data)
  },
  resetPasswd(id: string) {
    return get('/user/reset_passwd', {id})
  },
  del(ids: string[]) {
    return post('/user/del', ids)
  },
  clients(clientCode: number) {
    return Clients.filter(it => {
      const mask = 1 << it.val
      return (clientCode & mask) === 0
    })
  },
  bindClient(id: string, clients: number[]) {
    let clientCode = ClientUtil.getClientCode(clients)
    return get('/user/bind_client', {id, clientCode})
  },
}
