<template>
	<view>
		<view id="_drag_button" class="drag" :style="{top:top+'px',left:left+'px',opacity:firstIn?1:0}"
			@touchstart="touchstart" @touchmove.stop.prevent="touchmove" @touchend="touchend"
			@click.stop.prevent="click" :class="{transition: isDock && !isMove }">
			<button class="btn" style="border: none;padding: 0;margin: 0;">
				<image class="img" src="@/static/icon/cart.png" mode="widthFix"></image>
				<text>购物车</text>
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'drag-button',
		props: {
			isDock: {
				type: Boolean,
				default: false
			},
			customBar: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				top: 0,
				left: 0,
				width: 0,
				height: 0,
				offsetWidth: 0,
				offsetHeight: 0,
				windowWidth: 0,
				windowHeight: 0,
				isMove: true,
				edge: 10,
				firstIn: false,
				customBarHeight: this.CustomBar
			}
		},
		methods: {
			init() {
				// 获取手机信息配置接口
				const sys = uni.getSystemInfoSync();
				// 屏幕的宽高
				this.windowWidth = sys.windowWidth;
				this.windowHeight = sys.windowHeight;
				if (sys.windowTop) {
					this.windowHeight += sys.windowTop;
				}
				// 获取元素
				const query = uni.createSelectorQuery().in(this);
				query.select('#_drag_button').boundingClientRect(data => {
					this.width = data.width;
					this.height = data.height;
					this.offsetWidth = data.width / 2;
					this.offsetHeight = data.height / 2;
					// this.left = this.windowWidth - this.width - this.edge;
					// this.top = this.windowHeight - this.height - this.edge;
					
					// 若storage无值，设置初始值
					if(!uni.getStorageSync('top')) {
						uni.setStorageSync("top", this.windowHeight - this.height - this.edge - 100 - (this.customBar ? this.customBarHeight : 0));
					}
					if(!uni.getStorageSync('left')) {
						uni.setStorageSync("left", this.windowWidth - this.width - this.edge);
					}
					
					this.left = uni.getStorageSync('left');
					this.top = uni.getStorageSync('top') - (this.customBar ? this.customBarHeight : 0);
					this.$nextTick(() => {
						this.firstIn = true;
					})
				}).exec();
			},
			click() {
				uni.switchTab({
					url:'/pages/cart/index'
				})
			},
			touchstart(e) {
				
			},
			touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}
				this.isMove = true;

				this.left = e.touches[0].clientX - this.offsetWidth;

				let clientY = e.touches[0].clientY - this.offsetHeight;
				
				let edgeBottom = this.windowHeight - this.height - this.edge;

				// 上下触及边界
				if (clientY < this.edge) {
					this.top = this.edge;
				} else if (clientY > edgeBottom) {
					this.top = edgeBottom;
				} else {
					this.top = clientY
				}
				uni.setStorageSync("top", this.top + (this.customBar ? this.customBarHeight : 0));
			},
			touchend(e) {
				if (this.isDock) {
					let edgeRigth = this.windowWidth - this.width - this.edge;

					if (this.left < this.windowWidth / 2 - this.offsetWidth) {
						this.left = this.edge;
					} else {
						this.left = edgeRigth;
					}
				}
				uni.setStorageSync("left", this.left);
				this.isMove = false;
			},
		}
	}
</script>

<style lang="scss">
	.drag {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		font-size: $uni-font-size-sm;
		position: fixed;
		z-index: 999999;
		background: #FFA162;
		border: 4rpx solid #FE781F;

		&.transition {
			transition: left .3s ease, top .3s ease;
		}
	}

	.btn {
		background-color: transparent;
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		z-index: 9999;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		.img {
			width: 36rpx;
			display: block;
		}
		text {
			font-size: 22rpx;
			line-height: 24rpx;
			color: #FFFFFF;
			margin-top: 8rpx;
		}
	}

	button::after {
		border: none;
	}

</style>

