export default {
  pageInfo: {
    title: '列表数据',
    exportUrl: '',
    pageListApi: null,
    deleteApi: null,
    defaultQuery: {},
    // 是否手动加载
    customLoad: false,
    // 是否添加导出日志
    isLog: false,
  },
  data() {
    return {
      // 表格相关参数start
      tableLoading: false,
      total: 0,
      selectrows: [],
      tableData: [],
      page: {
        ...this.MXDefaultPage()
      },
      query: { ...this.MXPageInfo().defaultQuery },
      tableSort: {},
      // 表格相关参数end
      showSearch: true,
    };
  },
  created() {
    this.MXCreated();
    if (this.MXPageInfo().customLoad) {
      return;
    }
    this.getTableList();
  },
  methods: {
    MXCreated() {

    },
    MXPageInfo() {
      return this.$options.pageInfo || {}
    },
    MXDefaultPage() {
      return {
        pageNum: 1,
        pageSize: 10,
      };
    },
    // 导出数据
    exportData(e) {
      this.$exportExcelData(
        this,
        this.MXPageInfo().exportUrl,
        `${this.MXPageInfo().title}.xlsx`,
        null,
        this.MXCombiQuery(),
        false,
        null,
        !(e && e.hideConfirm),
        {
          isLog: this.MXPageInfo().isLog,
        }
      ).then(() => {
        this.$emit('exportData', {...this.query});
      });

    },
    async getTableList() {
      if (this.$refs.seachForm && this.queryRules) {
        const valid = await this.$refs.seachForm.validate();
        if (!valid) {

          return;
        }
      }

      this.$emit('getTableList', {...this.query});
      if (!this.MXPageInfo().pageListApi) {
        return;
      }
      this.tableLoading = true;
      console.log('合成入参：', this.MXCombiListQuery());
      this.MXPageInfo().pageListApi(this.MXCombiListQuery())
        .then((response) => {
          this.tableData = response.items || response.rows || response || [];
          this.total = response.total || this.tableData.length;
          this.tableLoading = false;
          this.didGetTableList(true);
        })
        .catch((_) => {
          this.tableLoading = false;
          this.didGetTableList(false);
        });
    },
    didGetTableList(success) {

    },
    queryTable() {
      this.page = {
        ...this.MXDefaultPage()
      };
      this.getTableList();
    },
    resetQueryTable() {
      this.$refs.multipleTable?.clearSort();
      this.tableSort = {};
      this.page = {
        ...this.MXDefaultPage()
      };

      this.query = { ...this.MXPageInfo().defaultQuery };
      this.getTableList();
    },
    // 改变多选
    handleSelectionChange(val) {
      this.selectrows = val;
    },
    // 改变排序
    sortChange({ column, prop, order }) {
      this.tableSort = { prop, order };
      this.getTableList();
    },
    // --- 表格相关函数end --
    toEdit(detail) {
      this.$refs.editDialog.open(detail);
    },
    toAdd() {
      this.$refs.editDialog.open();
    },
    importData() {
      this.$refs.importDialog.open();
    },
    toDelete(row) {
      this.$deleteConfirm().then(() => {
        this.MXPageInfo().deleteApi(row.id).then((_) => {
          this.$message.success('删除成功');
          this.getTableList();
        });
      });
    },
    toMultDelete() {
      const ids = this.selectrows.map((item) => item.id);
      this.$multDelConfirm(`${ids.length}条`).then(() => {
        this.MXPageInfo().deleteApi(ids).then((_) => {
          this.$message.success('删除成功');
          this.getTableList();
        });
      });
    }
  },
};
