<!-- 
// based on Pagination 分页器组件： https://ext.dcloud.net.cn/plugin?id=32
// 我修改过的版本，使用本地图标文件，不用 uni-icons。
-->
<script>
	export default {
		props: {
			leftText: {
				type: String,
				default: 'Prev'
			},
			rightText: {
				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: {
			clickLeftEnd(){
				this.currentIndex = 1
				this.change('LEFT_END')
			},
			clickLeft() {
				if (Number(this.currentIndex) === 1) {
					return
				}
				this.currentIndex -= 1
				this.change('LEFT')
			},
			clickRight() {
				if (Number(this.currentIndex) === this.maxPage) {
					return
				}
				this.currentIndex += 1
				this.change('RIGHT')
			},
			clickRightEnd() {
				if (Number(this.currentIndex) === this.maxPage) {
					return
				}
				this.currentIndex = this.maxPage
				this.change('RIGHT_END')
			},
			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="clickLeftEnd"> <!-- always enabled so as to refresh -->
				<template v-if="showIcon===true || showIcon === 'true'">
					<view class="sLeftEndButton"></view>
				</template>
				<template v-else><text class="uni-pagination__child-btn">{{ leftEndText }}</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="sLeftButton"></view>
				</template>
				<template v-else><text class="uni-pagination__child-btn">{{ leftText }}</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="sRightButton"></view>
				</template>
				<template v-else><text class="uni-pagination__child-btn">{{ rightText }}</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="clickRightEnd">
				<template v-if="showIcon===true || showIcon === 'true'">
					<view class="sRightEndButton"></view>
				</template>
				<template v-else><text class="uni-pagination__child-btn">{{ rightEndText }}</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;
	}

	.sLeftEndButton, .sLeftButton, .sRightButton, .sRightEndButton{
		width: 20px;
		height: 20px;
		background: no-repeat center center / contain;
	}
	.sLeftEndButton{
		background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+PHN2ZyB0PSIxNTgyNTk4ODQwNzk3IiBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHAtaWQ9IjUxMTciIHhtbG5zOnhsaW5rPSJodHRwOi8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