import {
  addTreeitem,
  axios,
  delTreeitem,
  editTreeitem,
  getTreeselect,
} from "@/request";

export enum DeptType {
  corps = 1,
  detachment = 2,
  brigade = 3,
}

export type Organization = {
  ancestors: string;
  children?: Organization[];
  deptType: DeptType;
  id: string;
  leader: string;
  level: number;
  name: string;
  parentId: string;
  parentName: string;
  phone: string;
  remark: string;
};

export const getOrganizationTree = async (type?: string) =>
  (await axios.get<Organization[]>(getTreeselect, { params: { type } })).data;

export const addOrganization = async (
  dept: Omit<Organization, "id">,
  deptIds: string[]
) => {
  await axios.post(addTreeitem, {
    superior: "sheq",
    ...dept,
    deptIdList: deptIds.join(","),
  });
};

export const setOrganization = async (
  dept: Organization,
  deptIds: string[]
) => {
  await axios.post(editTreeitem, {
    superior: "sheq",
    ...dept,
    deptIdList: deptIds.join(","),
  });
};

export const delOrganization = async (deptId: string) =>
  axios.post(delTreeitem + deptId);
