<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <!-- 游标分页模式 -->
    <div v-if="cursorMode" class="cursor-pagination">
      <el-button :disabled="currentPage === 1" @click="handlePrevPage"> 上一页 </el-button>
      <span class="page-info">第 {{ currentPage }} 页</span>
      <el-button :disabled="!hasMore" @click="handleNextPage"> 下一页 </el-button>
      <el-select v-model="pageSize" @change="handleSizeChange" style="width: 100px" class="ml-2">
        <el-option v-for="size in pageSizes" :key="size" :label="`${size}条/页`" :value="size" />
      </el-select>
    </div>

    <!-- 传统分页模式 -->
    <el-pagination
      v-else
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :background="background"
      :layout="layout"
      :page-sizes="pageSizes"
      :pager-count="pagerCount"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup name="Pagination" lang="ts">
import { scrollTo } from '@/utils/scroll-to';
import { propTypes } from '@/utils/propTypes';

const props = defineProps({
  total: propTypes.number,
  page: propTypes.number.def(1),
  limit: propTypes.number.def(20),
  way: propTypes.number.def(1),
  pageSizes: { type: Array<number>, default: () => [10, 20, 30, 50] },
  // 移动端页码按钮的数量端默认值5
  pagerCount: propTypes.number.def(document.body.clientWidth < 992 ? 5 : 7),
  layout: propTypes.string.def('total, sizes, prev, pager, next, jumper'),
  background: propTypes.bool.def(true),
  autoScroll: propTypes.bool.def(true),
  hidden: propTypes.bool.def(false),
  float: propTypes.string.def('right'),
  // 游标分页模式
  cursorMode: propTypes.bool.def(false),
  // 是否还有更多数据（游标分页使用）
  hasMore: propTypes.bool.def(true)
});

const emit = defineEmits(['update:page', 'update:limit', 'update:way', 'pagination']);
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);
  }
});
function handleSizeChange(val: number) {
  if (!props.cursorMode && currentPage.value * val > props.total) {
    currentPage.value = 1;
  }
  emit('pagination', { page: currentPage.value, limit: val });
  if (props.autoScroll) {
    scrollTo(0, 800);
  }
}
function handleCurrentChange(val: number) {
  emit('pagination', { page: val, limit: pageSize.value });
  if (props.autoScroll) {
    scrollTo(0, 800);
  }
}
// 游标分页：下一页
function handleNextPage() {
  if (props.hasMore) {
    currentPage.value += 1;
    emit('update:way', 1);
    emit('pagination', { page: currentPage.value, limit: pageSize.value, way: 1 });
    if (props.autoScroll) {
      scrollTo(0, 800);
    }
  }
}
// 游标分页：上一页
function handlePrevPage() {
  if (currentPage.value > 1) {
    currentPage.value -= 1;
    emit('update:way', 0);
    emit('pagination', { page: currentPage.value, limit: pageSize.value, way: 0 });
    if (props.autoScroll) {
      scrollTo(0, 800);
    }
  }
}
</script>

<style lang="scss" scoped>
.pagination-container {
  .el-pagination {
    float: v-bind(float);
  }

  .cursor-pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    float: v-bind(float);

    .page-info {
      font-size: 14px;
      color: #606266;
    }

    .ml-2 {
      margin-left: 8px;
    }
  }
}
.pagination-container.hidden {
  display: none;
}
</style>
