import { t as React, ah as AxiosRequest, j as jsxRuntimeExports, G as Grid, ai as Editor, B as Button, ac as tokenProcessingTest, ap as smetaModals, aq as DialogSetObjText, ar as getStyleWindow, ab as formEdit } from "./index-D15x63tD.js";
function DialogChildren({ props }) {
  const tableContainer = React.useRef(null);
  const [records, setRecords] = React.useState([]);
  const selected = React.useRef();
  React.useEffect(() => {
    if (!records || records.length === 0) {
      if (props.PersonID)
        getChildren(props.PersonID).then((res) => {
          setRecords(getRecords(Object.values(res)));
        });
    }
  }, []);
  function getChildren(PersonID) {
    if (PersonID) {
      const params = /* @__PURE__ */ new Map();
      params.set("prefix", "cadres").set("comand", "GetPersonChildren").set("PersonID", PersonID);
      const json = AxiosRequest(true, params);
      return json;
    }
  }
  async function onDropDownList(data) {
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", "persons").set("comand", "GetObjectValues").set("ObjType", "2040").set("ObjRef", data.objref);
    const json = await AxiosRequest(true, params);
    if (json && json.Items) {
      return json.Items.map((item, index) => {
        if (typeof item === "object") {
          return { label: item.text, id: item.id };
        }
        return { label: item, id: index };
      });
    }
  }
  function onEditButtonClick(data) {
    if (data.json) {
      tokenProcessingTest(data.json, data.subData);
    }
  }
  function getObjText(tag) {
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", "persons").set("comand", "GetObjectText").set("Flag", "3").set("ObjType", "2040").set("ObjRef", tag);
    const json = AxiosRequest(true, params);
    return json;
  }
  function setObjText(value) {
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", "persons").set("comand", "SetObjectText").set("Flag", "-1").set("ObjType", "2040").set("Text", value);
    const json = AxiosRequest(true, params);
    return json;
  }
  async function onEdit(data) {
    if (data.proof) {
      const checkAnswer = await setObjText(data.value);
      if (checkAnswer) {
        if (checkAnswer.Selection && checkAnswer.Selection.length !== 0) {
          smetaModals.open(/* @__PURE__ */ jsxRuntimeExports.jsx(DialogSetObjText, { list: checkAnswer.Selection, data, onEdit }), {
            Title: "Выберите элемент из списка",
            style: getStyleWindow(),
            hiddenButton: true,
            blockMaximize: true
          });
          return;
        }
        const json = await getObjText(checkAnswer.ObjRef);
        if (json && json.Text) {
          if (data.setInputValue && data.setValue) {
            data.setValue({ label: json.Text, id: checkAnswer.ObjRef });
            data.setInputValue(json.Text);
          }
          selected.current = null;
          getChildren(checkAnswer.ObjRef).then((res) => {
            setRecords(getRecords(Object.values(res)));
          });
        } else {
          setRecords(null);
        }
      }
    } else {
      const json = await getObjText(data.tag);
      if (json && json.Text) {
        if (data.setInputValue && data.setValue) {
          data.setValue({ label: json.Text, id: data.tag });
          data.setInputValue(json.Text);
        }
        selected.current = null;
        getChildren(data.tag).then((res) => {
          setRecords(getRecords(Object.values(res)));
        });
      } else {
        setRecords(null);
      }
    }
  }
  function getRecords(array) {
    if (array && array.length) {
      return array.map((item, id) => {
        const record = /* @__PURE__ */ jsxRuntimeExports.jsx(
          "tr",
          {
            ref: item.id === props.ObjRef ? selected : null,
            onDoubleClick: (ev) => {
              selected.current = ev.target;
              if (props.onEdit && selected.current) {
                props.onEdit({
                  value: selected.current.innerText,
                  tag: selected.current.id.split("_")[1],
                  name: props.Name,
                  record: props.record,
                  textchanged: "0",
                  setValue: props.setValue,
                  setInputValue: props.setInputValue,
                  type: props.Module,
                  addInfo: props.addInfo
                });
              }
              ev.target.closest("SMART-WINDOW").parentElement.remove();
            },
            onClick: (ev) => {
              if (selected.current) selected.current.style.backgroundColor = "";
              selected.current = ev.target;
              selected.current.style.backgroundColor = "#ede2d3";
            },
            onMouseEnter: (ev) => {
              if (selected.current === ev.target) return;
              ev.target.style.backgroundColor = "#eeeeee";
            },
            onMouseLeave: (ev) => {
              if (selected.current === ev.target) return;
              ev.target.style.backgroundColor = "";
            },
            style: {
              cursor: "default",
              backgroundColor: item.id === props.ObjRef ? "#ede2d3" : ""
            },
            text: item.text,
            children: /* @__PURE__ */ jsxRuntimeExports.jsx("td", { id: `td_${item.id}`, style: { borderBottom: "1px solid #cccccc", height: "27px" }, num: id, children: item.text })
          },
          `child_${item.id}`
        );
        return record;
      });
    }
    return [];
  }
  async function onSelect() {
    if (props.onEdit && selected.current) {
      props.onEdit({
        value: selected.current.innerText,
        tag: selected.current.id.split("_")[1],
        name: props.Name,
        record: props.record,
        textchanged: "0",
        setValue: props.setValue,
        setInputValue: props.setInputValue,
        type: props.Module,
        requestId: props.RequestID,
        addInfo: props.addInfo
      });
    }
    if (props.RequestID) {
      await formEdit(
        "1",
        props.func,
        { Name: "ObjRef", Value: selected.current.id.split("_")[1] },
        props.RequestID,
        props.from
      );
    }
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs(
      Grid,
      {
        container: true,
        direction: "column",
        justifyContent: "flex-start",
        alignItems: "flex-start",
        style: { height: "calc(100% - 50px)", userSelect: "none" },
        children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(
            "div",
            {
              style: {
                position: "absolute",
                top: "48px",
                left: "40px",
                backgroundColor: "#ffffff",
                height: "20px",
                color: "#cccccc",
                fontStyle: "italic"
              },
              children: "Сотрудник"
            }
          ),
          /* @__PURE__ */ jsxRuntimeExports.jsx(
            "div",
            {
              style: {
                position: "absolute",
                top: "105px",
                left: "40px",
                backgroundColor: "#ffffff",
                height: "20px",
                color: "#cccccc",
                fontStyle: "italic"
              },
              children: "Дети сотрудника"
            }
          ),
          /* @__PURE__ */ jsxRuntimeExports.jsx(
            Grid,
            {
              container: true,
              direction: "column",
              justifyContent: "center",
              alignItems: "center",
              style: {
                height: "47px",
                marginBottom: "10px",
                border: "1px solid #eeeeee",
                borderRadius: "5px",
                width: "calc(100% - 1px)"
              },
              children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                Grid,
                {
                  item: true,
                  style: {
                    width: "calc(100% - 40px)",
                    height: "27px",
                    border: "1px solid #cccccc"
                  },
                  children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                    Editor,
                    {
                      objref: props.PersonID,
                      value: props.parent,
                      placeholder: "Сотрудник",
                      editStyle: 17,
                      type: "persons",
                      datatype: "2040",
                      onDropDownList,
                      onEditButtonClick,
                      onEdit
                    }
                  )
                }
              )
            }
          ),
          /* @__PURE__ */ jsxRuntimeExports.jsx(
            Grid,
            {
              item: true,
              style: {
                width: "calc(100% - 1px)",
                height: "calc(100% - 57px)",
                border: "1px solid #eeeeee",
                borderRadius: "5px",
                padding: "20px"
              },
              ref: tableContainer,
              children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                "div",
                {
                  style: {
                    border: "1px solid #cccccc",
                    borderRadius: "5px",
                    height: "100%",
                    overflow: "auto"
                  },
                  children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                    "table",
                    {
                      style: {
                        border: "0"
                      },
                      cellSpacing: "1",
                      width: "100%",
                      cols: "1",
                      "table-layout": "auto",
                      onMouseUp: (ev) => {
                        if (ev.target.tagName === "TABLE") return;
                        if (selected.current) selected.current.style.backgroundColor = "";
                        selected.current = ev.target;
                        selected.current.style.backgroundColor = "#ede2d3";
                      },
                      children: /* @__PURE__ */ jsxRuntimeExports.jsx("tbody", { children: records })
                    }
                  )
                }
              )
            }
          )
        ]
      }
    ),
    /* @__PURE__ */ jsxRuntimeExports.jsx(
      Grid,
      {
        item: true,
        style: {
          width: "100%",
          height: "40px",
          marginTop: "5px"
        },
        children: /* @__PURE__ */ jsxRuntimeExports.jsxs(Grid, { container: true, direction: "row", justifyContent: "flex-end", alignItems: "center", style: { paddingTop: "8px" }, children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(
            Button,
            {
              size: "small",
              variant: "outlined",
              style: { textTransform: "none", marginRight: "10px" },
              value: "1",
              className: "button_Modal_Select",
              onClick: onSelect,
              children: "Выбрать"
            }
          ),
          /* @__PURE__ */ jsxRuntimeExports.jsx(
            Button,
            {
              size: "small",
              variant: "outlined",
              style: { textTransform: "none" },
              value: "2",
              className: "button_Modal_Close",
              children: "Отмена"
            }
          )
        ] })
      }
    )
  ] });
}
export {
  DialogChildren as default
};
//# sourceMappingURL=DialogChildren-C8EYhJPt.js.map
