import { usePagging } from "@/hook/pagging";
import {
  FirePaggingRoute,
  FireStatus,
  delFire,
  getFirePagging,
} from "@/app/fire/store/fire";
import { computed, watch, watchEffect } from "vue";
import { router } from "@/router";
import { FireRouteName } from "@/app/fire/routeConfig";
import { UN_REQ_NUM } from "@/constant/sys";

export const useFirePagging = () => {
  const isTeached = computed(
    () => router.currentRoute.value.name === FireRouteName.teaching
  );

  const pagging = usePagging({
    get: getFirePagging,
    del: delFire,
    mapper: {
      delMsg: "删除火调项目，相关档案也会一并删除，确定要删除吗？",
    },
    paramsTemlate: {
      projectSn: "",
      projectName: "",
      projectAddress: "",
      deptId: "",
      accidentDate: "",
      status: FireStatus.all,
      projectSiteCode: "",
      fireReason: UN_REQ_NUM.toString(),
      organizerUsers: "",
    } as any,
  });

  watch(
    () => [pagging.state.query.queryType, isTeached.value],
    () => {
      pagging.state.query.queryType = isTeached.value
        ? FirePaggingRoute.teached
        : FirePaggingRoute.fire;
    },
    { flush: "sync", immediate: true }
  );

  const params = [{ ...pagging.state.query }, { ...pagging.state.query }];
  watchEffect(
    () => {
      pagging.state.query = params[isTeached.value ? 0 : 1];
    },
    { flush: "sync" }
  );

  return { pagging, isTeached };
};
export type FirePagging = ReturnType<typeof useFirePagging>["pagging"];
