<template>
  <div class="common-table">
    <el-table :data="currentPageData" v-bind="$attrs" style="width: 100%">
      <template v-for="column in columns" :key="column.prop">
        <el-table-column v-bind="column">
          <template #default="scope">
            <slot
              :name="column.prop"
              v-bind="scope"
              :row="scope.row"
              :column="column"
            >
              {{ scope.row[column.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>

    <div class="pagination-container" v-if="showPagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="pageSizes"
        :total="total"
        layout="total,  prev, pager, next, sizes, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :total-text="paginationTexts.total || '总计'"
        :size-change-text="paginationTexts.sizeChange || '条/页'"
        :jumper-text="paginationTexts.jumper || '前往'"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch } from "vue";

const props = defineProps({
  // 表格数据
  data: {
    type: Array,
    required: true,
    default: () => [],
  },
  // 列配置
  columns: {
    type: Array,
    required: true,
    default: () => [],
  },
  // 是否显示分页
  showPagination: {
    type: Boolean,
    default: true,
  },
  // 分页大小选项
  pageSizes: {
    type: Array,
    default: () => [10, 20, 30, 50],
  },
  // 默认每页条数
  defaultPageSize: {
    type: Number,
    default: 10,
  },
  // 分页布局
  paginationLayout: {
    type: String,
    default: "total, sizes, prev, pager, next, jumper",
  },
  // 分页器文字配置
  paginationTexts: {
    type: Object,
    default: () => ({
      total: "总计", // 总计xxx条
      sizeChange: "条/页", // 每页显示数量的文字
      prev: "上一页",
      next: "下一页",
      jumper: "前往", // 跳转文字
    }),
  },
});

const emit = defineEmits([
  "update:current-page",
  "update:page-size",
  "pagination-change",
]);

// 分页相关
const currentPage = ref(1);
const pageSize = ref(props.defaultPageSize);
const total = computed(() => props.data.length);

// 当前页数据
const currentPageData = computed(() => {
  if (!props.showPagination) return props.data;
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + pageSize.value;
  return props.data.slice(start, end);
});

// 分页事件处理
const handleSizeChange = (val) => {
  pageSize.value = val;
  currentPage.value = 1;
  emitPaginationChange();
};

const handleCurrentChange = (val) => {
  currentPage.value = val;
  emitPaginationChange();
};

// 向父组件发送分页变化事件
const emitPaginationChange = () => {
  emit("pagination-change", {
    currentPage: currentPage.value,
    pageSize: pageSize.value,
    total: total.value,
  });
  emit("update:current-page", currentPage.value);
  emit("update:page-size", pageSize.value);
};

// 监听数据变化，重置分页
watch(
  () => props.data,
  () => {
    currentPage.value = 1;
  },
  { deep: true }
);
</script>

<style lang="scss" scoped>
.common-table {
  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
    padding: 10px 0;
  }
}
</style>

<style lang="scss">
.el-table__header,
.el-table__body {
  width: 100% !important;
}
</style>
