import { r as reactExports, aE as ViewClass, aF as returnPageBasedOnCLSID, j as jsxRuntimeExports, a7 as TabItem, aG as styles, I as IconButton, aH as SaveOutlinedIcon, aI as Tabs, aJ as getButtons, aK as LoadingMask, ah as AxiosRequest, ac as tokenProcessingTest } from "./index-D15x63tD.js";
import { P as PrintIcon, D as DocumentScannerOutlinedIcon } from "./PrintOutlined-B9HvI__H.js";
function DialogMultiSection({ props }) {
  const tabPanel = reactExports.useRef(null);
  const [multipage, setMultipage] = reactExports.useState();
  const [content, setContent] = reactExports.useState();
  const [reportButtonsVisible, setReportButtonsVisible] = reactExports.useState(false);
  reactExports.useEffect(() => {
    setMultipage(props.json);
  }, []);
  reactExports.useEffect(() => {
    if (multipage) {
      firstLoadContent();
    }
  }, [multipage]);
  async function firstLoadContent() {
    if (multipage && multipage.Pages !== void 0) {
      if (multipage.Pages[0].CLSID === ViewClass.CellObj) {
        setReportButtonsVisible(true);
      } else {
        setReportButtonsVisible(false);
      }
      if (multipage.Pages.length === 0) return;
      const firstLoadedElement = await returnPageBasedOnCLSID(
        multipage.Pages[0],
        void 0,
        props.SectionID
      );
      const elements = multipage.Pages.map((page, index) => {
        const innerContent = index === 0 && firstLoadedElement || /* @__PURE__ */ jsxRuntimeExports.jsx(LoadingMask, {});
        if (page.CLSID) {
          return /* @__PURE__ */ jsxRuntimeExports.jsx(TabItem, { style: { height: "100%", width: "100%" }, label: page.Name, children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { height: "100%", width: "100%" }, children: innerContent }) }, `multipage-${index}`);
        }
      });
      setContent([...elements]);
      return;
    }
    setContent(/* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, {}));
  }
  async function updatePage(ev) {
    if (ev.target !== null) return;
    if (content) {
      const id = tabPanel.current.selectedIndex || 0;
      const page = multipage.Pages[id];
      if (page.CLSID === ViewClass.CellObj) {
        setReportButtonsVisible(true);
      } else {
        setReportButtonsVisible(false);
      }
      const innerContent = await returnPageBasedOnCLSID(page, void 0, props.SectionID);
      content[id] = /* @__PURE__ */ jsxRuntimeExports.jsx(TabItem, { style: { height: "100%", width: "100%" }, label: page.Name, children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { height: "100%", width: "100%" }, children: innerContent }) }, `multipage-${id}`);
      setContent([...content]);
    }
  }
  function printReport() {
    let frame, tab;
    tab = tabPanel.current.nativeElement.querySelector("smart-tab-item[selected]");
    if (tab) {
      frame = tab.querySelector("IFRAME");
    }
    if (frame && frame.contentWindow) {
      frame.contentWindow.print();
    }
  }
  const getPages = async (panel, result) => {
    var _a, _b;
    if (!panel) return;
    const tabsContent = panel.querySelector(".smart-tabs-content-section");
    if (!tabsContent) return;
    const tabsChildren = Array.from(tabsContent.children);
    if (tabsChildren.length) {
      for (let child of tabsChildren) {
        const childPanel = (_b = (_a = child.firstChild) == null ? void 0 : _a.firstChild) == null ? void 0 : _b.firstChild;
        if (childPanel && childPanel.tagName === "SMART-TABS") {
          result.push({
            PageIndex: childPanel.selectedIndex || 0,
            Pages: []
          });
          await getPages(childPanel, result[result.length - 1].Pages);
        } else {
          let resultProperty = "ObjRef";
          let gridPanel = child.querySelector("#rcgrid_panel");
          if (!gridPanel) {
            gridPanel = child.querySelector("[rcsgrid_panel]");
            resultProperty = "Current";
          }
          if (!gridPanel || !gridPanel.grid) {
            result.push({});
            continue;
          }
          const recordSource = gridPanel.grid.source;
          const key = recordSource.getKeyFieldName() || "ID";
          const objRef = recordSource.getFieldValueSync(key) || gridPanel.grid.source.getFieldTextSync(key);
          result.push({ [resultProperty]: objRef });
        }
      }
    }
  };
  const onSelect = async (ev) => {
    var _a;
    const isDlgButton = ev.target.classList.contains("button_Modal_Select") || ev.target.classList.contains("button_Modal_Close");
    if (isDlgButton && props.RequestID) {
      const pagesData = [];
      await getPages(tabPanel.current.nativeElement, pagesData);
      const data = {
        Result: ev.target.value,
        PageIndex: tabPanel.current.selectedIndex || 0,
        Pages: pagesData
      };
      const params = /* @__PURE__ */ new Map();
      params.set("prefix", "project");
      params.set("comand", "ResumeRequest");
      params.set("RequestID", props.RequestID);
      params.set("WSM", "1");
      const json = await AxiosRequest(true, params, data);
      if (json && json.Token) {
        tokenProcessingTest(json, { func: props.func, from: props.from, propsId: props.RequestID });
      } else (_a = props.func) == null ? void 0 : _a.call(props);
    }
  };
  return /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: styles.mainDialogBlock, children: [
    reportButtonsVisible && /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: styles.dialogHeaderContainer, children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(IconButton, { title: "Печать документа", className: styles.dialogHeaderButton, onClick: printReport, children: /* @__PURE__ */ jsxRuntimeExports.jsx(PrintIcon, {}) }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(IconButton, { title: "Параметры документа", className: styles.dialogHeaderButton, children: /* @__PURE__ */ jsxRuntimeExports.jsx(DocumentScannerOutlinedIcon, {}) }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(IconButton, { title: "Сохранить в файл", className: styles.dialogHeaderButton, onClick: () => {
      }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(SaveOutlinedIcon, {}) })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: `${styles.fullDialogFlexContainer}`, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
      Tabs,
      {
        ref: tabPanel,
        onChange: updatePage,
        animation: "none",
        style: {
          height: "100%",
          width: "100%",
          backgroundColor: "s"
        },
        children: content
      }
    ) }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: styles.dialogButtonsContainer, onClick: onSelect, children: getButtons(props.json) })
  ] }) });
}
export {
  DialogMultiSection as default
};
//# sourceMappingURL=DialogMultiSection-R8CQrWj3.js.map
