import { getTaProjectOrg } from "@/service/TaProjectOrg";
import { getSysOrg } from "@/service/authCenter";
import { create } from "@zjxpcyc/react-tiny-store";

const useSysOrgModel = create(({ setState }) => {
  const state = {
    isLogin: false,
    orgListKey: [],
  };
  const updateState = (key) => (value) =>
    setState((x) => ({ ...x, [key]: value }));
  const setOrg = updateState("org");

  const getOrgList = () =>
    new Promise((resolve, reject) => {
      getSysOrg({ PageSize: 999 })
        .then((res) => {
          const records = res.records || [];
          const data = [];
          records.forEach((it) => {
            data[it.orgId] = it.name;
          });
          resolve(records);
          setState({
            isLogin: true,
            orgListKey: data,
          });
        })
        .catch((e) => reject(e));
    });
  return {
    state,
    actions: { getOrgList, setOrg },
  };
});

export default useSysOrgModel;
