/*  分页组件
 */
<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { scrollTo } from "@/utils/scroll-to";

export default {
  name: "ylPagination",
  props: {
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 20
    },
    pageSizes: {
      type: Array,
      default() {
        return [10, 15, 20, 30, 40, 50];
      }
    },
    layout: {
      type: String,
      default: "total, prev, pager, next,sizes,jumper"
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    }
  },
  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, limit: val });
      if (this.autoScroll) {
        scrollTo(0, 800);
      }
    },
    handleCurrentChange(val) {
      this.$emit("pagination", { page: val, limit: this.pageSize });
      if (this.autoScroll) {
        scrollTo(0, 800);
      }
    }
  }
};
</script>

<style scoped type="scss">
.pagination-container {
  padding: 20px 0;
  margin: 0 auto; /* 居中 这个是必须的，，其它的属性非必须 */
  width: 100%; /* 给个宽度 顶到浏览器的两边就看不出居中效果了 */
  text-align: center; /* 文字等内容居中 */
}
.pagination-container.hidden {
  display: none;
}
::v-deep .el-pagination .el-pager li {
  color: #505050;
  background-color: #fff;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
}
::v-deep .el-pagination__jump {
  margin-left: 0px;
}

::v-deep .el-pagination.is-background .btn-prev,
::v-deep .el-pagination.is-background .btn-next {
  background-color: #fff;
  color: #c0c0c0;
}
::v-deep .el-icon-arrow-left:before {
  content: "\e792";
  font-size: 14px;
}
::v-deep .el-icon-arrow-right:before {
  content: "\e791";
  font-size: 14px;
}
</style>
