import { http } from "@/utils/http";

type Result = {
  code: number;
  message: string;
  data?: any;
};

type ResultTable = {
  code: number;
  message: string;
  data?: {
    list: Array<any>;
    total: number;
    pageSize: number;
    currentPage: number;
  };
};

export const getReplenisherList = (params: {
  page?: number;
  pageSize?: number;
  keyword?: string;
  status?: number;
}) => {
  return http.request<ResultTable>("get", "/replenishers/list", { params });
};

export const getReplenisherById = (id: number) => {
  return http.request<Result>("get", `/replenishers/${id}`);
};

export const searchReplenishers = (keyword?: string) => {
  return http.request<Result>(
    "get",
    `/replenishers/search${keyword ? `?keyword=${keyword}` : ""}`
  );
};

export const createReplenisher = (data: {
  name: string;
  phone?: string;
  employeeId?: string;
}) => {
  return http.request<Result>("post", "/replenishers", { data });
};

export const updateReplenisher = (
  id: number,
  data: {
    name?: string;
    phone?: string;
    employeeId?: string;
    status?: number;
  }
) => {
  return http.request<Result>("put", `/replenishers/${id}`, { data });
};

export const updateReplenisherStatus = (id: number, data: { status: number }) => {
  return http.request<Result>("put", `/replenishers/${id}/status`, { data });
};

export const deleteReplenisher = (id: number) => {
  return http.request<Result>("delete", `/replenishers/${id}`);
};

export const getBoundDevices = (id: number) => {
  return http.request<Result>("get", `/replenishers/${id}/devices`);
};

export const bindDevices = (id: number, data: { deviceIds: string[] }) => {
  return http.request<Result>("post", `/replenishers/${id}/devices`, { data });
};

export const unbindDevice = (replenisherId: number, deviceId: string) => {
  return http.request<Result>(
    "delete",
    `/replenishers/${replenisherId}/devices/${deviceId}`
  );
};

export const batchBindDevices = (data: {
  replenisherIds: number[];
  deviceIds: string[];
}) => {
  return http.request<Result>("post", "/replenishers/batch-bind", { data });
};

/**
 * 生成补货员绑定码
 * @param id 补货员ID
 * @returns bindingCode
 */
export const generateBindingCode = (id: number) => {
  return http.request<Result>("post", `/replenishers/${id}/binding-code`);
};
