import { computed, ref, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import { isNil } from "lodash";

// project 页面及其子页面获取项目 ID
export default function useProjectID() {
  const route = useRoute();
  console.log("route", route);
  const router = useRouter();

  // 是否已经获取到了 projectID
  const shouldRender = ref(false);

  // 项目 ID
  const projectID = computed(() => route.query.projectID);
  watch(
    projectID,
    async (val) => {
      if (route.path.startsWith("/project") && (isNil(val) || val === "")) {
        shouldRender.value = false;
        await router.replace("/project-list");
      } else {
        shouldRender.value = true;
      }
    },
    {
      immediate: true,
    }
  );

  return {
    // 是否已经获取到了 projectID
    shouldRender,
    projectID,
  };
}
