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

<script setup lang="ts">
import { computed, PropType } from 'vue';
import { scrollTo } from '@/utils/scroll-to';

const props = defineProps({
  total: {
    required: true,
    type: Number as PropType<number>,
    default: 0
  },
  pageNumTo: {
    type: Number,
    default: 1
  },
  pageSizeTo: {
    type: Number,
    default: 10
  },
  pageSizes: {
    type: Array as PropType<number[]>,
    default() {
      return [10, 20, 30, 50];
    }
  },
  layout: {
    type: String,
    default: 'total, sizes, prev, pager, next, jumper'
  },
  autoScroll: {
    type: Boolean,
    default: true
  },
});

const emit = defineEmits(['update:pageNumTo', 'update:pageSizeTo', 'pagination']);

const currentPage = computed<number | undefined>({
  get: () => props.pageNumTo,
  set: value => {
    emit('update:pageNumTo', value);
  }
});

const pageSize = computed<number | undefined>({
  get() {
    return props.pageSizeTo;
  },
  set(val) {
    emit('update:pageSizeTo', val);
  }
});

function handleSizeChange(val: number) {
  emit('pagination', { pageNumTo: currentPage, pageSizeTo: val });
  if (props.autoScroll) {
    scrollTo(0, 800);
  }
}

function handleCurrentChange(val: number) {
  currentPage.value = val;
  emit('pagination', { pageNumTo: val, pageSizeTo: props.pageSizeTo });
  if (props.autoScroll) {
    scrollTo(0, 800);
  }
}
</script>

<style scoped lang="scss">
.pagination-container {
  background: #fff;
  padding: 10px 4px;
  width: 100%;
  overflow: hidden;
  :deep(.el-pagination){
    float: right;
    .btn-prev,.btn-next{
      background: #FFFFFF;
      border: 1px solid #DCDEE0;
    }
    .el-pager{
      .number{
        background: #FFFFFF;
        border: 1px solid #DCDEE0;
        &.is-active{
          background-color: var(--el-color-primary);
          color: var(--el-color-white);
          border-color: var(--el-color-primary)
        }
      }
    }
  }
}

</style>
