const antdTableConfig = {
  props: {
    bordered: Boolean,
    loading: {
      type: [Boolean, Object],
      default: () => {
        return true
      }
    },
    heigth: {
      type: [Number, String, Boolean],
      default: () => {
        return false;
      }
    },
    width: {
      type: [Number, String],
      default: () => {
        return undefined;
      }
    },
    columns: {
      type: Array,
      default: () => {
        return [];
      }
    },
    requestCall: {
      type: [Array, Function, Object],
      default: () => {
        return "";
      }
    },
    rowKey: {
      type: [String, Function],
      default: 'id'
    },
    pagination: {
      type: [Object, Boolean],
      default: true
    },
    selectedRows: {
      type: Object,
      default: () => {
        return {}
      }
    },
    expandedRowKeys: Array,
    expandedRowRender: Function,
    height: {
      type: [Boolean, String, Number],
      default: () => {
        return false
      }
    },
    formItemConfig: {
      type: Array,
      default: () => {
        return []
      }
    },
    /**
     * 查询参数
     */
    queryParams: {
      type: Object,
      default: () => {
        return {};
      },
    },
    /** 查询验证条件 */
    queryRules: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data () {
    return {
      pageConfig: {
        current: 1, // 当前页
        defaultCurrent: 1, // 默认当前初始页
        defaultPageSize: 20, // 默认分页大小
        disabled: false,
        hideOnSinglePage: false,
        pageSize: 20,
        pageSizeOptions: ['10', '20', '30', '50', '100', '150', '200', '300','1000'],
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      dataSource: [],
    }
  },
  activated () {
    this.$nextTick(() => {
      this.loadTableData();
      this.$emit('height', window.document.body.clientHeight - 120);
      if (this.height) {
        this.tableHeight = this.height;
      } else {
        this.tableHeight = window.document.body.clientHeight - 120;
      }
      const _this = this;
      if (!this.fixedHeight) {
        window.onresize = () => (() => {
          _this.tableHeight = window.document.body.clientHeight - 120;
        })();
      }
    })
  },
  methods: {
    /**
     * 初始化加载表格
     */
    loadTableData () {
      if (!this.requestCall) {
        this.$messageTips.warning('请求地址不存在，请检查配置.');
        return false;
      }
      if (typeof this.requestCall === 'function') {
        if (this.pagination) {
          this.queryParams.rows = this.pageConfig.pageSize;
          this.queryParams.page = this.pageConfig.current;
        }
        this.requestCall.call(this, this.queryParams).then(({ rows, total }) => {
          this.$emit('update:loading', false);
          if (this.pagination) {
            this.$nextTick(() => {
              this.dataSource = rows;
              this.pageConfig.total = total;
              if (this.callback) {
                this.callback(rows)
              }
            })
          } else {
            this.$refs['tableRef'].loadData(rows);
            if (this.callback) {
              this.callback(rows)
            }
          }
        })
      } else if (Array.isArray(this.requestCall)) {
        this.$refs['tableRef'].loadData(this.requestCall);
      } else if (typeof this.requestCall == 'object') {
        if (this.pagination) {
          this.$refs['tableRef'].loadData(this.requestCall['rows']);
          this.pageConfig.total = this.requestCall['total'];
        } else {
          this.dataSource = this.requestCall
        }
      }
    },
    onClear () {
      this.updateSelect([], []);
      this.$emit('clear')
    },
    onChange (pagination, filters, sorter, { currentDataSource }) {
      this.$emit('change', pagination, filters, sorter, { currentDataSource })
    },
    change (page, pageSize) {
      this.pageConfig.current = page;
      this.pageConfig.pageSize = pageSize;
      this.loadTableData();
    },
    showSizeChange (current, size) {
      this.pageConfig.current = current;
      this.pageConfig.pageSize = size;
      this.loadTableData();
    },
    updateSelect (selectedRowKeys, selectedRows) {
      this.$emit('update:selectedRows', selectedRows);
      this.$emit('selectedRowChange', selectedRowKeys, selectedRows)
    },
  }
};

const tableDefaultConfig = {
  checkbox: {
    type: 'checkbox'
  },
  default: {
    align: 'left',
    ellipsis: true,

  }
};

export { antdTableConfig, tableDefaultConfig }
