import { defineComponent as ye, ref as h, computed as O, watch as be, onMounted as he, onBeforeUnmount as Ce, resolveComponent as C, openBlock as _, createBlock as k, unref as p, withCtx as c, createVNode as r, createElementVNode as S, createTextVNode as E, isRef as xe, createCommentVNode as Y, createElementBlock as we, Fragment as _e, renderList as Se } from "vue";
import { aJ as Ie, ak as U, ap as ke, C as Ee, t as Ae, z as De, au as Oe, d as Te, o as ze, n as Ne, e as Ue, E as Me, q as je, p as Re, r as Pe, m as Fe, l as Le, at as Be, L as Je, av as H, A as q, H as Ke, a9 as j, aa as Xe, a5 as Ye, ac as He, v as qe, u as Ve, aF as We, _ as Ge } from "./index-Nx-v7OXU.js";
const Qe = { class: "container-left-arrow" }, Ze = { style: { "padding-left": "20px !important", "padding-right": "20px !important" } }, $e = { style: { display: "flex", "align-items": "center", margin: "0 20px 0 auto" } }, et = { style: { background: "#fff", textAlign: "center", height: "99%" } }, tt = { style: { padding: "10px" } }, lt = /* @__PURE__ */ ye({
  __name: "index - 副本",
  props: {
    id: {
      type: String,
      default: "20260325"
    }
  },
  setup(R) {
    const { execute: A } = We(), V = Ie(), T = R;
    let i = h({ list: [], config: { layout: "portrait", pageMargin: "zhai" } });
    const w = h(!1);
    let W = h(!1);
    const P = h("1"), F = { width: 210, height: 297 }, L = { width: 297, height: 210 }, G = h(1);
    let M = h(i.value.config.layout || "portrait");
    const Q = O(() => i.value.config.pageMargin || "zhai"), Z = O(() => {
      switch (Q.value) {
        case "cg":
          return U([2.54, 3.18, 2.54, 3.18]);
        case "zhai":
          return U([1.27, 1.27, 1.27, 1.27]);
        case "zd":
          return U([2.54, 1.91, 2.54, 1.91]);
        default:
          return U([1.27, 1.27, 1.27, 1.27]);
      }
    }), $ = O(() => M.value === "portrait" ? F.width : L.width), ee = O(() => M.value === "portrait" ? F.height : L.height);
    function te(t) {
      if (i.value.list.length > 0) {
        let l = Math.max(...i.value.list.map((a) => a.y + a.height));
        t.y = l + 10;
      }
      t.type === "table" && t.tableConfig && H(t.tableConfig);
      const e = new q(
        () => i.value,
        // 函数引用，获取元素列表
        [t]
        // 要添加的组件
      );
      A(e), z(t);
    }
    const d = h(null), m = h(/* @__PURE__ */ new Set());
    let b = h(null);
    const D = O(() => {
      if (d.value) {
        let t = i.value.list.find((e) => e.id === d.value) || null;
        if (t) {
          if (t != null && t.type === "table") {
            let e = { ...t };
            return b.value && (e.selectedCell = b.value), e;
          }
        } else for (const e of i.value.list)
          if (e.type === "table" && e.tableConfig && Array.isArray(e.tableConfig)) {
            for (const l of e.tableConfig)
              for (const a of l.tableCell)
                if (a.cellObj) {
                  for (const n of a.cellObj)
                    if (n.id === d.value)
                      return n;
                }
          }
        return t;
      } else
        return null;
    }), B = h(null);
    function z(t, e) {
      const l = document.activeElement;
      if (l && (l.isContentEditable || l.tagName === "INPUT" || l.tagName === "TEXTAREA" || l.tagName === "SELECT") && l.blur(), document.body.focus(), b.value = null, !t) {
        d.value = null, m.value = /* @__PURE__ */ new Set();
        return;
      }
      const a = (e == null ? void 0 : e.ctrlKey) || (e == null ? void 0 : e.metaKey), n = e == null ? void 0 : e.shiftKey;
      if (a) {
        const o = new Set(m.value);
        o.has(t.id) ? (o.delete(t.id), d.value === t.id && (d.value = o.size > 0 ? Array.from(o)[0] : null)) : (o.add(t.id), m.value.size === 0 && (d.value = t.id)), m.value = o;
      } else if (n && d.value) {
        const o = i.value.list.findIndex((u) => u.id === d.value), s = i.value.list.findIndex((u) => u.id === t.id);
        if (o !== -1 && s !== -1) {
          const u = Math.min(o, s), v = Math.max(o, s), f = /* @__PURE__ */ new Set();
          for (let y = u; y <= v; y++)
            f.add(i.value.list[y].id);
          m.value = f;
        } else
          d.value = t.id, m.value = /* @__PURE__ */ new Set([t.id]);
      } else
        d.value = t.id, m.value = /* @__PURE__ */ new Set([t.id]);
    }
    function le(t) {
      if (m.value.size <= 1) return;
      const { deltaX: e, deltaY: l, draggedElementId: a } = t;
      m.value.forEach((n) => {
        if (n === a) return;
        const o = i.value.list.find((s) => s.id === n);
        o && (o.x = Number((o.x + e).toFixed(2)), o.y = Number((o.y + l).toFixed(2)));
      });
    }
    function ae(t) {
      const e = t.target;
      (e.classList.contains("ruler-content") || e.classList.contains("ruler-container")) && (d.value = null, m.value = /* @__PURE__ */ new Set(), b.value = null);
    }
    function J(t, e, l, a, n, o) {
      return t >= l && t <= l + n && e >= a && e <= a + o;
    }
    function ne(t) {
      const e = t.x + t.width / 2, l = t.y + t.height / 2;
      for (let a = 0; a < i.value.list.length; a++) {
        const n = i.value.list[a];
        if (n.type === "table" && n.tableConfig && J(e, l, n.x, n.y, n.width, n.height) && Array.isArray(n.tableConfig)) {
          let o = n.x, s = n.y;
          for (let u = 0; u < n.tableConfig.length; u++) {
            const v = n.tableConfig[u];
            let f = s;
            for (let y = 0; y < v.tableCell.length; y++) {
              const x = v.tableCell[y];
              if (J(e, l, o, f, x.width, v.height))
                return {
                  table: n,
                  row: v,
                  cell: x
                };
              o += x.width;
            }
            o = n.x, s += v.height;
          }
        }
      }
      return null;
    }
    function N(t) {
      t.selectedCell && (b.value = t.selectedCell);
      const e = { ...t };
      if (delete e.selectedCell, e.x, e.y, e.type === "table") {
        const a = i.value.list.findIndex((n) => n.id === e.id);
        a !== -1 && i.value.list.splice(a, 1), i.value.list.push(e);
        return;
      }
      const l = ne(e);
      if (!l || l.cell.ignore !== !0) {
        const a = i.value.list.findIndex((n) => n.id === e.id);
        if (a !== -1)
          i.value.list.splice(a, 1);
        else {
          let n = !1;
          for (let o = 0; o < i.value.list.length; o++) {
            const s = i.value.list[o];
            if (s.type === "table" && s.tableConfig && Array.isArray(s.tableConfig))
              for (let u = 0; u < s.tableConfig.length; u++) {
                const v = s.tableConfig[u];
                for (let f = 0; f < v.tableCell.length; f++) {
                  const y = v.tableCell[f];
                  if (y.cellObj) {
                    const x = y.cellObj.findIndex((g) => g.id === e.id);
                    if (x !== -1) {
                      y.cellObj.splice(x, 1), n = !0;
                      break;
                    }
                  }
                }
                if (n) break;
              }
            if (n) break;
          }
        }
      }
      if (l) {
        l.cell.cellObj || (l.cell.cellObj = []);
        let a = l.table.x, n = l.table.y;
        if (Array.isArray(l.table.tableConfig) && Array.isArray(l.row.tableCell)) {
          const u = l.row.tableCell.indexOf(l.cell);
          if (u !== -1)
            for (let f = 0; f < u; f++)
              a += l.row.tableCell[f].width;
          const v = l.table.tableConfig.indexOf(l.row);
          if (v !== -1)
            for (let f = 0; f < v; f++)
              n += l.table.tableConfig[f].height;
        }
        const o = e.x - a, s = e.y - n;
        e.x = o, e.y = s, l.cell.cellObj.push(e);
      } else
        i.value.list.push(e);
    }
    function oe(t) {
      d.value && (b.value = t);
    }
    function ie(t) {
      N(t);
    }
    function re(t) {
      i.value.config = {
        ...i.value.config,
        ...t
      }, console.log("[index.vue] 报表配置已更新:", i.value.config);
    }
    be(() => b.value, (t) => {
      if (!t)
        return;
      const e = D.value;
      debugger;
      if (e.type === "table") {
        e.selectedCell = t, console.log("[index.vue] 选中的单元格已更新:", t);
        const l = { ...e };
        l.tableConfig && Array.isArray(l.tableConfig) && l.tableConfig.forEach((a, n) => {
          n + 1 === b.value.row && a.tableCell.forEach((o, s) => {
            s + 1 === b.value.col && (Object.assign(o, t), Object.assign(b.value, t));
          });
        }), N(l);
      }
    }, { deep: !0, immediate: !0 });
    function se(t) {
    }
    function ue() {
      console.log(JSON.stringify(i.value));
      const t = { ...i.value }, e = new Ke(
        () => i.value,
        // 函数引用，获取元素列表
        t,
        // 旧状态（当前状态）
        t
        // 新状态（相同，因为我们只是保存快照）
      );
      A(e), de();
    }
    const de = () => {
      j.post({ url: "/formapi/zrzy-form/form/report/edit", data: { id: T.id, content: Xe(JSON.stringify(i.value)) } }, { withToken: !0 }).then((e) => {
        e.success && Ye.success("保存成功");
      });
    };
    let K = h([]);
    function ce() {
      return new Promise((t, e) => {
        j.get({ url: "/formapi/zrzy-form/data/list", params: { formId: T.id } }, { withToken: !0 }).then((a) => {
          a && a.success ? (K.value = a.result, t(!0)) : t(!1);
        }).catch((a) => {
          t(!1);
        });
      });
    }
    let I = h(!1);
    function fe() {
      I.value = !0;
    }
    function pe() {
      window.open(window.location.origin + "/viewpdf?id=" + T.id);
    }
    function ge(t) {
      t.preventDefault(), t.stopPropagation(), t.dataTransfer && (t.dataTransfer.dropEffect = "copy");
    }
    function me(t) {
      if (t.preventDefault(), t.stopPropagation(), t.dataTransfer)
        try {
          const e = t.dataTransfer.getData("application/json");
          if (e) {
            const l = JSON.parse(e), a = document.querySelector(".ruler-content");
            if (a) {
              const n = a.getBoundingClientRect(), o = t.clientX - n.left, s = t.clientY - n.top;
              l.x = Math.max(0, o - l.width / 2), l.y = Math.max(0, s - l.height / 2), l.id = He(l.type), l.type === "table" && l.tableConfig && H(l.tableConfig);
              const u = new q(
                () => i.value,
                // 函数引用，获取元素列表
                [l]
                // 要添加的组件
              );
              A(u), z(l);
            }
          }
        } catch (e) {
          console.error("Failed to process dropped data:", e);
        }
    }
    function X(t) {
      const e = t.target;
      if (e) {
        const o = e.tagName;
        if (o === "INPUT" || o === "TEXTAREA" || o === "SELECT" || e.isContentEditable)
          return;
      }
      if (t.key !== "Delete" && t.key !== "Backspace") return;
      if (m.value.size > 1) {
        const o = new qe(
          () => i.value,
          Array.from(m.value)
        );
        A(o), d.value = null, m.value = /* @__PURE__ */ new Set();
        return;
      }
      if (!d.value) return;
      const l = i.value.list.findIndex((o) => o.id === d.value);
      if (l === -1) return;
      const a = i.value.list[l], n = new Ve(
        () => i.value,
        // 函数引用，获取元素列表
        a,
        // 要删除的组件
        l
        // 组件在列表中的索引
      );
      A(n), d.value = null;
    }
    function ve() {
      return new Promise((t, e) => {
        j.get({ url: "/formapi/zrzy-form/form/report/query", params: { id: T.id } }, { withToken: !0 }).then((a) => {
          a && a.success ? (i.value = JSON.parse(a.result.content), M.value = i.value.config.layout || "portrait", t(!0)) : t(!1);
        }).catch((a) => {
          t(!1);
        });
      });
    }
    return he(async () => {
      typeof document < "u" && document.addEventListener("keydown", X), await V.initToken(), await ce(), await ve(), ke(i.value);
    }), Ce(() => {
      typeof document < "u" && document.removeEventListener("keydown", X);
    }), (t, e) => {
      const l = C("a-tab-pane"), a = C("a-tabs"), n = C("a-layout-sider"), o = C("a-button"), s = C("a-modal"), u = C("a-space"), v = C("a-layout-header"), f = C("a-layout-content"), y = C("a-layout"), x = C("a-spin");
      return _(), k(x, {
        spinning: p(W),
        wrapperClassName: "cici-spin-css"
      }, {
        default: c(() => [
          r(y, { style: { height: "100%", padding: "5px 5px" } }, {
            default: c(() => [
              r(n, {
                width: "310",
                style: { left: 0, top: 0, bottom: 0, background: "#fff" },
                "collapsed-width": "0",
                collapsed: w.value,
                "onUpdate:collapsed": e[3] || (e[3] = (g) => w.value = g),
                trigger: null,
                collapsible: ""
              }, {
                default: c(() => [
                  r(a, {
                    activeKey: P.value,
                    "onUpdate:activeKey": e[0] || (e[0] = (g) => P.value = g),
                    class: "form-tabs"
                  }, {
                    default: c(() => [
                      r(l, { key: "1" }, {
                        tab: c(() => [
                          S("span", null, [
                            r(p(Ae)),
                            e[5] || (e[5] = E(" 数据 ", -1))
                          ])
                        ]),
                        default: c(() => [
                          r(Ee, {
                            formId: R.id,
                            data: p(K)
                          }, null, 8, ["formId", "data"])
                        ]),
                        _: 1
                      }),
                      r(l, { key: "2" }, {
                        tab: c(() => [
                          S("span", null, [
                            r(p(Te)),
                            e[6] || (e[6] = E(" 组件 ", -1))
                          ])
                        ]),
                        default: c(() => [
                          r(De, {
                            "data-source": p(Oe),
                            "onUpdate:elements": te
                          }, null, 8, ["data-source"])
                        ]),
                        _: 1
                      })
                    ]),
                    _: 1
                  }, 8, ["activeKey"]),
                  S("div", Qe, [
                    w.value ? (_(), k(p(ze), {
                      key: 0,
                      onClick: e[1] || (e[1] = () => w.value = !w.value)
                    })) : (_(), k(p(Ne), {
                      key: 1,
                      onClick: e[2] || (e[2] = () => w.value = !w.value)
                    }))
                  ])
                ]),
                _: 1
              }, 8, ["collapsed"]),
              r(y, { style: { margin: "0px 5px" } }, {
                default: c(() => [
                  r(v, { style: { background: "#fff", border: "1px solid #e4e7ed", borderBottom: "0px", display: "flex !important", justifyContent: "space-between !important" } }, {
                    default: c(() => [
                      S("div", Ze, [
                        r(u, null, {
                          default: c(() => [
                            r(o, {
                              onClick: ue,
                              type: "primary",
                              size: "small"
                            }, {
                              default: c(() => [
                                r(p(Ue)),
                                e[7] || (e[7] = E("保存 ", -1))
                              ]),
                              _: 1
                            }),
                            r(o, {
                              onClick: fe,
                              size: "small"
                            }, {
                              default: c(() => [
                                r(p(Me)),
                                e[8] || (e[8] = E(" JSON ", -1))
                              ]),
                              _: 1
                            }),
                            e[10] || (e[10] = E("   ", -1)),
                            r(s, {
                              open: p(I),
                              "onUpdate:open": e[4] || (e[4] = (g) => xe(I) ? I.value = g : I = g),
                              title: "代码",
                              width: "1000px",
                              footer: !1
                            }, {
                              default: c(() => [
                                p(I) ? (_(), k(je, {
                                  key: 0,
                                  code: JSON.stringify(p(i)),
                                  language: "json"
                                }, null, 8, ["code"])) : Y("", !0)
                              ]),
                              _: 1
                            }, 8, ["open"]),
                            r(o, {
                              onClick: pe,
                              type: "primary",
                              size: "small"
                            }, {
                              default: c(() => [
                                r(p(Re)),
                                e[9] || (e[9] = E("在线预览 ", -1))
                              ]),
                              _: 1
                            })
                          ]),
                          _: 1
                        })
                      ]),
                      S("div", $e, [
                        D.value ? (_(), k(Pe, {
                          key: 0,
                          textElement: D.value.type === "table" && p(b) ? p(b) : D.value,
                          "onUpdate:textElement": se,
                          style: { "padding-right": "20px" }
                        }, null, 8, ["textElement"])) : Y("", !0)
                      ])
                    ]),
                    _: 1
                  }),
                  r(f, { style: { overflow: "auto" } }, {
                    default: c(() => [
                      S("div", et, [
                        r(Fe, {
                          ref_key: "rulerRef",
                          ref: B,
                          width: $.value,
                          height: ee.value,
                          scale: G.value,
                          thick: 30,
                          margin: Z.value,
                          onDrop: me,
                          onDragover: ge,
                          onClick: ae
                        }, {
                          default: c(() => [
                            (_(!0), we(_e, null, Se(p(i).list, (g) => (_(), k(Le, {
                              key: g.id,
                              element: g,
                              rulerRef: B.value,
                              selected: g.id === d.value,
                              isMultiSelected: m.value.has(g.id),
                              hasMultiSelection: m.value.size > 1,
                              "onUpdate:element": N,
                              onSelect: z,
                              onMultiDrag: le
                            }, {
                              default: c(() => [
                                r(Be, {
                                  element: g,
                                  selectedCell: g.id === d.value ? p(b) : null,
                                  "onUpdate:element": N,
                                  "onUpdate:selectedCell": oe,
                                  onSelect: z
                                }, null, 8, ["element", "selectedCell"])
                              ]),
                              _: 2
                            }, 1032, ["element", "rulerRef", "selected", "isMultiSelected", "hasMultiSelection"]))), 128))
                          ]),
                          _: 1
                        }, 8, ["width", "height", "scale", "margin"])
                      ])
                    ]),
                    _: 1
                  })
                ]),
                _: 1
              }),
              r(n, {
                width: "330",
                style: { right: 0, top: 0, bottom: 0, background: "#fff", overflow: "auto" }
              }, {
                default: c(() => [
                  S("div", tt, [
                    r(Je, {
                      element: D.value,
                      reportConfig: p(i).config,
                      "onUpdate:element": ie,
                      "onUpdate:reportConfig": re
                    }, null, 8, ["element", "reportConfig"])
                  ])
                ]),
                _: 1
              })
            ]),
            _: 1
          })
        ]),
        _: 1
      }, 8, ["spinning"]);
    };
  }
}), it = /* @__PURE__ */ Ge(lt, [["__scopeId", "data-v-98e3c762"]]);
export {
  it as default
};
