import { t as React, a6 as GridLoader, dc as handleAnaliticRequest, ah as AxiosRequest, j as jsxRuntimeExports, I as IconButton, dd as ViewComfyOutlinedIcon, ax as SplitterLocal, aU as ContextMenu, aV as getDefaultMenu, de as handleAccountsPlanRequest, df as getAccsParams, ba as CheckMultiSelect, bI as SpaceDashboardOutlinedIcon, aw as Params, dg as ApplyTransFilter, a7 as TabItem, bM as SectionToolsJS, aI as Tabs } from "./index-D15x63tD.js";
/* empty css                            */
import SectionReportsAndDocuments from "./SectionReportsAndDocuments-DT2N3iGP.js";
import "./SectionReports-CR4VT6wR.js";
import "./SectionDocumentsChose-CWNMUy0u.js";
function AccountPlanMainBlock({ props }) {
  var _a, _b;
  const [mainGrid, setMainGrid] = React.useState();
  const subGrid = React.useRef();
  const [paramsColapsed, setParamsColapsed] = React.useState(false);
  const [selectedRecord, setSelectedRecord] = React.useState();
  const gridPanel = React.useRef();
  const gridPanel2 = React.useRef();
  const accPlans = React.useRef();
  const ObjType = React.useRef();
  React.useEffect(() => {
    GetAccPlans({ SectionID: props.SectionID }).then(async (res) => {
      accPlans.current = res.Items;
      return accPlans.current;
    }).then((res) => {
      ObjType.current = res.Current ? res.Current : "0";
      GridLoader(
        gridPanel.current,
        {
          Module: "accounts",
          ObjType: ObjType.current,
          ...props
        },
        { plan: true, setSelectedRecord }
      ).then((res2) => {
        setMainGrid(res2);
      });
    });
  }, []);
  React.useEffect(() => {
    if (selectedRecord) {
      if (subGrid.current === void 0) {
        GridLoader(
          gridPanel2.current,
          {
            Module: "accounts",
            ObjType: "-1",
            ObjRef: selectedRecord ? selectedRecord : "0",
            ...props
          },
          { analitic: true }
        ).then((res) => {
          subGrid.current = res;
        });
        return;
      }
      subGrid.current.source.onHandleRequest = handleAnaliticRequest({
        SectionID: props.SectionID,
        Module: "accounts",
        ObjType: "-1",
        ObjRef: selectedRecord
      });
      subGrid.current.grid.setSource(subGrid.current.source).then(async () => {
        await subGrid.current.grid.refreshSource(selectedRecord);
        await subGrid.current.grid.updateGridSize();
      });
    }
  }, [selectedRecord]);
  function colapseDetails() {
    setParamsColapsed(!paramsColapsed);
  }
  async function ContextMenuHandler(data) {
    const comand = data.value;
    switch (comand) {
      case "Plan":
        ObjType.current = data.ID;
        mainGrid.source.onHandleRequest = handleAccountsPlanRequest({
          SectionID: props.SectionID,
          Module: "accounts",
          ObjType: ObjType.current
        });
        mainGrid.grid.setSource(mainGrid.source).then(async () => {
          await mainGrid.grid.refreshSource(selectedRecord);
          await mainGrid.grid.updateGridSize();
        });
        break;
    }
  }
  async function GetAccPlans(data) {
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", "accounts").set("comand", "GetAccPlans").set("SectionID", data.SectionID);
    return AxiosRequest(true, params);
  }
  function onResize() {
    if (subGrid.current) subGrid.current.grid.updateGridSize();
    if (mainGrid) mainGrid.grid.updateGridSize();
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(
    "div",
    {
      style: {
        height: "100%",
        width: "100%",
        border: "1px solid rgb(204, 204, 204)",
        borderTop: "0"
      },
      children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          "div",
          {
            style: {
              height: "32px",
              width: "100%",
              border: "1px solid rgb(200, 181, 143)"
            },
            children: /* @__PURE__ */ jsxRuntimeExports.jsx(IconButton, { title: "Детали", onClick: colapseDetails, children: /* @__PURE__ */ jsxRuntimeExports.jsx(ViewComfyOutlinedIcon, { style: { fontSize: "large" } }) })
          }
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          "div",
          {
            style: {
              height: "calc(100% - 32px)",
              width: "100%"
            },
            children: /* @__PURE__ */ jsxRuntimeExports.jsxs(
              SplitterLocal,
              {
                onResizeEnd: onResize,
                onCollapse: onResize,
                onExpand: onResize,
                orientation: "horizontal",
                style: {
                  width: "100%",
                  height: "100%"
                },
                primaryIndex: 0,
                collapsed: paramsColapsed,
                transitionDuration: 250,
                sizeSecondary: "20%",
                hideButtons: true,
                children: [
                  /* @__PURE__ */ jsxRuntimeExports.jsx(
                    ContextMenu,
                    {
                      for: "accountsPlan",
                      Menu: getDefaultMenu("accountsPlan"),
                      onRightClick: () => {
                        if (accPlans.current && accPlans.current.items && accPlans.current.items.length) {
                          const items = accPlans.current.items.map((item) => {
                            const id = item.ID !== void 0 ? item.ID : "0";
                            return {
                              Caption: item.Text,
                              ID: id,
                              value: "Plan",
                              ImageIndex: ObjType.current === id ? "21" : void 0
                            };
                          });
                          const plans = { Caption: "Планы счетов", items };
                          return { arr: [plans] };
                        }
                      },
                      onMenuItemClick: ContextMenuHandler,
                      refs: { current: (_a = gridPanel == null ? void 0 : gridPanel.current) == null ? void 0 : _a.grid },
                      children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { height: "100%", width: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                        "div",
                        {
                          id: `rcgrid_panel`,
                          style: {
                            height: "100%",
                            width: "100%",
                            position: "relative"
                          },
                          ref: gridPanel
                        }
                      ) })
                    }
                  ),
                  /* @__PURE__ */ jsxRuntimeExports.jsx(
                    ContextMenu,
                    {
                      for: "accountsAnalitic",
                      Menu: getDefaultMenu("accountsAnalitic"),
                      onMenuItemClick: ContextMenuHandler,
                      refs: { current: (_b = gridPanel2 == null ? void 0 : gridPanel2.current) == null ? void 0 : _b.grid },
                      children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { height: "100%", width: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                        "div",
                        {
                          id: `rcgrid_panel`,
                          style: {
                            height: "100%",
                            width: "100%",
                            position: "relative"
                          },
                          ref: gridPanel2
                        }
                      ) })
                    }
                  )
                ]
              }
            )
          }
        )
      ]
    }
  );
}
function SectionAccountPlan(props) {
  return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
    "div",
    {
      style: {
        width: "100%",
        height: "100%",
        display: "flex",
        flexDirection: "column"
      },
      children: /* @__PURE__ */ jsxRuntimeExports.jsx(
        "div",
        {
          style: {
            width: "100%",
            display: "flex",
            flex: "1 1 0"
          },
          children: /* @__PURE__ */ jsxRuntimeExports.jsx(AccountPlanMainBlock, { props: { ...props } })
        }
      )
    }
  ) });
}
function TransactionsMainBlock(props) {
  const [mainGrid, setMainGrid] = React.useState();
  const [paramData, setParamData] = React.useState();
  const [output, setOutput] = React.useState("");
  const [paramsColapsed, setParamsColapsed] = React.useState(false);
  const [selectedRecordID, setSelectedRecordID] = React.useState();
  const [recordInfo, setRecordInfo] = React.useState("");
  const [needUpdateInfo, setNeedUpdateInfo] = React.useState();
  const gridPanel = React.useRef();
  React.useEffect(() => {
    GridLoader(
      gridPanel.current,
      { Module: "accounts", Path: "TransactionFilter" },
      {
        setSelectedRecord: setSelectedRecordID,
        onMultiSelectChange: () => {
          setNeedUpdateInfo(true);
        }
      }
    ).then((res) => {
      setMainGrid(res);
    });
    getAccsParams().then((res) => {
      setParamData(res);
    });
  }, []);
  React.useEffect(() => {
    if (mainGrid) {
      setNeedUpdateInfo(true);
    }
  }, [mainGrid]);
  React.useEffect(() => {
    if (needUpdateInfo) {
      setTableInfo();
      setNeedUpdateInfo(false);
    }
  }, [needUpdateInfo]);
  async function setTableInfo() {
    if (!mainGrid) return;
    if (mainGrid.grid.getMultiSelect().length === 0) {
      setRecordInfo(`Количество записей: ${mainGrid.source.recordCount}`);
    } else {
      let sum = await CheckMultiSelect(mainGrid.grid, "Amount", ";");
      sum = sum.split(";");
      sum = getSum(sum);
      let count = await CheckMultiSelect(mainGrid.grid, "Quantity", ";");
      count = count.split(";");
      count = getSum(count);
      setRecordInfo(`Количество записей: ${mainGrid.grid.getMultiSelect().length} | Сумма: ${sum} | Кол-во: ${count}`);
    }
  }
  function getSum(arr) {
    let result = 0;
    if (arr && arr.length) {
      for (let item of arr) {
        let number = item.replace(",", ".").replaceAll(" ", "");
        number = Number(number);
        if (number) result += number;
      }
    }
    return result;
  }
  async function onApplyClick() {
    const json = await ApplyTransFilter(props.SectionID);
    if (json && json.Text) {
      setOutput(json.Text);
    }
    await mainGrid.grid.refreshSource();
    setNeedUpdateInfo(true);
  }
  function updateGrid() {
    if (mainGrid) mainGrid.grid.updateGridSize();
  }
  function colapseTree() {
    setParamsColapsed(!paramsColapsed);
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(
    "div",
    {
      style: {
        height: "100%",
        width: "100%",
        border: "1px solid rgb(204, 204, 204)",
        borderTop: "0"
      },
      children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          "div",
          {
            style: {
              height: "32px",
              width: "100%",
              border: "1px solid rgb(200, 181, 143)"
            },
            children: /* @__PURE__ */ jsxRuntimeExports.jsx(IconButton, { style: { height: "32px", width: "32px" }, title: "Фильтр по проводкам", onClick: colapseTree, children: /* @__PURE__ */ jsxRuntimeExports.jsx(SpaceDashboardOutlinedIcon, { style: { transform: "rotate(+180deg)", fontSize: "large" } }) })
          }
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          "div",
          {
            style: {
              height: "calc(100% - 52px)",
              width: "100%"
            },
            children: /* @__PURE__ */ jsxRuntimeExports.jsxs(
              SplitterLocal,
              {
                onResizeEnd: updateGrid,
                onCollapse: updateGrid,
                onExpand: updateGrid,
                orientation: "vertical",
                style: {
                  width: "100%",
                  height: "100%"
                },
                primaryIndex: 1,
                collapsed: paramsColapsed,
                transitionDuration: 250,
                sizeSecondary: "20%",
                hideButtons: true,
                children: [
                  /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { style: { height: "100%", width: "100%" }, children: [
                    /* @__PURE__ */ jsxRuntimeExports.jsx(
                      "div",
                      {
                        style: {
                          height: "30px",
                          width: "100%",
                          display: "flex",
                          justifyContent: "center",
                          alignItems: "center"
                        },
                        children: "Фильтр по проводкам"
                      }
                    ),
                    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { height: "calc(50% - 60px)", width: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                      Params,
                      {
                        Module: "accounts",
                        reqCommand: "GetTransFilterParams",
                        GroupID: "0",
                        SectionID: props.SectionID,
                        data: paramData,
                        NoTitle: true,
                        NoHistory: true
                      }
                    ) }),
                    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { height: "30px", width: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx("button", { onClick: onApplyClick, className: "applyButton", children: "Применить" }) }),
                    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { height: "50%", width: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx("textarea", { disabled: true, style: { height: "100%", width: "100%", resize: "none" }, value: output }) })
                  ] }),
                  /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { height: "100%", width: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                    "div",
                    {
                      id: `rcgrid_panel`,
                      style: {
                        height: "100%",
                        width: "100%",
                        position: "relative"
                      },
                      ref: gridPanel
                    }
                  ) })
                ]
              }
            )
          }
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          "div",
          {
            style: {
              width: "100%",
              height: "20px",
              display: "flex",
              alignItems: "center",
              fontSize: "80%",
              paddingLeft: "10px",
              backgroundColor: "rgb(250,249,245)"
            },
            children: recordInfo
          }
        )
      ]
    }
  );
}
function SectionTransaction(props) {
  return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
    "div",
    {
      style: {
        width: "100%",
        height: "100%",
        display: "flex",
        flexDirection: "column"
      },
      children: /* @__PURE__ */ jsxRuntimeExports.jsx(
        "div",
        {
          style: {
            width: "100%",
            display: "flex",
            flex: "1 1 0"
          },
          children: /* @__PURE__ */ jsxRuntimeExports.jsx(TransactionsMainBlock, { SectionID: props.SectionID })
        }
      )
    }
  ) });
}
function DocsAndReportsContent(props) {
  const div = React.useRef();
  React.useEffect(() => {
    const arr = div.current.querySelectorAll(".smart-tabs-header-items");
    for (let item of arr) {
      if (item.ownerElement.id.includes("DocumentsAndReports")) item.style.display = "none";
    }
  }, []);
  return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { ref: div, style: { height: "100%", width: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
    SectionReportsAndDocuments,
    {
      CLSID: props.CLSID,
      id: props.id,
      onBreadCrumbClick: props.onBreadCrumbClick,
      noSectionTools: props.NoSectionTools
    }
  ) });
}
function SectionAccounting(props) {
  const tabPanel = React.useRef(null);
  const secTools = React.useRef();
  const [multipage, setMultipage] = React.useState({
    Pages: [
      {
        Name: "Журнал проводок",
        content: /* @__PURE__ */ jsxRuntimeExports.jsx(SectionTransaction, { sectionData: props.sectionData, SectionID: props.SectionID })
      },
      { Name: "Документы и отчеты", content: /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, {}) },
      { Name: "План счетов", content: /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, {}) }
    ]
  });
  const [content, setContent] = React.useState();
  React.useEffect(() => {
    firstLoadContent();
  }, []);
  function onBreadCrumbClick() {
    tabPanel.current.select(1);
  }
  function getContent(index) {
    switch (index) {
      case 0:
        return /* @__PURE__ */ jsxRuntimeExports.jsx(SectionTransaction, { sectionData: props.sectionData, SectionID: props.SectionID });
      case 1:
        return /* @__PURE__ */ jsxRuntimeExports.jsx(
          DocsAndReportsContent,
          {
            CLSID: props.CLSID,
            id: props.id,
            onBreadCrumbClick,
            NoSectionTools: true
          }
        );
      case 2:
        return /* @__PURE__ */ jsxRuntimeExports.jsx(SectionAccountPlan, { sectionData: props.sectionData, SectionID: props.SectionID });
    }
  }
  async function firstLoadContent() {
    if (multipage && multipage.Pages !== void 0) {
      const elements = [];
      let index = 0;
      for (let Page of multipage.Pages) {
        const innerContent = Page.content;
        elements.push(
          /* @__PURE__ */ jsxRuntimeExports.jsx(TabItem, { style: { height: "100%", width: "100%" }, label: Page.Name, children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { height: "100%", width: "100%" }, children: innerContent }) }, `multipage-${index}`)
        );
        index++;
      }
      setContent([...elements]);
      return;
    }
    setContent(/* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, {}));
  }
  async function updatePage(ev) {
    if (ev.target !== null) return;
    if (content) {
      const id = tabPanel.current.selectedIndex ? tabPanel.current.selectedIndex : 0;
      const Page = multipage.Pages[id];
      const innerContent = getContent(id);
      if (secTools.current) {
        const buttonsBox = secTools.current.querySelector(`#buttons_for_section${props.SectionID}`);
        if (buttonsBox) {
          if (id === 1) {
            buttonsBox.style.display = "flex";
          } else {
            buttonsBox.style.display = "none";
          }
        }
      }
      content[id] = /* @__PURE__ */ jsxRuntimeExports.jsx(TabItem, { style: { height: "100%", width: "100%" }, label: Page.Name, children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { height: "100%", width: "100%" }, children: innerContent }) }, `multipage-${id}`);
      setContent([...content]);
    }
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsxs(
    "div",
    {
      style: {
        width: "100%",
        height: "100%",
        display: "flex",
        flexDirection: "column"
      },
      children: [
        props.SectionID && props.SectionID > 100 ? /* @__PURE__ */ jsxRuntimeExports.jsx("div", { ref: secTools, style: { width: "100%", display: "flex" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(SectionToolsJS, { ID: props.SectionID }) }) : null,
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          "div",
          {
            style: {
              width: "100%",
              display: "flex",
              flex: "1 1 0",
              height: "100%"
            },
            children: /* @__PURE__ */ jsxRuntimeExports.jsx(
              Tabs,
              {
                ref: tabPanel,
                onChange: async (ev) => await updatePage(ev),
                animation: "none",
                style: {
                  height: "100%",
                  width: "100%",
                  backgroundColor: "s"
                },
                children: content
              }
            )
          }
        )
      ]
    }
  ) }) });
}
export {
  SectionAccounting as default
};
//# sourceMappingURL=SectionAccounting-GLoEIzCT.js.map
