<template>
	<view>
		<view class="list">
			<view class="item" v-for="(item,index) in list" :key="index" @click="go_video(item)">
				<view class="img">
					<image v-if="item.cover_picture" :src="item.cover_picture" mode="aspectFill"></image>
					<image v-else src="./icon/default.jpg" mode="aspectFill"></image>
					<view class="tip" v-if="item.camera_status == 1">在线</view>
					<view class="tip" v-if="item.camera_status == 2" style="background-color: #f30220;">离线</view>
					<view class="tip" v-if="item.camera_status == 3" style="background-color: #f30220;">异常</view>
				</view>
				<view class="name">
					<view class="text">{{item.camera_name}}</view>
					<!-- <view class="icon" @click.stop="popup()"> -->
					<view class="icon" @click.stop="go_playback()">
						<image src="./icon/icon.png" mode=""></image>
					</view>
				</view>
			</view>
		</view>
		<view>
			<uni-popup ref="popup" type="center">

			</uni-popup>
		</view>
		<view class="loading">
			<uni-popup ref="popup_loading" :mask-click="false" type="center">
				<image src="./icon/loading.gif" mode="aspectFill"></image>
				<view class="box">
					<view class="line" :style="'width:' + (loading_text*19) + '%'"></view>
					<view class="text" v-if="loading_text == 1">发送请求中...</view>
					<view class="text" v-if="loading_text == 2">获取视频流...</view>
					<view class="text" v-if="loading_text == 3">视频流切片中...</view>
					<view class="text" v-if="loading_text == 4">网络传输中...</view>
					<view class="text" v-if="loading_text == 5">即将播放...</view>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				mine_code: "",
				list: [],
				loading_text: 1,

				base_url: "",
				// 视频流方式
				url: "",
				
				mine_id:"",
				parent_id:""
			};
		},
		onLoad(option) {
			console.log(option)

			uni.setNavigationBarTitle({
				title: option.name
			})

			this.mine_code = uni.getStorageSync('mine_code')

			// 内外网
			if (this.mine_code.indexOf('_neiwang') == -1) {
				this.base_url = "http://video.nxjiewei.com:8011/api"
			} else {
				this.base_url = "http://n.video.nxjiewei.com:8011/api"
			}

			if (this.mine_code.indexOf('jinjiaqu') != -1) {
				this.url = "/camera/getrtsp"
			} else {
				this.url = "/camera/get_hik_rtsp"
			}

			this.mine_id = option.mine_id
			this.parent_id = option.parent_id
		},
		onShow() {
			this.get_camera()
		},
		methods: {
			get_camera() {
				uni.request({
					method: "POST",
					header: {
						"Content-Type": "multipart/form-data",
						'Content-Type': 'application/json;charset=UTF-8',
						'Content-Type': 'application/x-www-form-urlencoded',
						"Access-Control-Max-Age": "86400",
						"Authorization": uni.getStorageSync('video_type') + ' ' + uni.getStorageSync('video_token')
					},
					url: this.base_url + "/camera/getlist?mine_id=" + this.mine_id + "&parent_id=" + this.parent_id,
					// 内网
					// url: "http://n.video.nxjiewei.com:8011/api/camera/getlist?mine_id=" + mine_id + "&parent_id=" + parent_id,
					success: res => {
						console.log(res.data.content.data)
						this.list = res.data.content.data
					}
				})
			},
			go_video(item) {
				this.$refs.popup_loading.open()

				let loading_inter = setInterval(() => {
					console.log(this.loading_text)
					if (this.loading_text == 5) {
						this.loading_text = 5
					} else {
						this.loading_text++
					}
				}, 1600)

				uni.request({
					method: "POST",
					url: this.base_url + this.url,
					header: {
						"Content-Type": "multipart/form-data",
						'Content-Type': 'application/json;charset=UTF-8',
						'Content-Type': 'application/x-www-form-urlencoded',
						"Authorization": uni.getStorageSync('video_type') + ' ' + uni.getStorageSync('video_token')
					},
					data: {
						camera_id: item.camera_id,
						parent_id: item.parent_id
					},
					success: res => {
						// uni.hideLoading()
						clearInterval(loading_inter)
						this.loading_text = 1
						this.$refs.popup_loading.close()
						console.log(res)

						if (res.data.content.data) {
							let url = res.data.content.data.url

							console.log(url, item.camera_name)

							uni.navigateTo({
								url: "../video/video",
								success: (res) => {
									// 通过eventChannel向被打开页面传送数据
									res.eventChannel.emit('acceptDataFromOpenerPage', {
										url: url,
										title: item.camera_name
									})
								}
							})
						} else {
							clearInterval(loading_inter)
							this.loading_text = 1
							this.$refs.popup_loading.close()
							uni.showToast({
								icon: "none",
								title: "取流失败！"
							})
						}
					},
					fail() {
						clearInterval(loading_inter)
						this.loading_text = 1
						this.$refs.popup_loading.close()
						uni.showToast({
							icon: "none",
							title: "取流失败！"
						})
					}
				})

			},
			popup() {
				// this.$refs.popup.open()
			},
			
			go_playback(){
				uni.navigateTo({
					url:"../playback/playback"
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F2FAF7;
		box-sizing: border-box;
		padding: 20rpx 15rpx;
	}

	.list {
		.item {
			background-color: #FFFFFF;
			width: 350rpx;
			box-sizing: border-box;
			padding: 15rpx;
			margin-right: 20rpx;
			margin-bottom: 20rpx;

			float: left;

			.img {
				position: relative;

				.tip {
					position: absolute;
					top: 0;
					right: 0;
					background-color: #00c800;
					padding: 2rpx 8rpx;

					font-size: 24rpx;
					color: #FFFFFF;
				}

				image {
					width: 320rpx;
					height: 220rpx;
				}
			}

			.name {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.text {
					width: 240rpx;
					font-size: 28rpx;

					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;

				}

				.icon {
					image {
						width: 76rpx;
						height: 24rpx;
					}
				}
			}
		}

		.item:nth-child(2n) {
			margin-right: 0;
		}
	}

	.loading {
		width: 400rpx;
		text-align: center;

		image {
			width: 228rpx;
			height: 228rpx;
		}

		.box {
			position: relative;
			margin-top: 40rpx;
			width: 400rpx;
			height: 50rpx;
			border-radius: 50rpx;
			border: 1rpx solid #FFFFFF;
			overflow: hidden;

			.line {
				z-index: -999;
				position: absolute;
				top: 0;
				left: 0;
				height: 50rpx;
				width: 10rpx;
				background-color: #00A1E9;
			}

			.text {
				z-index: 999;
				line-height: 50rpx;
				font-size: 28rpx;
				-webkit-text-stroke: 1rpx #fff;
			}
		}
	}
</style>
