'use strict';

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

var vue = require('vue');
var core = require('@vueuse/core');
require('../../../../../hooks/index.js');
var index$1 = require('../../../plugins/message/index.js');
var lodashEs = require('lodash-es');
var iconsVue = require('@element-plus/icons-vue');
var tableCustomHeader = require('./tableCustomHeader.js');
var index = require('../../../../../hooks/store/useCustomSettings/index.js');

const _hoisted_1 = { class: "gm-mb-4 gm-flex gm-items-center gm-rounded gm-border gm-border-cyan-300 gm-bg-cyan-50 gm-p-2 gm-text-sm gm-text-[#777]" };
const _hoisted_2 = { class: "gm-ml-2" };
const _hoisted_3 = { class: "gm-text-cyan-500" };
const _hoisted_4 = { class: "custom-content gm-text-sm" };
const _hoisted_5 = { class: "gm-flex gm-w-14 gm-justify-center" };
const _hoisted_6 = /* @__PURE__ */ vue.createElementVNode(
  "li",
  { class: "gm-flex-1 gm-border-l gm-border-white" },
  "\u5217\u540D",
  -1
  /* HOISTED */
);
const _hoisted_7 = /* @__PURE__ */ vue.createElementVNode(
  "li",
  { class: "gm-w-36 gm-border-l gm-border-white" },
  "\u5217\u5BBD",
  -1
  /* HOISTED */
);
const _hoisted_8 = /* @__PURE__ */ vue.createElementVNode(
  "li",
  { class: "gm-w-24 gm-border-l gm-border-white" },
  "\u56FA\u5B9A",
  -1
  /* HOISTED */
);
const _hoisted_9 = {
  key: 0,
  class: "gm-w-28 gm-border-l gm-border-white"
};
const _hoisted_10 = {
  key: 0,
  class: "gm-flex gm-items-center gm-border gm-border-b-0 gm-border-gray-200"
};
const _hoisted_11 = { class: "gm-flex gm-w-14 gm-justify-center" };
const _hoisted_12 = /* @__PURE__ */ vue.createElementVNode(
  "li",
  { class: "gm-flex-1 gm-border-l gm-border-gray-200" },
  "\u64CD\u4F5C",
  -1
  /* HOISTED */
);
const _hoisted_13 = { class: "gm-w-36 gm-border-l gm-border-gray-200 gm-px-2" };
const _hoisted_14 = /* @__PURE__ */ vue.createElementVNode(
  "li",
  { class: "gm-flex gm-w-24 gm-items-center gm-justify-center gm-gap-4 gm-border-l gm-border-gray-200" },
  null,
  -1
  /* HOISTED */
);
const _hoisted_15 = {
  key: 0,
  class: "gm-w-28 gm-cursor-grabbing gm-border-l gm-border-gray-200"
};
const _hoisted_16 = ["draggable", "onDragstart", "onDragenter"];
const _hoisted_17 = { class: "gm-flex gm-w-14 gm-justify-center" };
const _hoisted_18 = { class: "gm-flex-1 gm-border-l gm-border-gray-200" };
const _hoisted_19 = { class: "gm-w-36 gm-border-l gm-border-gray-200 gm-px-2" };
const _hoisted_20 = { class: "gm-flex gm-w-24 gm-items-center gm-justify-center gm-gap-4 gm-border-l gm-border-gray-200" };
const _hoisted_21 = ["onClick"];
const _hoisted_22 = {
  t: "1748946230868",
  class: "icon",
  viewBox: "0 0 1024 1024",
  version: "1.1",
  xmlns: "http://www.w3.org/2000/svg",
  "p-id": "15386",
  width: "18",
  height: "18"
};
const _hoisted_23 = ["fill"];
const _hoisted_24 = ["onClick"];
const _hoisted_25 = {
  t: "1748946230868",
  class: "icon",
  viewBox: "0 0 1024 1024",
  version: "1.1",
  xmlns: "http://www.w3.org/2000/svg",
  "p-id": "15386",
  width: "18",
  height: "18"
};
const _hoisted_26 = ["fill"];
const _hoisted_27 = {
  key: 0,
  class: "gm-w-28 gm-cursor-grabbing gm-border-l gm-border-gray-200"
};
const _hoisted_28 = { class: "gm-flex gm-justify-between" };
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{
    name: "GmTableCustomHeader"
  },
  __name: "TableCustomHeader",
  props: /* @__PURE__ */ vue.mergeModels(tableCustomHeader.tableCustomHeaderProps, {
    "isShowDrawer": { type: Boolean, ...{
      required: true
    } },
    "isShowDrawerModifiers": {},
    "colPropsList": {
      required: true
    },
    "colPropsListModifiers": {}
  }),
  emits: /* @__PURE__ */ vue.mergeModels(["updateColumns"], ["update:isShowDrawer", "update:colPropsList"]),
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const isShowDrawer = vue.useModel(__props, "isShowDrawer");
    const colPropsList = vue.useModel(__props, "colPropsList");
    const customSettingsStore = index.useCustomSettingsStore();
    const isInited = vue.ref(false);
    const { cloned: clonedColPropsList, sync: syncPropsList } = core.useCloned(colPropsList);
    vue.watch(isShowDrawer, (newValue) => {
      if (newValue) {
        const cloned = lodashEs.cloneDeep(colPropsList.value);
        const editIndex = cloned.findIndex((it) => it.type === "edit");
        const editItem = cloned[editIndex];
        if (editIndex > -1) {
          cloned.splice(editIndex, 1);
        }
        clonedColPropsList.value = [...cloned, editItem];
      }
    });
    const customSettingsValue = vue.computed(() => customSettingsStore.customSettings[props.keyWord]?.[props.type] || []);
    const { cloned: clonedStore } = core.useCloned(customSettingsValue);
    const titleStyle = vue.computed(() => {
      const h = props.colDefaultPropsList.length * 44;
      const H = document.documentElement.clientHeight || document.body.clientHeight;
      return h > H - 240 ? { paddingRight: "4px" } : { paddingRight: 0 };
    });
    const updateColumns = () => {
      colPropsList.value = clonedColPropsList.value;
    };
    const initColumns = async () => {
      try {
        await customSettingsStore.getCustomSettings({ key_word: props.keyWord, type: props.type }, { defaultValue: [] });
      } catch {
      }
      const colnedDefaultColumns = lodashEs.cloneDeep(props.colDefaultPropsList);
      let columns = colnedDefaultColumns;
      if (clonedStore.value.length) {
        columns = [];
        clonedStore.value.forEach((it) => {
          const index = colnedDefaultColumns.findIndex((v) => v.prop === it.prop);
          if (index > -1) {
            const item = colnedDefaultColumns.splice(index, 1)[0];
            columns.push({
              ...it,
              ...item,
              isShow: it.isShow,
              width: it.width,
              fixed: it.fixed
            });
          }
        });
        clonedColPropsList.value = [...columns, ...colnedDefaultColumns];
      }
      updateColumns();
      isInited.value = true;
    };
    const checkedNumber = vue.computed(() => {
      return clonedColPropsList.value.filter((v) => v.isShow).length;
    });
    const checkedAll = vue.computed({
      get() {
        return clonedColPropsList.value.filter((it) => it.type !== "edit").every((v) => v.isShow);
      },
      set(value) {
        clonedColPropsList.value.forEach((v) => {
          if (v.type !== "edit") {
            v.isShow = Boolean(value);
          }
        });
      }
    });
    const isIndeterminate = vue.computed(() => !checkedAll.value && clonedColPropsList.value.filter((it) => it.type !== "edit").some((v) => v.isShow));
    const start = vue.ref();
    const end = vue.ref();
    const dragstart = (item) => {
      if (!props.dragHeader) return;
      document.body.style.userSelect = "none";
      start.value = item;
    };
    const dragenter = (item, e) => {
      if (!props.dragHeader) return;
      end.value = item;
      e.preventDefault();
    };
    const dragend = () => {
      if (!props.dragHeader) return;
      document.body.style.userSelect = "text";
      if (start.value === end.value) return;
      if (start.value && end.value) {
        const oldIndex = clonedColPropsList.value.findIndex((v) => v.prop === start.value?.prop);
        const newIndex = clonedColPropsList.value.findIndex((v) => v.prop === end.value?.prop);
        const newItems = [...clonedColPropsList.value];
        newItems.splice(oldIndex, 1);
        newItems.splice(newIndex, 0, start.value);
        clonedColPropsList.value = [...newItems];
      }
    };
    const dragover = (e) => {
      if (!props.dragHeader) return;
      e.preventDefault();
    };
    const submitLoading = vue.ref(false);
    const submitSettings = async (options) => {
      try {
        submitLoading.value = true;
        const { data } = await customSettingsStore.addCustomSettings({
          key_word: props.keyWord,
          value: JSON.stringify(clonedColPropsList.value),
          type: props.type
        });
        submitLoading.value = false;
        if (data.code !== 200) {
          return;
        }
        updateColumns();
        if (!options?.hiddenMessage) {
          index$1.GmMessage.success("\u4FDD\u5B58\u6210\u529F");
        }
        emit("updateColumns");
      } catch (e) {
        console.error(e);
        submitLoading.value = false;
      }
    };
    const onSyncRemote = async () => {
      try {
        submitLoading.value = true;
        await customSettingsStore.syncRemoteSetting({
          key_word: props.keyWord,
          type: props.type
        });
        submitLoading.value = false;
        initColumns();
        index$1.GmMessage.success("\u540C\u6B65\u6210\u529F");
      } catch (e) {
        console.error(e);
        submitLoading.value = false;
      }
    };
    const onReset = () => {
      clonedColPropsList.value = lodashEs.cloneDeep(props.colDefaultPropsList);
    };
    const handleToogleFixed = (row, fixed) => {
      if (row.fixed === fixed) {
        row.fixed = false;
        return;
      }
      row.fixed = fixed;
    };
    vue.onMounted(() => {
      if (props.disabledRemoteCol) {
        isInited.value = true;
      } else {
        initColumns();
      }
    });
    __expose({
      isInited,
      submitSettings,
      syncPropsList
    });
    return (_ctx, _cache) => {
      const _component_gm_icon = vue.resolveComponent("gm-icon");
      const _component_gm_checkbox = vue.resolveComponent("gm-checkbox");
      const _component_gm_input = vue.resolveComponent("gm-input");
      const _component_gm_tooltip = vue.resolveComponent("gm-tooltip");
      const _component_el_icon = vue.resolveComponent("el-icon");
      const _component_gm_button = vue.resolveComponent("gm-button");
      const _component_gm_drawer = vue.resolveComponent("gm-drawer");
      return vue.openBlock(), vue.createBlock(_component_gm_drawer, {
        modelValue: isShowDrawer.value,
        "onUpdate:modelValue": _cache[4] || (_cache[4] = ($event) => isShowDrawer.value = $event),
        title: "\u81EA\u5B9A\u4E49\u8868\u5934",
        direction: "rtl",
        size: "600px",
        "append-to-body": "",
        "destroy-on-close": "",
        class: "table-custom-header-drawer"
      }, vue.createSlots({
        default: vue.withCtx(() => [
          vue.createElementVNode("div", _hoisted_1, [
            vue.createVNode(_component_gm_icon, null, {
              default: vue.withCtx(() => [
                vue.createVNode(vue.unref(iconsVue.InfoFilled))
              ]),
              _: 1
              /* STABLE */
            }),
            vue.createElementVNode("p", _hoisted_2, [
              vue.createTextVNode(" \u5DF2\u9009\u4E2D "),
              vue.createElementVNode(
                "span",
                _hoisted_3,
                vue.toDisplayString(checkedNumber.value),
                1
                /* TEXT */
              ),
              vue.createTextVNode(" \u9879 ")
            ])
          ]),
          vue.createElementVNode("div", _hoisted_4, [
            vue.createElementVNode(
              "ul",
              {
                class: "gm-flex gm-items-center gm-border gm-border-y-0 gm-border-gray-200 gm-bg-gray-200 gm-font-semibold gm-text-[#333]",
                style: vue.normalizeStyle(titleStyle.value)
              },
              [
                vue.createElementVNode("li", _hoisted_5, [
                  vue.createVNode(_component_gm_checkbox, {
                    modelValue: checkedAll.value,
                    "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => checkedAll.value = $event),
                    indeterminate: isIndeterminate.value
                  }, null, 8, ["modelValue", "indeterminate"])
                ]),
                _hoisted_6,
                _hoisted_7,
                _hoisted_8,
                _ctx.dragHeader ? (vue.openBlock(), vue.createElementBlock("li", _hoisted_9, "\u62D6\u5730\u8C03\u6574\u987A\u5E8F")) : vue.createCommentVNode("v-if", true)
              ],
              4
              /* STYLE */
            ),
            vue.createVNode(vue.TransitionGroup, {
              class: "custom-group gm-border-b gm-border-gray-200 gm-text-gray-600",
              name: "sort",
              tag: "div"
            }, {
              default: vue.withCtx(() => [
                (vue.openBlock(true), vue.createElementBlock(
                  vue.Fragment,
                  null,
                  vue.renderList(vue.unref(clonedColPropsList), (item) => {
                    return vue.openBlock(), vue.createElementBlock(
                      vue.Fragment,
                      {
                        key: item.prop
                      },
                      [
                        item.type === "edit" ? (vue.openBlock(), vue.createElementBlock("ul", _hoisted_10, [
                          vue.createElementVNode("li", _hoisted_11, [
                            vue.createVNode(_component_gm_checkbox, {
                              modelValue: item.isShow,
                              "onUpdate:modelValue": ($event) => item.isShow = $event,
                              disabled: ""
                            }, null, 8, ["modelValue", "onUpdate:modelValue"])
                          ]),
                          _hoisted_12,
                          vue.createElementVNode("li", _hoisted_13, [
                            vue.createVNode(_component_gm_input, {
                              modelValue: item.width,
                              "onUpdate:modelValue": ($event) => item.width = $event,
                              class: "gm-w-full",
                              placeholder: "\u8BBE\u7F6E\u5BBD\u5EA6",
                              clearable: ""
                            }, null, 8, ["modelValue", "onUpdate:modelValue"])
                          ]),
                          _hoisted_14,
                          _ctx.dragHeader ? (vue.openBlock(), vue.createElementBlock("li", _hoisted_15)) : vue.createCommentVNode("v-if", true)
                        ])) : (vue.openBlock(), vue.createElementBlock("ul", {
                          key: 1,
                          class: "gm-flex gm-items-center gm-border gm-border-b-0 gm-border-gray-200",
                          draggable: _ctx.dragHeader,
                          onDragstart: ($event) => dragstart(item),
                          onDragenter: ($event) => dragenter(item, $event),
                          onDragend: dragend,
                          onDragover: _cache[1] || (_cache[1] = ($event) => dragover($event))
                        }, [
                          vue.createElementVNode("li", _hoisted_17, [
                            vue.createVNode(_component_gm_checkbox, {
                              modelValue: item.isShow,
                              "onUpdate:modelValue": ($event) => item.isShow = $event
                            }, null, 8, ["modelValue", "onUpdate:modelValue"])
                          ]),
                          vue.createElementVNode(
                            "li",
                            _hoisted_18,
                            vue.toDisplayString(item.label),
                            1
                            /* TEXT */
                          ),
                          vue.createElementVNode("li", _hoisted_19, [
                            vue.createVNode(_component_gm_input, {
                              modelValue: item.width,
                              "onUpdate:modelValue": ($event) => item.width = $event,
                              class: "gm-w-full",
                              placeholder: "\u8BBE\u7F6E\u5BBD\u5EA6",
                              clearable: ""
                            }, null, 8, ["modelValue", "onUpdate:modelValue"])
                          ]),
                          vue.createElementVNode("li", _hoisted_20, [
                            vue.createVNode(
                              _component_gm_tooltip,
                              {
                                effect: "dark",
                                content: "\u56FA\u5B9A\u5728\u5DE6\u4FA7",
                                placement: "top"
                              },
                              {
                                default: vue.withCtx(() => [
                                  vue.createElementVNode("div", {
                                    class: "gm-cursor-pointer",
                                    onClick: ($event) => handleToogleFixed(item, "left")
                                  }, [
                                    (vue.openBlock(), vue.createElementBlock("svg", _hoisted_22, [
                                      vue.createElementVNode("path", {
                                        d: "M958.9 895.8L732 668.9l-63 63 227 226.9 64 1.3zM792.3 379l-102.5-12.7-311-284.7c-25.2-24-65.7-23.4-90.1 1l-206 205.9c-24.6 24.6-25 65.1-1.1 90.2l284.8 311.1 12.7 102.5c0 74.7 55.1 104.7 108 51.7L844 487.1c53-53 23.1-108.1-51.7-108.1zM439.7 610.1c-4.2 4.2-9.7 6.4-15.6 6.4s-11.4-2.3-15.7-6.5l-254-254.3c-8.4-8.4-8.6-22.5-0.2-31l43.7-45c4.2-4.4 9.7-6.7 15.9-6.7 5.7 0 11.2 2.3 15.3 6.3 4.3 4 6.5 9.6 6.8 15.4 0.1 5.9-2.1 11.5-6.3 15.8l-28.5 29.3 238.6 239c8.6 8.6 8.5 22.6 0 31.3z",
                                        "p-id": "15387",
                                        fill: item.fixed === "left" ? "#217efd" : "#bbbbbb"
                                      }, null, 8, _hoisted_23)
                                    ]))
                                  ], 8, _hoisted_21)
                                ]),
                                _: 2
                                /* DYNAMIC */
                              },
                              1024
                              /* DYNAMIC_SLOTS */
                            ),
                            vue.createVNode(
                              _component_gm_tooltip,
                              {
                                effect: "dark",
                                content: "\u56FA\u5B9A\u5728\u53F3\u4FA7",
                                placement: "top"
                              },
                              {
                                default: vue.withCtx(() => [
                                  vue.createElementVNode("div", {
                                    class: "gm-rotate-90 gm-cursor-pointer",
                                    onClick: ($event) => handleToogleFixed(item, "right")
                                  }, [
                                    (vue.openBlock(), vue.createElementBlock("svg", _hoisted_25, [
                                      vue.createElementVNode("path", {
                                        d: "M958.9 895.8L732 668.9l-63 63 227 226.9 64 1.3zM792.3 379l-102.5-12.7-311-284.7c-25.2-24-65.7-23.4-90.1 1l-206 205.9c-24.6 24.6-25 65.1-1.1 90.2l284.8 311.1 12.7 102.5c0 74.7 55.1 104.7 108 51.7L844 487.1c53-53 23.1-108.1-51.7-108.1zM439.7 610.1c-4.2 4.2-9.7 6.4-15.6 6.4s-11.4-2.3-15.7-6.5l-254-254.3c-8.4-8.4-8.6-22.5-0.2-31l43.7-45c4.2-4.4 9.7-6.7 15.9-6.7 5.7 0 11.2 2.3 15.3 6.3 4.3 4 6.5 9.6 6.8 15.4 0.1 5.9-2.1 11.5-6.3 15.8l-28.5 29.3 238.6 239c8.6 8.6 8.5 22.6 0 31.3z",
                                        "p-id": "15387",
                                        fill: item.fixed === "right" ? "#217efd" : "#bbbbbb"
                                      }, null, 8, _hoisted_26)
                                    ]))
                                  ], 8, _hoisted_24)
                                ]),
                                _: 2
                                /* DYNAMIC */
                              },
                              1024
                              /* DYNAMIC_SLOTS */
                            )
                          ]),
                          _ctx.dragHeader ? (vue.openBlock(), vue.createElementBlock("li", _hoisted_27, [
                            vue.createVNode(_component_el_icon, { color: "#217efd" }, {
                              default: vue.withCtx(() => [
                                vue.createVNode(vue.unref(iconsVue.Rank))
                              ]),
                              _: 1
                              /* STABLE */
                            })
                          ])) : vue.createCommentVNode("v-if", true)
                        ], 40, _hoisted_16))
                      ],
                      64
                      /* STABLE_FRAGMENT */
                    );
                  }),
                  128
                  /* KEYED_FRAGMENT */
                ))
              ]),
              _: 1
              /* STABLE */
            })
          ])
        ]),
        _: 2
        /* DYNAMIC */
      }, [
        !_ctx.disabledRemoteCol ? {
          name: "footer",
          fn: vue.withCtx(() => [
            vue.createElementVNode("div", _hoisted_28, [
              vue.createElementVNode("div", null, [
                vue.createVNode(_component_gm_button, {
                  loading: submitLoading.value,
                  type: "primary",
                  size: "default",
                  onClick: onReset
                }, {
                  default: vue.withCtx(() => [
                    vue.createTextVNode("\u91CD\u7F6E")
                  ]),
                  _: 1
                  /* STABLE */
                }, 8, ["loading"]),
                vue.createVNode(_component_gm_button, {
                  loading: submitLoading.value,
                  type: "success",
                  size: "default",
                  onClick: onSyncRemote
                }, {
                  default: vue.withCtx(() => [
                    vue.createTextVNode("\u540C\u6B65")
                  ]),
                  _: 1
                  /* STABLE */
                }, 8, ["loading"])
              ]),
              vue.createElementVNode("div", null, [
                vue.createVNode(_component_gm_button, {
                  loading: submitLoading.value,
                  size: "default",
                  onClick: _cache[2] || (_cache[2] = ($event) => isShowDrawer.value = false)
                }, {
                  default: vue.withCtx(() => [
                    vue.createTextVNode("\u53D6\u6D88")
                  ]),
                  _: 1
                  /* STABLE */
                }, 8, ["loading"]),
                vue.createVNode(_component_gm_button, {
                  loading: submitLoading.value,
                  type: "primary",
                  size: "default",
                  onClick: _cache[3] || (_cache[3] = ($event) => submitSettings())
                }, {
                  default: vue.withCtx(() => [
                    vue.createTextVNode("\u4FDD\u5B58")
                  ]),
                  _: 1
                  /* STABLE */
                }, 8, ["loading"])
              ])
            ])
          ]),
          key: "0"
        } : void 0
      ]), 1032, ["modelValue"]);
    };
  }
});

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