import { request } from "./client";

export interface UserListVO {
  iIncrement: number;
  sUserName: string;
  iStaffId: number | null;
  staffName: string | null;
  sUserNo: string;
  department: string | null;
  sUserType: string;
  sLanguage: string;
  bCanModifyDocs?: boolean;
  permissionCategoryIds?: number[];
  bDeleted: boolean;
  tLastLoginDate: string | null;
  sCreatedBy: string | null;
  tCreateDate: string;
}

export interface PermissionCategoryVO {
  iIncrement: number;
  sCategoryCode: string;
  sCategoryName: string;
  iParentId: number | null;
  iSortOrder: number;
}

export interface UserListPage {
  records: UserListVO[];
  total: number;
  pageNum: number;
  pageSize: number;
}

export interface UserListParams {
  field?: string;
  match?: string;
  value?: string;
  pageNum?: number;
  pageSize?: number;
}

export interface UserDTO {
  sUserNo: string;
  sUserName: string;
  iStaffId?: number | null;
  sUserType: string;
  sLanguage: string;
  bCanModifyDocs?: boolean;
  permissionCategoryIds?: number[];
}

export function listUsers(params: UserListParams = {}): Promise<UserListPage> {
  return request<UserListPage>({
    url: "/usr/users",
    method: "GET",
    params,
  });
}

export function getUser(id: number): Promise<UserListVO> {
  return request<UserListVO>({ url: `/usr/users/${id}`, method: "GET" });
}

export function listPermissionCategories(): Promise<PermissionCategoryVO[]> {
  return request<PermissionCategoryVO[]>({
    url: "/usr/permission-categories",
    method: "GET",
  });
}

export function createUser(dto: UserDTO): Promise<{ iIncrement: number; sUserNo: string }> {
  return request({ url: "/usr/users", method: "POST", data: dto });
}

export function updateUser(id: number, dto: UserDTO): Promise<{ iIncrement: number }> {
  return request({ url: `/usr/users/${id}`, method: "PUT", data: dto });
}
