import { u as useAtomValue, z as currentTheme, r as reactExports, j as jsxRuntimeExports, b_ as CheckBoxOutlineBlankIcon, bb as clsx, bL as CheckBoxIcon, G as Grid, I as IconButton, b$ as CloseIcon, F as FormControl, f as InputLabel, c0 as inputLabelClasses, O as OutlinedInput, g as InputAdornment, bJ as SearchIcon, A as Autocomplete, c1 as Popper, c2 as autocompleteClasses, _ as TextField, c3 as ArrowDropDownIcon, c4 as outlinedInputClasses, c5 as inputBaseClasses, i as FormControlLabel, k as Checkbox, c6 as getMonthInfo, bg as smetaAxios, V as isEmptyObject } from "./index-D15x63tD.js";
function SearchPersons(props) {
  const theme = useAtomValue(currentTheme);
  const [onlyWork, setOnlyWork] = reactExports.useState(true);
  const [personsList, setPersonsList] = reactExports.useState([]);
  const [persNumber, setPersNumber] = reactExports.useState("");
  const [persName, setPersName] = reactExports.useState("");
  const [persNameValue, setPersNameValue] = reactExports.useState("");
  const [open, setOpen] = reactExports.useState(false);
  const popperRef = reactExports.useRef(null);
  const nameSearchRef = reactExports.useRef(null);
  const [showAllName, setShowAllName] = reactExports.useState(true);
  reactExports.useEffect(() => {
    document.addEventListener("mousedown", handleClickOutside);
    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, []);
  const handleOnSelect = (value) => {
    var _a;
    (_a = props.onSelect) == null ? void 0 : _a.call(props, value);
  };
  function handleClickOutside(ev) {
    const target = ev.target;
    if (popperRef.current && nameSearchRef.current) {
      if (!nameSearchRef.current.contains(target) && !popperRef.current.contains(target)) {
        setOpen(false);
      }
    }
  }
  const searchPersNumber = () => {
    if (persNumber) {
      let firstDate = "0", lastDate = "0";
      if (onlyWork && props.selectedPeriod) {
        const monthInfo = getMonthInfo(props.selectedPeriod.year, props.selectedPeriod.month);
        firstDate = String(monthInfo.firstDayInt);
        lastDate = String(monthInfo.lastDayInt);
      }
      smetaAxios.get("SearchPerson", {
        prefix: props.module,
        params: {
          SectionID: props.sectionId,
          ObjType: props.objType,
          Number: persNumber,
          Check: onlyWork ? "1" : void 0,
          FirstDate: onlyWork ? firstDate : void 0,
          LastDate: onlyWork ? lastDate : void 0
        },
        loadMask: true
      }).then((json) => {
        if (json.ID) handleOnSelect(json.ID);
      });
    }
  };
  const searchPersName = () => {
    if (open) {
      setOpen(false);
    } else if (persName) {
      setShowAllName(true);
      let firstDate = "0", lastDate = "0";
      if (onlyWork && props.selectedPeriod) {
        const monthInfo = getMonthInfo(props.selectedPeriod.year, props.selectedPeriod.month);
        firstDate = String(monthInfo.firstDayInt);
        lastDate = String(monthInfo.lastDayInt);
      }
      smetaAxios.get("SearchPerson", {
        prefix: props.module,
        params: {
          SectionID: props.sectionId,
          ObjType: props.objType,
          Text: persName,
          Check: onlyWork ? "1" : void 0,
          FirstDate: onlyWork ? firstDate : void 0,
          LastDate: onlyWork ? lastDate : void 0
        },
        loadMask: true
      }).then((json) => {
        if (!isEmptyObject(json)) {
          const array = [];
          Object.values(json).forEach((value) => {
            array.push({ label: value.text, id: value.id });
          });
          setPersonsList(array);
          setOpen(true);
        }
      });
    }
  };
  const uncheckedIcon = /* @__PURE__ */ jsxRuntimeExports.jsx(CheckBoxOutlineBlankIcon, { className: clsx("fontColorStimate", { light: theme === "light" }) });
  const checkedIcon = /* @__PURE__ */ jsxRuntimeExports.jsx(CheckBoxIcon, { className: clsx("fontColorStimate", { light: theme === "light" }) });
  const inputPersNameStyle = props.orientation === "horizontal" ? {
    marginTop: "16px",
    marginLeft: "10px",
    marginRight: "10px"
  } : {
    width: "246px",
    marginTop: "16px",
    marginLeft: "10px",
    marginRight: "10px"
  };
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(
    Grid,
    {
      style: {
        height: "100%",
        width: "100%",
        border: "1px solid #cccccc"
      },
      children: [
        /* @__PURE__ */ jsxRuntimeExports.jsxs(
          Grid,
          {
            container: true,
            direction: "row",
            justifyContent: "flex-start",
            alignItems: "center",
            style: {
              height: "25px",
              width: "100%",
              background: "linear-gradient(to top, #cccccc, 30%, #f9f9f9)"
            },
            children: [
              /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { whiteSpace: "nowrap", marginLeft: "5px" }, children: "Поиск" }),
              /* @__PURE__ */ jsxRuntimeExports.jsx(
                IconButton,
                {
                  style: {
                    position: "absolute",
                    left: "calc(100% - 25px)",
                    height: "25px",
                    width: "25px",
                    fontSize: "100%",
                    borderRadius: "0px"
                  },
                  onClick: () => {
                    var _a;
                    return (_a = props.colapseSearch) == null ? void 0 : _a.call(props);
                  },
                  children: /* @__PURE__ */ jsxRuntimeExports.jsx(CloseIcon, {})
                }
              )
            ]
          }
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsxs(
          Grid,
          {
            className: clsx("backgroundColorStimate", { light: theme === "light" }),
            style: {
              height: "calc(100% - 25px)",
              display: "flex",
              flexDirection: "row",
              flexWrap: "wrap",
              alignContent: "flex-start",
              overflowX: "auto"
            },
            children: [
              /* @__PURE__ */ jsxRuntimeExports.jsxs(
                FormControl,
                {
                  fullWidth: props.orientation === "horizontal",
                  size: "small",
                  style: {
                    marginTop: "16px",
                    marginLeft: "10px",
                    marginRight: "10px"
                  },
                  variant: "outlined",
                  onKeyDown: (ev) => {
                    if (ev.key === "Enter") searchPersNumber();
                  },
                  children: [
                    /* @__PURE__ */ jsxRuntimeExports.jsx(
                      InputLabel,
                      {
                        sx: {
                          transform: "translate(14px, 3px) scale(1)",
                          height: "auto",
                          [`&.${inputLabelClasses.shrink}`]: {
                            transform: "translate(14px, -9px) scale(0.75)"
                          }
                        },
                        children: "Табельный номер"
                      }
                    ),
                    /* @__PURE__ */ jsxRuntimeExports.jsx(
                      OutlinedInput,
                      {
                        style: { height: "28px", background: "#fff", borderRadius: "0px" },
                        size: "small",
                        value: persNumber,
                        onChange: (ev) => setPersNumber(ev.target.value),
                        endAdornment: /* @__PURE__ */ jsxRuntimeExports.jsx(InputAdornment, { position: "end", children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                          IconButton,
                          {
                            className: clsx("iconButtonStimate", {
                              light: theme === "light"
                            }),
                            edge: "end",
                            onClick: searchPersNumber,
                            children: /* @__PURE__ */ jsxRuntimeExports.jsx(SearchIcon, {})
                          }
                        ) }),
                        label: "Табельный номер"
                      }
                    )
                  ]
                }
              ),
              /* @__PURE__ */ jsxRuntimeExports.jsx(
                Autocomplete,
                {
                  fullWidth: props.orientation === "horizontal",
                  style: inputPersNameStyle,
                  size: "small",
                  ref: nameSearchRef,
                  open,
                  disableClearable: true,
                  freeSolo: true,
                  selectOnFocus: true,
                  options: personsList,
                  value: persNameValue,
                  onChange: (ev, newValue) => {
                    if (typeof newValue === "object" && newValue.id) {
                      setPersNameValue(newValue.label);
                      setOpen(false);
                      handleOnSelect(newValue.id);
                    }
                  },
                  inputValue: persName,
                  onInputChange: (ev, newInputValue) => {
                    setPersName(newInputValue);
                    if (!open) {
                      setPersNameValue("");
                    } else setShowAllName(false);
                  },
                  isOptionEqualToValue: (option, value) => {
                    return option.id === value.id || option.label === value.label;
                  },
                  filterOptions: (options, { inputValue }) => {
                    if (showAllName) return options;
                    return options.filter((option) => option.label.toLowerCase().includes(inputValue.toLowerCase()));
                  },
                  renderInput: (params) => /* @__PURE__ */ jsxRuntimeExports.jsx(
                    TextField,
                    {
                      sx: {
                        height: "28px",
                        borderRadius: "0px",
                        background: "#fff",
                        [`& .${outlinedInputClasses.root}`]: {
                          borderRadius: "0px",
                          height: "100%",
                          padding: "0 14px",
                          [`& .${inputBaseClasses.input}`]: {
                            padding: "16px 0",
                            height: "100%",
                            boxSizing: "border-box"
                          }
                        },
                        [`& .${inputLabelClasses.root}`]: {
                          transform: "translate(14px, 3px) scale(1)",
                          height: "auto"
                        },
                        [`& .${inputLabelClasses.shrink}`]: {
                          transform: "translate(14px, -9px) scale(0.75)"
                        }
                      },
                      ...params,
                      onKeyDown: (ev) => {
                        if (ev.key === "Enter") searchPersName();
                        if (ev.key === "Escape") setOpen(false);
                      },
                      label: "Ф.И.О.",
                      InputProps: {
                        ...params.InputProps,
                        endAdornment: /* @__PURE__ */ jsxRuntimeExports.jsx(InputAdornment, { position: "end", children: /* @__PURE__ */ jsxRuntimeExports.jsx(IconButton, { size: "small", onClick: searchPersName, children: /* @__PURE__ */ jsxRuntimeExports.jsx(ArrowDropDownIcon, {}) }) })
                      }
                    }
                  ),
                  renderOption: (props2, option) => {
                    return /* @__PURE__ */ reactExports.createElement(
                      "li",
                      {
                        ...props2,
                        title: option.label,
                        style: {
                          height: "30px",
                          fontSize: "13px",
                          borderBottom: "1px solid #eeeeee",
                          overflow: "hidden",
                          whiteSpace: "nowrap",
                          textOverflow: "ellipsis"
                        },
                        key: `${option.label}-${option.id}`
                      },
                      option.label
                    );
                  },
                  slotProps: {
                    popper: {
                      placement: "bottom-start",
                      style: {
                        minWidth: nameSearchRef.current ? `${nameSearchRef.current.clientWidth}px` : "200px",
                        maxWidth: `${document.body.clientWidth / 2}px`,
                        width: `fit-content`,
                        overflow: "auto",
                        border: "1px solid #cccccc",
                        borderRadius: "6px",
                        boxShadow: "5px 5px 5px #dddddd"
                      },
                      sx: {
                        [`& .${autocompleteClasses.listbox}`]: {
                          listStyle: "none",
                          margin: "0",
                          padding: "0",
                          maxHeight: "40vh",
                          overflow: "auto",
                          position: "relative"
                        }
                      }
                    }
                  },
                  PopperComponent: (params) => /* @__PURE__ */ jsxRuntimeExports.jsx(Popper, { ...params, ref: popperRef })
                }
              ),
              /* @__PURE__ */ jsxRuntimeExports.jsx(
                FormControlLabel,
                {
                  style: {
                    marginTop: "16px",
                    marginLeft: "0px"
                  },
                  control: /* @__PURE__ */ jsxRuntimeExports.jsx(
                    Checkbox,
                    {
                      icon: uncheckedIcon,
                      checkedIcon,
                      size: "small",
                      checked: onlyWork,
                      onChange: () => setOnlyWork(!onlyWork)
                    }
                  ),
                  label: "Без уволенных"
                }
              )
            ]
          }
        )
      ]
    }
  );
}
export {
  SearchPersons as S
};
//# sourceMappingURL=SearchPersons-C1hfxGSb.js.map
