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                                                               */
import { useModel, computed, ref, watch, createBlock, openBlock, withCtx, createElementVNode, createVNode, unref, createTextVNode } from "vue";
import { RefreshRight as refresh_right_default, Delete as delete_default } from "../../../../node_modules/@element-plus/icons-vue/dist/index.mjs";
import { getEditConfigData, getEditingType, cloneDeep } from "../../packages/utils/common.mjs";
/* empty css                                                              */
/* empty css                                                                */
/* empty css                                                              */
/* empty css                                                                    */
/* 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 { 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: "field-list-container" };
const _sfc_main = {
  __name: "fieldRecycleBin",
  props: {
    "modelValue": { required: true },
    "modelModifiers": {}
  },
  emits: ["update:modelValue"],
  setup(__props) {
    const dialogVisible = useModel(__props, "modelValue");
    const editConfigData = computed(() => {
      return getEditConfigData();
    });
    const deletedFields = ref([]);
    const selectedFields = ref([]);
    const tableRef = ref(null);
    watch(dialogVisible, (val) => {
      if (val) {
        loadDeletedFields();
      }
    });
    const loadDeletedFields = () => {
      deletedFields.value = editConfigData.value.deletedFields || [];
    };
    const handleSelectionChange = (selection) => {
      selectedFields.value = selection;
    };
    const handleRestore = () => {
      if (selectedFields.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;
        selectedFields.value.forEach((field) => {
          const restoredItem = cloneDeep(field.data);
          restoredItem.y = currentY;
          currentY += restoredItem.h;
          editConfigData.value.fieldsData.push(restoredItem);
          const index = deletedFields.value.findIndex((f) => f === field);
          if (index !== -1) {
            deletedFields.value.splice(index, 1);
          }
        });
      } else if (getEditingType() == "table") {
        selectedFields.value.forEach((field) => {
          const restoredItem = cloneDeep(field.data);
          editConfigData.value.fieldsData.push(restoredItem);
          const index = deletedFields.value.findIndex((f) => f === field);
          if (index !== -1) {
            deletedFields.value.splice(index, 1);
          }
        });
      }
      editConfigData.value.deletedFields = cloneDeep(deletedFields.value);
      ElMessage.success(`已恢复 ${selectedFields.value.length} 个字段`);
      selectedFields.value = [];
    };
    const handlePermanentDelete = async () => {
      if (selectedFields.value.length === 0) return;
      try {
        await ElMessageBox.confirm(
          `确定要彻底删除选中的 ${selectedFields.value.length} 个字段吗？此操作不可恢复。`,
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        );
        selectedFields.value.forEach((field) => {
          const index = deletedFields.value.findIndex((f) => f === field);
          if (index !== -1) {
            deletedFields.value.splice(index, 1);
          }
        });
        editConfigData.value.deletedFields = cloneDeep(deletedFields.value);
        selectedFields.value = [];
        ElMessage.success("字段已彻底删除");
      } catch {
      }
    };
    const handleClearRecycleBin = async () => {
      if (deletedFields.value.length === 0) return;
      try {
        await ElMessageBox.confirm(
          "确定要清空回收站吗？此操作将永久删除所有字段，不可恢复。",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        );
        deletedFields.value = [];
        editConfigData.value.deletedFields = [];
        ElMessage.success("回收站已清空");
      } catch {
      }
    };
    return (_ctx, _cache) => {
      const _component_el_button = ElButton;
      const _component_el_table_column = ElTableColumn;
      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(() => [
          createVNode(_component_el_button, {
            onClick: _cache[0] || (_cache[0] = ($event) => dialogVisible.value = false)
          }, {
            default: withCtx(() => _cache[5] || (_cache[5] = [
              createTextVNode("关闭")
            ])),
            _: 1
          })
        ]),
        default: withCtx(() => [
          createElementVNode("div", _hoisted_1, [
            createElementVNode("div", _hoisted_2, [
              createVNode(_component_el_button, {
                type: "primary",
                icon: unref(refresh_right_default),
                disabled: selectedFields.value.length === 0,
                onClick: handleRestore
              }, {
                default: withCtx(() => _cache[2] || (_cache[2] = [
                  createTextVNode(" 恢复 ")
                ])),
                _: 1
              }, 8, ["icon", "disabled"]),
              createVNode(_component_el_button, {
                type: "danger",
                icon: unref(delete_default),
                disabled: selectedFields.value.length === 0,
                onClick: handlePermanentDelete
              }, {
                default: withCtx(() => _cache[3] || (_cache[3] = [
                  createTextVNode(" 彻底删除 ")
                ])),
                _: 1
              }, 8, ["icon", "disabled"])
            ]),
            createVNode(_component_el_button, {
              type: "danger",
              icon: unref(delete_default),
              plain: "",
              disabled: deletedFields.value.length === 0,
              onClick: handleClearRecycleBin
            }, {
              default: withCtx(() => _cache[4] || (_cache[4] = [
                createTextVNode(" 清空回收站 ")
              ])),
              _: 1
            }, 8, ["icon", "disabled"])
          ]),
          createElementVNode("div", _hoisted_3, [
            createVNode(_component_el_table, {
              ref_key: "tableRef",
              ref: tableRef,
              data: deletedFields.value,
              onSelectionChange: handleSelectionChange,
              height: "400"
            }, {
              default: withCtx(() => [
                createVNode(_component_el_table_column, {
                  type: "selection",
                  width: "55"
                }),
                createVNode(_component_el_table_column, {
                  prop: "label",
                  label: "字段标签",
                  "min-width": "120"
                }),
                createVNode(_component_el_table_column, {
                  prop: "fieldName",
                  label: "字段名称",
                  "min-width": "120"
                }),
                createVNode(_component_el_table_column, {
                  prop: "fieldType",
                  label: "字段类型",
                  width: "100"
                }),
                createVNode(_component_el_table_column, {
                  prop: "deleteTime",
                  label: "删除时间",
                  width: "180"
                })
              ]),
              _: 1
            }, 8, ["data"])
          ])
        ]),
        _: 1
      }, 8, ["modelValue"]);
    };
  }
};
const FieldRecycleBin = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-d770fcac"]]);
export {
  FieldRecycleBin as default
};
