import { useEffect } from "react";
import FormContent from "./components/Form.js";
import { jsxs, jsx, Fragment } from "../node_modules/.pnpm/@emotion_react@11.10.4_q4mq7tnhdumujvomvw24zx33y4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
import "../lib/useAppState.js";
import "../components/Tabs.js";
import { PageTile } from "../page/PageTile.js";
import { toast } from "react-hot-toast";
import "../node_modules/.pnpm/react-sortablejs@6.1.4_pkvhgat2fktt3g4eqjjiisgkie/node_modules/react-sortablejs/dist/index.js";
import "../tailwind.js";
import "../lib/AppContext.js";
import { ReadForm } from "./ReadForm.js";
import Left from "../node_modules/.pnpm/@icon-park_react@1.4.2_biqbaboplfbrettd7655fr4n2y/node_modules/@icon-park/react/es/icons/Left.js";
const PageForm = ({
  fetcher,
  navigate,
  id,
  itemId,
  name,
  fragment,
  title,
  subTitle,
  desc,
  roles,
  role,
  columns,
  pageHeader,
  header,
  type,
  tableName,
  loadUrl,
  redirectUrl,
  onBack = true,
  cols = 3
}) => {
  var _a;
  const data = type === "read" || type === "update" ? (_a = fetcher.data) == null ? void 0 : _a.data : {};
  const redirect = redirectUrl || `/admin/crud/${id}`;
  function fetchData() {
    fetcher.submit({
      tableName: name,
      fragment,
      id,
      itemId,
      action: "load"
    }, {
      method: "post",
      replace: true,
      action: loadUrl || `/api/crud/${id}/${itemId}`
    });
  }
  useEffect(() => {
    if (type === "update" || type === "read") {
      fetchData();
    }
  }, [id, itemId]);
  const handleSave = (res) => {
    console.log("handleForm-->", JSON.stringify(res));
    if (type === "create") {
      fetcher.submit({
        tableName: name,
        data: JSON.stringify(res),
        action: "create",
        url: redirect
      }, {
        method: "post",
        action: `/api/crud/${id}`
      });
      return;
    }
    fetcher == null ? void 0 : fetcher.submit({
      tableName: name,
      data: JSON.stringify(res),
      action: type === "create" ? "create" : "form",
      url: redirect
    }, {
      method: "post",
      action: type === "update" ? loadUrl || `/api/crud/${id}/${itemId}` : `/api/crud/${id}`
    });
  };
  useEffect(() => {
    var _a2, _b, _c, _d;
    if (fetcher.type === "actionRedirect") {
      if (type === "update") {
        toast.success("\u66F4\u65B0\u6210\u529F\uFF01");
        if (redirectUrl) {
          fetchData();
        }
      }
      if (type === "create") {
        toast.success("\u521B\u5EFA\u6210\u529F\uFF01");
      }
    }
    if (fetcher.type === "done" && ((_a2 = fetcher.data) == null ? void 0 : _a2.ok)) {
      console.log("---fetcher.data.ok---");
      toast.success((_b = fetcher.data) == null ? void 0 : _b.ok);
    }
    if (fetcher.type === "done" && ((_c = fetcher.data) == null ? void 0 : _c.error)) {
      toast.error((_d = fetcher.data) == null ? void 0 : _d.error);
    }
  }, [fetcher]);
  return /* @__PURE__ */ jsxs(Fragment, {
    children: [onBack && /* @__PURE__ */ jsx("nav", {
      className: "sticky top-0 left-0 right-0 z-50 mb-4 bg-white shadow-md",
      children: /* @__PURE__ */ jsx("div", {
        className: "container flex-1 mx-auto",
        children: /* @__PURE__ */ jsx("div", {
          className: "flex justify-between",
          children: /* @__PURE__ */ jsxs("div", {
            className: "flex items-center flex-shrink-0 h-16 cursor-pointer font-bolder",
            onClick: () => navigate(-1),
            children: [/* @__PURE__ */ jsx(Left, {
              theme: "multi-color",
              size: "29",
              fill: ["#333", "#2F88FF", "#FFF", "#43CCF8"]
            }), " ", title]
          })
        })
      })
    }), /* @__PURE__ */ jsx("div", {
      className: "container flex flex-1 mx-auto mt-6",
      children: /* @__PURE__ */ jsxs("div", {
        className: "flex flex-col flex-1 overflow-hidden bg-white rounded-t-lg shadow",
        children: [/* @__PURE__ */ jsx("div", {
          className: "border-b border-gray-200",
          children: header && /* @__PURE__ */ jsx(PageTile, {
            title: `${type === "update" ? "\u7F16\u8F91" : ""} ${(data == null ? void 0 : data.title) || (data == null ? void 0 : data.name) || ""}`,
            className: "px-6 py-6",
            titleClassName: "text-xl",
            ...header
          })
        }), /* @__PURE__ */ jsx("div", {
          className: "p-6",
          children: type === "read" ? /* @__PURE__ */ jsx(ReadForm, {
            values: data,
            columns
          }) : /* @__PURE__ */ jsx(FormContent, {
            loading: fetcher.state === "submitting" || fetcher.state === "loading",
            defaultValues: data,
            data: columns,
            cols,
            type,
            onSubmit: (e) => handleSave(e)
          })
        })]
      })
    })]
  });
};
export {
  PageForm
};
//# sourceMappingURL=PageForm.js.map
