import { usePagging } from "@/hook/pagging";
import { SceneType, getScenePagging } from "@/store/scene";
import { computed, reactive, watch, watchEffect } from "vue";

export const useScenePaggingParams = () => {
  const pagging = usePagging({
    get: getScenePagging,
    paramsTemlate: {
      type: SceneType.SWKK,
      sceneName: "",
      modelTitle: "",
      deptId: "",
      snCode: "",
    },
  });

  const isSwmx = computed(() => pagging.state.query.type === SceneType.SWMX);
  const keyword = computed({
    get: () =>
      isSwmx.value
        ? pagging.state.query.modelTitle
        : pagging.state.query.sceneName,
    set: (val: string) => {
      pagging.state.query.modelTitle = val;
      pagging.state.query.sceneName = val;
    },
  });

  let oldSnCode = pagging.state.query.snCode;
  watchEffect(() => {
    if (isSwmx.value) {
      oldSnCode = pagging.state.query.snCode;
      pagging.state.query.snCode = "";
    } else {
      pagging.state.query.snCode = oldSnCode;
    }
  });

  watch(
    () => pagging.state.query.type,
    () => {
      pagging.state.pag.currentPage = 1;
    }
  );

  const queryResetRaw = pagging.queryReset;
  pagging.queryReset = () => {
    const type = pagging.state.query.type;
    queryResetRaw();
    pagging.state.query.type = type;
  };

  return reactive({ pagging, keyword, isSwmx });
};
export type ScenePagging = ReturnType<typeof useScenePaggingParams>["pagging"];
