import { UN_REQ_NUM } from "@/constant/sys";
import {
  PaggingReq,
  PaggingRes,
  axios,
  fireDetail,
  getFireList,
  getMessageList,
  insertFire,
  insertMessage,
  setTeach,
  setUnTeach,
  updateFire,
} from "@/request";

export enum FireStatus {
  all = UN_REQ_NUM,
  incomplete = 0,
  complete = 1,
}

export type Fire = {
  accidentDate: string;
  createTime: string;
  creatorDeptId: string;
  caseId: number;
  creatorId: string;
  creatorName: string;
  deptId: string;
  editTime: string;
  editorId: string;
  editorName: string;
  fireReason: string;
  id: string;
  isTeached: number;
  organizerDeptName: string;
  organizerUsers: string;
  projectAddress: string;
  projectName: string;
  projectSite: string;
  projectSiteCode: string;
  projectSn: string;
  status: FireStatus;
  statusDesc: string;
  updateTime: string;
  isDelete?: number;
};

export enum FirePaggingRoute {
  fire = 1,
  teached = 2,
}

type FirePaggingParams = PaggingReq<Fire & { queryType: FirePaggingRoute }>;
export const getFirePagging = async (params: FirePaggingParams) =>
  (await axios.get(getFireList, { params })).data as PaggingRes<Fire>;

export const addFire = async (fire: Omit<Fire, "id">) =>
  axios.post(insertFire, fire);

export const setFire = async (fire: Fire) => await axios.post(updateFire, fire);

export const delFire = (fire: Fire) => setFire({ ...fire, isDelete: 1 });

export const getFire = async (projectId: string) =>
  (await axios.get<Fire>(fireDetail, { params: { type: 0, projectId } })).data;

export const revokeFireTeachs = (ids: string[]) =>
  axios.post(setUnTeach, { ids: ids.join(",") });

export const setFireTeachs = (ids: string[]) =>
  axios.post(setTeach, { ids: ids.join(",") });

export type FireLeaveMsg = {
  id: string;
  content: string;
  userName: string;
  createTime: number;
};

export const getFireLeaveMsgPagging = async (
  params: PaggingReq<{ projectId: string }>
) =>
  (await axios.get(getMessageList, { params }))
    .data as PaggingRes<FireLeaveMsg>;

export const addFireLeaveMsg = (params: {
  content: string;
  projectId: string;
}) => axios.post(insertMessage, params);
