<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination :background="background" v-model:current-page="currentPage" v-model:page-size="pageSize"
      :layout="layout" :page-sizes="pageSizes" :pager-count="pagerCount" :total="total" :small="small"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
</template>

<script lang="ts" name="pagination" setup>
import { computed, onMounted, ref } from 'vue';
import mittBus from '/@/utils/mitt';
// 定义父组件传过来的值
const props = defineProps({
  total: {
    required: true,
    type: Number,
  },
  page: {
    type: Number,
    default: 1,
  },
  limit: {
    type: Number,
    default: 20,
  },
  pageSizes: {
    type: Array,
    default() {
      return [10, 20, 30, 50, 100]
    },
  },
  // 移动端页码按钮的数量端默认值5
  pagerCount: {
    type: Number,
    default: document.body.clientWidth < 992 ? 5 : 7,
  },
  layout: {
    type: String,
    default: 'total, sizes, prev, pager, next, jumper',
  },
  background: {
    type: Boolean,
    default: false,
  },
  autoScroll: {
    type: Boolean,
    default: true,
  },
  hidden: {
    type: Boolean,
    default: false,
  },
})

// 定义子组件向父组件传值/事件
const emit = defineEmits(['update:page', 'update:limit', 'pagination']);

// 定义变量内容
const small = ref(false);
const currentPage = computed({
  get() {
    return props.page
  },
  set(val) {
    emit('update:page', val)
  },
})
const pageSize = computed({
  get() {
    return props.limit
  },
  set(val) {
    emit('update:limit', val)
  },
})
const handleSizeChange = (val: any) => {
  emit('pagination', { page: currentPage.value, limit: val });
  mittBus.emit('scrollTopEmit', { top: 0 });// 分页发送监听事件 滚动到页面顶部
}
const handleCurrentChange = (val: any) => {
  emit('pagination', { page: val, limit: pageSize.value })
  mittBus.emit('scrollTopEmit', { top: 0 });// 分页发送监听事件 滚动到页面顶部
}
onMounted(() => {
  // 监听布局大小 改变分页的大小
  // let themeConfig = Local.get('themeConfig');
  // small.value = themeConfig.globalComponentSize == 'small';
})
</script>
<style scoped>
.pagination-container {
  padding-top: 20px;
  display: flex;
  justify-content: flex-end;
}
.pagination-container.hidden {
  display: none;
}
</style>
