import { r as reactExports, b8 as SmetaModalsButtonsContext, j as jsxRuntimeExports, bb as clsx, bc as dialogStyles, ai as Editor, bd as CheckBoxMultiCheck, t as React, a3 as Tree, a5 as GetDialogParamsForSearch, a6 as GridLoader, ah as AxiosRequest, a7 as TabItem, aK as LoadingMask, be as IMemo, a8 as Search, a9 as ContentOrganization, aa as getBookContextMenu, aZ as updateTabContentFunc, a_ as getDetailContent, ac as tokenProcessingTest, ab as formEdit, an as CheckOutlinedIcon, ap as smetaModals, ar as getStyleWindow, bf as Prefixes, bg as smetaAxios, ad as PostHandleTable, ae as getRightsData, af as parseColumns, ag as handleTable, bh as CreateCollapsibleRecords, ba as CheckMultiSelect } from "./index-D15x63tD.js";
function DialogCreateGroup(props) {
  const { name = "", number = "", onSelect } = props;
  const [editorsValues, setEditorsValues] = reactExports.useState({
    name,
    count: "0",
    number,
    firstNumber: "1"
  });
  const [checkState, setCheckState] = reactExports.useState("0");
  const [disabledCard, setDisabledCard] = reactExports.useState(true);
  const setButtons = reactExports.useContext(SmetaModalsButtonsContext);
  reactExports.useEffect(() => {
    setButtons == null ? void 0 : setButtons({
      1: { label: "Создать", onClick: onSelectButtonClick, disabled: !(Number(editorsValues.count) > 0) },
      2: {
        label: "Отмена"
      }
    });
  }, [setButtons, editorsValues, checkState]);
  const changeCardActive = () => {
    if (checkState === "1") {
      setDisabledCard(false);
    } else setDisabledCard(true);
  };
  reactExports.useEffect(changeCardActive, [checkState]);
  const fixNegativeCount = () => {
    const editValCount = Number(editorsValues.count);
    if (!isNaN(editValCount) && editValCount < 0) {
      editorsValues.count = "0";
      setEditorsValues({ ...editorsValues });
    }
  };
  reactExports.useEffect(fixNegativeCount, [editorsValues]);
  const onEdit = (data) => {
    setEditorsValues({ ...editorsValues, [data.name]: data.value });
  };
  const onCheck = (data) => {
    setCheckState(data.CheckState);
  };
  const onSelectButtonClick = () => {
    const data = {
      name: editorsValues.name,
      code: editorsValues.number,
      first: editorsValues.firstNumber,
      count: editorsValues.count,
      group: checkState === "1"
    };
    onSelect(data);
  };
  return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: clsx(dialogStyles.mainDialogBlock), children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: clsx(dialogStyles.fullDialogFlexContainer, dialogStyles.dialogOverflow), children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: clsx(dialogStyles.mainDialogBlock), children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: clsx(dialogStyles.dialogCreateGroupCard), children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: clsx(dialogStyles.dialogCreateGroupItem), children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: clsx(dialogStyles.dialogCreateGroupItemLabel), children: "Наименование объекта:" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: clsx(dialogStyles.dialogCreateGroupItemEditor), children: /* @__PURE__ */ jsxRuntimeExports.jsx(Editor, { name: "name", value: editorsValues == null ? void 0 : editorsValues.name, onEdit }) })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: clsx(dialogStyles.dialogCreateGroupItem), children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: clsx(dialogStyles.dialogCreateGroupItemLabel), children: "Количество:" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          "div",
          {
            className: clsx(
              dialogStyles.dialogCreateGroupItemEditor,
              dialogStyles.dialogCreateGroupItemEditorShort
            ),
            children: /* @__PURE__ */ jsxRuntimeExports.jsx(Editor, { name: "count", value: editorsValues == null ? void 0 : editorsValues.count, editStyle: 8, onEdit })
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: clsx(dialogStyles.dialogCreateGroupCard), children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: clsx(dialogStyles.dialogCreateGroupCheckItem), children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: clsx(dialogStyles.dialogCreateGroupItemLabel), children: "Создать в группе:" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: clsx(dialogStyles.dialogCreateGroupCheck), children: /* @__PURE__ */ jsxRuntimeExports.jsx(CheckBoxMultiCheck, { CheckState: checkState, onEdit: onCheck }) })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: clsx(dialogStyles.dialogCreateGroupItem), children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          "div",
          {
            className: clsx(
              dialogStyles.dialogCreateGroupItemLabel,
              disabledCard && dialogStyles.dialogCreateGroupLabelDisabled
            ),
            children: "Инвентарный номер:"
          }
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: clsx(dialogStyles.dialogCreateGroupItemEditor), children: /* @__PURE__ */ jsxRuntimeExports.jsx(Editor, { disabled: disabledCard, name: "number", value: editorsValues == null ? void 0 : editorsValues.number, onEdit }) })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: clsx(dialogStyles.dialogCreateGroupItem), children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          "div",
          {
            className: clsx(
              dialogStyles.dialogCreateGroupItemLabel,
              disabledCard && dialogStyles.dialogCreateGroupLabelDisabled
            ),
            children: "Первый номер:"
          }
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          "div",
          {
            className: clsx(
              dialogStyles.dialogCreateGroupItemEditor,
              dialogStyles.dialogCreateGroupItemEditorShort
            ),
            children: /* @__PURE__ */ jsxRuntimeExports.jsx(
              Editor,
              {
                disabled: disabledCard,
                name: "firstNumber",
                value: editorsValues == null ? void 0 : editorsValues.firstNumber,
                onEdit,
                editStyle: 8
              }
            )
          }
        )
      ] })
    ] })
  ] }) }) });
}
const getDialogParams = async (data) => {
  const params = /* @__PURE__ */ new Map([
    ["prefix", data.module],
    ["comand", "GetDialogParams"],
    ["ObjType", data.objType],
    ["ObjRef", data.objRef]
  ]);
  return AxiosRequest(true, params);
};
async function saveCurrentGrid(data) {
  const json = await PostHandleTable(
    data.props.Module,
    "0",
    data.groupID,
    data.props.SectionID,
    data.OrgTypeCls ? data.OrgTypeCls : "0"
  );
  if (json) {
    data.gridObject.source.rights = getRightsData(json.Rights);
  }
  if (json && json.Details && data.detailValues) {
    data.detailValues.set(data.groupID, json.Details.items);
  }
  const objType = json ? json.ObjType : data.props.ObjType;
  const columns = parseColumns(json, { collapsibleField: "Name" });
  data.gridObject.source.onHandleRequest = handleTable({
    Module: data.props.Module,
    ObjType: objType,
    GroupID: data.groupID,
    selectedRecordID: data.ObjRef ? data.ObjRef : "0",
    SectionID: data.props.SectionID,
    OrgTypeCls: data.OrgTypeCls ? data.OrgTypeCls : "0"
  });
  data.gridObject.grid.canModify = data.canModify;
  data.gridObject.source.CreateCollapsibleRecords = CreateCollapsibleRecords;
  data.gridObject.grid.setColumns(columns);
  data.gridObject.source.onRecordIndexChanged = function(source) {
    data.setSelectedRecordID(source.getFieldTextSync("ID"));
  };
  data.gridObject.source.onRecordCollapsed = async function(event) {
    let RecordIndex;
    if (event.currentTarget !== void 0) {
      RecordIndex = event.currentTarget.getAttribute("recordindex");
    } else {
      RecordIndex = event;
    }
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", data.props.Module).set("comand", "RecordCollapsed").set("SectionID", data.SectionID).set("ObjType", objType).set("RecordIndex", RecordIndex);
    await AxiosRequest(true, params);
    await data.gridObject.grid.refreshSource("!");
  };
  data.gridObject.grid.setImageFields(["Options"]);
  data.gridObject.source.booksGetImage = (value) => {
    if (value & 4) return 3;
    return 0;
  };
  await data.gridObject.grid.setSource(data.gridObject.source);
  await data.gridObject.grid.refreshSource(data.ObjRef ? data.ObjRef : "0");
  await data.gridObject.grid.updateGridSize();
  const id = data.gridObject.source.getFieldTextSync("ID");
  data.setSelectedRecordID(id);
  data.mainSources.set(data.groupID, {
    columns,
    ID: id,
    request: handleTable,
    objType
  });
}
function DialogNFA({ props }) {
  const [mainGrid, setMainGrid] = React.useState(void 0);
  const [selectedRecordID, setSelectedRecordID] = React.useState(props.ObjRef);
  const [selectRecord, setSelectRecord] = React.useState();
  const [countRecords, setCountRecords] = React.useState("0");
  const gridPanel = React.useRef();
  const tabsPanel = React.useRef();
  const saveText = React.useRef();
  const [groupID, setGroupID] = React.useState(props.NodeID ? props.NodeID : "0");
  const [searchResult, setSearchResult] = React.useState();
  const mainSources = React.useRef(/* @__PURE__ */ new Map());
  const showAll = React.useRef(true);
  const detailValues = React.useRef(/* @__PURE__ */ new Map());
  const [needUpdate, setNeedUpdate] = React.useState(false);
  const [groupIDChange, setGroupIDChange] = React.useState(true);
  const needUpdateMainGrid = React.useRef();
  const [orgTypeCls, setOrgTypeCls] = React.useState();
  const [tabs, setTabs] = React.useState();
  const [timer, setTimer] = React.useState(null);
  const [tree, setTree] = React.useState(null);
  const treeRef = React.useRef();
  React.useEffect(() => {
    if (tree === null) {
      setTree(
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          Tree,
          {
            props: {
              ref: treeRef,
              CLSID: props.CLSID,
              multiCheck: false,
              ObjType: props.ObjType,
              SectionID: props.SectionID,
              Module: props.Module,
              Current: groupID,
              ObjRef: groupID,
              selectID: setGroupID,
              UsedDate: "0",
              Info: "0",
              Internal: "0"
            }
          }
        )
      );
    }
    if (tree) {
      setMainGridLoader();
    }
  }, [tree]);
  React.useEffect(() => {
    if (orgTypeCls !== void 0) {
      mainSources.current = /* @__PURE__ */ new Map();
      if (groupID && tree) {
        setMainSource();
      }
    }
  }, [orgTypeCls]);
  React.useLayoutEffect(() => {
    if (mainGrid && groupID != "0") {
      saveCurrentGrid({
        props,
        gridObject: mainGrid,
        groupID,
        setSelectedRecordID,
        selectedRecordID,
        mainSources: mainSources.current,
        ObjRef: selectedRecordID ? selectedRecordID : "0",
        detailValues: detailValues.current,
        OrgTypeCls: orgTypeCls,
        SectionID: props.SectionID,
        canModify: canModifyNfa
      }).then(() => {
        setCountRecords(mainGrid.source.recordCount);
        setTabs(getTabs(groupID));
        setTimeout(() => {
          setNeedUpdate(true);
        });
      });
    }
    if (mainGrid) {
      setTimeout(() => {
        mainGrid.grid.updateGridSize();
      }, 251);
    }
  }, [mainGrid]);
  React.useLayoutEffect(() => {
    if (mainGrid) {
      if (groupID && groupID !== "0") {
        setMainSource();
      }
      if (mainGrid) setCountRecords(mainGrid.source.recordCount);
      if (mainSources.current.get(groupID)) {
        setSelectedRecordID(mainSources.current.get(groupID).ID);
      }
      if (needUpdateMainGrid.current) {
        mainGrid.grid.refreshSource(needUpdateMainGrid.current);
        needUpdateMainGrid.current = void 0;
      }
      setGroupIDChange(true);
    }
  }, [groupID]);
  React.useLayoutEffect(() => {
    onRecChange();
  }, [selectedRecordID]);
  function onRecChange() {
    if (mainGrid !== void 0 && mainSources.current.get(groupID)) {
      mainSources.current.get(groupID).ID = selectedRecordID;
    }
    if (mainGrid !== void 0) {
      const tabID = tabsPanel.current.selectedIndex;
      updateTab(tabID, tabs);
    }
  }
  React.useLayoutEffect(() => {
    if (mainGrid && searchResult) {
      const id = GetDialogParamsForSearch(props.Module, props.ObjType, searchResult).NodeID;
      if (id === groupID) {
        findSearchRecordFromTable();
      } else {
        setTree(null);
        setGroupID(id);
        setTabs(getTabs(id));
        setTimeout(() => {
          setNeedUpdate(true);
        });
        setGroupIDChange(true);
      }
    }
  }, [searchResult]);
  React.useLayoutEffect(() => {
    if (needUpdate) {
      updateTab(tabsPanel.current.selectedIndex, tabs);
      setNeedUpdate(false);
    }
  }, [needUpdate]);
  async function setMainGridLoader() {
    if (!mainGrid) {
      setMainGrid(
        await GridLoader(gridPanel.current, props, {
          main: true,
          GroupID: groupID,
          ReturnRecord: setSelectedRecordID,
          selectedRecordID
        })
      );
    }
  }
  const canModifyNfa = (flag, column, me) => {
    if (me.OnlyViewMode) return false;
    if ((column == null ? void 0 : column.fieldName) === "Name") {
      if (me.getActiveSource().getFieldTextSync("Options") & 4) return false;
    }
    switch (flag) {
      case 0:
        return me.getActiveSource().rights.insert;
      case 1:
        return me.getActiveSource().rights.modify;
      case 2:
        return me.getActiveSource().rights.delete;
    }
    return false;
  };
  async function setMainSource() {
    if (mainGrid && mainSources.current.get(groupID) === void 0) {
      await saveCurrentGrid({
        props,
        gridObject: mainGrid,
        groupID,
        setSelectedRecordID,
        selectedRecordID,
        mainSources: mainSources.current,
        detailValues: detailValues.current,
        OrgTypeCls: orgTypeCls,
        SectionID: props.SectionID,
        canModify: canModifyNfa
      });
    } else if (mainGrid) {
      const mainSource = mainSources.current.get(groupID);
      mainGrid.grid.setColumns(mainSource.columns);
      mainGrid.source.onHandleRequest = mainSource.request({
        Module: props.Module,
        ObjType: mainSource.objType,
        GroupID: groupID,
        selectedRecordID: mainSource.ID ? mainSource.ID : "0",
        SectionID: props.SectionID,
        OrgTypeCls: orgTypeCls
      });
      mainGrid.source.onRecordCollapsed = async function(event) {
        let RecordIndex;
        if (event.currentTarget !== void 0) {
          RecordIndex = event.currentTarget.getAttribute("recordindex");
        } else {
          RecordIndex = event;
        }
        let params = /* @__PURE__ */ new Map();
        params.set("prefix", props.Module).set("comand", "RecordCollapsed").set("SectionID", props.SectionID).set("ObjType", mainSource.objType).set("RecordIndex", RecordIndex);
        await AxiosRequest(true, params);
        await mainGrid.grid.refreshSource("!");
      };
      if (!(selectRecord || searchResult)) {
        await mainGrid.grid.refreshSource(mainSource.ID);
      }
      await mainGrid.grid.setSource(mainGrid.source);
      await mainGrid.grid.updateGridSize();
    }
    if (selectRecord) {
      await findSelectRecordFromTable();
      return;
    }
    if (searchResult) {
      await findSearchRecordFromTable();
      return;
    }
    if (mainGrid) {
      setCountRecords(mainGrid.source.recordCount);
    }
    setTabs(getTabs(groupID));
    setTimeout(() => {
      setNeedUpdate(true);
    });
  }
  async function findSearchRecordFromTable() {
    await mainGrid.grid.refreshSource(searchResult);
    setSelectedRecordID(searchResult);
    setSearchResult(void 0);
  }
  async function findSelectRecordFromTable() {
    await mainGrid.grid.refreshSource(selectRecord.ID);
    setSelectedRecordID(selectRecord.ID);
    setSelectRecord(void 0);
  }
  function getTabs(groupID2) {
    const details = detailValues.current.get(groupID2) ? detailValues.current.get(groupID2).map((item, index) => {
      return getLoadTab(item, index);
    }) : [];
    const defaultTabs = getDefaultTabs();
    return [...details, ...defaultTabs];
  }
  function getLoadTab(detailItem, index) {
    return /* @__PURE__ */ jsxRuntimeExports.jsx(
      TabItem,
      {
        id: `LoadTab_${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}`,
            load: "true",
            style: { width: "100%", height: "100%", position: "relative" },
            children: /* @__PURE__ */ jsxRuntimeExports.jsx(LoadingMask, {})
          }
        )
      },
      `TabItem_${detailItem.Caption}_${index}`
    );
  }
  function getFullTab(detailItem, content, index) {
    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}_${index}`
    );
  }
  function updateTabsContent(data, firstLoad) {
    if (firstLoad) {
      data.firstLoadFunc();
    } else {
      updateTabContentFunc(data);
    }
  }
  function getContentForTab(detail) {
    if (detail.Params.prm !== void 0) {
      return "params";
    }
    if (detail.Params.ProgID !== void 0) {
      return "documents";
    }
    return "emptyTab";
  }
  function getReqForContent(selector) {
    switch (selector) {
      case "params":
        return async (data) => {
          return handleParametersNfa(data.Module, data.detailItem, data.selectedRecordID);
        };
    }
  }
  function updateTab(index, tabs2) {
    if (index !== void 0 && tabs2) {
      clearTimeout(timer);
      const newTimer = setTimeout(async () => {
        if (tabs2[index] && tabs2[index].key === "tabItem_Text") {
          const [text, id] = Object.values(await mainGrid.source.getActiveRecordText());
          tabs2[index] = /* @__PURE__ */ jsxRuntimeExports.jsx(
            TabItem,
            {
              style: {
                textTransform: "none",
                width: "100%",
                height: "100%",
                padding: "0px",
                display: "inline-block"
              },
              label: "Описание",
              children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { id: "item_Text", style: { height: "100%", width: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                IMemo,
                {
                  id: `item_index_${id}`,
                  Text: text,
                  style: {
                    height: "calc(100% - 3px)",
                    width: "100%",
                    outline: "0",
                    resize: "none"
                  },
                  onBlur: TextSave,
                  onChange: onTextChange
                }
              ) })
            },
            "tabItem_Text"
          );
          setTabs([...tabs2]);
          return;
        }
        if (tabs2[index] && tabs2[index].key === "tabItem_Search" && groupIDChange) {
          if (tabs2[index].props.id !== `LoadTab_Search`) {
            tabs2[index] = getLoadSearch();
            setTabs([...tabs2]);
          }
          tabs2[index] = getSearch();
          setTabs([...tabs2]);
          setGroupIDChange(false);
          return;
        }
        if (!detailValues.current || !detailValues.current.get(groupID)) return;
        const detailItem = detailValues.current.get(groupID)[index];
        if (!detailItem || !detailItem.Params) return;
        const selector = getContentForTab(detailItem);
        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),
            firstLoadFunc: () => {
              const tabContent = getDetailContent(selector, props, {
                selectedRecordID,
                onRequest: getReqForContent(selector),
                detailItem
              });
              const tab = getFullTab(detailItem, tabContent, index);
              tabs2[index] = tab;
              setTabs([...tabs2]);
            }
          },
          firstLoad
        );
      }, 100);
      setTimer(newTimer);
    }
  }
  function getDefaultTabs() {
    return [getText(), getLoadSearch()];
  }
  function getText() {
    return /* @__PURE__ */ jsxRuntimeExports.jsx(
      TabItem,
      {
        style: {
          textTransform: "none",
          width: "100%",
          height: "100%",
          padding: "0px",
          display: "inline-block"
        },
        label: "Описание",
        children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { id: "item_Text", style: { height: "100%", width: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
          "textarea",
          {
            style: {
              height: "calc(100% - 3px)",
              width: "100%",
              outline: "0",
              resize: "none"
            }
          }
        ) })
      },
      "tabItem_Text"
    );
  }
  function getLoadSearch() {
    return /* @__PURE__ */ jsxRuntimeExports.jsx(
      TabItem,
      {
        id: "LoadTab_Search",
        style: {
          textTransform: "none",
          width: "100%",
          height: "100%",
          padding: "0px",
          display: "inline-block"
        },
        label: "Поиск",
        children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { id: "item_Search", style: { height: "100%", width: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(LoadingMask, {}) })
      },
      "tabItem_Search"
    );
  }
  function getSearch() {
    return /* @__PURE__ */ jsxRuntimeExports.jsx(
      TabItem,
      {
        style: {
          textTransform: "none",
          width: "100%",
          height: "100%",
          padding: "0px",
          display: "inline-block"
        },
        label: "Поиск",
        children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { id: "item_Search", style: { height: "100%", width: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Search, { GroupId: groupID, Module: props.Module, ObjType: props.ObjType, return: setSearchResult }) })
      },
      "tabItem_Search"
    );
  }
  function handleParametersNfa(module, item, selectedRecordID2) {
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", module).set("comand", item.Token).set("prm", item.Params.prm).set("Path", item.Params.Path).set("NeedRefresh", "1");
    if (selectedRecordID2) params.set("GroupId", selectedRecordID2);
    return AxiosRequest(true, params);
  }
  async function TextSave(ev) {
    const id = ev.target.id, value = ev.target.value;
    if (saveText.current) {
      const params = /* @__PURE__ */ new Map();
      params.set("prefix", props.Module).set("comand", "SetNotes").set("GroupID", selectedRecordID);
      const json = await AxiosRequest(true, params, { $content: value });
      if (json && json.Result === "1") {
        mainGrid.source.setRecordText(value, Number(id.split("_")[2]));
      }
    }
  }
  function onTextChange(ev) {
    if (ev.target.value !== mainGrid.source.getActiveRecordText()) {
      saveText.current = true;
    } else {
      saveText.current = false;
    }
  }
  async function onDropDownList() {
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", "departments").set("comand", "GetObjectValues").set("ObjType", "2773").set("ObjRef", "0");
    const json = await AxiosRequest(true, params);
    const result = [{ label: "Все учреждения", id: "0" }];
    if (json && json.Items) {
      json.Items.forEach((item, index) => {
        if (typeof item === "object") {
          result.push({ label: item.text, id: item.id });
        } else {
          result.push({ label: item, id: index + 1 });
        }
      });
    }
    return result;
  }
  function onEditButtonClick(data) {
    if (data.json) tokenProcessingTest(data.json, data.subData);
  }
  function onEdit(data) {
    if (!data.value) {
      data.setValue({ label: "Все учреждения", id: "0" });
      data.setInputValue("Все учреждения");
      setOrgTypeCls("0");
      return;
    }
    mainSources.current = /* @__PURE__ */ new Map();
    data.setValue({ label: data.value, id: data.tag });
    data.setInputValue(data.value);
    setSelectedRecordID("0");
    setOrgTypeCls(data.tag);
  }
  async function onSelect() {
    const short = gridPanel.current.grid.source.getFieldTextSync("ShortName") !== "" ? `(${gridPanel.current.grid.source.getFieldTextSync("ShortName")})` : "";
    const full = gridPanel.current.grid.source.getFieldTextSync("FullName");
    const name = gridPanel.current.grid.source.getFieldTextSync("Name");
    let value = `${short} ${full}`;
    if (value === " ") {
      value = name;
    }
    if (props.onEdit && props.setValue && props.setInputValue) {
      props.onEdit({
        value: value.trim(),
        tag: selectedRecordID,
        name: props.Name,
        record: props.record,
        textchanged: "0",
        setValue: props.setValue,
        setInputValue: props.setInputValue,
        type: props.Module,
        requestId: props.RequestID,
        addInfo: props.addInfo
      });
      return;
    }
    if (props.RequestID) {
      await formEdit(
        "1",
        props.func,
        {
          Name: "ObjRef",
          Value: selectedRecordID
        },
        props.RequestID,
        props.from
      );
    }
  }
  function BookContextMenuHandler(data) {
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", props.Module).set("comand", "BookContextMenuHandler").set("ID", data.id).set("ObjRef", data.ObjRef).set("ObjType", data.ObjType).set("Selection", data.selection).set("WSM", "1");
    if (data.SectionID) params.set("SectionID", data.SectionID);
    return AxiosRequest(true, params);
  }
  const changeDataOfItem = (val) => {
    var _a;
    const changeValue = {
      AllObjects: changeAllObjects
    };
    return ((_a = changeValue[val.value]) == null ? void 0 : _a.call(changeValue, val)) || val;
  };
  const changeAllObjects = (val) => {
    const img = showAll.current && /* @__PURE__ */ jsxRuntimeExports.jsx(CheckOutlinedIcon, {});
    const newOptions = {
      Check: showAll.current,
      Image: img
    };
    return { ...val, ...newOptions };
  };
  const contextMenuHandler = (data) => {
    var _a;
    const comands = {
      ShowGroupID: showGroupID,
      AllObjects: allObjects,
      CreateGroup: createGroup
    };
    ((_a = comands[data.value]) == null ? void 0 : _a.call(comands, data)) || defaultContextClick(data);
  };
  const createGroup = () => {
    const name = mainGrid == null ? void 0 : mainGrid.source.getFieldTextSync("Name");
    const number = mainGrid == null ? void 0 : mainGrid.source.getFieldTextSync("InvNumText");
    smetaModals.open(/* @__PURE__ */ jsxRuntimeExports.jsx(DialogCreateGroup, { name, number, onSelect: onGenGroup }), {
      Title: "Создать группу объектов",
      style: getStyleWindow(),
      NewModalWindow: true,
      blockMaximize: true
    });
  };
  const onGenGroup = async (data) => {
    const params = {
      Name: data.name,
      Code: data.code,
      First: data.first,
      Count: data.count,
      Object: selectedRecordID,
      Group: data.group ? selectedRecordID : groupID
    };
    const config = {
      prefix: Prefixes.storobj,
      method: "GET",
      url: "GenerateObjects",
      params
    };
    await smetaAxios(config);
    await (mainGrid == null ? void 0 : mainGrid.grid.refreshSource(selectedRecordID));
  };
  const showGroupID = async (data) => {
    const index = selectedRecordID;
    const json = await getDialogParams({ module: props.Module, objType: props.ObjType, objRef: index });
    if (json && json.NodeID) {
      if (json.NodeID !== groupID) {
        needUpdateMainGrid.current = index;
        await treeRef.current.selectGroupID(json.NodeID);
      } else {
        mainGrid.grid.refreshSource(index);
        return;
      }
    }
  };
  const allObjects = (data) => {
    showAll.current = !showAll.current;
    mainGrid.grid.refreshSource(selectedRecordID, !showAll.current && "1");
  };
  async function getSelection() {
    if (!mainGrid) return;
    if (mainGrid.grid.getMultiSelect().length !== 0) {
      return await CheckMultiSelect(mainGrid.grid, "ID", ",");
    }
  }
  const defaultContextClick = async (data) => {
    if (data.IDfix !== void 0) {
      const selection = await getSelection();
      let json = await BookContextMenuHandler({
        id: data.IDfix,
        ObjRef: selectedRecordID,
        ObjType: mainSources.current.get(groupID).objType,
        selection
      });
      if (json.Token) {
        tokenProcessingTest(json, {
          func: () => mainGrid.grid.refreshSource(selectedRecordID)
        });
      } else mainGrid.grid.refreshSource(selectedRecordID);
    }
  };
  return /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: /* @__PURE__ */ jsxRuntimeExports.jsx(
    ContentOrganization,
    {
      props,
      data: {
        editor: { editStyle: "17" },
        tree,
        gridPanel,
        mainGrid,
        countRecords,
        tabs,
        tabsPanel,
        updateTab,
        nfa: true,
        editorProps: {
          editStyle: "17",
          placeholder: "Учреждение",
          onDropDownList,
          onEdit,
          onEditButtonClick,
          value: "Все учреждения",
          objref: "0",
          datatype: "2773",
          type: "departments"
        },
        onSelect,
        mainMenuSelector: "NFA",
        mainMenuRequest: getBookContextMenu,
        ContextMenuHandler: contextMenuHandler,
        changeDataOfItem,
        objType: mainSources.current.get(groupID) ? mainSources.current.get(groupID).objType : props.ObjType,
        selectedRecordID: selectedRecordID ? selectedRecordID : "0"
      },
      buttons: {
        search: tabs ? tabs.length - 1 : 1,
        tree: true,
        detail: true
      }
    }
  ) });
}
export {
  DialogNFA as default
};
//# sourceMappingURL=DialogNFA-9n-PsP-P.js.map
