import { _ as d, f as m, u as f, s as g } from "./import-file-2iB1izw1.js";
import { resolveComponent as b, createElementBlock as x, openBlock as h, createElementVNode as i, normalizeClass as y, normalizeStyle as p, createBlock as w, createCommentVNode as v } from "vue";
const $ = {
  data() {
    return {
      formValues: {},
      scheme: null,
      table: "",
      token: "",
      api: "",
      style: null
    };
  },
  mounted() {
    this.getFormScheme();
  },
  computed: {
    color() {
      return g.value.buttonColor || "blue";
    }
  },
  methods: {
    flattenMenu(t) {
      const e = [];
      return t.forEach((a) => {
        a.menu ? e.push(...this.flattenMenu(a.menu)) : e.push(a);
      }), e;
    },
    async getFormScheme() {
      var l, o;
      const t = this.flattenMenu(f.value);
      t != null && t.length || this.$router.replace("/404");
      const e = t == null ? void 0 : t.find((s) => {
        var r, n;
        return (s == null ? void 0 : s.path) == ((n = (r = this.$route) == null ? void 0 : r.query) == null ? void 0 : n.table);
      });
      e || this.$router.replace("/404");
      const a = e == null ? void 0 : e.table;
      this.table = a;
      try {
        const s = await m.get(`/api/table-data/${a}`), { data: r } = await m.get(
          `/api/template/form/${(l = s == null ? void 0 : s.data) == null ? void 0 : l.form}`
        );
        this.scheme = (r == null ? void 0 : r.schema) || r, this.style = (r == null ? void 0 : r.style) || null, this.api = (r == null ? void 0 : r.api) || "", this.token = ((o = s == null ? void 0 : s.data) == null ? void 0 : o.addToken) || "";
      } catch {
      }
    },
    getGroupedErrorNotification(t, e) {
      if (!t || !e) return "";
      const a = {};
      return Object.entries(t).forEach(([o, s]) => {
        var n;
        const r = ((n = e[o]) == null ? void 0 : n.ua) || o;
        s.forEach((c) => {
          const u = c.message;
          a[u] || (a[u] = []), a[u].push(`"${r}"`);
        });
      }), Object.entries(a).map(([o, s]) => `<b>${o}:</b> ${s.join(", ")}`).join("<br>") || "Сталася помилка валідації";
    },
    async createObject() {
      var e, a, l;
      const t = (a = (e = this.$refs.form) == null ? void 0 : e.$refs) == null ? void 0 : a.form;
      try {
        await t.doValidation(), await m.post(
          this.api || `/api/table/${this.token}`,
          this.formValues
        ), await this.$router.back(), await this.$notify({
          title: "Успішно!",
          message: "Об'єкт успішно створено",
          type: "success"
        });
      } catch (o) {
        const s = this.getGroupedErrorNotification(
          t == null ? void 0 : t.formErrors,
          t == null ? void 0 : t.scheme
        );
        this.$notify({
          title: "Помилка!",
          message: ((l = o == null ? void 0 : o.response) == null ? void 0 : l.data) || s,
          type: "error"
        });
      }
    }
  }
}, k = { class: "bg-gray-100 lg:w-[calc(100vw-260px)] w-[100vw]" }, V = { class: "h-[76px] mt-[15px] flex items-center justify-between mx-[20px] px-[20px] bg-white rounded-xl" }, _ = { class: "flex items-center gap-[6px]" }, j = { class: "bg-white w-full rounded-xl" };
function E(t, e, a, l, o, s) {
  var n;
  const r = b("VsFormWithNavigation");
  return h(), x("div", k, [
    i("div", V, [
      e[3] || (e[3] = i("h2", { class: "text-xl font-medium" }, "Створити", -1)),
      i("div", _, [
        i("button", {
          onClick: e[0] || (e[0] = (c) => t.$router.back()),
          class: "flex items-center px-3 py-2 text-sm font-medium text-gray-800 duration-300 bg-white border border-gray-100 rounded-lg shadow gap-x-2 focus:outline-none hover:bg-gray-50 hover:border-gray-100"
        }, " Скасувати "),
        i("button", {
          onClick: e[1] || (e[1] = (...c) => s.createObject && s.createObject(...c)),
          class: y(`inline-flex items-center px-3 py-2 text-sm font-medium text-white duration-300 bg-${s.color}-700 border border-transparent rounded-lg gap-x-2 hover:bg-${s.color}-800 hover:text-white`)
        }, " Зберегти ", 2)
      ])
    ]),
    i("div", {
      style: p({
        height: ((n = t.$adminSettings) == null ? void 0 : n.header) === !1 ? "calc(100vh - 91px)" : "calc(100vh - 150px)"
      }),
      class: "bg-gray-100 p-[20px] flex lg:w-[calc(100vw-260px)] w-[100vw] overflow-hidden"
    }, [
      i("div", j, [
        o.scheme ? (h(), w(r, {
          key: 0,
          ref: "form",
          scheme: o.scheme,
          style: p(o.style),
          modelValue: o.formValues,
          "onUpdate:modelValue": e[2] || (e[2] = (c) => o.formValues = c),
          class: "h-full p-[20px]"
        }, null, 8, ["scheme", "style", "modelValue"])) : v("", !0)
      ])
    ], 4)
  ]);
}
const N = /* @__PURE__ */ d($, [["render", E]]);
export {
  N as default
};
