"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
require("../../../../node_modules/element-plus/es/index.js");
;/* empty css                                                          */
;/* empty css                                                               */
;/* empty css                                                                */
;/* empty css                                                               */
;/* empty css                                                               */
;/* empty css                                                             */
;/* empty css                                                                  */
;/* empty css                                                              */
const Vue = require("vue");
const logo = require("../../assets/logo.png.js");
const index$3 = require("../../../../node_modules/@element-plus/icons-vue/dist/index.js");
const TemplateDialog = require("../../components/TemplateDialog.vue.js");
const headerBtn$1 = require("../../packages/table/headerBtn.vue.js");
const headerBtn = require("../../packages/form/headerBtn.vue.js");
const common = require("../../packages/utils/common.js");
const index$8 = require("../../api/index.js");
const index = require("../../store/index.js");
const _package = require("../../../package.json.js");
const index$a = require("../../packages/form/aside/index.js");
const index$b = require("../../packages/table/aside/index.js");
;/* empty css            */
const index$1 = require("../../../../node_modules/element-plus/es/components/container/index.js");
const index$2 = require("../../../../node_modules/element-plus/es/components/icon/index.js");
const index$4 = require("../../../../node_modules/element-plus/es/components/dialog/index.js");
const index$5 = require("../../../../node_modules/element-plus/es/components/form/index.js");
const index$6 = require("../../../../node_modules/element-plus/es/components/input/index.js");
const index$7 = require("../../../../node_modules/element-plus/es/components/button/index.js");
const index$9 = require("../../../../node_modules/element-plus/es/components/message/index.js");
const _hoisted_1 = { class: "dev-title" };
const _hoisted_2 = { class: "logo" };
const _hoisted_3 = { class: "version" };
const _hoisted_4 = { class: "dev-header-title" };
const _hoisted_5 = { class: "title-row" };
const _hoisted_6 = { class: "template-name" };
const _hoisted_7 = { class: "template-key" };
const _hoisted_8 = { class: "opt-btn-content" };
const _hoisted_9 = { class: "dialog-footer" };
const _sfc_main = {
  __name: "index",
  setup(__props) {
    const currentInstance = Vue.getCurrentInstance();
    const ctx = currentInstance.proxy;
    const editingHeaderBtn = Vue.computed(() => {
      switch (index.default.getters.editingType) {
        case "table":
          return headerBtn$1.default;
        case "form":
          return headerBtn.default;
      }
      return null;
    });
    const editConfigData = Vue.computed(() => {
      return common.getEditConfigData();
    });
    const editingType = Vue.computed(() => common.getEditingType());
    const version = _package.default.version;
    const getTemplateData = () => {
      return editConfigData.value || {};
    };
    const editDialogVisible = Vue.ref(false);
    const editFormRef = Vue.ref(null);
    const editForm = Vue.reactive({
      templateName: "",
      templateKey: "",
      templateDesc: ""
    });
    const editFormRules = {
      templateKey: [{ required: true, message: "请输入模板标识", trigger: "blur" }],
      templateName: [
        { required: true, message: "请输入模板名称", trigger: "blur" }
      ],
      templateDesc: [
        { max: 200, message: "长度不能超过 200 个字符", trigger: "blur" }
      ]
    };
    function openEditDialog() {
      const data = getTemplateData();
      editForm.templateName = data.templateName || "";
      editForm.templateKey = data.templateKey || "";
      editForm.templateDesc = data.templateDesc || "";
      editDialogVisible.value = true;
    }
    function saveTemplateInfo() {
      var _a;
      (_a = editFormRef.value) == null ? void 0 : _a.validate((valid) => {
        if (valid) {
          const data = getTemplateData();
          data.templateName = editForm.templateName;
          data.templateKey = editForm.templateKey;
          data.templateDesc = editForm.templateDesc;
          editDialogVisible.value = false;
        } else {
          return false;
        }
      });
    }
    function save() {
      const data = getTemplateData();
      const templateData = {
        pkId: data.pkId,
        templateKey: data.templateKey,
        templateName: data.templateName,
        context: JSON.stringify(data)
      };
      index$8.default.updateById(ctx.$http, templateData).then(() => {
        index$9.ElMessage.success("保存成功");
      });
    }
    const templateDialogVisible = Vue.ref(false);
    function openTemplateSelector() {
      templateDialogVisible.value = true;
    }
    function handleTemplateSelect({ row, context }) {
      var _a, _b;
      editConfigData.value.fieldsData.splice(0);
      if (!editConfigData.value.hiddenFields) {
        editConfigData.value.hiddenFields = [];
      } else {
        editConfigData.value.hiddenFields.splice(0);
      }
      const type = row.type;
      if (type == "form") {
        const columns = editConfigData.value.columns;
        context.formItem.forEach((item, index2) => {
          setTimeout(() => {
            var _a2;
            const id = generateId();
            const x = index2 % columns;
            const y = Math.floor(index2 / columns);
            if (item.fieldType != "empty" && item.fieldName != "" && item.label != "") {
              if (item.isShow) {
                const custom = {};
                let fieldType = item.fieldType;
                if (fieldType == "label") {
                  custom.fieldType = "subTitle";
                }
                if (item.dataType == "money(yuan)") {
                  custom.fieldType = "number";
                  custom.format = "yuan";
                  custom.suffix = "元";
                } else if (item.dataType == "percentile") {
                  custom.fieldType = "number";
                  custom.format = "percentage";
                  custom.suffix = "%";
                }
                if (fieldType == "select" && item.optionType == "2") {
                  custom.optionConfig = {
                    optionSource: "dictionary",
                    dictionaryValue: item.dicKey
                  };
                }
                const required = (_a2 = item.rules) == null ? void 0 : _a2.find(
                  (rule) => rule.type == "required"
                );
                if (required) {
                  custom.required = true;
                }
                if (item.maxlength) {
                  custom.maxLength = item.maxlength;
                }
                editConfigData.value.fieldsData.push({
                  x,
                  y,
                  w: 1,
                  h: 1,
                  i: id,
                  isResizable: true,
                  component: {
                    ...index$a.defaultProperty,
                    id,
                    type: "widgets",
                    fieldType,
                    label: item.label,
                    fieldName: item.fieldName,
                    disabled: item.disabled || item.readOnly,
                    readOnly: item.readOnly,
                    defaultValue: item.value,
                    showWordCount: item["show-word-limit"],
                    ...custom
                  },
                  moved: false,
                  id
                });
              } else {
                const newField = {
                  id,
                  fieldType: "text",
                  type: "widgets",
                  label: item.label,
                  fieldName: item.fieldName,
                  disabled: item.disabled,
                  readOnly: item.readOnly,
                  value: item.value,
                  defaultValue: item.value
                };
                editConfigData.value.hiddenFields.push(newField);
              }
            }
          }, 5);
        });
      } else if (type == "table") {
        if ((_a = editConfigData.value.queryConfig) == null ? void 0 : _a.filterFields) {
          (_b = editConfigData.value.queryConfig) == null ? void 0 : _b.filterFields.splice(0);
        } else {
          editConfigData.value.queryConfig.filterFields = [];
        }
        context.columns.forEach((item, index2) => {
          setTimeout(() => {
            const id = generateId();
            if (item.isShow) {
              const custom = {};
              let fieldType = item.fieldType;
              if (item.dataType == "money(yuan)") {
                custom.fieldType = "number";
                custom.format = "yuan";
                custom.suffix = "元";
                custom.align = "right";
              } else if (item.dataType == "percentile") {
                custom.fieldType = "number";
                custom.format = "percentage";
                custom.suffix = "%";
              }
              if (fieldType == "select" && item.optionType == "2") {
                custom.optionConfig = {
                  optionSource: "dictionary",
                  dictionaryValue: item.dicKey
                };
              }
              if (item.isAutoWidth) {
                custom.widthMode = "auto";
              } else {
                custom.widthMode = "fixed";
                custom.width = item.width;
              }
              editConfigData.value.fieldsData.push({
                ...index$b.defaultProperty,
                id,
                type: "widgets",
                fieldType,
                label: item.label,
                fieldName: item.prop,
                disabled: item.disabled,
                readOnly: item.readOnly,
                defaultValue: item.value,
                ...custom
              });
            } else {
              const newField = {
                id,
                fieldType: "text",
                type: "widgets",
                label: item.label,
                fieldName: item.prop
              };
              editConfigData.value.hiddenFields.push(newField);
            }
            if (item.isQuery) {
              editConfigData.value.queryConfig.filterFields.push({
                id,
                fieldId: id,
                type: "filterWidgets",
                queryType: "fuzzy"
              });
            }
          }, 5);
        });
      }
      index$9.ElMessage.success(`已选择模板: ${row.templateName}`);
    }
    let lastTime = 0;
    let sequence = 0;
    function generateId() {
      const now = Date.now();
      if (now === lastTime) {
        sequence++;
      } else {
        sequence = 0;
        lastTime = now;
      }
      return `${now}${sequence}`;
    }
    function handleTemplateDialogClose() {
    }
    return (_ctx, _cache) => {
      const _component_el_icon = index$2.ElIcon;
      const _component_el_input = index$6.ElInput;
      const _component_el_form_item = index$5.ElFormItem;
      const _component_el_form = index$5.ElForm;
      const _component_el_button = index$7.ElButton;
      const _component_el_dialog = index$4.ElDialog;
      const _component_el_header = index$1.ElHeader;
      return Vue.openBlock(), Vue.createBlock(_component_el_header, { class: "dev-root-header" }, {
        default: Vue.withCtx(() => [
          Vue.createElementVNode("div", _hoisted_1, [
            Vue.createElementVNode("div", _hoisted_2, [
              _cache[6] || (_cache[6] = Vue.createElementVNode("img", {
                src: logo.default,
                alt: "logo",
                class: "logo-img"
              }, null, -1)),
              _cache[7] || (_cache[7] = Vue.createElementVNode("div", { class: "title" }, "表单设计器", -1)),
              Vue.createElementVNode("div", _hoisted_3, "v" + Vue.toDisplayString(Vue.unref(version)), 1)
            ]),
            Vue.createElementVNode("div", {
              class: "btn-item",
              onClick: save
            }, [
              Vue.createVNode(_component_el_icon, null, {
                default: Vue.withCtx(() => [
                  Vue.createVNode(Vue.unref(index$3.UploadFilled))
                ]),
                _: 1
              }),
              _cache[8] || (_cache[8] = Vue.createElementVNode("span", null, "保存", -1))
            ]),
            Vue.createElementVNode("div", {
              class: "btn-item",
              onClick: openTemplateSelector
            }, [
              Vue.createVNode(_component_el_icon, null, {
                default: Vue.withCtx(() => [
                  Vue.createVNode(Vue.unref(index$3.DocumentAdd))
                ]),
                _: 1
              }),
              _cache[9] || (_cache[9] = Vue.createElementVNode("span", null, "导入旧版", -1))
            ])
          ]),
          Vue.createElementVNode("div", _hoisted_4, [
            Vue.createElementVNode("div", _hoisted_5, [
              Vue.createElementVNode("div", _hoisted_6, Vue.toDisplayString(getTemplateData().templateName), 1),
              Vue.createVNode(_component_el_icon, {
                class: "edit-icon",
                onClick: openEditDialog
              }, {
                default: Vue.withCtx(() => [
                  Vue.createVNode(Vue.unref(index$3.Edit))
                ]),
                _: 1
              })
            ]),
            Vue.createElementVNode("div", _hoisted_7, Vue.toDisplayString(getTemplateData().templateKey), 1)
          ]),
          Vue.createElementVNode("div", _hoisted_8, [
            (Vue.openBlock(), Vue.createBlock(Vue.resolveDynamicComponent(editingHeaderBtn.value)))
          ]),
          Vue.createVNode(_component_el_dialog, {
            modelValue: editDialogVisible.value,
            "onUpdate:modelValue": _cache[4] || (_cache[4] = ($event) => editDialogVisible.value = $event),
            title: "编辑模板信息",
            width: "500px",
            draggable: "",
            overflow: "",
            "append-to-body": "",
            class: "fec-dev-designer-dialog"
          }, {
            footer: Vue.withCtx(() => [
              Vue.createElementVNode("div", _hoisted_9, [
                Vue.createVNode(_component_el_button, {
                  onClick: _cache[3] || (_cache[3] = ($event) => editDialogVisible.value = false)
                }, {
                  default: Vue.withCtx(() => _cache[10] || (_cache[10] = [
                    Vue.createTextVNode("取消")
                  ])),
                  _: 1
                }),
                Vue.createVNode(_component_el_button, {
                  type: "primary",
                  onClick: saveTemplateInfo
                }, {
                  default: Vue.withCtx(() => _cache[11] || (_cache[11] = [
                    Vue.createTextVNode("确定")
                  ])),
                  _: 1
                })
              ])
            ]),
            default: Vue.withCtx(() => [
              Vue.createVNode(_component_el_form, {
                model: Vue.unref(editForm),
                rules: editFormRules,
                ref_key: "editFormRef",
                ref: editFormRef,
                "label-width": "100px"
              }, {
                default: Vue.withCtx(() => [
                  Vue.createVNode(_component_el_form_item, {
                    label: "模板标识",
                    prop: "templateKey"
                  }, {
                    default: Vue.withCtx(() => [
                      Vue.createVNode(_component_el_input, {
                        modelValue: Vue.unref(editForm).templateKey,
                        "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => Vue.unref(editForm).templateKey = $event),
                        placeholder: "请输入模板标识"
                      }, null, 8, ["modelValue"])
                    ]),
                    _: 1
                  }),
                  Vue.createVNode(_component_el_form_item, {
                    label: "模板名称",
                    prop: "templateName"
                  }, {
                    default: Vue.withCtx(() => [
                      Vue.createVNode(_component_el_input, {
                        modelValue: Vue.unref(editForm).templateName,
                        "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => Vue.unref(editForm).templateName = $event),
                        placeholder: "请输入模板名称"
                      }, null, 8, ["modelValue"])
                    ]),
                    _: 1
                  }),
                  Vue.createVNode(_component_el_form_item, {
                    label: "模板描述",
                    prop: "templateDesc"
                  }, {
                    default: Vue.withCtx(() => [
                      Vue.createVNode(_component_el_input, {
                        modelValue: Vue.unref(editForm).templateDesc,
                        "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => Vue.unref(editForm).templateDesc = $event),
                        type: "textarea",
                        rows: 4,
                        placeholder: "请输入模板描述",
                        resize: "none",
                        maxlength: "200",
                        "show-word-limit": ""
                      }, null, 8, ["modelValue"])
                    ]),
                    _: 1
                  })
                ]),
                _: 1
              }, 8, ["model"])
            ]),
            _: 1
          }, 8, ["modelValue"]),
          Vue.createVNode(TemplateDialog.default, {
            modelValue: templateDialogVisible.value,
            "onUpdate:modelValue": _cache[5] || (_cache[5] = ($event) => templateDialogVisible.value = $event),
            templateVersion: "v4",
            templateType: editingType.value,
            title: "选择旧版模板",
            onConfirm: handleTemplateSelect,
            onClose: handleTemplateDialogClose
          }, null, 8, ["modelValue", "templateType"])
        ]),
        _: 1
      });
    };
  }
};
exports.default = _sfc_main;
