import { pushScopeId, popScopeId, defineComponent, ref, onMounted, resolveComponent, createElementBlock, openBlock, Fragment, createVNode, mergeProps, toHandlers, unref, withCtx, renderSlot, normalizeProps, guardReactiveProps, createTextVNode, toDisplayString, createBlock, renderList, createElementVNode } from 'vue';
import { useCloned } from '@vueuse/core';
import '../../../../utils/index.mjs';
import '../../base/form/index.mjs';
import '../../../../hooks/index.mjs';
import '../../../index.mjs';
import { MemoryHistoryType, MemoryHistoryExt, memoryFormEmits, memoryFormProps } from './memoryForm.mjs';
import { getGmEvent } from '../../../../utils/src/elementPlus/event.mjs';
import { formEmits } from '../../base/form/form.mjs';
import { getGmExports } from '../../../../utils/src/elementPlus/exports.mjs';
import { useCustomSettingsStore } from '../../../../hooks/store/useCustomSettings/index.mjs';
import { GmMessageBox } from '../../plugins/messageBox/index.mjs';
import { GmMessage } from '../../plugins/message/index.mjs';

const _withScopeId = (n) => (pushScopeId("data-v-c17deec9"), n = n(), popScopeId(), n);
const _hoisted_1 = { class: "gm-flex-center gm-flex-justify-between gm-mr-2 gm-flex gm-flex-1" };
const _hoisted_2 = { class: "gm-flex gm-gap-2" };
var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{
    name: "GmMemoryForm"
  },
  __name: "MemoryForm",
  props: memoryFormProps,
  emits: memoryFormEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const elEvents = getGmEvent(formEmits, emit);
    const elRef = ref();
    const exposeNames = ["validate", "validateField", "resetFields", "clearValidate", "scrollToField"];
    const { exposeFns } = getGmExports(elRef, exposeNames);
    const submit = () => {
      elRef.value?.validate((valid) => {
        if (valid) {
          emit("submit", props.model);
        }
      });
    };
    const reset = () => {
      elRef.value?.resetFields();
      emit("reset", props.model);
    };
    const customSettingsStore = useCustomSettingsStore();
    const searchHistoryList = ref([]);
    const getSearchHistoryList = async () => {
      const data = await customSettingsStore.getCustomSettings(
        { key_word: props.pageName + MemoryHistoryExt, type: MemoryHistoryType },
        { defaultValue: [] }
      );
      if (!data) {
        return;
      }
      const { cloned: result } = useCloned(data || []);
      searchHistoryList.value = result.value;
    };
    const saveModel = () => {
      elRef.value?.validate((valid) => {
        if (valid) {
          GmMessageBox.prompt("\u8BF7\u8F93\u5165\u4FDD\u5B58\u7684\u540D\u79F0", "\u4FDD\u5B58\u67E5\u8BE2\u6761\u4EF6", {
            confirmButtonText: "\u786E\u5B9A",
            cancelButtonText: "\u53D6\u6D88",
            inputPattern: /^.{1,}$/,
            inputErrorMessage: "\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A"
          }).then(async ({ value }) => {
            const list = searchHistoryList.value;
            const item = {
              label: value,
              value: JSON.stringify(props.model)
            };
            list.unshift(item);
            searchHistoryList.value = list.slice(0, 10);
            customSettingsStore.addCustomSettings({
              key_word: props.pageName + MemoryHistoryExt,
              value: JSON.stringify(searchHistoryList.value),
              type: MemoryHistoryType
            });
            GmMessage({
              message: "\u67E5\u8BE2\u6761\u4EF6\u4FDD\u5B58\u6210\u529F",
              type: "success"
            });
            getSearchHistoryList();
          }).catch(() => {
          });
        }
      });
    };
    const showModels = ref(false);
    const viewModels = () => {
      showModels.value = true;
    };
    const useModel = (value) => {
      const model = JSON.parse(value);
      emit("useHistory", model);
      emit("update:model", model);
      emit("submit", model);
      showModels.value = false;
    };
    const getValues = (value) => {
      if (!value) return [];
      const model = JSON.parse(value);
      return Object.values(model).filter((val) => !!val || val === 0 || val === false);
    };
    onMounted(() => {
      getSearchHistoryList();
    });
    __expose({
      ...exposeFns
    });
    return (_ctx, _cache) => {
      const _component_gm_button = resolveComponent("gm-button");
      const _component_gm_form_item = resolveComponent("gm-form-item");
      const _component_el_button = resolveComponent("el-button");
      const _component_gm_tooltip = resolveComponent("gm-tooltip");
      const _component_gm_form = resolveComponent("gm-form");
      const _component_el_tag = resolveComponent("el-tag");
      const _component_gm_collapse_item = resolveComponent("gm-collapse-item");
      const _component_gm_collapse = resolveComponent("gm-collapse");
      const _component_gm_dialog = resolveComponent("gm-dialog");
      return openBlock(), createElementBlock(
        Fragment,
        null,
        [
          createVNode(_component_gm_form, mergeProps({
            id: _ctx.$attrs.id,
            ref_key: "elRef",
            ref: elRef,
            class: _ctx.$attrs.class
          }, props, toHandlers(unref(elEvents))), {
            default: withCtx((slotValue) => [
              renderSlot(_ctx.$slots, "default", normalizeProps(guardReactiveProps(slotValue)), void 0, true),
              renderSlot(_ctx.$slots, "buttons", {}, () => [
                createVNode(_component_gm_form_item, null, {
                  default: withCtx(() => [
                    createVNode(_component_gm_button, {
                      type: "primary",
                      onClick: submit
                    }, {
                      default: withCtx(() => [
                        createTextVNode(
                          toDisplayString(props.submitText || "\u63D0\u4EA4"),
                          1
                          /* TEXT */
                        )
                      ]),
                      _: 1
                      /* STABLE */
                    }),
                    createVNode(_component_gm_button, { onClick: reset }, {
                      default: withCtx(() => [
                        createTextVNode(
                          toDisplayString(props.resetText || "\u91CD\u7F6E"),
                          1
                          /* TEXT */
                        )
                      ]),
                      _: 1
                      /* STABLE */
                    })
                  ]),
                  _: 1
                  /* STABLE */
                })
              ], true),
              createVNode(_component_gm_form_item, null, {
                default: withCtx(() => [
                  createVNode(_component_gm_button, {
                    type: "primary",
                    onClick: saveModel
                  }, {
                    default: withCtx(() => [
                      createTextVNode(" \u4FDD\u5B58\u67E5\u8BE2\u6761\u4EF6 ")
                    ]),
                    _: 1
                    /* STABLE */
                  }),
                  searchHistoryList.value.length ? (openBlock(), createBlock(_component_gm_button, {
                    key: 0,
                    onClick: viewModels
                  }, {
                    default: withCtx(() => [
                      createTextVNode(" \u6211\u7684\u67E5\u8BE2\u6761\u4EF6 ")
                    ]),
                    _: 1
                    /* STABLE */
                  })) : (openBlock(), createBlock(_component_gm_tooltip, {
                    key: 1,
                    class: "box-item",
                    effect: "dark",
                    content: "\u8FD8\u6CA1\u6709\u4FDD\u5B58\u67E5\u8BE2\u6761\u4EF6",
                    placement: "bottom"
                  }, {
                    default: withCtx(() => [
                      createVNode(_component_el_button, { disabled: "" }, {
                        default: withCtx(() => [
                          createTextVNode("\u6211\u7684\u67E5\u8BE2\u6761\u4EF6")
                        ]),
                        _: 1
                        /* STABLE */
                      })
                    ]),
                    _: 1
                    /* STABLE */
                  }))
                ]),
                _: 1
                /* STABLE */
              })
            ]),
            _: 3
            /* FORWARDED */
          }, 16, ["id", "class"]),
          createVNode(_component_gm_dialog, {
            modelValue: showModels.value,
            "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => showModels.value = $event),
            title: "\u6211\u7684\u67E5\u8BE2\u6761\u4EF6",
            width: "30%"
          }, {
            default: withCtx(() => [
              createVNode(_component_gm_collapse, { accordion: "" }, {
                default: withCtx(() => [
                  (openBlock(true), createElementBlock(
                    Fragment,
                    null,
                    renderList(searchHistoryList.value, (item) => {
                      return openBlock(), createBlock(_component_gm_collapse_item, {
                        key: item.label,
                        title: item.label,
                        name: item.label
                      }, {
                        title: withCtx(() => [
                          createElementVNode("div", _hoisted_1, [
                            createElementVNode(
                              "span",
                              null,
                              toDisplayString(item.label),
                              1
                              /* TEXT */
                            ),
                            createTextVNode(),
                            createVNode(_component_gm_button, {
                              type: "primary",
                              size: "small",
                              onClick: ($event) => useModel(item.value)
                            }, {
                              default: withCtx(() => [
                                createTextVNode(" \u4F7F\u7528 ")
                              ]),
                              _: 2
                              /* DYNAMIC */
                            }, 1032, ["onClick"])
                          ])
                        ]),
                        default: withCtx(() => [
                          createElementVNode("div", _hoisted_2, [
                            (openBlock(true), createElementBlock(
                              Fragment,
                              null,
                              renderList(getValues(item.value), (value, i) => {
                                return openBlock(), createBlock(
                                  _component_el_tag,
                                  {
                                    key: i,
                                    type: "info",
                                    effect: "dark"
                                  },
                                  {
                                    default: withCtx(() => [
                                      createTextVNode(
                                        toDisplayString(value),
                                        1
                                        /* TEXT */
                                      )
                                    ]),
                                    _: 2
                                    /* DYNAMIC */
                                  },
                                  1024
                                  /* DYNAMIC_SLOTS */
                                );
                              }),
                              128
                              /* KEYED_FRAGMENT */
                            ))
                          ])
                        ]),
                        _: 2
                        /* DYNAMIC */
                      }, 1032, ["title", "name"]);
                    }),
                    128
                    /* KEYED_FRAGMENT */
                  ))
                ]),
                _: 1
                /* STABLE */
              })
            ]),
            _: 1
            /* STABLE */
          }, 8, ["modelValue"])
        ],
        64
        /* STABLE_FRAGMENT */
      );
    };
  }
});

export { _sfc_main as default };
//# sourceMappingURL=MemoryForm.vue2.mjs.map
