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

export const useScenePaggingParams = () => {
  const pagging = usePagging({
    get: getCaseFusionList,
    paramsTemlate: {
      isShare: 0,
      sceneName: "",
      modelTitle: "",
      fusionTitle: "",
      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.fusionTitle = val;
      pagging.state.query.sceneName = val;
    },
  });
  const isShare = computed({
    get: () =>pagging.state.query.isShare,
    set: (val) => {
      pagging.state.query.isShare = val;
    },
  });
  let oldSnCode = pagging.state.query.snCode;
  watchEffect(() => {
    console.log('pagging.state.query', pagging.state.query);
    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, isShare });
};
export type ScenePagging = ReturnType<typeof useScenePaggingParams>["pagging"];
