<template>
	<view>
		<view id="_drag_button" class="drag" :style="'left: ' + left + 'px; top:' + top + 'px;'"
			@touchstart="touchstart" @touchmove.stop.prevent="touchmove" @touchend="touchend"
			:class="{transition: isDock && !isMove }">
			<view class="drag-box" @click.stop.prevent="click(videoInfo[0])">
				<image :src="videoInfo[0].titleImg" mode="aspectFill"></image>
				<view class="drag-close flex align-center justify-center">
					<image @click.stop.prevent="clickClose" src="../../static/images/index/closeVideo.png" mode="">
					</image>
				</view>
				<view class="drag-playing">
					<image src="../../static/images/index/playVideoIcon.png" mode=""></image>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		name: 'drag-button',
		props: {
			isDock: {
				type: Boolean,
				default: false
			},
			existTabBar: {
				type: Boolean,
				default: false
			},
			videoInfo: {
				type: Array,
				default: []
			}
		},
		data() {
			return {
				top: 0,
				left: 0,
				width: 0,
				height: 0,
				offsetWidth: 0,
				offsetHeight: 0,
				windowWidth: 0,
				windowHeight: 0,
				isMove: true,
				edge: 16,
				text: '按钮'
			}
		},
		mounted() {
			const sys = uni.getSystemInfoSync();

			this.windowWidth = sys.windowWidth;
			this.windowHeight = sys.windowHeight;

			// #ifdef APP-PLUS
			this.existTabBar && (this.windowHeight -= 50);
			// #endif
			if (sys.windowTop) {
				this.windowHeight += sys.windowTop;
			}
			console.log(sys)

			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.left = this.edge;
				this.top = this.windowHeight - (2 * this.height) - this.edge;
			}).exec();
		},
		methods: {
			//关闭
			clickClose() {
				this.$emit('clickClose')
			},
			//点击封面
			click(item) {
				this.$emit('btnClick', item);
			},
			touchstart(e) {
				this.$emit('btnTouchstart');
			},
			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;
				// #ifdef H5
				// clientY += this.height;
				// #endif
				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
				}

			},
			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;
					}

				}

				this.isMove = false;

				this.$emit('btnTouchend');
			},
		}
	}
</script>

<style lang="scss">
	.drag {
		width: 150rpx;
		position: fixed;
		z-index: 888;

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

		.drag-box {
			width: 100%;
			position: relative;

			image {
				width: 100%;
				height: 200rpx;
				border-radius: 8rpx;
			}
		}


		.drag-close {
			// width: 100%;
			// margin-top: 10rpx;
			position: absolute;
			top: -10rpx;
			right: -10rpx;

			image {
				width: 30rpx;
				height: 30rpx;
				border-radius: 50%;
			}
		}

		.drag-playing {
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);

			image {
				width: 30rpx;
				height: 34rpx;
			}
		}
	}
</style>