import axios from 'axios';
import qs from 'query-string';
import type { Pagination } from '@/types/global';
import type { CommonPageResult, CommonStrIdParams } from './common';
import { encryptWithBase64 } from '@/utils/encrypt';
import type { PaymentRecord } from './sys-user-payment';
import type {
  KamiApiSysUserV1UserAddReq,
  KamiApiSysUserV1UserEditReq
} from './generated';

export interface UserRecord extends CommonStrIdParams {
  username: string;
  nickName: string;
  userStatus: number;
  payment?: PaymentRecord;
}

export interface UserRecordWithId extends CommonStrIdParams {
  password: string;
}

export interface UserListParams extends Partial<UserRecord>, Pagination {}

export function queryUserList(params: UserListParams) {
  return axios.get<CommonPageResult<UserRecord>>('/api/user/list', {
    params,
    paramsSerializer: obj => {
      return qs.stringify(obj);
    }
  });
}

export interface UserAddRecord {
  username: string;
  password: string;
}

// 添加用户
export function addUserItem(data: KamiApiSysUserV1UserAddReq) {
  return axios.post('/api/user/add', {
    ...data,
    password: encryptWithBase64(data.password)
  });
}

// 编辑用户
export function updateUserItem(data: KamiApiSysUserV1UserEditReq) {
  return axios.put('/api/user/edit', {
    ...data,
    password: encryptWithBase64(data.password)
  });
}

export function deleteUserItem(params: CommonStrIdParams) {
  return axios.delete('/api/user/delete', {
    params,
    paramsSerializer: obj => {
      return qs.stringify(obj);
    }
  });
}
