<template>
  <div :class="{ hidden: hidden }" class="pagination">
    <el-pagination
      :background="background"
      :layout="layout"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handSizeChange"
      :page-sizes="pageSizes"
      :current-page.sync="currentPage"
      :page-size="getPageSize"
      v-bind="$attrs" 
    ></el-pagination>
  </div>
</template>
<script>
export default {
  props: {
    total: {
      required: true,
      type: Number,
      default: 0
    },
    pageNum: {
      type: Number,
      default: 1
    },
    pageSize: {
      type: Number,
      default: 5
    },
    pageSizes: {
      type: Array,
      default() {
        return [5, 10, 15, 20, 25];
      }
    },
    layout: {
      type: String,
      default: "prev, pager, next, sizes, total, jumper"
    },
    background: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {};
  },
  methods: {
    handleCurrentChange(val) {
      this.$emit("pagination", { pageNum: val, pageSize: this.pageSize });
    },
    handSizeChange(val) {
      // 每次改变pageSize之后需要从第一页发起请求，不然会存在数据不够的问题
      this.currentPage = 1;
      this.$emit("pagination", { pageNum: this.currentPage, pageSize: val });
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.pageNum;
      },
      set(val) {
        this.$emit("update:pageNum", val);
      }
    },
    getPageSize: {
      get() {
        return this.pageSize;
      },
      set(val) {
        this.$emit("update:pageSize", val);
      }
    }
  }
};
</script>
<style lang="stylus" scoped>
.pagination.hidden {
  display: none;
}
</style>