<template>
  <div
    :class="{ hidden: hidden }"
    class="pagination-container"
    :id="refresh ? 'paddingRight' : ''"
  >
    <div :class="size">
      <el-pagination
        :background="background"
        :current-page.sync="currentPage"
        :page-size.sync="limit"
        :layout="layout"
        :page-sizes="pageSizes"
        :pager-count="pageCount"
        :total="total"
        v-bind="$attrs"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
      <i
        class="el-icon-refresh"
        @click="handleRefresh"
        v-if="refresh"
        :id="layout.indexOf('jump') != -1 ? 'refresh_jump' : 'refresh'"
      ></i>
    </div>
  </div>
</template>
<script>
export default {
  name: "pagination",
  data() {
    return {};
  },
  props: {
    total: {
      required: true,
      type: Number,
    },
    pageSize: {
      type: Number,
      default: 20,
    },
    page: {
      type: Number,
      default: 1,
    },
    refresh: {
      type: Boolean,
    },
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 30, 50];
      },
    },
    size: {
      type: String,
      default: "middle",
    },
    layout: {
      type: String,
      default: "total, sizes, prev, pager, next, jumper",
    },
    background: {
      type: Boolean,
      default: true,
    },
    autoScroll: {
      type: Boolean,
      default: true,
    },
    hidden: {
      type: Boolean,
      default: false,
    },
    pageCount: {
      type: Number,
      default: 7,
    },
  },
  computed: {
    currentPage: {
      get() {
        return this.page;
      },
      set(val) {
        this.$emit("update:page", val);
      },
    },
    limit: {
      get() {
        return this.pageSize;
      },
      set(val) {
        this.$emit("update:pageSize", val);
      },
    },
  },
  methods: {
    handleSizeChange(val) {
      this.$emit("pagination", { page: this.currentPage, pageSize: val });
      if (this.autoScroll) {
        scrollTo(0, 0);
      }
    },
    handleCurrentChange(val) {
      this.$emit("pagination", { page: val, pageSize: this.limit });
      if (this.autoScroll) {
        scrollTo(0, 0);
      }
    },
    handleRefresh() {
      this.$emit("pagination", {
        page: this.currentPage,
        pageSize: this.limit,
      });
      if (this.autoScroll) {
        scrollTo(0, 0);
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.middle {
  padding: 10px;
  /deep/.el-pagination {
    text-align: right;
    padding-right: 20px;
  }
  /deep/.el-input--medium .el-input__inner {
    height: 28px !important;
    font-size: 14px;
    border-radius: 2px;
  }
  #paddingRight {
    /deep/.btn-next {
      margin-right: 24px;
    }
  }
  .pagination-container {
    position: relative;
    margin: 20px 0;
  }
  #refresh_jump {
    position: absolute;
    top: 8px;
    right: 118px;
    font-size: 20px;
    color: #606266;
  }
  #refresh {
    position: absolute;
    top: 8px;
    right: 30px;
    font-size: 20px;
    color: #606266;
  }
}
.small {
  padding: 10px;
  /deep/.el-pagination button,
  .el-pagination span {
    display: inline-block;
    font-size: 12px;
    min-width: 25px;
    height: 24px;
    line-height: 24px;
    vertical-align: top;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
  /deep/.el-input--medium .el-input__inner {
    height: 24px !important;
    font-size: 12px;
    border-radius: 2px;
  }
  #paddingRight {
    /deep/.btn-next {
      margin-right: 24px;
    }
  }
  .pagination-container {
    position: relative;
    margin: 15px 0;
  }
  #refresh_jump {
    position: absolute;
    top: 8px;
    right: 118px;
    font-size: 20px;
    color: #606266;
  }
  #refresh {
    position: absolute;
    top: 8px;
    right: 30px;
    font-size: 20px;
    color: #606266;
  }
  .el-pagination .el-select .el-input {
    width: 83px;
    margin: 0 -2px;
  }
  /deep/.el-input--mini .el-input__inner {
    height: 24px;
    line-height: 24px;
  }
  /deep/.el-pagination.is-background .btn-next,
  .el-pagination.is-background .btn-prev,
  .el-pagination.is-background .el-pager li {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    min-width: 25px;
    border-radius: 2px;
  }
  /deep/.el-pager li {
    padding: 0 4px;
    font-size: 12px;
    min-width: 35.5px;
    height: 24px;
    min-width: 25px;
    line-height: 24px;
    box-sizing: border-box;
    text-align: center;
  }
  .el-pagination button,
  .el-pagination span:not([class*="suffix"]) {
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    vertical-align: top;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }

  /deep/.el-pagination__editor {
    line-height: 24px;
    padding: 0 2px;
    height: 24px;
    text-align: center;
    margin: 0 2px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    border-radius: 3px;
  }
  /deep/.el-pagination__editor.el-input .el-input__inner {
    height: 24px;
  }
  /deep/.el-pagination__editor.el-input {
    width: 40px;
  }
  /deep/.el-pagination .el-select .el-input {
    width: 80px;
    margin: 0 5px;
  }
  /deep/.el-pagination__sizes .el-input .el-input__inner {
    font-size: 12px;
    padding-left: 8px;
  }
}
</style>
