import { u as useAtomValue, z as currentTheme, j as jsxRuntimeExports, bT as Card, bU as CardActionArea, G as Grid, bb as clsx, bV as Typography, E as useAtom, b7 as selectedSectionAtom, r as reactExports, bW as HideAllFooterData, V as isEmptyObject, bX as openDocumentInModal, bG as SmetaTabs, bH as SmetaTabItem, bY as Scrollbars, b9 as SectionDocuments, bf as Prefixes, bg as smetaAxios, bZ as clearNoneBreadCrumbs, aW as makeCallBacks } from "./index-D15x63tD.js";
const DocumentCard = ({
  item: { DocCfgID: docCfgId, Name: name, RecordCount: recordCount },
  onClick
}) => {
  const theme = useAtomValue(currentTheme);
  return /* @__PURE__ */ jsxRuntimeExports.jsx(Card, { style: {}, sx: { width: 310, height: 100 }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
    CardActionArea,
    {
      doccfgid: docCfgId,
      name,
      sx: { height: "100px" },
      onClick: () => onClick({ DocCfgID: docCfgId, Name: name }),
      children: /* @__PURE__ */ jsxRuntimeExports.jsxs(Grid, { container: true, direction: "row", justifyContent: "flex-start", alignItems: "center", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          Grid,
          {
            className: clsx("headerStimate", {
              light: theme === "light"
            }),
            item: true,
            sx: {
              width: "100px",
              height: "100px"
            },
            children: /* @__PURE__ */ jsxRuntimeExports.jsxs(
              "svg",
              {
                xmlns: "http://www.w3.org/2000/svg",
                width: "48",
                height: "48",
                viewBox: "0 0 24 24",
                className: clsx("iconColorStimatesvg", {
                  light: theme === "light"
                }),
                style: {
                  marginTop: "25%",
                  marginLeft: "25%"
                },
                children: [
                  /* @__PURE__ */ jsxRuntimeExports.jsx("path", { d: "M19.903 8.586a.997.997 0 0 0-.196-.293l-6-6a.997.997 0 0 0-.293-.196c-.03-.014-.062-.022-.094-.033a.991.991 0 0 0-.259-.051C13.04 2.011 13.021 2 13 2H6c-1.103 0-2 .897-2 2v16c0 1.103.897 2 2 2h12c1.103 0 2-.897 2-2V9c0-.021-.011-.04-.013-.062a.952.952 0 0 0-.051-.259c-.01-.032-.019-.063-.033-.093zM16.586 8H14V5.414L16.586 8zM6 20V4h6v5a1 1 0 0 0 1 1h5l.002 10H6z" }),
                  /* @__PURE__ */ jsxRuntimeExports.jsx("path", { d: "M8 12h8v2H8zm0 4h8v2H8zm0-8h2v2H8z" })
                ]
              }
            )
          },
          `div-1-${docCfgId}}`
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Grid, { item: true, sx: { ml: 2, width: "calc(100% - 155px)" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
          Typography,
          {
            variant: "subtitle1",
            component: "div",
            doccfgid: docCfgId,
            title: name,
            sx: {
              textAlign: "center",
              fontFamily: "Oswald",
              fontSize: "1.25rem",
              letterSpacing: "0.02em",
              lineHeight: "1"
            },
            style: {
              wordWrap: "anywhere",
              display: "flex",
              justifyContent: "center"
            },
            children: name
          }
        ) }, `div-2-${docCfgId}`),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Grid, { item: true, sx: { pt: 1, width: "39px", height: "100px" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
          Typography,
          {
            variant: "caption",
            component: "div",
            doccfgid: docCfgId,
            title: "Количество записей",
            sx: {
              fontFamily: "Oswald",
              textAlign: "center",
              fontSize: "0.75rem",
              lineHeight: "1"
            },
            style: {
              wordWrap: "anywhere"
            },
            children: recordCount || ""
          }
        ) }, `div-3-${docCfgId}`)
      ] })
    }
  ) });
};
const SectionDocumentsChose = ({ id, onSelect, sectionTools, setParentFunc }) => {
  const [selectedSection, setSelectedSection] = useAtom(selectedSectionAtom);
  const [sectionDocs, setSectionDocs] = reactExports.useState([]);
  const [tabIndex, setTabIndex] = reactExports.useState(0);
  const [recordCount, setRecorcCount] = reactExports.useState(0);
  const [selectedDocData, setSelectedDocData] = reactExports.useState({
    DocCfgID: "",
    Name: "",
    Index: "",
    RecordCount: ""
  });
  const theme = useAtomValue(currentTheme);
  const getSectionDocs = async () => {
    const smetaReqConfig = {
      prefix: Prefixes.doc,
      params: {
        sectionID: id,
        simple: 1
      }
    };
    const sectionDocs2 = await smetaAxios.get("GetSectionDocs", smetaReqConfig);
    clearNoneBreadCrumbs();
    setSectionDocs(sectionDocs2);
    if (sectionDocs2.length === 1) {
      setTabIndex(1);
      if (selectedSection.Selected === void 0 || selectedSection.Selected.DocCfgID === void 0) {
        setSelectedDocData({
          DocCfgID: sectionDocs2[0].DocCfgID,
          Name: sectionDocs2[0].Name,
          Index: 0
        });
      }
    }
  };
  const returnToDocSelect = () => {
    makeCallBacks().then((bool) => {
      if (!bool) return;
      setTabIndex(0);
      onSelect && onSelect({}, false);
    });
  };
  reactExports.useEffect(() => {
    getSectionDocs();
    setParentFunc && setParentFunc(returnToDocSelect);
  }, []);
  reactExports.useEffect(() => {
    HideAllFooterData();
  }, [tabIndex]);
  reactExports.useEffect(() => {
    if (!recordCount && !selectedDocData.Index) return;
    const docsWithRecCount = sectionDocs.toSpliced(selectedDocData.Index, 1, {
      DocCfgID: selectedDocData.DocCfgID,
      Name: selectedDocData.Name,
      RecordCount: recordCount
    });
    setSectionDocs(docsWithRecCount);
  }, [recordCount]);
  reactExports.useEffect(() => {
    updateSelected();
  }, [selectedSection, sectionDocs]);
  const findDocInfo = (dcfg) => {
    if (sectionDocs.length === 0 || isEmptyObject(sectionDocs)) return;
    return sectionDocs.find((value) => value.DocCfgID === dcfg);
  };
  const updateSelected = () => {
    if (selectedSection.ID !== id) return;
    if (!(selectedSection == null ? void 0 : selectedSection.Selected) || sectionDocs.length === 0 || isEmptyObject(selectedSection == null ? void 0 : selectedSection.Selected)) return;
    const {
      Selected: { DocCfgID: docCfgId, docid, card, modal }
    } = selectedSection;
    const databoutdoc = findDocInfo(docCfgId || "");
    if (!databoutdoc && isEmptyObject(databoutdoc)) return;
    const index = sectionDocs.indexOf(databoutdoc);
    setSelectedDocData({
      ...databoutdoc,
      Index: index,
      docid,
      card
    });
    if (modal === true) {
      openDocumentInModal(docCfgId || "", docid || "");
    }
    setTabIndex(1);
  };
  reactExports.useEffect(() => {
    if (tabIndex === 0) {
      setSelectedDocData({
        DocCfgID: void 0,
        Name: void 0,
        Index: void 0
      });
    }
  }, [tabIndex]);
  function clickDoc(data) {
    if (onSelect) onSelect(data, true);
    setSelectedSection({ ...selectedSection, Selected: data });
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(
    SmetaTabs,
    {
      tabIndex,
      style: { height: "100%", width: "100%" },
      position: "hidden",
      animation: "none",
      id: "DocumentsAndReports",
      children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          SmetaTabItem,
          {
            style: {
              textTransform: "none",
              paddingBottom: "0px",
              width: "100%",
              display: sectionDocs.length > 0 ? "" : "none"
            },
            label: "Документы",
            children: /* @__PURE__ */ jsxRuntimeExports.jsx(
              Scrollbars,
              {
                autoHide: true,
                className: clsx("backgroundColorStimate", {
                  light: theme === "light"
                }),
                children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                  Grid,
                  {
                    container: true,
                    direction: "column",
                    justifyContent: "flex-start",
                    alignItems: "flex-start",
                    spacing: 2,
                    children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                      Grid,
                      {
                        item: true,
                        container: true,
                        direction: "row",
                        justifyContent: "flex-start",
                        alignItems: "flex-start",
                        spacing: 2,
                        sx: { mt: 0, ml: 0, mb: 1 },
                        style: { width: "initial", height: "inherit" },
                        children: sectionDocs.length > 0 ? sectionDocs.map((item) => {
                          return /* @__PURE__ */ jsxRuntimeExports.jsx(Grid, { item: true, children: /* @__PURE__ */ jsxRuntimeExports.jsx(DocumentCard, { item, onClick: clickDoc }) }, `${item.DocCfgID}`);
                        }) : /* @__PURE__ */ jsxRuntimeExports.jsx("div", {}, "empty-doc-div")
                      }
                    )
                  },
                  "DocumentsSelect" + id
                )
              }
            )
          }
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsx(SmetaTabItem, { style: { textTransform: "none", paddingBottom: "0px", width: "100%" }, label: "Документ", children: /* @__PURE__ */ jsxRuntimeExports.jsx(
          SectionDocuments,
          {
            id,
            DocCfgID: selectedDocData.DocCfgID,
            docName: selectedDocData.Name,
            SectionToolsJS: sectionTools,
            setRecorcCount,
            ObjRef: selectedDocData.docid
          }
        ) })
      ]
    },
    "Section" + id
  );
};
export {
  SectionDocumentsChose as default
};
//# sourceMappingURL=SectionDocumentsChose-CWNMUy0u.js.map
