'use strict';

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

var vue = require('vue');
require('../../../../hooks/index.js');
require('../../../../utils/index.js');
var draggable = require('vuedraggable');
var lodashEs = require('lodash-es');
require('../config/index.js');
var index = require('../../../../hooks/use-locale/index.js');
var createNamespace = require('../../../../utils/create-namespace.js');
var mainTable = require('../config/main-table.js');

"use strict";
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{
    name: "LlMainTable"
  },
  __name: "main-table",
  props: mainTable.mainTableProps,
  emits: mainTable.mainTableEmits,
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const attrs = vue.useAttrs();
    const { t } = index.useLocale();
    const newColumns = vue.ref([]);
    const settingColumns = vue.ref([]);
    const newDataSource = vue.ref([]);
    const newExpandedRowKeys = vue.ref([]);
    let preIndex = -1;
    let nextIndex = -1;
    const bem = createNamespace.createNamespace("table");
    const handleAddSerialNumberAndOperateToColumns = () => {
      if (props.showIndexColumn) {
        newColumns.value.unshift({
          title: "#",
          dataIndex: "#",
          key: "#",
          maxWidth: 60,
          minWidth: 60,
          width: 60,
          fixed: "left",
          class: "table-serial-number-cell"
        });
      }
      if (props.showOperate) {
        let maxWidth = 70;
        let minWidth = 120;
        let width = 120;
        if (props.operateWidth) {
          const operateWidth = +props.operateWidth;
          maxWidth = operateWidth;
          minWidth = operateWidth;
          width = operateWidth;
        }
        newColumns.value.push({
          title: t("plus.table.operate"),
          dataIndex: "",
          key: "operate",
          maxWidth,
          minWidth,
          width,
          fixed: "right",
          class: "table-serial-operate-cell"
        });
      }
    };
    const handleColumnsSortByFixed = (list, isDrag) => {
      const newList = list;
      const leftFixedColumns = [], normalFixedColumns = [], rightFixedColumns = [];
      newList.forEach((item) => {
        const fixed = item.fixed;
        if (fixed === "left") {
          leftFixedColumns.push(item);
        } else if (fixed === "right") {
          rightFixedColumns.push(item);
        } else {
          normalFixedColumns.push(item);
        }
      });
      leftFixedColumns.sort(
        (a, b) => a.columnSortIndex - b.columnSortIndex
      );
      normalFixedColumns.sort(
        (a, b) => a.columnSortIndex - b.columnSortIndex
      );
      rightFixedColumns.sort(
        (a, b) => a.columnSortIndex - b.columnSortIndex
      );
      newColumns.value = [
        ...leftFixedColumns,
        ...normalFixedColumns,
        ...rightFixedColumns
      ];
      if (isDrag) {
        handleAddSerialNumberAndOperateToColumns();
      }
    };
    vue.watch(
      () => props.dataSource,
      (newValue) => {
        newDataSource.value = [...newValue];
      },
      {
        deep: true,
        immediate: true
      }
    );
    vue.watch(
      () => [
        props.columns,
        props.settingKey,
        props.showIndexColumn,
        props.showOperate
      ],
      (arr) => {
        const [newValue, key] = arr;
        let newcolumns = [];
        if (key && localStorage.getItem(key)) {
          const localStorageColumns = localStorage.getItem(key);
          if (localStorageColumns) {
            newcolumns = JSON.parse(localStorageColumns);
          }
        } else {
          newcolumns = newValue.map((item, index) => ({
            ...item,
            isCheck: true,
            columnSortIndex: item.columnSortIndex === 0 || item.columnSortIndex ? item.columnSortIndex : index
          }));
        }
        newColumns.value = [...newcolumns].filter((item) => item.isCheck);
        handleAddSerialNumberAndOperateToColumns();
        handleColumnsSortByFixed(newColumns.value);
        settingColumns.value = [...newcolumns];
      },
      {
        deep: true,
        immediate: true
      }
    );
    vue.watch(
      () => props.expandedRowKeys,
      (newValue) => {
        if (Array.isArray(newValue)) {
          newExpandedRowKeys.value = [...newValue];
        }
      },
      {
        deep: true,
        immediate: true
      }
    );
    const handleExpand = (expanded, record) => {
      if (expanded) {
        newExpandedRowKeys.value.push(record.id);
      } else {
        const index = newExpandedRowKeys.value.findIndex(
          (e) => e === record.id
        );
        newExpandedRowKeys.value.splice(index, 1);
      }
      props.expand && props.expand(expanded, record);
    };
    const handleResizeColumn = (w, col) => {
      col.width = w;
      props.resizeColumn && props.resizeColumn(w, col);
    };
    const dragCustomRow = (_, index) => {
      const normalConfig = {
        props: {
          // draggable: 'true'
        },
        draggable: true,
        style: {
          cursor: "pointer"
        },
        // 鼠标移入
        onMouseenter: (event) => {
          const target = event.target;
          target.draggable = true;
        },
        // 开始拖拽
        onDragstart: (event) => {
          event.stopPropagation();
          preIndex = index;
        },
        // 拖动元素经过的元素
        onDragover: (event) => {
          event.preventDefault();
        },
        // 鼠标松开
        onDrop: (event) => {
          let sortSource = [];
          event.stopPropagation();
          nextIndex = index;
          const dataSource = newDataSource.value;
          const preItem = { ...dataSource[preIndex] };
          const nextItem = { ...dataSource[nextIndex] };
          const preSort = preItem.sort;
          const nextSort = nextItem.sort;
          preItem.sort = nextSort;
          nextItem.sort = preSort;
          dataSource[preIndex] = nextItem;
          dataSource[nextIndex] = preItem;
          sortSource = [...dataSource];
          emits("handleChangeDataSource", sortSource);
          props.dragChange && props.dragChange(sortSource);
        }
      };
      const extraConfig = props.customRowProps ? { ...props.customRowProps(_, index) } : {};
      return { ...normalConfig, ...extraConfig };
    };
    const isAllCheck = vue.computed(() => {
      return settingColumns.value.every((item) => item.isCheck);
    });
    const isIndeterminate = vue.computed(() => {
      const getIsChecks = settingColumns.value.filter((item) => item.isCheck);
      return getIsChecks.length === 0 ? false : settingColumns.value.length > getIsChecks.length;
    });
    const handleChangeColumns = () => {
      const isCheckSettingColumns = settingColumns.value.filter(
        (item, index) => {
          item.columnSortIndex = index;
          return item.isCheck;
        }
      );
      handleColumnsSortByFixed(isCheckSettingColumns, true);
    };
    const handleColumnItemChange = (value, item) => {
      const findSettingIndex = settingColumns.value.findIndex(
        (citem) => item.key === citem.key
      );
      if (findSettingIndex > -1) {
        settingColumns.value[findSettingIndex].isCheck = value;
        handleChangeColumns();
        handleClickSubmitBtn();
      }
    };
    const handleAllColumnChange = (e) => {
      const checked = e.target.checked;
      settingColumns.value.forEach((item) => item.isCheck = checked);
      if (checked) {
        newColumns.value = [...props.columns];
      } else {
        newColumns.value = [];
      }
      handleAddSerialNumberAndOperateToColumns();
      handleClickSubmitBtn();
    };
    const dragChange = () => {
      handleChangeColumns();
      handleClickSubmitBtn();
    };
    const handleClickResetBtn = () => {
      const newcolumns = props.columns.map((item, index) => ({
        ...item,
        isCheck: true,
        columnSortIndex: index
      }));
      newColumns.value = [...newcolumns];
      handleAddSerialNumberAndOperateToColumns();
      settingColumns.value = [...newcolumns];
      handleClickSubmitBtn();
    };
    const handleClickSubmitBtn = () => {
      if (props.settingKey) {
        localStorage.setItem(props.settingKey, JSON.stringify(settingColumns.value));
      }
    };
    const handleColumnMove = (element, way) => {
      const fixed = element.fixed;
      if (fixed === way) {
        element.fixed = false;
      } else {
        element.fixed = way;
      }
      handleColumnsSortByFixed(newColumns.value);
      handleClickSubmitBtn();
    };
    return (_ctx, _cache) => {
      const _component_a_checkbox = vue.resolveComponent("a-checkbox");
      const _component_ll_icon = vue.resolveComponent("ll-icon");
      const _component_a_button = vue.resolveComponent("a-button");
      const _component_a_divider = vue.resolveComponent("a-divider");
      const _component_a_popover = vue.resolveComponent("a-popover");
      const _component_ll_empty = vue.resolveComponent("ll-empty");
      const _component_a_table = vue.resolveComponent("a-table");
      return vue.openBlock(), vue.createBlock(_component_a_table, vue.mergeProps({
        class: `${vue.unref(bem).e("main-content")}`,
        "row-key": "id"
      }, vue.unref(attrs), {
        columns: newColumns.value,
        "data-source": newDataSource.value,
        "expanded-row-keys": newExpandedRowKeys.value,
        "custom-row": props.showDrag ? dragCustomRow : _ctx.customRowProps,
        pagination: false,
        "show-header": "",
        scroll: _ctx.scroll,
        onExpand: handleExpand,
        onResizeColumn: handleResizeColumn,
        onChange: props.change,
        onExpandedRowsChange: props.expandedRowsChange
      }), vue.createSlots({
        headerCell: vue.withCtx((slots) => [
          vue.createElementVNode(
            "div",
            {
              class: vue.normalizeClass(`${vue.unref(bem).e("header-cell-content")}`)
            },
            [
              slots.column.key === "operate" && _ctx.showSetting ? (vue.openBlock(), vue.createElementBlock(
                "div",
                {
                  key: 0,
                  class: vue.normalizeClass(`${vue.unref(bem).e("operate")}`)
                },
                [
                  vue.createElementVNode(
                    "span",
                    null,
                    vue.toDisplayString(slots.column.title),
                    1
                    /* TEXT */
                  ),
                  vue.createVNode(_component_a_popover, {
                    trigger: "click",
                    placement: "bottomRight",
                    "overlay-inner-style": {
                      padding: "0"
                    }
                  }, {
                    content: vue.withCtx(() => [
                      vue.createElementVNode(
                        "div",
                        {
                          class: vue.normalizeClass(`${vue.unref(bem).e("setting-popover")}`)
                        },
                        [
                          vue.createCommentVNode(" \u5934\u90E8 "),
                          vue.createElementVNode(
                            "div",
                            {
                              class: vue.normalizeClass(`${vue.unref(bem).e("setting-popover-header")}`)
                            },
                            [
                              vue.createVNode(_component_a_checkbox, {
                                indeterminate: isIndeterminate.value,
                                checked: isAllCheck.value,
                                onChange: handleAllColumnChange
                              }, {
                                default: vue.withCtx(() => [..._cache[0] || (_cache[0] = [
                                  vue.createTextVNode(
                                    " \u5217\u5C55\u793A ",
                                    -1
                                    /* CACHED */
                                  )
                                ])]),
                                _: 1
                                /* STABLE */
                              }, 8, ["indeterminate", "checked"]),
                              vue.createElementVNode(
                                "span",
                                {
                                  class: vue.normalizeClass(`${vue.unref(bem).e("setting-reset-btn")}`),
                                  onClick: handleClickResetBtn
                                },
                                " \u91CD\u7F6E ",
                                2
                                /* CLASS */
                              )
                            ],
                            2
                            /* CLASS */
                          ),
                          vue.createCommentVNode(" \u5185\u5BB9 "),
                          vue.createElementVNode(
                            "div",
                            {
                              class: vue.normalizeClass(`${vue.unref(bem).e("setting-popover-content")}`)
                            },
                            [
                              vue.createElementVNode("div", null, [
                                vue.createVNode(vue.unref(draggable), {
                                  list: settingColumns.value,
                                  "item-key": "key",
                                  group: { name: "g1" },
                                  onChange: dragChange
                                }, {
                                  item: vue.withCtx(({ element }) => [
                                    vue.createElementVNode(
                                      "div",
                                      {
                                        class: vue.normalizeClass(`${vue.unref(bem).e("setting-popover-menu")}`)
                                      },
                                      [
                                        vue.createCommentVNode(" \u62D6\u62FD "),
                                        vue.createElementVNode(
                                          "div",
                                          {
                                            class: vue.normalizeClass(`flex items-center ${vue.unref(bem).e(
                                              "setting-popover-menu-left"
                                            )}`)
                                          },
                                          [
                                            vue.createVNode(_component_a_button, {
                                              type: "text",
                                              class: vue.normalizeClass(`${vue.unref(bem).e("setting-popover-menu-icon")}`),
                                              style: {
                                                marginright: "5px"
                                              }
                                            }, {
                                              default: vue.withCtx(() => [
                                                vue.createVNode(_component_ll_icon, {
                                                  "icon-name": "icon-move",
                                                  "class-name": "icon"
                                                })
                                              ]),
                                              _: 1
                                              /* STABLE */
                                            }, 8, ["class"]),
                                            vue.createVNode(_component_a_checkbox, {
                                              checked: element.isCheck,
                                              onChange: (e) => {
                                                handleColumnItemChange(
                                                  e.target?.checked,
                                                  element
                                                );
                                              }
                                            }, {
                                              default: vue.withCtx(() => [
                                                vue.createTextVNode(
                                                  vue.toDisplayString(element.title),
                                                  1
                                                  /* TEXT */
                                                )
                                              ]),
                                              _: 2
                                              /* DYNAMIC */
                                            }, 1032, ["checked", "onChange"])
                                          ],
                                          2
                                          /* CLASS */
                                        ),
                                        vue.createElementVNode(
                                          "div",
                                          {
                                            class: vue.normalizeClass(`flex items-center ${vue.unref(bem).e(
                                              "setting-popover-menu-right"
                                            )}`)
                                          },
                                          [
                                            vue.createCommentVNode(" \u5DE6\u79FB "),
                                            vue.createVNode(_component_a_button, {
                                              type: "text",
                                              class: vue.normalizeClass(`${vue.unref(bem).e("setting-popover-menu-icon")}`),
                                              onClick: ($event) => handleColumnMove(element, "left")
                                            }, {
                                              default: vue.withCtx(() => [
                                                vue.createVNode(_component_ll_icon, {
                                                  "icon-name": "icon-zuoyi",
                                                  "class-name": `icon ${element.fixed === "left" ? vue.unref(bem).e("setting-popover-menu-fixed-icon") : ""}`
                                                }, null, 8, ["class-name"])
                                              ]),
                                              _: 2
                                              /* DYNAMIC */
                                            }, 1032, ["class", "onClick"]),
                                            vue.createVNode(_component_a_divider, { type: "vertical" }),
                                            vue.createCommentVNode(" \u53F3\u79FB "),
                                            vue.createVNode(_component_a_button, {
                                              type: "text",
                                              class: vue.normalizeClass(`${vue.unref(bem).e("setting-popover-menu-icon")}`),
                                              onClick: ($event) => handleColumnMove(element, "right")
                                            }, {
                                              default: vue.withCtx(() => [
                                                vue.createVNode(_component_ll_icon, {
                                                  "icon-name": "icon-youyi",
                                                  "class-name": `icon ${element.fixed === "right" ? vue.unref(bem).e("setting-popover-menu-fixed-icon") : ""}`
                                                }, null, 8, ["class-name"])
                                              ]),
                                              _: 2
                                              /* DYNAMIC */
                                            }, 1032, ["class", "onClick"])
                                          ],
                                          2
                                          /* CLASS */
                                        )
                                      ],
                                      2
                                      /* CLASS */
                                    )
                                  ]),
                                  _: 1
                                  /* STABLE */
                                }, 8, ["list"])
                              ])
                            ],
                            2
                            /* CLASS */
                          )
                        ],
                        2
                        /* CLASS */
                      )
                    ]),
                    default: vue.withCtx(() => [
                      vue.createVNode(_component_a_button, {
                        type: "text",
                        class: vue.normalizeClass(`${vue.unref(bem).e("setting-icon")}`)
                      }, {
                        default: vue.withCtx(() => [
                          vue.createVNode(_component_ll_icon, { "icon-name": "icon-set-line" })
                        ]),
                        _: 1
                        /* STABLE */
                      }, 8, ["class"])
                    ]),
                    _: 1
                    /* STABLE */
                  })
                ],
                2
                /* CLASS */
              )) : vue.unref(lodashEs.has)(slots.column, "RC_TABLE_INTERNAL_COL_DEFINE") ? vue.renderSlot(_ctx.$slots, "expandedTitle", { key: 1 }) : (vue.openBlock(), vue.createElementBlock(
                vue.Fragment,
                { key: 2 },
                [
                  vue.createTextVNode(
                    vue.toDisplayString(slots.column.title),
                    1
                    /* TEXT */
                  )
                ],
                64
                /* STABLE_FRAGMENT */
              )),
              vue.createCommentVNode(" \u989D\u5916header\u5355\u5143\u683C\u7684\u63D2\u69FD\u5185\u5BB9 "),
              vue.renderSlot(_ctx.$slots, "tableHeaderCell", vue.normalizeProps(vue.guardReactiveProps(slots)))
            ],
            2
            /* CLASS */
          )
        ]),
        bodyCell: vue.withCtx((slots) => [
          vue.createCommentVNode(" \u6DFB\u52A0\u5E8F\u5217\u5217 "),
          slots.column.key === "#" && _ctx.showIndexColumn ? (vue.openBlock(), vue.createElementBlock(
            vue.Fragment,
            { key: 0 },
            [
              vue.createTextVNode(
                vue.toDisplayString(slots.index + 1),
                1
                /* TEXT */
              )
            ],
            64
            /* STABLE_FRAGMENT */
          )) : slots.column.key === "operate" ? (vue.openBlock(), vue.createElementBlock(
            vue.Fragment,
            { key: 1 },
            [
              vue.createCommentVNode(" \u6DFB\u52A0\u64CD\u4F5C\u5217 "),
              vue.renderSlot(_ctx.$slots, "tableOperate", vue.normalizeProps(vue.guardReactiveProps(slots)))
            ],
            64
            /* STABLE_FRAGMENT */
          )) : vue.createCommentVNode("v-if", true),
          vue.createCommentVNode(" \u989D\u5916Body\u5355\u5143\u683C\u7684\u63D2\u69FD\u5185\u5BB9 "),
          vue.renderSlot(_ctx.$slots, "tableBodyCell", vue.normalizeProps(vue.guardReactiveProps(slots)))
        ]),
        emptyText: vue.withCtx(() => [
          vue.renderSlot(_ctx.$slots, "emptyText", {}, () => [
            vue.createVNode(_component_ll_empty)
          ])
        ]),
        _: 2
        /* DYNAMIC */
      }, [
        _ctx.$slots.expandedRow ? {
          name: "expandedRowRender",
          fn: vue.withCtx((slots) => [
            vue.renderSlot(_ctx.$slots, "expandedRow", vue.normalizeProps(vue.guardReactiveProps(slots)))
          ]),
          key: "0"
        } : void 0,
        _ctx.$slots.customFilterDropdown ? {
          name: "customFilterDropdown",
          fn: vue.withCtx((slots) => [
            vue.renderSlot(_ctx.$slots, "customFilterDropdown", vue.normalizeProps(vue.guardReactiveProps(slots)))
          ]),
          key: "1"
        } : void 0,
        _ctx.$slots.customFilterIcon ? {
          name: "customFilterIcon",
          fn: vue.withCtx((slots) => [
            vue.renderSlot(_ctx.$slots, "customFilterIcon", vue.normalizeProps(vue.guardReactiveProps(slots)))
          ]),
          key: "2"
        } : void 0,
        _ctx.$slots.summary ? {
          name: "summary",
          fn: vue.withCtx((slots) => [
            vue.renderSlot(_ctx.$slots, "summary", vue.normalizeProps(vue.guardReactiveProps(slots)))
          ]),
          key: "3"
        } : void 0
      ]), 1040, ["class", "columns", "data-source", "expanded-row-keys", "custom-row", "scroll", "onChange", "onExpandedRowsChange"]);
    };
  }
});

exports.default = _sfc_main;
//# sourceMappingURL=main-table.vue2.js.map
