import { _ as d, f as m, u as f, s as g } from "./import-file-r6B0-WFX.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(o) {
      const t = [];
      return o.forEach((a) => {
        a.menu ? t.push(...this.flattenMenu(a.menu)) : t.push(a);
      }), t;
    },
    async getFormScheme() {
      var l, r;
      const o = this.flattenMenu(f.value);
      o != null && o.length || this.$router.replace("/404");
      const t = o == null ? void 0 : o.find((s) => {
        var e, n;
        return (s == null ? void 0 : s.path) == ((n = (e = this.$route) == null ? void 0 : e.query) == null ? void 0 : n.table);
      });
      t || this.$router.replace("/404");
      const a = t == null ? void 0 : t.table;
      this.table = a;
      try {
        const s = await m.get(`/api/table-data/${a}`), { data: e } = await m.get(
          `/api/template/form/${(l = s == null ? void 0 : s.data) == null ? void 0 : l.form}`
        );
        this.scheme = (e == null ? void 0 : e.schema) || e, this.style = (e == null ? void 0 : e.style) || null, this.api = (e == null ? void 0 : e.api) || "", this.token = ((r = s == null ? void 0 : s.data) == null ? void 0 : r.addToken) || "";
      } catch {
      }
    },
    getGroupedErrorNotification(o, t) {
      if (!o || !t) return "";
      const a = {};
      return Object.entries(o).forEach(([r, s]) => {
        var n;
        const e = ((n = t[r]) == null ? void 0 : n.ua) || r;
        s.forEach((c) => {
          const u = c.message;
          a[u] || (a[u] = []), a[u].push(`"${e}"`);
        });
      }), Object.entries(a).map(([r, s]) => `<b>${r}:</b> ${s.join(", ")}`).join("<br>") || "Сталася помилка валідації";
    },
    async createObject() {
      var t, a, l, r, s;
      const o = (a = (t = this.$refs.form) == null ? void 0 : t.$refs) == null ? void 0 : a.form;
      try {
        await o.doValidation(), await m.post(
          this.api || `/api/table/${this.token}`,
          this.formValues
        ), await this.$router.back(), await this.$notify({
          title: "Успішно!",
          message: "Об'єкт успішно створено",
          type: "success"
        });
      } catch (e) {
        const n = this.getGroupedErrorNotification(
          o == null ? void 0 : o.formErrors,
          o == null ? void 0 : o.scheme
        );
        this.$notify({
          title: "Помилка!",
          message: ((r = (l = e == null ? void 0 : e.response) == null ? void 0 : l.data) == null ? void 0 : r.error) || ((s = e == null ? void 0 : e.response) == null ? void 0 : s.data) || n,
          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(o, t, a, l, r, s) {
  var n;
  const e = b("VsFormWithNavigation");
  return h(), x("div", k, [
    i("div", V, [
      t[3] || (t[3] = i("h2", { class: "text-xl font-medium" }, "Створити", -1)),
      i("div", _, [
        i("button", {
          onClick: t[0] || (t[0] = (c) => o.$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: t[1] || (t[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 = o.$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, [
        r.scheme ? (h(), w(e, {
          key: 0,
          ref: "form",
          scheme: r.scheme,
          style: p(r.style),
          modelValue: r.formValues,
          "onUpdate:modelValue": t[2] || (t[2] = (c) => r.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
};
