import "../../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                          */
/* empty css                                                                */
/* empty css                                                               */
/* empty css                                                                  */
/* empty css                                                               */
import "../../../../node_modules/element-plus/theme-chalk/el-tooltip.css.mjs";
/* empty css                                                                 */
/* empty css                                                              */
/* empty css                                                              */
/* empty css                                                            */
/* empty css                                                                     */
/* empty css                                                               */
import { useModel, ref, computed, watch, createBlock, openBlock, withCtx, createElementVNode, createVNode, unref, createTextVNode, toDisplayString } from "vue";
import { Plus as plus_default, RefreshRight as refresh_right_default, Delete as delete_default } from "../../../../node_modules/@element-plus/icons-vue/dist/index.mjs";
import { getEditConfigData, getEditingType, cloneDeep, getEditConfigDataFields } from "../../packages/utils/common.mjs";
import { defaultProperty } from "../../packages/form/aside/index.mjs";
import { defaultProperty as defaultProperty$1 } from "../../packages/table/aside/index.mjs";
import ValueSelector from "../../components/ValueSelector.vue.mjs";
/* empty css                        */
import _export_sfc from "../../../../_virtual/_plugin-vue_export-helper.mjs";
import { ElDialog } from "../../../../node_modules/element-plus/es/components/dialog/index.mjs";
import { ElButton } from "../../../../node_modules/element-plus/es/components/button/index.mjs";
import { ElTable, ElTableColumn } from "../../../../node_modules/element-plus/es/components/table/index.mjs";
import { ElInput } from "../../../../node_modules/element-plus/es/components/input/index.mjs";
import { ElMessage } from "../../../../node_modules/element-plus/es/components/message/index.mjs";
import { ElMessageBox } from "../../../../node_modules/element-plus/es/components/message-box/index.mjs";
const _hoisted_1 = { class: "action-bar" };
const _hoisted_2 = { class: "left-buttons" };
const _hoisted_3 = { class: "count-info" };
const _hoisted_4 = { class: "field-list-container" };
const _hoisted_5 = { class: "field-name-cell" };
const _hoisted_6 = { class: "dialog-footer" };
const _sfc_main = {
  __name: "HiddenFieldDialog",
  props: {
    "modelValue": { required: true },
    "modelModifiers": {}
  },
  emits: ["update:modelValue"],
  setup(__props) {
    const dialogVisible = useModel(__props, "modelValue");
    const tableRef = ref(null);
    const selectedRows = ref([]);
    const editConfigData = computed(() => getEditConfigData());
    const hiddenFields = ref([]);
    watch(dialogVisible, (val) => {
      if (val) {
        loadHiddenFields();
      }
    });
    const loadHiddenFields = () => {
      var _a;
      hiddenFields.value = (((_a = editConfigData.value) == null ? void 0 : _a.hiddenFields) || []).map(
        (field) => ({
          ...field,
          value: field.value || field.defaultValue
        })
      );
    };
    function handleSelectionChange(rows) {
      selectedRows.value = rows;
    }
    function addHiddenField() {
      const newField = {
        id: Date.now(),
        fieldType: "text",
        type: "widgets",
        label: "",
        fieldName: "",
        value: ""
      };
      hiddenFields.value.push(newField);
      setTimeout(() => {
        const cells = document.querySelectorAll(".el-table__body .el-input input");
        if (cells.length > 0) {
          cells[cells.length - 3].focus();
          cells[cells.length - 3].select();
        }
      }, 100);
    }
    function handleSave() {
      for (const field of hiddenFields.value) {
        if (!field.label.trim()) {
          ElMessage.warning("标签名不能为空");
          return;
        }
        if (!field.fieldName.trim()) {
          ElMessage.warning("字段名不能为空");
          return;
        }
      }
      const hiddenFieldNames = hiddenFields.value.map((f) => f.fieldName);
      const formFieldNames = (getEditConfigDataFields() || []).map(
        (f) => f.fieldName
      );
      const hiddenDuplicates = hiddenFieldNames.filter(
        (name, index) => hiddenFieldNames.indexOf(name) !== index
      );
      if (hiddenDuplicates.length > 0) {
        ElMessage.error(`隐藏域字段名重复: ${hiddenDuplicates.join(", ")}`);
        return;
      }
      const conflictedFields = hiddenFieldNames.filter(
        (name) => formFieldNames.includes(name)
      );
      if (conflictedFields.length > 0) {
        ElMessage.error(`与表单字段冲突: ${conflictedFields.join(", ")}`);
        return;
      }
      if (editConfigData.value) {
        editConfigData.value.hiddenFields = cloneDeep(hiddenFields.value);
      }
      dialogVisible.value = false;
    }
    function deleteRow(row) {
      ElMessageBox.confirm("确定要删除此隐藏域吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        const index = hiddenFields.value.findIndex((f) => f.id === row.id);
        if (index !== -1) {
          hiddenFields.value.splice(index, 1);
          ElMessage.success("删除成功");
          if (editConfigData.value) {
            editConfigData.value.hiddenFields = cloneDeep(hiddenFields.value);
          }
        }
      }).catch(() => {
      });
    }
    function batchDelete() {
      ElMessageBox.confirm(
        `确定要删除选中的 ${selectedRows.value.length} 个隐藏域吗？`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        selectedRows.value.forEach((row) => {
          const index = hiddenFields.value.findIndex((f) => f.id === row.id);
          if (index !== -1) {
            hiddenFields.value.splice(index, 1);
          }
        });
        selectedRows.value = [];
        ElMessage.success("批量删除成功");
        if (editConfigData.value) {
          editConfigData.value.hiddenFields = cloneDeep(hiddenFields.value);
        }
      }).catch(() => {
      });
    }
    function handleRestore() {
      if (selectedRows.value.length === 0) return;
      if (!editConfigData.value.fieldsData) {
        editConfigData.value.fieldsData = [];
      }
      if (getEditingType() == "form") {
        const maxY = editConfigData.value.fieldsData.reduce(
          (max, item) => Math.max(max, item.y + item.h),
          0
        );
        let currentY = maxY;
        selectedRows.value.forEach((field) => {
          let component = cloneDeep(field);
          component.defaultValue = field.value;
          delete component.value;
          component.id = component.id + "";
          component.fieldType = component.fieldType || "text";
          component.type = component.type || "widgets";
          component = { ...defaultProperty, ...component };
          let finalWH = { w: 1, h: 1 };
          if (component.fieldType == "textarea") {
            finalWH = { w: 1, h: 2 };
          } else if (component.fieldType == "subTitle" || component.fieldType == "h2") {
            finalWH = {
              w: editConfigData.value.columns,
              h: 1,
              isResizable: false
            };
          } else if (component.fieldType == "subForm" || component.fieldType == "subTable" || component.fieldType == "approvalHistory") {
            finalWH = { w: editConfigData.value.columns, h: 2 };
          }
          finalWH.y = currentY;
          currentY += finalWH.h;
          const item = {
            x: 0,
            y: finalWH.y,
            w: finalWH.w,
            h: finalWH.h,
            i: component.id,
            isResizable: finalWH.isResizable === false ? false : true,
            component,
            id: component.id
          };
          editConfigData.value.fieldsData.push(item);
          const index = hiddenFields.value.findIndex((f) => f.id == field.id);
          if (index !== -1) {
            hiddenFields.value.splice(index, 1);
          }
        });
      } else if (getEditingType() == "table") {
        selectedRows.value.forEach((field) => {
          let component = cloneDeep(field);
          component.id = component.id + "";
          component.fieldType = component.fieldType || "text";
          component.type = component.type || "widgets";
          component = { ...defaultProperty$1, ...component };
          editConfigData.value.fieldsData.push(component);
          const index = hiddenFields.value.findIndex((f) => f.id == field.id);
          if (index !== -1) {
            hiddenFields.value.splice(index, 1);
          }
        });
      }
      editConfigData.value.hiddenFields = cloneDeep(hiddenFields.value);
      ElMessage.success(`已恢复 ${selectedRows.value.length} 个字段`);
      selectedRows.value = [];
    }
    return (_ctx, _cache) => {
      const _component_el_button = ElButton;
      const _component_el_table_column = ElTableColumn;
      const _component_el_input = ElInput;
      const _component_el_table = ElTable;
      const _component_el_dialog = ElDialog;
      return openBlock(), createBlock(_component_el_dialog, {
        modelValue: dialogVisible.value,
        "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => dialogVisible.value = $event),
        title: "隐藏域配置",
        width: "900px",
        draggable: "",
        overflow: "",
        "append-to-body": "",
        class: "fec-dev-designer-dialog"
      }, {
        footer: withCtx(() => [
          createElementVNode("div", _hoisted_6, [
            createVNode(_component_el_button, {
              onClick: _cache[0] || (_cache[0] = ($event) => dialogVisible.value = false)
            }, {
              default: withCtx(() => _cache[6] || (_cache[6] = [
                createTextVNode("取消")
              ])),
              _: 1
            }),
            createVNode(_component_el_button, {
              type: "primary",
              onClick: handleSave
            }, {
              default: withCtx(() => _cache[7] || (_cache[7] = [
                createTextVNode("保存")
              ])),
              _: 1
            })
          ])
        ]),
        default: withCtx(() => [
          createElementVNode("div", _hoisted_1, [
            createElementVNode("div", _hoisted_2, [
              createVNode(_component_el_button, {
                type: "primary",
                icon: unref(plus_default),
                onClick: addHiddenField
              }, {
                default: withCtx(() => _cache[2] || (_cache[2] = [
                  createTextVNode(" 添加隐藏域 ")
                ])),
                _: 1
              }, 8, ["icon"]),
              createVNode(_component_el_button, {
                type: "primary",
                icon: unref(refresh_right_default),
                disabled: selectedRows.value.length === 0,
                onClick: handleRestore
              }, {
                default: withCtx(() => _cache[3] || (_cache[3] = [
                  createTextVNode(" 恢复 ")
                ])),
                _: 1
              }, 8, ["icon", "disabled"]),
              createVNode(_component_el_button, {
                type: "danger",
                icon: unref(delete_default),
                disabled: !selectedRows.value.length,
                onClick: batchDelete
              }, {
                default: withCtx(() => _cache[4] || (_cache[4] = [
                  createTextVNode(" 批量删除 ")
                ])),
                _: 1
              }, 8, ["icon", "disabled"])
            ]),
            createElementVNode("div", _hoisted_3, "共 " + toDisplayString(hiddenFields.value.length) + " 个隐藏域", 1)
          ]),
          createElementVNode("div", _hoisted_4, [
            createVNode(_component_el_table, {
              ref_key: "tableRef",
              ref: tableRef,
              data: hiddenFields.value,
              onSelectionChange: handleSelectionChange,
              height: "400"
            }, {
              default: withCtx(() => [
                createVNode(_component_el_table_column, {
                  type: "selection",
                  width: "50"
                }),
                createVNode(_component_el_table_column, {
                  label: "标签名",
                  width: "160"
                }, {
                  default: withCtx(({ row }) => [
                    createVNode(_component_el_input, {
                      modelValue: row.label,
                      "onUpdate:modelValue": ($event) => row.label = $event,
                      placeholder: "请输入标签名"
                    }, null, 8, ["modelValue", "onUpdate:modelValue"])
                  ]),
                  _: 1
                }),
                createVNode(_component_el_table_column, {
                  label: "字段名",
                  width: "160"
                }, {
                  default: withCtx(({ row }) => [
                    createElementVNode("div", _hoisted_5, [
                      createVNode(_component_el_input, {
                        modelValue: row.fieldName,
                        "onUpdate:modelValue": ($event) => row.fieldName = $event,
                        placeholder: "请输入字段名"
                      }, null, 8, ["modelValue", "onUpdate:modelValue"])
                    ])
                  ]),
                  _: 1
                }),
                createVNode(_component_el_table_column, { label: "初值" }, {
                  default: withCtx(({ row }) => [
                    createVNode(ValueSelector, {
                      modelValue: row.value,
                      "onUpdate:modelValue": ($event) => row.value = $event,
                      size: "default"
                    }, null, 8, ["modelValue", "onUpdate:modelValue"])
                  ]),
                  _: 1
                }),
                createVNode(_component_el_table_column, {
                  label: "操作",
                  width: "120",
                  fixed: "right"
                }, {
                  default: withCtx(({ row }) => [
                    createVNode(_component_el_button, {
                      link: "",
                      type: "danger",
                      icon: unref(delete_default),
                      onClick: ($event) => deleteRow(row)
                    }, {
                      default: withCtx(() => _cache[5] || (_cache[5] = [
                        createTextVNode(" 删除 ")
                      ])),
                      _: 2
                    }, 1032, ["icon", "onClick"])
                  ]),
                  _: 1
                })
              ]),
              _: 1
            }, 8, ["data"])
          ])
        ]),
        _: 1
      }, 8, ["modelValue"]);
    };
  }
};
const HiddenFieldDialog = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-b2acaeb1"]]);
export {
  HiddenFieldDialog as default
};
