'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var core = require('@vueuse/core');
require('../../../../utils/index.js');
require('../../base/form/index.js');
require('../../../../hooks/index.js');
require('../../../index.js');
var memoryForm = require('./memoryForm.js');
var event = require('../../../../utils/src/elementPlus/event.js');
var form = require('../../base/form/form.js');
var exports$1 = require('../../../../utils/src/elementPlus/exports.js');
var index = require('../../../../hooks/store/useCustomSettings/index.js');
var index$1 = require('../../plugins/messageBox/index.js');
var index$2 = require('../../plugins/message/index.js');

const _withScopeId = (n) => (vue.pushScopeId("data-v-c17deec9"), n = n(), vue.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__ */ vue.defineComponent({
  ...{
    name: "GmMemoryForm"
  },
  __name: "MemoryForm",
  props: memoryForm.memoryFormProps,
  emits: memoryForm.memoryFormEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const elEvents = event.getGmEvent(form.formEmits, emit);
    const elRef = vue.ref();
    const exposeNames = ["validate", "validateField", "resetFields", "clearValidate", "scrollToField"];
    const { exposeFns } = exports$1.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 = index.useCustomSettingsStore();
    const searchHistoryList = vue.ref([]);
    const getSearchHistoryList = async () => {
      const data = await customSettingsStore.getCustomSettings(
        { key_word: props.pageName + memoryForm.MemoryHistoryExt, type: memoryForm.MemoryHistoryType },
        { defaultValue: [] }
      );
      if (!data) {
        return;
      }
      const { cloned: result } = core.useCloned(data || []);
      searchHistoryList.value = result.value;
    };
    const saveModel = () => {
      elRef.value?.validate((valid) => {
        if (valid) {
          index$1.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 + memoryForm.MemoryHistoryExt,
              value: JSON.stringify(searchHistoryList.value),
              type: memoryForm.MemoryHistoryType
            });
            index$2.GmMessage({
              message: "\u67E5\u8BE2\u6761\u4EF6\u4FDD\u5B58\u6210\u529F",
              type: "success"
            });
            getSearchHistoryList();
          }).catch(() => {
          });
        }
      });
    };
    const showModels = vue.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);
    };
    vue.onMounted(() => {
      getSearchHistoryList();
    });
    __expose({
      ...exposeFns
    });
    return (_ctx, _cache) => {
      const _component_gm_button = vue.resolveComponent("gm-button");
      const _component_gm_form_item = vue.resolveComponent("gm-form-item");
      const _component_el_button = vue.resolveComponent("el-button");
      const _component_gm_tooltip = vue.resolveComponent("gm-tooltip");
      const _component_gm_form = vue.resolveComponent("gm-form");
      const _component_el_tag = vue.resolveComponent("el-tag");
      const _component_gm_collapse_item = vue.resolveComponent("gm-collapse-item");
      const _component_gm_collapse = vue.resolveComponent("gm-collapse");
      const _component_gm_dialog = vue.resolveComponent("gm-dialog");
      return vue.openBlock(), vue.createElementBlock(
        vue.Fragment,
        null,
        [
          vue.createVNode(_component_gm_form, vue.mergeProps({
            id: _ctx.$attrs.id,
            ref_key: "elRef",
            ref: elRef,
            class: _ctx.$attrs.class
          }, props, vue.toHandlers(vue.unref(elEvents))), {
            default: vue.withCtx((slotValue) => [
              vue.renderSlot(_ctx.$slots, "default", vue.normalizeProps(vue.guardReactiveProps(slotValue)), void 0, true),
              vue.renderSlot(_ctx.$slots, "buttons", {}, () => [
                vue.createVNode(_component_gm_form_item, null, {
                  default: vue.withCtx(() => [
                    vue.createVNode(_component_gm_button, {
                      type: "primary",
                      onClick: submit
                    }, {
                      default: vue.withCtx(() => [
                        vue.createTextVNode(
                          vue.toDisplayString(props.submitText || "\u63D0\u4EA4"),
                          1
                          /* TEXT */
                        )
                      ]),
                      _: 1
                      /* STABLE */
                    }),
                    vue.createVNode(_component_gm_button, { onClick: reset }, {
                      default: vue.withCtx(() => [
                        vue.createTextVNode(
                          vue.toDisplayString(props.resetText || "\u91CD\u7F6E"),
                          1
                          /* TEXT */
                        )
                      ]),
                      _: 1
                      /* STABLE */
                    })
                  ]),
                  _: 1
                  /* STABLE */
                })
              ], true),
              vue.createVNode(_component_gm_form_item, null, {
                default: vue.withCtx(() => [
                  vue.createVNode(_component_gm_button, {
                    type: "primary",
                    onClick: saveModel
                  }, {
                    default: vue.withCtx(() => [
                      vue.createTextVNode(" \u4FDD\u5B58\u67E5\u8BE2\u6761\u4EF6 ")
                    ]),
                    _: 1
                    /* STABLE */
                  }),
                  searchHistoryList.value.length ? (vue.openBlock(), vue.createBlock(_component_gm_button, {
                    key: 0,
                    onClick: viewModels
                  }, {
                    default: vue.withCtx(() => [
                      vue.createTextVNode(" \u6211\u7684\u67E5\u8BE2\u6761\u4EF6 ")
                    ]),
                    _: 1
                    /* STABLE */
                  })) : (vue.openBlock(), vue.createBlock(_component_gm_tooltip, {
                    key: 1,
                    class: "box-item",
                    effect: "dark",
                    content: "\u8FD8\u6CA1\u6709\u4FDD\u5B58\u67E5\u8BE2\u6761\u4EF6",
                    placement: "bottom"
                  }, {
                    default: vue.withCtx(() => [
                      vue.createVNode(_component_el_button, { disabled: "" }, {
                        default: vue.withCtx(() => [
                          vue.createTextVNode("\u6211\u7684\u67E5\u8BE2\u6761\u4EF6")
                        ]),
                        _: 1
                        /* STABLE */
                      })
                    ]),
                    _: 1
                    /* STABLE */
                  }))
                ]),
                _: 1
                /* STABLE */
              })
            ]),
            _: 3
            /* FORWARDED */
          }, 16, ["id", "class"]),
          vue.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: vue.withCtx(() => [
              vue.createVNode(_component_gm_collapse, { accordion: "" }, {
                default: vue.withCtx(() => [
                  (vue.openBlock(true), vue.createElementBlock(
                    vue.Fragment,
                    null,
                    vue.renderList(searchHistoryList.value, (item) => {
                      return vue.openBlock(), vue.createBlock(_component_gm_collapse_item, {
                        key: item.label,
                        title: item.label,
                        name: item.label
                      }, {
                        title: vue.withCtx(() => [
                          vue.createElementVNode("div", _hoisted_1, [
                            vue.createElementVNode(
                              "span",
                              null,
                              vue.toDisplayString(item.label),
                              1
                              /* TEXT */
                            ),
                            vue.createTextVNode(),
                            vue.createVNode(_component_gm_button, {
                              type: "primary",
                              size: "small",
                              onClick: ($event) => useModel(item.value)
                            }, {
                              default: vue.withCtx(() => [
                                vue.createTextVNode(" \u4F7F\u7528 ")
                              ]),
                              _: 2
                              /* DYNAMIC */
                            }, 1032, ["onClick"])
                          ])
                        ]),
                        default: vue.withCtx(() => [
                          vue.createElementVNode("div", _hoisted_2, [
                            (vue.openBlock(true), vue.createElementBlock(
                              vue.Fragment,
                              null,
                              vue.renderList(getValues(item.value), (value, i) => {
                                return vue.openBlock(), vue.createBlock(
                                  _component_el_tag,
                                  {
                                    key: i,
                                    type: "info",
                                    effect: "dark"
                                  },
                                  {
                                    default: vue.withCtx(() => [
                                      vue.createTextVNode(
                                        vue.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 */
      );
    };
  }
});

exports.default = _sfc_main;
//# sourceMappingURL=MemoryForm.vue2.js.map
