<template>
  <div class="ui-pagination">
    <div class="ui-pagination__bar">
      <div class="ui-pagination__switch">
        <span
          class="ui-pagination__switch-prev"
          :class="{'is-disabled': disabledPrevPage}"
          title="前一页"
          @click="handlePrevPageClick"
        ></span>
        <span
          class="ui-pagination__switch-next"
          :class="{'is-disabled': disabledNextPage}"
          title="后一页"
          @click="handleNextPageClick"
        ></span>
      </div>
      <ui-popover
        class="ui-pagination__page"
        v-model="showPagePopover"
        @input="handlePopoverVisibleChange"
      >
        <div
          class="ui-pagination__page-handle"
          slot="reference"
        >{{internalCurrentPage}}/{{internalPageCount}} 页</div>
        <div class="ui-pagination__popover" v-if="showPagePopover">
          <div class="ui-pagination__popover-size">
            <div class="ui-pagination__size">
              <span class="ui-pagination__size-label">每页显示</span>
              <div class="ui-pagination__size-target">
                <ui-tabbar
                  v-model="showPageSize"
                  :items="pageSizeData"
                  label-field="text"
                  name-field="value"
                  @input="handlePageSizeChange"
                ></ui-tabbar>
              </div>
            </div>
          </div>
          <div class="ui-pagination__popover-jump">
            <div class="ui-pagination__jump">
              <span class="ui-pagination__jump-label">前往页</span>
              <div class="ui-pagination__jump-target">
                <div class="ui-pagination__jump-input">
                  <ui-spinner
                    size="mini"
                    type="simple"
                    v-model="inputCurrentPage"
                    :max="internalPageCount"
                    :min="1"
                  ></ui-spinner>
                </div>
                <div class="ui-pagination__jump-confirm">
                  <ui-button
                    size="mini"
                    type="primary"
                    @click="handleConfirmPageClick"
                  ><span class="ui-pagination__jump-confirm-icon"></span></ui-button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </ui-popover>
      <div class="ui-pagination__total">共 {{total}} 个{{title}}</div>
      <slot name="addon"></slot>
    </div>
    <el-pagination
      class="ui-pagination__control"
      ref="control"
      layout="total, sizes, ->, prev, pager, next, jumper"
      :page-sizes="pageSizeList"
      :page-size="innerPageSize"
      :current-page="pageIndex"
      :total="total"
      @current-change="handlePageIndexChange"
      @size-change="handlePageSizeChange"
    ></el-pagination>
  </div>
</template>
<script>
import Pagination from 'element-ui/lib/pagination'
import 'element-ui/lib/theme-chalk/pagination.css'
import UiSpinner from '@huntianning/components/packages/spinner'
import UiTabbar from '@huntianning/components/packages/tabbar'
import UiPopover from '@huntianning/components/packages/popover'

export default {
  name: 'UiPagination',
  components: {
    UiTabbar,
    UiSpinner,
    UiPopover,
    ElPagination: Pagination
  },
  props: {
    title: {
      type: String
    },
    pageSize: {
      type: Number,
      default: 30
    },
    pageIndex: {
      type: Number,
      default: 0
    },
    total: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      showPagePopover: false,
      innerPageSize: this.pageSize,
      inputCurrentPage: null,
      internalPageCount: null,
      internalCurrentPage: null
    }
  },
  computed: {
    showPageSize: {
      get() {
        return this.innerPageSize + ''
      },
      set(val) {
        this.innerPageSize = Number(val)
      }
    },
    disabledPrevPage() {
      return this.internalCurrentPage <= 1
    },
    disabledNextPage() {
      return this.internalCurrentPage >= this.internalPageCount
    },
    pageSizeList() {
      return [30, 50, 100]
    },
    pageSizeData() {
      return this.pageSizeList.map(v => {
        return {
          text: v + '',
          value: v + ''
        }
      })
    }
  },
  mounted() {
    this.internalPageCount = this.$refs.control.internalPageCount
    this.internalCurrentPage = this.$refs.control.internalCurrentPage
    this.$refs.control.$watch('internalPageCount', (val) => {
      this.internalPageCount = val
    })
    this.$refs.control.$watch('internalCurrentPage', (val) => {
      this.internalCurrentPage = val
    })
  },
  methods: {
    handlePrevPageClick() {
      if (this.disabledPrevPage) {
        return
      }
      const nextPage = Math.max(this.pageIndex - 1, 1)
      this.$emit('page-change', {
        pageIndex: nextPage,
        pageSize: this.pageSize
      })
    },
    handleNextPageClick() {
      if (this.disabledNextPage) {
        return
      }
      const nextPage = Math.min(this.pageIndex + 1, this.internalPageCount)
      this.$emit('page-change', {
        pageIndex: nextPage,
        pageSize: this.pageSize
      })
    },
    handleConfirmPageClick() {
      this.$emit('page-change', {
        pageIndex: this.inputCurrentPage,
        pageSize: this.pageSize
      })
      this.showPagePopover = false
    },
    handlePopoverVisibleChange(visible) {
      if (visible) {
        this.inputCurrentPage = this.internalCurrentPage
      }
    },
    // 每页显示行数发生变化触发
    handlePageSizeChange(value) {
      value = Number(value)
      if (this.pageSize === value) {
        return
      }
      this.innerPageSize = value
      const pageIndex = Math.max(Math.min(
        Math.ceil(this.total / value),
        this.pageIndex
      ), 1)
      this.$emit('page-change', {
        pageIndex,
        pageSize: value
      })
    },
    // 分页改变时触发
    handlePageIndexChange(value) {
      const pageIndex = value
      if (this.pageIndex === pageIndex) {
        return
      }
      this.$emit('page-change', {
        pageIndex,
        pageSize: this.innerPageSize
      })
    }
  }
}
</script>
<style lang="less">
.ui-pagination{
  text-align: left;
  font-size: 14px;
}
.ui-pagination__popover {
  width: 184px;
  background: #FFFFFF;
  box-shadow: 0px 0px 4px 2px rgba(211,211,211,0.5000);
  border-radius: 4px;
  font-size: 12px;
}
.ui-pagination__popover-size{
  padding: 6px 12px;
  border-bottom: 1px solid #D5D8DC;
  .ui-tabbar {
    width: 100%;
  }
}
.ui-pagination__popover-jump{
  padding: 6px 12px;
}
.ui-pagination__size,
.ui-pagination__jump {
  display: flex;
  flex-direction: row;
  align-items: center;
}
.ui-pagination__size-label,
.ui-pagination__jump-label{
  white-space: nowrap;
  width: 60px;
  min-width: 60px;
}
.ui-pagination__jump-label{
  color: #606C80;
}
.ui-pagination__size-target,
.ui-pagination__jump-target{
  display: flex;
  flex-direction: row;
  flex: 1;
  min-width: 0;
}
.ui-pagination__jump-input{
  margin-right: 6px;
}
.ui-pagination__bar{
  display: flex;
  flex-direction: row;
  align-items: center;
}
.ui-pagination__switch{
  display: flex;
  flex-direction: row;
}
.ui-pagination__switch-prev,
.ui-pagination__switch-next{
  height: 26px;
  width: 26px;
  border: 1px solid #E6E7EA;
  cursor: pointer;
  &:not(.is-disabled):hover {
    background-color: #f3f3f3;
  }
  &.is-disabled {
    cursor: not-allowed;
    opacity: .5;
  }
}
.ui-pagination__switch-prev{
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  &:after {
    content: '';
    display: inline-block;
    width: 6px;
    height: 6px;
    border: 1px solid #999;
    transform: rotate(-45deg);
    border-bottom: 0;
    border-right: 0;
    vertical-align: top;
    margin-left: 11px;
    margin-top: 9px;
  }
}
.ui-pagination__switch-next{
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  margin-left: -1px;
  &:after {
    content: '';
    display: inline-block;
    width: 6px;
    height: 6px;
    border: 1px solid #999;
    transform: rotate(135deg);
    border-bottom: 0;
    border-right: 0;
    vertical-align: top;
    margin-left: 8px;
    margin-top: 9px;
  }
}
.ui-pagination__page{
  display: inline-block;
  margin-left: 6px;
}
.ui-pagination__page-handle{
  height: 28px;
  line-height: 28px;
  border: 4px;
  padding: 0 12px;
  cursor: pointer;
  transition: all .25s;
  border-radius: 4px;
  &:after {
    content: '';
    display: inline-block;
    width: 0;
    height: 0;
    overflow: hidden;
    border: 4px solid #999;
    border-left-color: transparent;
    border-right-color: transparent;
    border-bottom: 0;
    vertical-align: top;
    margin-top: 13px;
    margin-left: 4px;
  }
  &:hover {
    background-color: #f3f3f3;
  }
}
.ui-pagination__total{
  position: relative;
  padding-left: 12px;
  height: 28px;
  line-height: 28px;
  margin-left: 6px;
  &:before {
    content: '';
    height: 14px;
    width: 1px;
    background-color: #D5D8DC;
    top: 50%;
    left: 0;
    position: absolute;
    transform: translateY(-50%);
  }
}
.ui-pagination__control{
  display: none!important;
}
.ui-pagination__jump-confirm{
  .ui-button{
    padding-left: 9px;
    padding-right: 9px;
  }
}
.ui-pagination__jump-confirm-icon{
  display: inline-block;
  height: 10px;
  width: 12px;
  background: url('./icons/arrow.svg') no-repeat 50% 50%;
  background-size: 100%;
}
</style>
