export default {
  data() {
    return {
      loading: false,
      total: 0,
      selection: [],
      list: [],
      page: {
        ...this.mx_pageParams()
      },
      query: { ...this.mx_pageInfo().query },
      sort: { ...this.mx_sortParams() },
    };
  },
  created() {
    this.mx_created();
    if (this.mx_pageInfo().customLoad) {
      return;
    }
    this.getTableList();
  },
  methods: {
    mx_created() {

    },
    mx_pageInfo() {
      return {
        title: '列表数据',
        exportUrl: '',
        pageListApi: null,
        deleteApi: null,
        queryOption: [],
        query: {},
        // 是否手动加载
        customLoad: false,
      };
    },
    mx_pageParams() {
      return {
        pageNum: 1,
        pageSize: 10,
      };
    },
    mx_sortParams() {
      return {};
    },
    // 导出数据
    handleExport() {
      this.download(this.mx_pageInfo().exportUrl, {
        ...this.query,
        ...this.sort
      }, `${this.mx_pageInfo().title}.xlsx`)
    },
    // 搜索列表数据
    getTableList() {
      
      if (!this.mx_pageInfo().pageListApi) {
        return;
      }
      this.loading = true;
      
      this.mx_pageInfo().pageListApi({
        ...this.page,
        ...this.query,
        ...this.sort
      })
        .then((response) => {
          this.list = response.rows || response || [];
          this.total = response.total || this.list.length;
          this.loading = false;
          this.didGetTableList(true);
        })
        .catch((_) => {
          this.loading = false;
          this.didGetTableList(false);
        });
    },
    didGetTableList(success) {
      
    },
    // 重置页码搜索
    queryTable() {
      this.page.pageNum = 1;
      this.getTableList();
    },
    // 重置所有搜索条件
    resetQueryTable() {
      this.$refs.table?.clearSort();
      this.sort = {
        ...this.mx_sortParams()
      };
      this.page = {
        ...this.mx_pageParams()
      };
      this.query = { ...this.mx_pageInfo().query };
      this.getTableList();
    },
    // 改变多选
    handleSelectionChange(val) {
      this.selection = val;
    },
    // 改变排序
    sortChange(col) {
      this.$sortBy(col, this.sort);
      this.getTableList();
    },
    // 去编辑
    handleEdit(detail) {
      this.$refs.editDialog.open(detail);
    },
    // 去编辑
    handleAdd() {
      this.$refs.editDialog.open();
    },
    // importData() {
    //   this.$refs.importDialog.open();
    // },
    handleDelete(row) {
      this.$confirm(`确定删除数据?`, '删除提示', {
        type: 'warning'
      }).then(() => {
        this.mx_pageInfo().deleteApi(row.id).then((_) => {
          this.$message.success('删除成功');
          this.getTableList();
        });
      });
    },
    handleMultDelete() {
      const ids = this.selection.map((item) => item.id);
      this.$confirm(`确定删除选中的${ids.length}条数据?`, '批量删除提示', {
        type: 'warning'
      }).then(() => {
        this.mx_pageInfo().deleteApi(ids).then((_) => {
          this.$message.success('删除成功');
          this.getTableList();
        });
      });
    }
  },
};