import type {
  BasicFetchResult,
  BasicPageParams,
  RelationRequest,
  StatusParams,
} from '#/api/model';

import { requestClient } from '#/api/request';

export namespace UserApi {
  export interface PageParams extends BasicPageParams {
    account?: string;
    name?: string;
    phone?: string;
  }

  export interface BasicRecordItem {
    account: string;
    realName: string;
    nickName: string;
    phone: string;
    sex: number;
  }

  export interface RecordItem extends BasicRecordItem {
    id: number;
  }

  export type PageResult = BasicFetchResult<RecordItem>;

  export const getPage = (params: PageParams) =>
    requestClient.get<PageResult>('/user/page', { params });

  export const getDetail = (id: number) =>
    requestClient.get<RecordItem>('/user/entity', {
      params: { id },
    });

  export const addDetail = (data: BasicRecordItem) =>
    requestClient.post('/user', data);

  export const editDetail = (data: RecordItem) =>
    requestClient.put('/user', data);

  export const deleteDetail = (id: number) =>
    requestClient.delete('/user', { data: { id } });

  export const getRoleIds = (id: number) =>
    requestClient.get<number[]>('/user/role-ids', { params: { id } });

  export const updateStatus = (data: StatusParams) =>
    requestClient.put('/user/status', data);
  export const resetPassword = (ids: number[]) =>
    requestClient.post('/user/reset-password', ids);
  export const updateGrantRole = (data: RelationRequest) =>
    requestClient.post('/user/grant', data);

  export const updatePassword = (data: any) =>
    requestClient.put('/user/password', data);
}
