<template>
	<view class="img-list">
		<view class="img-navs">
			<view v-for="(item,index) in navList" :key="index" class="nav-btn"
				:class="{'check-nav':index===curNavIndex,'last-nav':index===navList.length-1}"
				@click="changeNav(index)">
				{{item.TagName||'其他'}}
			</view>
		</view>
		<view class="img-list-box">
			<view class="left">
				<view v-for="(item,index) in dataList.TagPicList" :key="item.ID" class="img-list-item"
					v-if="index%2==0">
					<view class="img-box" @click="previewImage(index,item.Path)" v-if="item.type==='image'">
						<image :src="item.Path" mode="widthFix"></image>
					</view>
					<view class="img-box" style="position: relative" v-if="item.type==='video'">
						<video id="myVideo" :src="item.Path" :controls='controls' :show-center-play-btn='false'
							:show-fullscreen-btn='false' @fullscreenchange='videoControl' object-fit='fill'></video>
						<!-- 播放按钮 -->
						<view @click="enlarge"
							style="position: absolute;left: 50%;top: 50%;transform: translate(-50%,-50%);width: 98rpx;height: 98rpx;border-radius: 20rpx;background-color: rgba(255,255,255,.4);display: flex;align-items: center;justify-content: center;">
							<image
								src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1616471847000_345.png"
								style="width: 37rpx;height: 44rpx;"></image>
						</view>
					</view>
				</view>
			</view>
			<view class="right">
				<view v-for="(item,index) in dataList.TagPicList" :key="item.ID" class="img-list-item"
					v-if="index%2==1">
					<view class="img-box" @click="previewImage(index,item.Path)" v-if="item.type==='image'">
						<image :src="item.Path" mode="widthFix"></image>
					</view>
					<view class="img-box" style="position: relative" v-if="item.type==='video'">
						<video id="myVideo" :src="item.Path" :controls='controls' :show-center-play-btn='false'
							:show-fullscreen-btn='false' @fullscreenchange='videoControl' object-fit='fill'></video>
						<!-- 播放按钮 -->
						<view @click="enlarge"
							style="position: absolute;left: 50%;top: 50%;transform: translate(-50%,-50%);width: 98rpx;height: 98rpx;border-radius: 20rpx;background-color: rgba(255,255,255,.4);display: flex;align-items: center;justify-content: center;">
							<image
								src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1616471847000_345.png"
								style="width: 37rpx;height: 44rpx;"></image>
						</view>
					</view>
				</view>
			</view>

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

<script>
	export default {
		data() {
			return {
				ID: 0,
				navList: [],
				curNavIndex: 0,
				dataList: [],
				controls: false, //显示默认控件
				videoContext: "",
				preImgList: []
			}
		},
		watch: {
			curNavIndex(val) {

			}
		},
		created() {
			uni.setNavigationBarTitle({
				title: "风景图片",
			});

		},
		onLoad(options) {
			if (options && options.id) {
				this.ID = options.id
				this.getDetail()
			}
		},
		methods: {
			//获取详情
			getDetail(index = 0) {
				this.request2({
						url: '/api/AppletDining/GetTicketCouponsDetails',
						data: {
							ID: this.ID
						}
					},
					res => {
						if (res.resultCode == 1) {
							this.navList = res.data.AllPicList;
							this.dataList = res.data.AllPicList[index]
							this.preImgList = []
							this.dataList.TagPicList.map(i => {
								if (i.Path) {
									this.preImgList.push(i.Path)
								}
							})
							this.dataList.TagPicList.forEach(item => {
								if (item.Path.indexOf('jpg') !== -1 || item.Path.indexOf('png') !== -1 || item
									.Path.indexOf('jpeg') !== -1) {
									item.type = 'image'
								} else if (item.Path.indexOf('mp4') !== -1) {
									item.type = 'video'
								}

							})
						}
					}
				);
			},
			changeNav(index) {
				this.curNavIndex = index
				this.dataList = this.navList[index]
				this.preImgList = []
				this.dataList.TagPicList.map(i => {
					if (i.Path) {
						this.preImgList.push(i.Path)
					}
				})
				this.dataList.TagPicList.forEach(item => {
					if (item.Path.indexOf('jpg') !== -1 || item.Path.indexOf('png') !== -1 || item.Path.indexOf(
							'jpeg') !== -1) {
						item.type = 'image'
					} else if (item.Path.indexOf('mp4') !== -1) {
						item.type = 'video'
					}

				})
			},
			//预览图片
			previewImage(i, url) {
				uni.previewImage({
					urls: this.preImgList,
					current: i,
					longPressActions: {
						itemList: ["发送给朋友", "保存图片", "收藏"],
						success: function(data) {
							console.log(data.errMsg);
						},
						fail: function(err) {
							console.log(err.errMsg);
						},
					},
				});
			},
			enlarge(file) {
				// 全屏
				this.videoContext = uni.createVideoContext('myVideo');
				this.videoContext.requestFullScreen({
					direction: 0
				})

			},
			videoControl(e) {
				if(e.detail.fullScreen===false){
					this.videoContext.stop()
					this.controls=false
				}else{
					this.videoContext.play()
					this.controls=true
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.img-list {
		.img-navs {
			display: flex;
			margin: 0 45rpx;
			padding: 20rpx 0;
			overflow-x: scroll;
			overflow-y: hidden;

			.nav-btn {
				// width: 230rpx;
				height: 60rpx;
				background-color: #FFFFFF;
				border: 1rpx solid #111111;
				border-radius: 20rpx;
				font-family: PingFang SC;
				font-weight: 500;
				text-align: center;
				line-height: 58rpx;
				padding: 0 20rpx;
				margin-right: 30rpx;
				white-space: nowrap;
			}

			.check-nav {
				background-color: #111111;
				color: #FFFFFF;
			}

			.last-nav {
				margin-right: 0 !important;
			}
		}

		.img-list-box {
			padding: 0 45rpx;
			display: flex;
			justify-content: space-between;

			.left {
				width: 315rpx;
			}

			.right {
				width: 315rpx;
			}

			.img-list-item {
				width: 315rpx;

				.img-box {
					width: 315rpx;
					box-shadow: 0rpx 10rpx 30rpx 0rpx rgba(36, 36, 36, 0.2);
					border-radius: 20rpx;
					overflow: hidden;
					margin-bottom: 20rpx;

					image {
						width: 100%;
					}

					#myVideo {
						width: 315rpx;
						height: 315rpx;
					}
				}
			}
		}
	}
</style>
