import { r as reactExports, ad as PostHandleTable, a6 as GridLoader, j as jsxRuntimeExports, a7 as TabItem, aK as LoadingMask, a9 as ContentOrganization, ab as formEdit, aE as ViewClass, aZ as updateTabContentFunc, a_ as getDetailContent, a$ as getObjText, b0 as createGridStateString, ah as AxiosRequest } from "./index-D15x63tD.js";
function DialogClsBook({ props }) {
  const gridpanelRef = reactExports.useRef(null);
  const [mainGrid, setMainGrid] = reactExports.useState();
  const [collapsedButtons, setCollapsedButtons] = reactExports.useState({});
  const [countRecords, setCountRecords] = reactExports.useState("0");
  const [tabs, setTabs] = reactExports.useState(null);
  const [needUpdate, setNeedUpdate] = reactExports.useState(false);
  const [deleteFilter, setDeleteFilter] = reactExports.useState();
  const [timer, setTimer] = reactExports.useState(null);
  const [selectedRecordID, setSelectedRecordID] = reactExports.useState(props.ID);
  const tabsPanel = reactExports.useRef();
  const pages = reactExports.useRef([]);
  const buttonsRef = reactExports.useRef();
  reactExports.useLayoutEffect(() => {
    PostHandleTable(props.Module, props.ObjType, void 0, props.SectionID).then((res) => {
      if (res.Pages) {
        pages.current = res.Pages.split("\r\n").map((page) => {
          return { Caption: page };
        });
      }
      if (res.Filter) setDeleteFilter("1");
      if (res.Columns.filter)
        res.Columns = res.Columns.filter((item) => {
          if (item.FieldName !== "Name") return true;
        });
      GridLoader(gridpanelRef.current, props, {
        json: res,
        setSelectedRecord: setSelectedRecordID,
        setCountRecords
      }).then((res2) => {
        setMainGrid(res2);
      });
    });
  }, []);
  reactExports.useEffect(() => {
    if (mainGrid) {
      setTabs(getTabs());
      setNeedUpdate(true);
      setTimeout(() => {
        mainGrid.grid.updateGridSize();
      }, 251);
    }
  }, [mainGrid]);
  reactExports.useLayoutEffect(() => {
    if (selectedRecordID) {
      setNeedUpdate(true);
    }
  }, [selectedRecordID]);
  reactExports.useLayoutEffect(() => {
    if (!collapsedButtons.detail) {
      setTimeout(() => {
        setNeedUpdate(true);
      }, 251);
    }
  }, [collapsedButtons.detail]);
  reactExports.useLayoutEffect(() => {
    if (tabsPanel.current && needUpdate) {
      updateTab(tabsPanel.current.selectedIndex, tabs);
      setNeedUpdate(false);
    }
  }, [needUpdate]);
  function getTabs() {
    let details = [];
    if (pages.current && pages.current.length) {
      details = pages.current.map((page) => {
        return getLoadTab(page.Caption);
      });
    }
    return details;
  }
  function getLoadTab(caption) {
    return /* @__PURE__ */ jsxRuntimeExports.jsx(
      TabItem,
      {
        id: `LoadTab_${caption}`,
        style: {
          textTransform: "none",
          width: "100%",
          height: "100%",
          padding: "0px",
          display: "inline-block"
        },
        label: caption,
        children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { id: `item_${caption}`, load: "true", style: { width: "100%", height: "100%", position: "relative" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(LoadingMask, {}) })
      },
      `TabItem_${caption}`
    );
  }
  function getFullTab(detailItem, content) {
    return /* @__PURE__ */ jsxRuntimeExports.jsx(
      TabItem,
      {
        id: `Tab_${detailItem.Caption}`,
        style: {
          textTransform: "none",
          width: "100%",
          height: "100%",
          padding: "0px",
          display: "inline-block"
        },
        label: detailItem.Caption,
        children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { id: `item_${detailItem.Caption}`, style: { width: "100%", height: "100%", position: "relative" }, children: content })
      },
      `TabItem_${detailItem.Caption}`
    );
  }
  function getClsParams(data) {
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", data.Module).set("comand", "GetClsParams").set("ID", data.ID).set("SectionID", data.SectionID).set("Name", data.Name).set("ObjType", data.ObjType);
    if (data.Info) {
      params.set("Info", data.Info);
    }
    if (data.Path) {
      params.set("Path", data.Path);
    }
    if (data.ChangeID) {
      params.set("ChangeID", data.ChangeID);
    }
    if (data.ProgID) params.set("ProgID", data.ProgID);
    return AxiosRequest(true, params);
  }
  async function getContentForTab(pages2, index) {
    let json;
    let page = pages2.current[index];
    if (page.Info !== void 0) {
      json = await getClsParams({
        Module: props.Module,
        ID: selectedRecordID,
        ObjType: props.ObjType,
        SectionID: props.SectionID,
        Info: page.Info,
        ProgID: page.ProgID,
        Path: page.Path,
        ChangeID: page.ChangeID
      });
      if (json.Props) {
        page.Items = page.Items.map((item) => {
          if (json.Props[item.ShortName]) {
            item = {
              ...item,
              Value: json.Props[item.ShortName].Value,
              EditVal: json.Props[item.ShortName].EditVal,
              ID: json.Props[item.ShortName].ID,
              TextColor: json.Props[item.ShortName].TextColor,
              CheckState: json.Props[item.ShortName].CheckState
            };
            return item;
          }
        });
      }
      page.ObjRef = json.ObjRef;
      page.ParentID = json.ParentID;
      page.DocCfgID = json.ObjType;
      page = pages2.current[index] = { ...page, ...json };
    } else {
      json = await getClsParams({
        Module: props.Module,
        ID: selectedRecordID,
        SectionID: props.SectionID,
        Name: page.Caption,
        ObjType: props.ObjType
      });
      json.DocCfgID = json.ObjType;
      json.ObjType = props.ObjType;
      page = pages2.current[index] = {
        ...page,
        ...json
      };
    }
    switch (page.CLSID) {
      case ViewClass.ClsBook:
        return "clsbook";
      case ViewClass.ParamDlg:
        return "params";
      default:
        return "documents";
    }
  }
  function updateTabsContent(data, firstLoad) {
    if (firstLoad) {
      data.firstLoadFunc();
    } else {
      updateTabContentFunc(data);
    }
  }
  function getReqForContent(selector, detailItem) {
    switch (selector) {
      case "params":
        return async () => detailItem;
    }
  }
  async function updateTab(index, tabs2) {
    if (index !== void 0 && tabs2 && pages.current && pages.current.length) {
      if (collapsedButtons.detail) return;
      clearTimeout(timer);
      const newTimer = setTimeout(async () => {
        const selector = await getContentForTab(pages, index);
        const detailItem = pages.current[index];
        const blockTab = tabsPanel.current.nativeElement.getTabContent(index);
        const firstLoad = blockTab.querySelector("div[load='true']");
        updateTabsContent(
          {
            Module: props.Module,
            detailItem,
            selectedRecordID,
            index,
            element: blockTab.ownerElement,
            selector,
            SectionID: props.SectionID,
            onRequest: getReqForContent(selector, detailItem),
            firstLoadFunc: () => {
              const tabContent = getDetailContent(selector, props, {
                selectedRecordID,
                onRequest: getReqForContent(selector, detailItem),
                detailItem
              });
              const tab = getFullTab(pages.current[index], tabContent);
              tabs2[index] = tab;
              setTabs([...tabs2]);
            }
          },
          firstLoad
        );
      }, 100);
      setTimer(newTimer);
    }
  }
  function checkRequest(request) {
    if (!request) {
      const reqValue = props.KeyField === "Code" ? gridpanelRef.current.grid.source.getFieldValueSync("CODE") : gridpanelRef.current.grid.source.getFieldTextSync(props.KeyField.toUpperCase());
      return { Name: "ObjRef", Value: reqValue };
    }
    return request;
  }
  async function onSelect(func, requestId, from, req) {
    const request = checkRequest(req);
    let selection = "";
    if (props.MultiSel) {
      selection = [];
      for (let value of Object.values(mainGrid.grid.ClsSelection)) {
        selection.push(value);
      }
      selection = selection.join(",");
    }
    if (props.onEdit && props.setValue && props.setInputValue) {
      const objRef = props.KeyField === "Code" ? gridpanelRef.current.grid.source.getFieldValueSync("CODE") : gridpanelRef.current.grid.source.getFieldTextSync(props.KeyField.toUpperCase());
      if (props.record) {
        props.onEdit({
          tag: objRef,
          name: props.Name,
          fieldname: props.FieldName,
          current: props.Current,
          record: props.record,
          textchanged: "0",
          selection,
          multiselect: props.MultiSel,
          setValue: props.setValue,
          setInputValue: props.setInputValue,
          type: props.Module,
          requestId: props.RequestID,
          addInfo: props.addInfo
        });
        return;
      } else if (props.setValue && props.setInputValue) {
        props.onEdit({
          value: (await getObjText(props.Module, props.ObjType, objRef, "0")).Text,
          tag: objRef,
          name: props.Name,
          setValue: props.setValue,
          setInputValue: props.setInputValue,
          type: props.Module,
          requestId: props.RequestID,
          addInfo: props.addInfo
        });
        return;
      }
    }
    await formEdit("1", func, request, requestId, from);
  }
  function showOnlyActual(module, objType, value) {
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", module).set("comand", "ShowOnlyActual").set("ObjType", objType).set("Value", value);
    return AxiosRequest(true, params);
  }
  function changeStatus(module, objType, id, hidden) {
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", module).set("comand", "ChangeStatus").set("ObjType", objType).set("ID", id).set("Hidden", hidden);
    return AxiosRequest(true, params);
  }
  async function contextMenuHandler(data) {
    const comand = data.value;
    const objRef = props.KeyField === "Code" ? gridpanelRef.current.grid.source.getFieldValueSync("CODE") : gridpanelRef.current.grid.source.getFieldTextSync(props.KeyField.toUpperCase());
    switch (comand) {
      case "ShowActualCls":
        showOnlyActual(props.Module, props.ObjType, data.state ? "0" : "1");
        mainGrid.grid.refreshSource();
        break;
      case "ShowCls":
        changeStatus(props.Module, props.ObjType, objRef, data.state);
        mainGrid.grid.refreshSource();
        break;
      case "HideCls":
        changeStatus(props.Module, props.ObjType, objRef, data.state);
        mainGrid.grid.refreshSource();
        break;
    }
  }
  function getBookContextMenu() {
  }
  function resetFilter(module, objType, sectionID) {
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", module).set("comand", "ResetFilter").set("ObjType", objType).set("SectionID", sectionID);
    return AxiosRequest(true, params);
  }
  function onDeleteFilter() {
    setDeleteFilter(void 0);
    resetFilter(props.Module, props.ObjType, props.SectionID);
    mainGrid.grid.refreshSource();
  }
  async function saveGridState() {
    const state = createGridStateString(mainGrid.source.getGridState());
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", "config").set("comand", "SaveGridState").set("SectionID", props.SectionID).set("ObjType", props.ObjType);
    return AxiosRequest(true, params, { State: state });
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsx(
    ContentOrganization,
    {
      props,
      data: {
        getCollapsed: setCollapsedButtons,
        gridPanel: gridpanelRef,
        mainGrid,
        countRecords,
        tabs,
        tabsPanel,
        onDeleteFilter,
        updateTab,
        mainMenuSelector: "clsbook",
        mainMenuRequest: getBookContextMenu,
        ContextMenuHandler: contextMenuHandler,
        onSelect: async () => {
          await saveGridState();
          return onSelect(props.func, props.RequestID, props.from, props.request);
        },
        onCancel: async () => {
          await saveGridState();
          return formEdit("0", props.func, props.RequestID, props.from, props.request);
        },
        docFooter: true,
        buttonsRef
        // selectedRecordID: selectedRecordID ? selectedRecordID : "0",
      },
      buttons: {
        detail: true,
        deleteFilter
      }
    }
  );
}
export {
  DialogClsBook as default
};
//# sourceMappingURL=DialogClsBook-Bom3EoDl.js.map
