'use strict';

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

var vue = require('vue');
var uuid = require('uuid');
var index = require('../../../../../hooks/store/useCustomSettings/index.js');
var index$1 = require('../../../plugins/message/index.js');
var objects = require('../../../../../utils/src/objects.js');
var general = require('../../../../../utils/src/general.js');
var is = require('../../../../../utils/src/is.js');
var table = require('../../../../../constants/table.js');
var iconsVue = require('@element-plus/icons-vue');
var tableToolbar = require('./tableToolbar.js');

const _hoisted_1 = { class: "gm-inline-block gm-min-w-[20px] gm-text-center" };
const _hoisted_2 = { class: "gm-ml-auto" };
const _hoisted_3 = { class: "gm-flex" };
const _hoisted_4 = {
  key: 0,
  class: "gm-my-5 gm-text-center gm-text-base gm-font-bold gm-text-gray-700"
};
const _hoisted_5 = { class: "gm-m-2" };
const customSettingType = "quickQueryList";
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{
    name: "GmTableToolbar"
  },
  __name: "TableToolbar",
  props: /* @__PURE__ */ vue.mergeModels(tableToolbar.tableToolbarProps, {
    "showSearch": { type: Boolean, ...{ required: true } },
    "showSearchModifiers": {},
    "queryParams": {},
    "queryParamsModifiers": {}
  }),
  emits: /* @__PURE__ */ vue.mergeModels(tableToolbar.tableToolbarEmits, ["update:showSearch", "update:queryParams"]),
  setup(__props, { emit: __emit }) {
    const tableCtx = vue.inject(table.TableCtxKey, {});
    const props = __props;
    const emit = __emit;
    const showSearch = vue.useModel(__props, "showSearch");
    const queryParams = vue.useModel(__props, "queryParams");
    const customSettingsStore = index.useCustomSettingsStore();
    const customSettingsValue = vue.computed(() => customSettingsStore.customSettings[props.tableId]?.[customSettingType] || []);
    const saveSearchVisible = vue.ref(false);
    const searchform = vue.ref({
      name: ""
    });
    const rules = {
      name: [{ required: true, message: "\u67E5\u8BE2\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A", trigger: "blur" }]
    };
    const openSaveSearchDialog = () => {
      saveSearchVisible.value = true;
      searchform.value.name = "";
    };
    const applySettings = (item) => {
      tableCtx.queryRef?.value?.resetFields?.();
      for (const key in item.value) {
        if (objects.hasOwn(item.value, key) && objects.hasOwn(queryParams.value, key)) {
          queryParams.value[key] = item.value[key];
        }
      }
      emit("getList");
      tableCtx?.tableRef?.value?.scrollTo({ top: 0, behavior: "smooth" });
      tableCtx.tableV2Ref?.value?.scrollTo({ scrollLeft: 0, scrollTop: 0 });
    };
    const submitLoading = vue.ref(false);
    const addSettings = () => {
      const params = JSON.stringify([{ uuid: uuid.v4(), name: searchform.value.name, value: queryParams.value }, ...customSettingsValue.value]);
      return submitSettings(params);
    };
    const deleteSettins = (item) => {
      general.remove(customSettingsValue.value, item);
      return submitSettings(JSON.stringify(customSettingsValue.value));
    };
    const syncLoading = vue.ref(false);
    const onSyncRemote = async () => {
      try {
        syncLoading.value = true;
        await customSettingsStore.syncRemoteSetting({
          key_word: props.tableId,
          type: customSettingType
        });
        syncLoading.value = false;
        index$1.GmMessage.success("\u540C\u6B65\u6210\u529F");
      } catch (e) {
        console.error(e);
        syncLoading.value = false;
      }
    };
    const submitSettings = async (value) => {
      try {
        submitLoading.value = true;
        const { data } = await customSettingsStore.addCustomSettings({
          key_word: props.tableId,
          value,
          type: customSettingType
        });
        submitLoading.value = false;
        if (data.code !== 200) {
          return;
        }
        index$1.GmMessage.success("\u64CD\u4F5C\u6210\u529F");
        saveSearchVisible.value = false;
      } catch (e) {
        console.error(e);
        submitLoading.value = false;
      }
    };
    function toggleSearch() {
      showSearch.value = !showSearch.value;
    }
    function refresh() {
      emit("getList");
    }
    vue.onMounted(async () => {
      if (props.immediate) {
        await customSettingsStore.getCustomSettings({ key_word: props.tableId, type: customSettingType }, { defaultValue: [] });
      }
    });
    return (_ctx, _cache) => {
      const _component_gm_col = vue.resolveComponent("gm-col");
      const _component_gm_button = vue.resolveComponent("gm-button");
      const _component_gm_icon = vue.resolveComponent("gm-icon");
      const _component_gm_dropdown_item = vue.resolveComponent("gm-dropdown-item");
      const _component_gm_dropdown_menu = vue.resolveComponent("gm-dropdown-menu");
      const _component_gm_dropdown = vue.resolveComponent("gm-dropdown");
      const _component_gm_tooltip = vue.resolveComponent("gm-tooltip");
      const _component_gm_form_item_module = vue.resolveComponent("gm-form-item-module");
      const _component_GmDialogForm = vue.resolveComponent("GmDialogForm");
      const _component_gm_row = vue.resolveComponent("gm-row");
      return vue.openBlock(), vue.createElementBlock("div", null, [
        vue.createVNode(_component_gm_row, {
          gutter: 10,
          style: { "margin-right": "0" },
          class: "gm-gap-y-2"
        }, {
          default: vue.withCtx(() => [
            vue.unref(is.isNumber)(_ctx.total) ? (vue.openBlock(), vue.createBlock(_component_gm_col, {
              key: 0,
              span: 1.5,
              class: "gm-text-[14px] gm-leading-[32px]"
            }, {
              default: vue.withCtx(() => [
                vue.createElementVNode(
                  "span",
                  null,
                  "\u5171 " + vue.toDisplayString(_ctx.total) + " \u6761",
                  1
                  /* TEXT */
                )
              ]),
              _: 1
              /* STABLE */
            })) : vue.createCommentVNode("v-if", true),
            vue.unref(is.isNumber)(_ctx.selectedCount) ? (vue.openBlock(), vue.createBlock(_component_gm_col, {
              key: 1,
              span: 2.5,
              class: "gm-mr-2 gm-text-[14px] gm-leading-[32px]"
            }, {
              default: vue.withCtx(() => [
                vue.createElementVNode("span", null, [
                  vue.createTextVNode("\u5DF2\u9009\u62E9"),
                  vue.createElementVNode(
                    "span",
                    _hoisted_1,
                    vue.toDisplayString(_ctx.selectedCount),
                    1
                    /* TEXT */
                  ),
                  vue.createTextVNode("\u9879")
                ])
              ]),
              _: 1
              /* STABLE */
            })) : vue.createCommentVNode("v-if", true),
            vue.renderSlot(_ctx.$slots, "default"),
            vue.createCommentVNode(` <right-toolbar v-model:showSearch="showSearch" @queryTable="emits('getList')" /> `),
            vue.createElementVNode("div", _hoisted_2, [
              vue.createElementVNode("div", _hoisted_3, [
                vue.createVNode(_component_gm_button, {
                  plain: "",
                  class: "gm-mr-2 gm-block",
                  onClick: _cache[0] || (_cache[0] = vue.withModifiers(($event) => openSaveSearchDialog(), ["stop"]))
                }, {
                  default: vue.withCtx(() => [
                    vue.createTextVNode("\u4FDD\u5B58\u67E5\u8BE2")
                  ]),
                  _: 1
                  /* STABLE */
                }),
                vue.createVNode(_component_gm_dropdown, {
                  trigger: "click",
                  class: "gm-mr-3 hover:gm-outline-none"
                }, {
                  dropdown: vue.withCtx(() => [
                    vue.createVNode(_component_gm_dropdown_menu, { class: "gm-min-w-[200px]" }, {
                      default: vue.withCtx(() => [
                        customSettingsValue.value.length === 0 ? (vue.openBlock(), vue.createElementBlock("div", _hoisted_4, "\u6682\u65E0\u6570\u636E")) : vue.createCommentVNode("v-if", true),
                        (vue.openBlock(true), vue.createElementBlock(
                          vue.Fragment,
                          null,
                          vue.renderList(customSettingsValue.value, (item) => {
                            return vue.openBlock(), vue.createBlock(_component_gm_dropdown_item, {
                              key: item.uuid,
                              class: "gm-group gm-flex gm-justify-between",
                              onClick: ($event) => applySettings(item)
                            }, {
                              default: vue.withCtx(() => [
                                vue.createElementVNode(
                                  "span",
                                  null,
                                  vue.toDisplayString(item.name),
                                  1
                                  /* TEXT */
                                ),
                                vue.createVNode(_component_gm_icon, {
                                  class: "gm-hidden hover:gm-text-red-500 group-hover:gm-block",
                                  onClick: vue.withModifiers(($event) => deleteSettins(item), ["stop"])
                                }, {
                                  default: vue.withCtx(() => [
                                    vue.createVNode(vue.unref(iconsVue.Delete))
                                  ]),
                                  _: 2
                                  /* DYNAMIC */
                                }, 1032, ["onClick"])
                              ]),
                              _: 2
                              /* DYNAMIC */
                            }, 1032, ["onClick"]);
                          }),
                          128
                          /* KEYED_FRAGMENT */
                        )),
                        vue.createElementVNode("div", _hoisted_5, [
                          vue.createCommentVNode(' <gm-button :loading="submitLoading" size="small" type="" @click="onClear">\u6E05\u7A7A</gm-button> '),
                          vue.createVNode(_component_gm_button, {
                            loading: syncLoading.value,
                            size: "small",
                            type: "success",
                            onClick: onSyncRemote
                          }, {
                            default: vue.withCtx(() => [
                              vue.createTextVNode("\u540C\u6B65")
                            ]),
                            _: 1
                            /* STABLE */
                          }, 8, ["loading"])
                        ])
                      ]),
                      _: 1
                      /* STABLE */
                    })
                  ]),
                  default: vue.withCtx(() => [
                    vue.createVNode(_component_gm_button, {
                      plain: "",
                      type: ""
                    }, {
                      default: vue.withCtx(() => [
                        vue.createTextVNode(" \u5FEB\u6377\u67E5\u8BE2"),
                        vue.createVNode(_component_gm_icon, { class: "el-icon--right" }, {
                          default: vue.withCtx(() => [
                            vue.createVNode(vue.unref(iconsVue.ArrowDown))
                          ]),
                          _: 1
                          /* STABLE */
                        })
                      ]),
                      _: 1
                      /* STABLE */
                    })
                  ]),
                  _: 1
                  /* STABLE */
                }),
                vue.createVNode(_component_gm_tooltip, {
                  class: "item",
                  effect: "dark",
                  content: showSearch.value ? "\u9690\u85CF\u641C\u7D22" : "\u663E\u793A\u641C\u7D22",
                  placement: "top"
                }, {
                  default: vue.withCtx(() => [
                    vue.createVNode(_component_gm_button, {
                      circle: "",
                      plain: "",
                      icon: vue.unref(iconsVue.Search),
                      onClick: _cache[1] || (_cache[1] = ($event) => toggleSearch())
                    }, null, 8, ["icon"])
                  ]),
                  _: 1
                  /* STABLE */
                }, 8, ["content"]),
                vue.createVNode(_component_gm_tooltip, {
                  class: "item",
                  effect: "dark",
                  content: "\u5237\u65B0",
                  placement: "top"
                }, {
                  default: vue.withCtx(() => [
                    vue.createVNode(_component_gm_button, {
                      circle: "",
                      plain: "",
                      icon: vue.unref(iconsVue.Refresh),
                      onClick: _cache[2] || (_cache[2] = ($event) => refresh())
                    }, null, 8, ["icon"])
                  ]),
                  _: 1
                  /* STABLE */
                })
              ]),
              vue.createVNode(_component_GmDialogForm, {
                modelValue: saveSearchVisible.value,
                "onUpdate:modelValue": _cache[4] || (_cache[4] = ($event) => saveSearchVisible.value = $event),
                formData: searchform.value,
                loading: submitLoading.value,
                rules,
                title: "\u4FDD\u5B58\u5F53\u524D\u67E5\u8BE2",
                labelWidth: "auto",
                onSubmit: addSettings
              }, {
                default: vue.withCtx(() => [
                  vue.createVNode(_component_gm_form_item_module, {
                    modelValue: searchform.value.name,
                    "onUpdate:modelValue": _cache[3] || (_cache[3] = ($event) => searchform.value.name = $event),
                    field: "name",
                    label: "\u67E5\u8BE2\u540D\u79F0",
                    type: "GmInput",
                    props: { placeholder: "\u8BF7\u8F93\u5165", maxlength: "15", showWordLimit: true }
                  }, null, 8, ["modelValue"])
                ]),
                _: 1
                /* STABLE */
              }, 8, ["modelValue", "formData", "loading"])
            ])
          ]),
          _: 3
          /* FORWARDED */
        })
      ]);
    };
  }
});

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