<template>
  <div class="pagination">
    <el-pagination
      v-model:current-page="pageData.page"
      v-model:page-size="pageData.limit"
      :layout="layout"
      :page-sizes="pageSizes"
      :pager-count="pagerCount"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
interface Props {
  // 分页参数对象
  modelValue: Record<string, any>
  // 总条数
  total: number
  // 每页条数选项
  pageSizes?: number[]
  // 页码按钮数量
  pagerCount?: number
  // 布局
  layout?: string
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: () => ({}),
  pageSizes: () => [10, 20, 50, 100],
  pagerCount: document.body.clientWidth < 992 ? 5 : 7,
  layout: 'total, sizes, prev, pager, next, jumper'
})

const emit = defineEmits<{
  (event: 'update:modelValue', value: Record<string, any>): void
  (event: 'change'): void
}>()

const pageData = computed({
  get() {
    return props.modelValue
  },
  set(val) {
    emit('update:modelValue', val)
  }
})

const handleSizeChange = () => {
  pageData.value.page = 1
  emit('change')
}

const handleCurrentChange = () => {
  emit('change')
}
</script>
