import "../../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                          */
/* empty css                                                               */
/* empty css                                                                */
/* empty css                                                               */
/* empty css                                                               */
/* empty css                                                             */
/* empty css                                                                  */
/* empty css                                                              */
import { getCurrentInstance, computed, ref, reactive, createBlock, openBlock, withCtx, createElementVNode, createVNode, toDisplayString, unref, resolveDynamicComponent, createTextVNode } from "vue";
import _imports_0 from "../../assets/logo.png.mjs";
import { UploadFilled as upload_filled_default, DocumentAdd as document_add_default, Edit as edit_default } from "../../../../node_modules/@element-plus/icons-vue/dist/index.mjs";
import TemplateDialog from "../../components/TemplateDialog.vue.mjs";
import tableHeaderBtn from "../../packages/table/headerBtn.vue.mjs";
import formHeaderBtn from "../../packages/form/headerBtn.vue.mjs";
import { getEditConfigData, getEditingType } from "../../packages/utils/common.mjs";
import api from "../../api/index.mjs";
import store from "../../store/index.mjs";
import packageJson from "../../../package.json.mjs";
import { defaultProperty } from "../../packages/form/aside/index.mjs";
import { defaultProperty as defaultProperty$1 } from "../../packages/table/aside/index.mjs";
/* empty css            */
import { ElHeader } from "../../../../node_modules/element-plus/es/components/container/index.mjs";
import { ElIcon } from "../../../../node_modules/element-plus/es/components/icon/index.mjs";
import { ElDialog } from "../../../../node_modules/element-plus/es/components/dialog/index.mjs";
import { ElForm, ElFormItem } from "../../../../node_modules/element-plus/es/components/form/index.mjs";
import { ElInput } from "../../../../node_modules/element-plus/es/components/input/index.mjs";
import { ElButton } from "../../../../node_modules/element-plus/es/components/button/index.mjs";
import { ElMessage } from "../../../../node_modules/element-plus/es/components/message/index.mjs";
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 = getCurrentInstance();
    const ctx = currentInstance.proxy;
    const editingHeaderBtn = computed(() => {
      switch (store.getters.editingType) {
        case "table":
          return tableHeaderBtn;
        case "form":
          return formHeaderBtn;
      }
      return null;
    });
    const editConfigData = computed(() => {
      return getEditConfigData();
    });
    const editingType = computed(() => getEditingType());
    const version = packageJson.version;
    const getTemplateData = () => {
      return editConfigData.value || {};
    };
    const editDialogVisible = ref(false);
    const editFormRef = ref(null);
    const editForm = 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)
      };
      api.updateById(ctx.$http, templateData).then(() => {
        ElMessage.success("保存成功");
      });
    }
    const templateDialogVisible = 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, index) => {
          setTimeout(() => {
            var _a2;
            const id = generateId();
            const x = index % columns;
            const y = Math.floor(index / 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: {
                    ...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, index) => {
          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({
                ...defaultProperty$1,
                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);
        });
      }
      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 = ElIcon;
      const _component_el_input = ElInput;
      const _component_el_form_item = ElFormItem;
      const _component_el_form = ElForm;
      const _component_el_button = ElButton;
      const _component_el_dialog = ElDialog;
      const _component_el_header = ElHeader;
      return openBlock(), createBlock(_component_el_header, { class: "dev-root-header" }, {
        default: withCtx(() => [
          createElementVNode("div", _hoisted_1, [
            createElementVNode("div", _hoisted_2, [
              _cache[6] || (_cache[6] = createElementVNode("img", {
                src: _imports_0,
                alt: "logo",
                class: "logo-img"
              }, null, -1)),
              _cache[7] || (_cache[7] = createElementVNode("div", { class: "title" }, "表单设计器", -1)),
              createElementVNode("div", _hoisted_3, "v" + toDisplayString(unref(version)), 1)
            ]),
            createElementVNode("div", {
              class: "btn-item",
              onClick: save
            }, [
              createVNode(_component_el_icon, null, {
                default: withCtx(() => [
                  createVNode(unref(upload_filled_default))
                ]),
                _: 1
              }),
              _cache[8] || (_cache[8] = createElementVNode("span", null, "保存", -1))
            ]),
            createElementVNode("div", {
              class: "btn-item",
              onClick: openTemplateSelector
            }, [
              createVNode(_component_el_icon, null, {
                default: withCtx(() => [
                  createVNode(unref(document_add_default))
                ]),
                _: 1
              }),
              _cache[9] || (_cache[9] = createElementVNode("span", null, "导入旧版", -1))
            ])
          ]),
          createElementVNode("div", _hoisted_4, [
            createElementVNode("div", _hoisted_5, [
              createElementVNode("div", _hoisted_6, toDisplayString(getTemplateData().templateName), 1),
              createVNode(_component_el_icon, {
                class: "edit-icon",
                onClick: openEditDialog
              }, {
                default: withCtx(() => [
                  createVNode(unref(edit_default))
                ]),
                _: 1
              })
            ]),
            createElementVNode("div", _hoisted_7, toDisplayString(getTemplateData().templateKey), 1)
          ]),
          createElementVNode("div", _hoisted_8, [
            (openBlock(), createBlock(resolveDynamicComponent(editingHeaderBtn.value)))
          ]),
          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: withCtx(() => [
              createElementVNode("div", _hoisted_9, [
                createVNode(_component_el_button, {
                  onClick: _cache[3] || (_cache[3] = ($event) => editDialogVisible.value = false)
                }, {
                  default: withCtx(() => _cache[10] || (_cache[10] = [
                    createTextVNode("取消")
                  ])),
                  _: 1
                }),
                createVNode(_component_el_button, {
                  type: "primary",
                  onClick: saveTemplateInfo
                }, {
                  default: withCtx(() => _cache[11] || (_cache[11] = [
                    createTextVNode("确定")
                  ])),
                  _: 1
                })
              ])
            ]),
            default: withCtx(() => [
              createVNode(_component_el_form, {
                model: unref(editForm),
                rules: editFormRules,
                ref_key: "editFormRef",
                ref: editFormRef,
                "label-width": "100px"
              }, {
                default: withCtx(() => [
                  createVNode(_component_el_form_item, {
                    label: "模板标识",
                    prop: "templateKey"
                  }, {
                    default: withCtx(() => [
                      createVNode(_component_el_input, {
                        modelValue: unref(editForm).templateKey,
                        "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => unref(editForm).templateKey = $event),
                        placeholder: "请输入模板标识"
                      }, null, 8, ["modelValue"])
                    ]),
                    _: 1
                  }),
                  createVNode(_component_el_form_item, {
                    label: "模板名称",
                    prop: "templateName"
                  }, {
                    default: withCtx(() => [
                      createVNode(_component_el_input, {
                        modelValue: unref(editForm).templateName,
                        "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => unref(editForm).templateName = $event),
                        placeholder: "请输入模板名称"
                      }, null, 8, ["modelValue"])
                    ]),
                    _: 1
                  }),
                  createVNode(_component_el_form_item, {
                    label: "模板描述",
                    prop: "templateDesc"
                  }, {
                    default: withCtx(() => [
                      createVNode(_component_el_input, {
                        modelValue: unref(editForm).templateDesc,
                        "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => 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"]),
          createVNode(TemplateDialog, {
            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
      });
    };
  }
};
export {
  _sfc_main as default
};
