import axios from './instance'
import {
  GET_MEMBER_LIST,
  ADD_MEMBER,
  DEL_MEMBER,
  UPDATE_MEBER,
  CHECK_MEMBER_PHONE
} from './constant'
import type { PageResult, PageRequest, Project, Role } from './'

export interface Member {
  teamId: number
  userName: string
  nickName: string
  projectName?: Project['projectName']
  projectId: Project['projectId']
  roleName?: Role['roleName']
  roles: Role[]
  bindAccount: string
  remark: string
}

export type Members = Member[]

export const fetchMembers = (
  params: PageRequest<
    Pick<Member, 'userName'> & { projectId: Project['projectId'] }
  >
) => axios.post<PageResult<Member>>(GET_MEMBER_LIST, params)

export const deleteMember = (teamId: Member['teamId']) =>
  axios.post(DEL_MEMBER, { teamId })

export type SetMemberProps = Omit<PartialPart<Member, 'teamId'>, 'roles'> & {
  roles: Role['roleId'][]
}
export const addMember = (member: SetMemberProps) =>
  axios.post(ADD_MEMBER, { ...member })

export const updateMember = (
  member: Omit<SetMemberProps, 'projectId'> & { teamId: Member['teamId'] }
) => axios.post(UPDATE_MEBER, member)

export const checkMemberUserName = (userName: Member['userName']) =>
  axios.get(CHECK_MEMBER_PHONE, { params: { userName } })
