<!-- 
// based on Pagination 分页器组件： https://ext.dcloud.net.cn/plugin?id=32
-->
<script>
	export default {
		name: 'cPager',
		props: {
			prevText: {
				type: String,
				default: 'Prev'
			},
			nextText: {
				type: String,
				default: 'Next'
			},
			current: {
				type: [Number, String],
				default: 1
			},
			total: { // 数据总量
				type: [Number, String],
				default: 0
			},
			pageSize: { // 每页数据量
				type: [Number, String],
				default: 10
			},
			showIcon: { // 是否以 icon 形式展示按钮
				type: [Boolean, String],
				default: true
			},
			showEdge: {
				type: [Boolean, String],
				default: false
			}
		},
		data() {
			return {
				currentIndex: 1
			}
		},
		computed: {
			maxPage() {
				let maxPage = 1
				let total = Number(this.total)
				let pageSize = Number(this.pageSize)
				if (total && pageSize) {
					maxPage = Math.ceil(total / pageSize)
				}
				return maxPage
			}
		},
		watch: {
			current(val) {
				this.currentIndex = +val
			}
		},
		created() {
			this.currentIndex = +this.current
		},
		methods: {
			clickLeftMost(){
				this.currentIndex = 1
				this.change('leftMost')
			},
			clickLeft() {
				if (Number(this.currentIndex) === 1) {
					return
				}
				this.currentIndex -= 1
				this.change('prev')
			},
			clickRight() {
				if (Number(this.currentIndex) === this.maxPage) {
					return
				}
				this.currentIndex += 1
				this.change('next')
			},
			clickRightMost() {
				if (Number(this.currentIndex) === this.maxPage) {
					return
				}
				this.currentIndex = this.maxPage
				this.change('rightMost')
			},
			change(e) {
				this.$emit('change', {
					type: e,
					current: this.currentIndex
				})
			}
		}
	}
</script>

<template>
	<view class="uni-pagination">

		<view class="uni-pagination__btns">
			<view v-if="(showEdge===true || showEdge==='true')" class="uni-pagination__btn" @click="clickLeftMost"> <!-- always enabled so as to refresh -->
				<template v-if="showIcon===true || showIcon === 'true'">
					<view class="sLeftMostButton"></view>
				</template>
				<template v-else><text class="uni-pagination__child-btn">{{ leftMostText }}</text></template>
			</view>
			<view class="uni-pagination__btn" :class="currentIndex === 1 ? 'uni-pagination--disabled' : 'uni-pagination--enabled'"
			 :hover-class="currentIndex === 1 ? '' : 'uni-pagination--hover'" :hover-start-time="20" :hover-stay-time="70"
			 @click="clickLeft">
				<template v-if="showIcon===true || showIcon === 'true'">
					<view class="sPrevButton"></view>
				</template>
				<template v-else><text class="uni-pagination__child-btn">{{ prevText }}</text></template>
			</view>
			<view class="uni-pagination__btn" :class="currentIndex === maxPage ? 'uni-pagination--disabled' : 'uni-pagination--enabled'"
			 :hover-class="currentIndex === maxPage ? '' : 'uni-pagination--hover'" :hover-start-time="20" :hover-stay-time="70"
			 @click="clickRight">
				<template v-if="showIcon===true || showIcon === 'true'">
					<view class="sNextButton"></view>
				</template>
				<template v-else><text class="uni-pagination__child-btn">{{ nextText }}</text></template>
			</view>
		  <view v-if="(showEdge===true || showEdge==='true')" class="uni-pagination__btn" :class="currentIndex === maxPage ? 'uni-pagination--disabled' : 'uni-pagination--enabled'"
			 :hover-class="currentIndex === maxPage ? '' : 'uni-pagination--hover'" :hover-start-time="20" :hover-stay-time="70"
			 @click="clickRightMost">
				<template v-if="showIcon===true || showIcon === 'true'">
					<view class="sRightMostButton"></view>
				</template>
				<template v-else><text class="uni-pagination__child-btn">{{ rightMostText }}</text></template>
			</view>
		</view>

		<view class="uni-pagination__num">
			<view class="uni-pagination__num-current">
				<text class="uni-pagination__num-current-text" style="color:#007aff">{{ currentIndex }}</text><text class="uni-pagination__num-current-text">/{{ maxPage || 0 }}</text>
			</view>
		</view>
	</view>
</template>

<style lang="scss" scoped>
	.uni-pagination {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		/* #ifdef APP-NVUE */
		padding: 0 20px;
		/* #endif */
		width: 400px;
		position: relative;
		overflow: hidden;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}

	.uni-pagination__btns {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex: 1;
		justify-content: space-between;
		align-items: center;
		flex-direction: row;
	}

	.uni-pagination__btn {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		width: 60px;
		height: 30px;
		line-height: 30px;
		font-size: $uni-font-size-base;
		position: relative;
//		background-color: $uni-bg-color-grey;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		text-align: center;
	}

	.uni-pagination__child-btn {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		font-size: $uni-font-size-base;
		position: relative;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		text-align: center;
	}

	.uni-pagination__num {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		position: absolute;
		margin: 0 auto;
		top: 0;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		width: 50px;
		height: 30px;
		line-height: 30px;
		font-size: $uni-font-size-base;
		color: $uni-text-color;
	}

	.uni-pagination__num-current {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
	}

	.uni-pagination__num-current-text {
		font-size: 15px;
	}

	.uni-pagination--enabled {
		color: #333333;
		opacity: 1;
	}

	.uni-pagination--disabled {
		opacity: 0.3;
	}

	.uni-pagination--hover {
		color: rgba(0, 0, 0, .6);
		background-color: $uni-bg-color-hover;
	}

	.sLeftMostButton, .sPrevButton, .sNextButton, .sRightMostButton{
		width: 20px;
		height: 20px;
		background: no-repeat center center / contain;
	}
	.sLeftMostButton{
		background-image: url(./cPager.leftMost.png)
	}
	.sPrevButton{
		background-image: url(./cPager.previous.png)
	}
	.sNextButton{
		background-image: url(./cPager.next.png)
	}
	.sRightMostButton{
		background-image: url(./cPager.rightMost.png)
	}
</style>
