import { j as jsxRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { u as useStyles } from "./styles-Dnu4P1Zm.js";
import { S as SelectWithPagination } from "./index-CfN7Ll0p.js";
import { a5 as useCreateFilialMutation, a6 as clearApiCacheByTags, a7 as rolesApi } from "./index-DfebxkL9.js";
import { g as getValidateErrors } from "./validation-Ds00Zghx.js";
import { c as createFilialSchema } from "./validate-CA6bwF22.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { z as useForm, b as Box, a0 as SimpleGrid, E as TextInput, B as Button, U as UnstyledButton, V as LoadingOverlay, s as showNotification } from "./vendor-mantine-h0WlM2Mj.js";
const CreateFilialForm = ({
  onClose,
  onSuccess
}) => {
  var _a;
  const {
    classes
  } = useStyles();
  const {
    t
  } = useTranslation();
  const [createFilial, {
    isLoading
  }] = useCreateFilialMutation();
  const form = useForm({
    initialValues: {
      Company: "",
      Title: "",
      Parent: {
        Id: "",
        Name: ""
      }
    },
    validateInputOnChange: true,
    validate: (values) => getValidateErrors(createFilialSchema(t), values)
  });
  const handleSubmit = async (values_0) => {
    var _a2, _b;
    const filialValues = {
      ...values_0,
      Title: values_0.Title.trim()
    };
    try {
      const res = await createFilial(filialValues);
      if ("error" in res) {
        showNotification({
          title: `${(_b = (_a2 = res == null ? void 0 : res.error) == null ? void 0 : _a2.data) == null ? void 0 : _b.ErrorMessage}`,
          message: "",
          autoClose: true,
          color: "red",
          style: {
            zIndex: 1003
          }
        });
      } else {
        showNotification({
          title: t("filials.filialSuccess"),
          message: "",
          autoClose: true,
          color: "green",
          style: {
            zIndex: 1003
          }
        });
        onSuccess && onSuccess();
        onClose();
        clearApiCacheByTags(rolesApi, ["Roles"]);
      }
    } catch (err) {
      console.warn(t("filials.filialError"), err);
    }
  };
  const handleClose = () => {
    onClose();
    form.reset();
  };
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs("form", { className: classes.modalRoot, onSubmit: form.onSubmit(handleSubmit), children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: classes.modalContainer, children: /* @__PURE__ */ jsxRuntimeExports.jsxs(SimpleGrid, { cols: 1, w: "100%", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(TextInput, { placeholder: t("filials.settings.filialName"), label: t("filials.addName"), withAsterisk: true, "data-value": "Title", ...form.getInputProps("Title") }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(TextInput, { placeholder: t("filials.settings.filialCode"), label: t("filials.settings.filialCode"), "data-value": "Company", ...form.getInputProps("Company") }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(SelectWithPagination, { form, fieldName: "Parent.Id", defaultValue: null, label: t("filials.addParrent"), placeholder: t("profile.choose"), value: +((_a = form == null ? void 0 : form.getTransformedValues().Parent) == null ? void 0 : _a.Id), filialName: form.getInputProps("Parent.Name").value, customerField: "Parent.Name" })
      ] }) }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.modalFooter, children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { size: "sm", radius: 8, w: "49%", type: "submit", disabled: !form.isValid(), children: t("create") }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(UnstyledButton, { ml: 8, className: classes.modalCancelBtn, w: "49%", onClick: handleClose, children: t("cancel") })
      ] })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(LoadingOverlay, { visible: isLoading })
  ] });
};
export {
  CreateFilialForm as C
};
//# sourceMappingURL=createForm-CM9BvDgp.js.map
