import { u as useAtomValue, t as React, j as jsxRuntimeExports, a3 as Tree, ah as AxiosRequest, ap as smetaModals, G as Grid, ax as SplitterLocal, ai as Editor, B as Button, b7 as selectedSectionAtom, ab as formEdit } from "./index-D15x63tD.js";
function DialogOficialStructure({ props, smetaModalId = "" }) {
  const selectedSection = useAtomValue(selectedSectionAtom);
  const [tree, setTree] = React.useState(null);
  const [groupId, setGroupID] = React.useState(props.NodeID);
  const [onlyActual, setOnlyActual] = React.useState("1");
  const [jobModes, setJobModes] = React.useState(props.jobModes);
  const [jobModeValue, setJobModeValue] = React.useState("");
  const [rateCount, setRateCount] = React.useState("1");
  const [newRendering, setNewRendering] = React.useState("1");
  const [firstDate, setFirstDate] = React.useState(props.firstDateNewRendering);
  const [lastDate, setLastDate] = React.useState("");
  const [firstRender, setFirstRender] = React.useState(true);
  const tableContainer = React.useRef(null);
  const [records, setRecords] = React.useState([]);
  const selected = React.useRef(null);
  React.useEffect(() => {
    if (tree === null) {
      setTree(
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          Tree,
          {
            props: {
              Module: "departments",
              ObjType: "2790",
              SectionID: selectedSection.ID,
              Info: "0",
              UsedDate: "0",
              Current: props.NodeID ? props.NodeID : "0",
              ObjRef: props.NodeID ? props.NodeID : "0",
              selectID: setGroupID
            }
          }
        )
      );
    }
  }, [tree]);
  React.useEffect(() => {
    if (props.createNewRendering && groupId) {
      getJobModes(groupId).then((res) => {
        const list = res.Value.split("\r\n");
        setJobModes(
          list.map((item, index) => ({
            label: item,
            id: index.toString()
          }))
        );
        if (firstRender) {
          setJobModeValue(props.jobModeValueIndex >= list.length ? list[0] : list[props.jobModeValueIndex]);
          setFirstRender(false);
        } else setJobModeValue(list[0]);
      });
    }
  }, [groupId]);
  React.useEffect(() => {
    if (groupId) {
      getAppointment(groupId, onlyActual).then((result) => {
        const res = Object.values(result);
        setRecords(getRecords(res));
      });
    }
  }, [groupId, onlyActual]);
  function getAppointment(groupId2, onlyActual2) {
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", "staff").set("comand", "GetAppointmentsStringList").set("GroupID", groupId2).set("OnlyActual", onlyActual2);
    const json = AxiosRequest(true, params);
    return json;
  }
  function getJobModes(groupId2) {
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", "staff").set("comand", "GetJobModes").set("ObjType", 2790).set("ObjRef", groupId2);
    const json = AxiosRequest(true, params);
    return json;
  }
  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.createNewRendering) {
                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
                  });
                }
                if (props.NewModalWindow) {
                  smetaModals.close(smetaModalId);
                } else 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.Name,
            children: /* @__PURE__ */ jsxRuntimeExports.jsx("td", { id: `td_${item.ID}`, style: { borderBottom: "1px solid #cccccc" }, num: id, children: item.Name })
          },
          `child_${item.ID}`
        );
        return record;
      });
    }
    return [];
  }
  async function onSelect() {
    if (props.onEdit && selected.current) {
      const paramsNewRendering = {};
      if (props.createNewRendering) {
        paramsNewRendering.FirstDate = firstDate;
        paramsNewRendering.LastDate = lastDate;
        paramsNewRendering.NewRendering = newRendering;
        paramsNewRendering.RateCount = rateCount;
        paramsNewRendering.JobMode = jobModes.findIndex((item) => item.label === jobModeValue);
      }
      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,
        paramsNewRendering
      });
      if (props.NewModalWindow) smetaModals.close(smetaModalId);
    }
    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, { style: { height: props.createNewRendering ? "calc(100% - 130px)" : "calc(100% - 50px)" }, children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(Grid, { container: true, direction: "row", justifyContent: "flex-start", alignItems: "center", style: { height: "30px" }, children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { width: "calc(100% - 171px)" }, children: "Теущее штатное расписание" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { width: "150px" }, children: "Только действующие" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          "input",
          {
            defaultChecked: true,
            type: "checkbox",
            onChange: (ev) => {
              setOnlyActual(ev.target.checked ? "1" : "0");
            }
          }
        )
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(
        SplitterLocal,
        {
          style: {
            height: "calc(100% - 30px)"
          },
          hideButtons: true,
          sizeSecondary: "50%",
          orientation: "vertical",
          children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx("div", { children: tree }),
            /* @__PURE__ */ jsxRuntimeExports.jsx("div", { ref: tableContainer, style: { border: "1px solid #cccccc", overflow: "auto" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
              "table",
              {
                style: {
                  border: "0px solid #cccccc"
                },
                bordercolor: "#cccccc",
                cellSpacing: "1",
                width: "100%",
                cols: "1",
                "table-layout": "auto",
                onMouseUp: (ev) => {
                  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%",
          marginTop: "5px"
        },
        children: /* @__PURE__ */ jsxRuntimeExports.jsxs(Grid, { container: true, direction: "row", justifyContent: "flex-end", alignItems: "center", style: { paddingTop: "8px" }, children: [
          props.createNewRendering && /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
            /* @__PURE__ */ jsxRuntimeExports.jsxs(Grid, { container: true, spacing: 2, children: [
              /* @__PURE__ */ jsxRuntimeExports.jsxs(
                Grid,
                {
                  item: true,
                  style: {
                    display: "flex",
                    flexDirection: "column",
                    justifyContent: "space-between",
                    height: "70px"
                  },
                  children: [
                    /* @__PURE__ */ jsxRuntimeExports.jsxs(
                      "div",
                      {
                        style: {
                          display: "flex",
                          alignItems: "center",
                          justifyContent: "space-between",
                          height: "24px",
                          width: "200px"
                        },
                        children: [
                          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { alignContent: "center", marginLeft: "10px" }, children: "Назначение:" }),
                          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { border: "2px solid grey", width: "100px", height: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Editor, { editStyle: 2, value: firstDate, onEdit: (ev) => setFirstDate(ev.value) }) })
                        ]
                      }
                    ),
                    /* @__PURE__ */ jsxRuntimeExports.jsxs(
                      "div",
                      {
                        style: {
                          display: "flex",
                          alignItems: "center",
                          justifyContent: "space-between",
                          height: "24px",
                          width: "200px"
                        },
                        children: [
                          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { alignContent: "center", marginLeft: "10px" }, children: "Снятие:" }),
                          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { border: "2px solid grey", width: "100px", height: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Editor, { editStyle: 2, onEdit: (ev) => setLastDate(ev.value) }) })
                        ]
                      }
                    )
                  ]
                }
              ),
              /* @__PURE__ */ jsxRuntimeExports.jsxs(
                Grid,
                {
                  item: true,
                  style: {
                    display: "flex",
                    flexDirection: "column",
                    justifyContent: "space-between",
                    height: "70px"
                  },
                  children: [
                    /* @__PURE__ */ jsxRuntimeExports.jsxs(
                      "div",
                      {
                        style: {
                          display: "flex",
                          alignItems: "center",
                          justifyContent: "space-between",
                          height: "24px",
                          width: "320px"
                        },
                        children: [
                          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { alignContent: "center", marginLeft: "10px" }, children: "Форма назначения:" }),
                          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { border: "2px solid grey", width: "175px" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                            Editor,
                            {
                              editStyle: 65,
                              listValues: jobModes,
                              value: jobModeValue,
                              onEdit: (ev) => setJobModeValue(ev.value)
                            }
                          ) })
                        ]
                      }
                    ),
                    /* @__PURE__ */ jsxRuntimeExports.jsxs(
                      "div",
                      {
                        style: {
                          display: "flex",
                          alignItems: "center",
                          justifyContent: "space-between",
                          height: "24px",
                          width: "320px"
                        },
                        children: [
                          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { alignContent: "center", marginLeft: "10px" }, children: "Число занимаемых ставок:" }),
                          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { border: "2px solid grey", width: "120px" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Editor, { value: rateCount, onEdit: (ev) => setRateCount(ev.value), editStyle: 8 }) })
                        ]
                      }
                    )
                  ]
                }
              )
            ] }),
            /* @__PURE__ */ jsxRuntimeExports.jsxs(
              Grid,
              {
                container: true,
                direction: "row",
                justifyContent: "flex-start",
                alignItems: "center",
                style: { height: "30px", marginLeft: "6px" },
                children: [
                  /* @__PURE__ */ jsxRuntimeExports.jsx(
                    "input",
                    {
                      defaultChecked: true,
                      type: "checkbox",
                      onChange: (ev) => setNewRendering(ev.target.checked ? "1" : "0")
                    }
                  ),
                  /* @__PURE__ */ jsxRuntimeExports.jsx("div", { 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",
              onClick: () => {
                if (props.NewModalWindow) smetaModals.close(smetaModalId);
              },
              children: "Отмена"
            }
          )
        ] })
      }
    )
  ] });
}
export {
  DialogOficialStructure as default
};
//# sourceMappingURL=DialogOficialStructure-CvZZAODg.js.map
