<template>
  <div :class="{'hidden':hidden}" class="pagination-container">
    <el-pagination
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="eachPage"
      :page-sizes="pageSizes"
      :layout="layout"
      :prev-text="prevText"
      :next-text="nextText"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"/>
  </div>
</template>

<script>
import { scrollTo } from '@/utils/scrollTo'

export default {
  name: 'Pagination',
  props: {
    total: {
      required: true,
      type: Number
    },
    pageindex: {
      type: Number,
      default: 1
    },
    pagesize: {
      type: Number,
      default: process.env.PAGESIZE
    },
    pageSizes: {
      type: Array,
      default() {
        // return process.env.PAGESIZES
        return JSON.parse(this.$store.getters.serverConfig.PAGESIZES)
      }
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    prevText: {
      type: String,
      default: '上一页'
    },
    nextText: {
      type: String,
      default: '下一页'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    // 当前第几页
    currentPage: {
      get() {
        return this.pageindex
      },
      set(val) {
        this.$emit('update:pageindex', val)
      }
    },
    // 每页几条数据
    eachPage: {
      get() {
        return this.pagesize
      },
      set(val) {
        this.$emit('update:pagesize', val)
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      this.$emit('pagination', { pageindex: this.currentPage, pagesize: val })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleCurrentChange(val) {
      this.$emit('pagination', { pageindex: val, pagesize: this.eachPage })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    }
  }
}
</script>

<style scoped>
	.pagination-container.hidden {
		display: none;
	}
</style>
