<template>
  <div class="tablePaganations">
      <a-table
        class="ant-table-striped"
        bordered
        size="small"
        height="1000px"
        :loading="isLoading"
        :data-source="dataSource"
        :columns="columns"
        :row-key="(record) => record.id"
        :scroll="scrollValue"
        :pagination="havePage ? pagination : false"
        :row-selection="
          isSelect
            ? {
              selectedRowKeys: state.selectedRowKeys,
              onSelect: selectEvent,
              onSelectAll: selectAllEvent,
            }
            : null
        "
        :row-class-name="
          (_record, index) => (index % 2 === 1 ? 'table-striped' : null)
        "
        @resize-column="handleResizeColumn"
      >
        <template
          v-for="(item, index) in renderArr"
          #[item]="scope"
          :key="index"
        >
          <slot :name="item" :scope="scope" v-bind="scope || {}" />
        </template>
      </a-table>
  </div>
</template>
  
<script setup>
import {
  useSlots,
  ref,
  reactive,
  defineProps,
  defineEmits,
  watch,
  onMounted,
  defineExpose,
} from 'vue';
const props = defineProps({
  dataSource: {
    type: Object,
    required: true,
  },
  columns: {
    type: Object,
    required: true,
  },
  isSelect: {
    type: Boolean,
  },
  isLoading: {
    type: Boolean,
  },
  total: {
    type: Number,
    default: 0,
  },
  havePage: {
    type: Boolean,
    default: true,
  },
  scrollValue: {
    type: Object,
    default: () => ({ y: 410 }),
  },
});

//  选择数据
const state = reactive({
  selectedRows: [],
  selectedRowKeys: [],
});

// 分页配置
const pagination = reactive({
  showQuickJumper: true,
  current: 1,
  pageSize: 20, // 默认每页显示数量
  showSizeChanger: true, // 显示可改变每页数量
  pageSizeOptions: ['20', '50', '100', '200', '300', '500'], // 每页数量选项值
  showTotal: (total, range) =>
    range[0] + '-' + range[1] + '条' + ' 共' + total + '条', // 显示总数
  onShowSizeChange: (current, pageSize) => showSizeChange(current, pageSize),
  onChange: (current, pageSize) => changePage(current, pageSize), //点击页码事件
  total: props.total,
});
const emit = defineEmits(['pageParams', 'selectColumn']);

// 改变每页数量时更新显示
const showSizeChange = (current, pageSize) => {
  setTimeout(() => {
    pagination.current = 1;
    emit('pageParams', pagination.current, pageSize);
  });
  pagination.pageSize = pageSize;
};

//点击页码事件
const changePage = (current, size) => {
  pagination.current = current;
  emit('pageParams', pagination.current, size);
};

// 选择每一项操作
const selectEvent = (record, selected, selectedRows) => {
  if (selected) {
    state.selectedRows.push(record);
    state.selectedRowKeys.push(record.id);
  } else {
    let index = state.selectedRowKeys.indexOf(record.id);
    if (index >= 0) {
      state.selectedRows.splice(index, 1);
      state.selectedRowKeys.splice(index, 1);
    }
  }
  emit('selectColumn', state.selectedRows);
};

// 全选按钮操作
const selectAllEvent = (selected, selectedRows, changeRows) => {
  if (selected) {
    state.selectedRows = state.selectedRows.concat(changeRows);
    state.selectedRows.forEach(item => {
      state.selectedRowKeys.push(item.id);
    });
  } else {
    changeRows.forEach(item => {
      state.selectedRows.forEach(row => {
        if (item.id == row.id) {
          state.selectedRows.splice(0, 1);
        }
      });
      let index = state.selectedRowKeys.indexOf(item.id);
      if (index >= 0) {
        state.selectedRows.splice(index, 1);
        state.selectedRowKeys.splice(index, 1);
      }
    });
  }
  emit('selectColumn', state.selectedRows);
};
// 伸缩列
const handleResizeColumn = (w, col) => {
  col.width = w;
};
// 清空选择
const clear = () => {
  state.selectedRowKeys = [];
  state.selectedRows = [];
  emit('selectColumn', state.selectedRows);
};
// 回到第一页
const backFirstPage = () => {
  pagination.current = 1;
  emit('pageParams', pagination.current, pagination.pageSize);
};
defineExpose({ clear, backFirstPage });

// 监听total变化
watch(
  props,
  newData => {
    pagination.total = newData.total;
  },
  { immediate: true, deep: true },
);

// 插槽的实例
const slots = useSlots();
const renderArr = Object.keys(slots);
</script>
<style scoped>
.tablePaganations {
  width: 100%;
  margin-top: 20px;
  margin-bottom: 20px;
}
.ant-table-striped :deep(.table-striped) td {
  background-color: #fafafa;
}
</style>