'use strict';

var vue = require('vue');
var elementPlus = require('element-plus');
var themeConfig = require('../stores/themeConfig.js');
var userInfo = require('../stores/userInfo.js');
var lodashEs = require('lodash-es');
require('../api/index.js');
var index = require('../api/base/index.js');

const useVxeTable = (opt, extras) => {
  const vxeSize = themeConfig.useThemeConfig().themeConfig.globalComponentSize == "small" ? "mini" : themeConfig.useThemeConfig().themeConfig.globalComponentSize == "default" ? "small" : "medium";
  const userStore = userInfo.useUserInfo();
  const processColumns = (columns) => {
    columns.forEach((col) => {
      var _a;
      if ((_a = col.dictExportConfig) == null ? void 0 : _a.code) {
        if (!col.exportMethod) {
          col.exportMethod = ({ row }) => {
            var _a2, _b;
            const val = row[col.field];
            if (val === null || val === void 0 || val === "") return "";
            if ((_a2 = col.dictExportConfig) == null ? void 0 : _a2.multiple) {
              const values = String(val).split(",");
              return values.map((v) => {
                var _a3;
                return ((_a3 = userStore.getDictItemByValue(col.dictExportConfig.code, v.trim())) == null ? void 0 : _a3.label) || v;
              }).join(", ");
            }
            return ((_b = userStore.getDictItemByValue(col.dictExportConfig.code, String(val))) == null ? void 0 : _b.label) || val;
          };
        }
      }
      if (col.children) {
        processColumns(col.children);
      }
    });
  };
  if (opt.columns) {
    processColumns(opt.columns);
  }
  opt.id = opt.id ? opt.id : opt.name;
  const options = vue.reactive({
    stripe: false,
    id: opt.id,
    height: "auto",
    autoResize: true,
    size: vxeSize,
    loading: false,
    align: "center",
    // 自动监听父元素的变化去重新计算表格（对于父元素可能存在动态变化、显示隐藏的容器中、列宽异常等场景中的可能会用到）
    // data: [] as Array<T>,
    columns: opt.columns,
    showFooter: opt.showFooter,
    footerData: opt.footerData,
    footerMethod: opt.footerMethod,
    toolbarConfig: {
      size: vxeSize,
      slots: { buttons: "toolbar_buttons", tools: "toolbar_tools" },
      refresh: true,
      refreshOptions: {
        code: "query"
      },
      export: true,
      print: true,
      zoom: true,
      custom: true
    },
    checkboxConfig: { range: true },
    rowClassName: ({ row }) => {
      if (row.activated === false) {
        return "row-deactivate-hightlight";
      }
      return "";
    },
    // sortConfig: { trigger: 'cell', remote: true },
    exportConfig: {
      remote: false,
      // 设置使用服务端导出
      original: false,
      // 使用格式化后的数据
      filename: `${opt.name || "\u5217\u8868"}\u5BFC\u51FA_${elementPlus.dayjs().format("YYYYMMDDHHmmss")}`,
      modes: ["current", "selected", "all"]
      // 当前页、选中项、所有
    },
    pagerConfig: {
      enabled: true,
      size: vxeSize,
      pageSize: 50
    },
    printConfig: { sheetName: "" },
    // proxyConfig: {
    // 	enabled: true,
    // 	autoLoad: false,
    // 	sort: true,
    // 	props: {
    // 		list: 'data.result', // 不分页时
    // 		result: 'data.result.items', // 分页时
    // 		total: 'data.result.total',
    // 		message: 'data.message',
    // 	},
    // },
    customConfig: {
      storage: {
        // 是否启用 localStorage 本地保存，会将列操作状态保留在本地（需要有 id）
        visible: true,
        // 启用显示/隐藏列状态
        resizable: true,
        // 启用列宽状态
        sort: true,
        // 启用列顺序缓存
        fixed: true
        // 启用冻结列状态缓存
      }
    }
  });
  if (opt.data) {
    options.data = opt.data;
  } else {
    options.proxyConfig = {
      enabled: true,
      autoLoad: false,
      sort: true,
      response: {
        list: "data.result",
        // 全量
        result: "data.result.items",
        // 分页
        total: "data.result.total",
        message: "data.message"
      }
    };
  }
  if (opt.sortConfig) {
    options.sortConfig = opt.sortConfig;
  } else {
    options.sortConfig = { remote: true };
  }
  if (opt.remoteCustom && options.customConfig != void 0) {
    options.customConfig.restoreStore = async ({ id, storeData }) => {
      var _a, _b, _c, _d;
      const { data } = await index.useBaseApi("sysColumnCustom").get({ id }, "sysColumnCustomDetail");
      if ((_a = data.result) == null ? void 0 : _a.fixedData) storeData.fixedData = data.result.fixedData;
      if ((_b = data.result) == null ? void 0 : _b.resizableData) storeData.resizableData = data.result.resizableData;
      if ((_c = data.result) == null ? void 0 : _c.sortData) storeData.sortData = data.result.sortData;
      if ((_d = data.result) == null ? void 0 : _d.visibleData) storeData.visibleData = data.result.visibleData;
      return storeData;
    };
    options.customConfig.updateStore = async ({ id, type, storeData }) => {
      if (type === "reset") await index.useBaseApi("sysColumnCustom").post({ gridId: id }, "sysColumnCustomReset");
      else {
        await index.useBaseApi("sysColumnCustom").post({
          gridId: id,
          fixedData: storeData == null ? void 0 : storeData.fixedData,
          resizableData: storeData == null ? void 0 : storeData.resizableData,
          sortData: storeData == null ? void 0 : storeData.sortData,
          visibleData: storeData == null ? void 0 : storeData.visibleData
        }, "sysColumnCustomStore");
      }
    };
  }
  return extras ? lodashEs.merge(options, extras) : options;
};

exports.useVxeTable = useVxeTable;
