import "../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                       */
/* empty css                                                             */
/* empty css                                                            */
/* empty css                                                           */
/* empty css                                                               */
/* empty css                                                            */
/* 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                                                            */
import { getCurrentInstance, computed, ref, watch, createBlock, openBlock, withCtx, createElementVNode, createVNode, withKeys, unref, createTextVNode } from "vue";
import { Search as search_default } from "../../../node_modules/@element-plus/icons-vue/dist/index.mjs";
import api from "../api/index.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 { ElInput } from "../../../node_modules/element-plus/es/components/input/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 { ElPagination } from "../../../node_modules/element-plus/es/components/pagination/index.mjs";
import { ElMessage } from "../../../node_modules/element-plus/es/components/message/index.mjs";
const _hoisted_1 = { class: "search-bar" };
const _hoisted_2 = { class: "dialog-footer" };
const _sfc_main = {
  __name: "TemplateDialog",
  props: {
    modelValue: {
      type: Boolean,
      default: false
    },
    templateType: {
      type: String,
      default: ""
    },
    title: {
      type: String,
      default: "选择模板"
    },
    width: {
      type: String,
      default: "50%"
    },
    searchPlaceholder: {
      type: String,
      default: "搜索模板名称/标识"
    },
    nameLabel: {
      type: String,
      default: "模板名称"
    },
    keyLabel: {
      type: String,
      default: "模板标识"
    },
    templateVersion: {
      type: String,
      default: "v5"
    }
  },
  emits: ["update:modelValue", "confirm", "close"],
  setup(__props, { emit: __emit }) {
    const currentInstance = getCurrentInstance();
    const ctx = currentInstance.proxy;
    const props = __props;
    const emit = __emit;
    const dialogVisible = computed({
      get: () => props.modelValue,
      set: (val) => emit("update:modelValue", val)
    });
    const searchText = ref("");
    const templateList = ref([]);
    const total = ref(0);
    const currentRow = ref(null);
    const pageInfo = ref({
      pageNo: 1,
      pageSize: 10
    });
    watch(
      () => props.modelValue,
      (val) => {
        if (val) {
          searchText.value = "";
          currentRow.value = null;
          pageInfo.value = {
            pageNo: 1,
            pageSize: 10
          };
          loadTemplateList();
        }
      },
      { immediate: true }
    );
    async function loadTemplateList() {
      try {
        const params = {
          pageNo: pageInfo.value.pageNo,
          pageSize: pageInfo.value.pageSize,
          type: props.templateType,
          templateVersion: props.templateVersion
        };
        if (searchText.value) {
          params.dynamicQuery = searchText.value;
        }
        const data = await api.findTemplateByPagePop(ctx.$http, params);
        templateList.value = data.records || data.list || [];
        total.value = data.total || 0;
      } catch (error) {
        console.error("加载表单列表失败:", error);
        templateList.value = [];
        total.value = 0;
      }
    }
    function handleSearch() {
      pageInfo.value.pageNo = 1;
      loadTemplateList();
    }
    function handleCurrentChange(row) {
      currentRow.value = row;
    }
    function handleSizeChange(size) {
      pageInfo.value.pageSize = size;
      pageInfo.value.pageNo = 1;
      loadTemplateList();
    }
    function handlePageChange(page) {
      pageInfo.value.pageNo = page;
      loadTemplateList();
    }
    function closeDialog() {
      dialogVisible.value = false;
      emit("close");
    }
    function onConfirm() {
      if (!currentRow.value) {
        ElMessage.warning("请先选择表单");
        return;
      }
      const context = JSON.parse(currentRow.value.context);
      emit("confirm", {
        row: currentRow.value,
        context
      });
      dialogVisible.value = false;
    }
    return (_ctx, _cache) => {
      const _component_el_button = ElButton;
      const _component_el_input = ElInput;
      const _component_el_table_column = ElTableColumn;
      const _component_el_table = ElTable;
      const _component_el_pagination = ElPagination;
      const _component_el_dialog = ElDialog;
      return openBlock(), createBlock(_component_el_dialog, {
        modelValue: dialogVisible.value,
        "onUpdate:modelValue": _cache[3] || (_cache[3] = ($event) => dialogVisible.value = $event),
        width: __props.width,
        title: __props.title,
        draggable: "",
        overflow: "",
        "append-to-body": "",
        class: "fec-dev-designer-dialog"
      }, {
        footer: withCtx(() => [
          createElementVNode("div", _hoisted_2, [
            createVNode(_component_el_button, {
              onClick: closeDialog,
              size: "default"
            }, {
              default: withCtx(() => _cache[4] || (_cache[4] = [
                createTextVNode("取消")
              ])),
              _: 1
            }),
            createVNode(_component_el_button, {
              type: "primary",
              onClick: onConfirm,
              size: "default"
            }, {
              default: withCtx(() => _cache[5] || (_cache[5] = [
                createTextVNode("确定")
              ])),
              _: 1
            })
          ])
        ]),
        default: withCtx(() => [
          createElementVNode("div", _hoisted_1, [
            createVNode(_component_el_input, {
              modelValue: searchText.value,
              "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => searchText.value = $event),
              placeholder: __props.searchPlaceholder || "搜索表单名称/表单标识",
              clearable: "",
              onClear: handleSearch,
              onKeyup: withKeys(handleSearch, ["enter"]),
              style: { "width": "300px" },
              size: "default"
            }, {
              append: withCtx(() => [
                createVNode(_component_el_button, {
                  icon: unref(search_default),
                  onClick: handleSearch
                }, null, 8, ["icon"])
              ]),
              _: 1
            }, 8, ["modelValue", "placeholder"])
          ]),
          createVNode(_component_el_table, {
            data: templateList.value,
            "highlight-current-row": "",
            "max-height": "500",
            onCurrentChange: handleCurrentChange,
            style: { "width": "100%", "margin-top": "10px" },
            size: "default"
          }, {
            default: withCtx(() => [
              createVNode(_component_el_table_column, {
                prop: "templateName",
                label: __props.nameLabel || "表单名称",
                "min-width": "200"
              }, null, 8, ["label"]),
              createVNode(_component_el_table_column, {
                prop: "templateKey",
                label: __props.keyLabel || "表单标识",
                "min-width": "200"
              }, null, 8, ["label"])
            ]),
            _: 1
          }, 8, ["data"]),
          createVNode(_component_el_pagination, {
            size: "small",
            "current-page": pageInfo.value.pageNo,
            "onUpdate:currentPage": _cache[1] || (_cache[1] = ($event) => pageInfo.value.pageNo = $event),
            "page-size": pageInfo.value.pageSize,
            "onUpdate:pageSize": _cache[2] || (_cache[2] = ($event) => pageInfo.value.pageSize = $event),
            "page-sizes": [10, 20, 50, 100],
            total: total.value,
            layout: "total, sizes, prev, pager, next, jumper",
            onSizeChange: handleSizeChange,
            onCurrentChange: handlePageChange,
            style: { "margin-top": "20px", "justify-content": "flex-end" }
          }, null, 8, ["current-page", "page-size", "total"])
        ]),
        _: 1
      }, 8, ["modelValue", "width", "title"]);
    };
  }
};
const TemplateDialog = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-ea3685a4"]]);
export {
  TemplateDialog as default
};
