// 医院列表 Store（管理后台通用）
// 多个页面（orders/list.vue、orders/report.vue 等）需要按医院筛选，
// 这里把"加载医院列表 + 缓存"抽出来，避免每个页面重复请求。
import { defineStore } from "pinia";
import { ref } from "vue";
import { getAdminHospitals } from "@/service/modular/admin";

export const useHospitalStore = defineStore("hospital", () => {
  const list = ref([]);
  const loaded = ref(false);
  const loading = ref(false);

  // 加载医院列表（仅首次加载；之后可手动调用 refresh 强制刷新）
  const ensureLoaded = async (force = false) => {
    if (loading.value) return;
    // if (loaded.value && !force) return;
    loading.value = true;
    try {
      const res = await getAdminHospitals({ page: 1, pageSize: 1000 });
      list.value = res?.list || [];
      loaded.value = true;
    } catch (_) {
      list.value = [];
    } finally {
      loading.value = false;
    }
  };

  const refresh = () => ensureLoaded(true);

  return {
    list,
    loaded,
    loading,
    ensureLoaded,
    refresh,
  };
});