<template>
  <div class="c-pagination l-flex__none">
    <el-pagination
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :page-sizes="pageSizes"
      :pager-count="5"
      :total="Number(total)"
      :layout="layout"
      :background="background"
      hide-on-single-page
      v-bind="$attrs"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
    />
  </div>
</template>

<script>
export default {
  name: 'Pagination',
  props: {
    total: {
      required: true,
      type: [Number, String]
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 20
    },
    pageSizes: {
      type: Array,
      default () {
        return [10, 20, 50]
      }
    },
    layout: {
      type: String,
      // sizes
      default: 'total, prev, pager, next, jumper'
    },
    background: {
      type: [Boolean, String],
      default: true
    }
  },
  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: {
    onSizeChange (val) {
      this.$emit('pagination', { page: this.currentPage, limit: val })
    },
    onCurrentChange (val) {
      this.$emit('pagination', { page: val, limit: this.pageSize })
    }
  }
}
</script>

<style lang="scss" scoped>
.c-pagination {
  padding-top: $spacing;
  text-align: center;

  &:empty {
    display: none;
  }

  ::v-deep .el-pagination.is-background .el-pager li:not(.disabled).active {
    background-color: $blue;
  }
}
</style>
