import { u as useAtomValue, bt as animationsAtom, r as reactExports, dh as getLoadTab, j as jsxRuntimeExports, I as IconButton, dd as ViewComfyOutlinedIcon, ax as SplitterLocal, aU as ContextMenu, aV as getDefaultMenu, aI as Tabs, ad as PostHandleTable, a6 as GridLoader, aZ as updateTabContentFunc, a_ as getDetailContent, di as getFullTab, aX as updateVisibleGrids, ac as tokenProcessingTest, bS as getIcons, dj as ImgURLSRC, ah as AxiosRequest, bM as SectionToolsJS } from "./index-D15x63tD.js";
const configRestoreRequest = (data) => {
  const params = /* @__PURE__ */ new Map();
  params.set("prefix", data.Module).set("comand", "ConfigRestore").set("ID", data.ID);
  return AxiosRequest(true, params);
};
const getDialogParams = (data) => {
  const params = /* @__PURE__ */ new Map();
  params.set("prefix", data.Module).set("comand", "GetDialogParams").set("OnlyView", data.OnlyView).set("ObjType", data.ObjType).set("ObjRef", data.ObjRef);
  return AxiosRequest(true, params);
};
async function getContentForTab(pages, index) {
  let page = pages.current[index];
  const content = {
    Параметры: "params",
    Зависимости: "treeMultiCheck",
    Поиск: "paytypesSearch",
    Связи: "links",
    Программы: "paytypes"
  };
  return content[page.Caption];
}
function PayTypesMainBlock({ props }) {
  const animations = useAtomValue(animationsAtom);
  const gridpanelRef = reactExports.useRef(null);
  const [mainGrid, setMainGrid] = reactExports.useState();
  const [countRecords, setCountRecords] = reactExports.useState("0");
  const [tabs, setTabs] = reactExports.useState(null);
  const [needUpdate, setNeedUpdate] = reactExports.useState(false);
  const [collapsedDetails, setCollapsedDetails] = reactExports.useState(false);
  const [timer, setTimer] = reactExports.useState(null);
  const [selectedRecordID, setSelectedRecordID] = reactExports.useState(props.ID);
  const tabsPanel = reactExports.useRef();
  const Pages = reactExports.useRef([]);
  const loadGrid = async () => {
    const json = await PostHandleTable("paytypes", "2380", void 0, props.SectionID);
    const gridObject = await GridLoader(
      gridpanelRef.current,
      { ...props, Module: "paytypes", ObjType: "2380" },
      {
        json,
        setSelectedRecord: setSelectedRecordID,
        setCountRecords,
        Columns: "Section"
      }
    );
    setMainGrid(gridObject);
  };
  reactExports.useEffect(() => {
    loadGrid();
    Pages.current = [
      { Caption: "Параметры" },
      { Caption: "Зависимости" },
      { Caption: "Поиск" },
      { Caption: "Связи" },
      { Caption: "Программы" }
    ];
  }, []);
  reactExports.useEffect(() => {
    if (!mainGrid) return;
    setTabs(getTabs());
    setNeedUpdate(true);
    if (animations) {
      setTimeout(() => {
        mainGrid.grid.refreshSource("!");
      }, 251);
    }
  }, [mainGrid]);
  reactExports.useEffect(() => {
    if (selectedRecordID) {
      setNeedUpdate(true);
    }
  }, [selectedRecordID]);
  reactExports.useEffect(() => {
    if (tabsPanel.current && needUpdate) {
      updateTab(tabsPanel.current.selectedIndex, tabs);
      setNeedUpdate(false);
    }
  }, [needUpdate]);
  function getTabs() {
    var _a;
    if (!((_a = Pages.current) == null ? void 0 : _a.length)) return;
    return Pages.current.map((page, index) => getLoadTab(page.Caption, index));
  }
  function updateTabsContent(data, firstLoad) {
    if (firstLoad) {
      data.firstLoadFunc();
    } else {
      updateTabContentFunc(data);
    }
  }
  function getReqForContent(selector) {
    const requests = {
      params: async () => {
        const params = /* @__PURE__ */ new Map();
        params.set("prefix", "paytypes").set("comand", "GetPayTypeParams").set("GroupID", "0").set("Code", selectedRecordID).set("NeedRefresh", "1");
        return AxiosRequest(true, params);
      },
      links: async (id) => {
        const params = /* @__PURE__ */ new Map();
        params.set("prefix", "paytypes").set("comand", "GetPayTypeLinks").set("Code", id !== void 0 ? id : selectedRecordID);
        return AxiosRequest(true, params);
      }
    };
    return requests[selector];
  }
  function onSearch(id) {
    mainGrid.grid.refreshSource(id);
  }
  async function updateTab(index, tabs2) {
    if (!(index !== void 0 && tabs2 && Pages.current && Pages.current.length && selectedRecordID)) 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: "payTypes",
          detailItem,
          selectedRecordID,
          index,
          element: blockTab.ownerElement,
          selector,
          SectionID: props.SectionID,
          onRequest: getReqForContent(selector),
          onSearch,
          firstLoadFunc: () => {
            const tabContent = getDetailContent(selector, props, {
              selectedRecordID,
              onRequest: getReqForContent(selector),
              onSearch,
              detailItem
            });
            const tab = getFullTab(Pages.current[index], tabContent, index);
            tabs2[index] = tab;
            setTabs([...tabs2]);
          }
        },
        firstLoad
      );
    }, 100);
    setTimer(newTimer);
  }
  const changeDataOfItem = (val) => {
    var _a;
    const changeValue = {
      ConfigRestore: changeConfigRestore
    };
    return ((_a = changeValue[val.value]) == null ? void 0 : _a.call(changeValue, val)) || val;
  };
  const changeConfigRestore = (val) => {
    const newOptions = {
      Disabled: mainGrid.source.getFieldTextSync("ID") < 0
    };
    return { ...val, ...newOptions };
  };
  async function contextMenuHandler(data) {
    var _a;
    const comands = {
      ConfigRestore: configRestore
    };
    (_a = comands[data.value]) == null ? void 0 : _a.call(comands);
  }
  const configRestore = async () => {
    await configRestoreRequest({ Module: "paytypes", ID: selectedRecordID });
    mainGrid.grid.refreshSource("!");
  };
  function refreshGrid() {
    updateVisibleGrids();
  }
  function collapseDetail() {
    setCollapsedDetails(!collapsedDetails);
  }
  const openPaytypesSettings = async () => {
    const json = await getDialogParams({ Module: "paytypes", ObjType: "2290", OnlyView: "1", ObjRef: "0" });
    if (!json.Break) json.Break = "";
    if (!json.Token) json.Token = "HandleTable";
    tokenProcessingTest(json, { props: { Buttons: 2 } });
  };
  function getIcon(data) {
    const json = getIcons(data.image);
    if (json) {
      return ImgURLSRC(json);
    }
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "mainBlock", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "headerPaytypesContainer", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(IconButton, { title: "Детали", onClick: collapseDetail, children: /* @__PURE__ */ jsxRuntimeExports.jsx(ViewComfyOutlinedIcon, { sx: { fontSize: "large" } }) }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(IconButton, { title: "Наборы видов оплаты", onClick: openPaytypesSettings, children: /* @__PURE__ */ jsxRuntimeExports.jsx("img", { src: getIcon({ image: "24" }) }) })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "fullFlexContainer", children: /* @__PURE__ */ jsxRuntimeExports.jsxs(
      SplitterLocal,
      {
        onResizeEnd: refreshGrid,
        onCollapse: refreshGrid,
        onExpand: refreshGrid,
        orientation: "vertical",
        style: {
          width: "100%",
          height: "100%"
        },
        transitionDuration: 251,
        primaryIndex: 0,
        collapsed: collapsedDetails,
        sizeSecondary: "20%",
        hideButtons: true,
        children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "fullSpaceContainer", children: /* @__PURE__ */ jsxRuntimeExports.jsx(
            ContextMenu,
            {
              for: "paytypes",
              changeDataOfItem,
              Menu: getDefaultMenu("paytypes"),
              onMenuItemClick: contextMenuHandler,
              children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                "div",
                {
                  id: `rcgrid_panel`,
                  style: {
                    height: "100%",
                    width: "100%",
                    position: "relative"
                  },
                  ref: gridpanelRef
                }
              )
            }
          ) }),
          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "fullSpaceContainer", children: /* @__PURE__ */ jsxRuntimeExports.jsx(
            Tabs,
            {
              enableMouseWheelAction: true,
              onChange: (ev) => {
                if (ev.target) return;
                setNeedUpdate(true);
              },
              style: { height: "100%", width: "100%" },
              ref: tabsPanel,
              children: tabs
            }
          ) })
        ]
      }
    ) })
  ] });
}
function SectionPayTypes(props) {
  return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "mainSectionBlock", children: [
    props.SectionID && props.SectionID > 100 && /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "sectionToolsContainer", children: /* @__PURE__ */ jsxRuntimeExports.jsx(SectionToolsJS, { ID: props.SectionID }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "fullFlexContainer", children: /* @__PURE__ */ jsxRuntimeExports.jsx(PayTypesMainBlock, { props }) })
  ] }) });
}
export {
  SectionPayTypes as default
};
//# sourceMappingURL=SectionPayTypes-BO5EvqNM.js.map
