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