<template>
  <div class="pagination-wraper">
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      prev-text="上一页"
      next-text="下一页"
      :total="total"
      :page-sizes="pageSizes"
      :page-size.sync="pageSize"
      :current-page.sync="currentPage"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<script>

export default {
  props: {
    total: {
      required: true,
      type: Number,
      default: 0
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 10
    },
    pageSizes: {
      type: Array,
      default() {
        return [5, 10, 20, 30,40,50]
      }
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      this.$emit('pagination', { page: this.currentPage, size: val })
    },
    handleCurrentChange(val) {
      // 解决有时切换每页显示条数时，偶尔会出现无数据的情况
      setTimeout(() => {
        this.$emit('pagination', { page: val, size: this.limit })
      }, 0)
    }
  }
}
</script>
<style lang="scss" scoped>
.pagination-wraper {
  text-align: center;
}

</style>
