<template>
	<view class="container">
		<view class="search-box">
			<uni-search-bar ref="searchBar" radius="5" placeholder="搜索" clearButton="auto" cancelButton="none" bgColor="#F8F8F8"
			 @confirm="search" class="search-bar" />
		</view>
		<scroll-view class="video-box" scroll-y="true" @scrolltolower="handleLoadMore()">
			<view style="padding-top: 20px;background-color: #FFFFFF;" v-if="!videoList.length">
				<u-divider color="#909399" border-color="#909399">没有更多了</u-divider>
			</view>
			<view class="video-col" v-for="(item, index) in videoList" :key="index" @click="goToVideoDetail(item)">
				<image class="video-img" :src="videoType == 1 ? item.imgUrl : item.coverUrl" mode="aspectFill"></image>
				<view class="video-title">{{videoType == 1 ? item.liveName : item.videoName}}</view>
				<view class="video-date" v-if="videoType == 1">直播时间：{{item.liveStartTime}}-{{item.liveEndTime}}</view>
				<view class="video-mask" v-if="videoType == 1 && item.liveStatus == 2">
					<view class="video-mask-horn"></view>
					<view class="video-mask-text">未开播</view>
				</view>
			</view>
		</scroll-view>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		data() {
			return {
				videoType: 1,
				pageIndex: 1,
				isOver: false,
				videoName: '',
				videoList: [],
			}
		},
		onLoad(options) {
			this.videoType = options.videoType
			if (this.videoType == 2) {
				uni.setNavigationBarTitle({
					title: '短视频'
				});
			}
			this.getVideoList()
		},
		onPullDownRefresh() {
			this.pageIndex = 1
			this.videoList = []
			this.getVideoList('refresh')
		},
		methods: {
			//  搜索直播
			search(data) {
				this.videoName = data.value
				this.videoList = []
				this.pageIndex = 1
				this.getVideoList()
			},
			//  懒加载
			handleLoadMore() {
				if (!this.isOver) {
					this.pageIndex++
					this.getVideoList()
				}
			},
			//  获取直播列表
			getVideoList(type) {
				NET.request((this.videoType == 1 ? API.getLiveTelecastList : API.getShortVideo) + '/' + this.pageIndex + '/10', {
					liveName: this.videoName,
					videoName: this.videoName,
				}, 'GET').then(res => {
					if (type == 'refresh') {
						uni.stopPullDownRefresh();
					}
					this.isOver = res.data.list.length != 10
					this.videoList = this.videoList.concat(res.data.list)
				}).catch(res => {
					this.$refs.uTips.show({
						title: '获取直播列表失败',
						type: 'warning',
					})
				})
			},
			//  跳转直播详情
			goToVideoDetail(item) {
				if (this.videoType != 1) {
					uni.setStorage({
						key: 'videoUrl',
						data: item.mediaUrl
					})
				}else{
					uni.setStorage({
						key: 'liveImgUrl',
						data: item.imgUrl
					})
					uni.setStorage({
						key: 'liveName',
						data: item.liveName
					})
				}
				uni.navigateTo({
					url: '/pagesGood/' + (this.videoType == 1 ? ('liveDetail?liveId=' + item.liveId + '&roomId=' + item.roomId) :
						('videoDetail?videoId=' + item.videoId))
				});
			}
		},
	}
</script>

<style lang="less" scoped>
	page {
		width: 100%;
		height: 100%;
	}

	.container {
		width: 100%;
		height: 100%;

		.search-box {
			width: 100%;
			height: 58px;
			box-sizing: border-box;
			padding: 2px 12px 1px 12px;
		}

		.video-box {
			width: 100%;
			height: calc(100% - 58px) !important;
			overflow-y: auto;

			.video-col {
				float: left;
				background: #FFFFFF;
				box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.15);
				border-radius: 5px;
				width: calc(50% - 18px);
				margin-top: 4px;
				margin-bottom: 6px;
				padding-bottom: 10px;
				position: relative;

				.video-img {
					width: 100%;
					height: 170px;
					object-fit: cover;
					float: left;
				}

				.video-title {
					width: 100%;
					height: 18px;
					box-sizing: border-box;
					padding-left: 8px;
					float: left;
					font-size: 15px;
					font-family: PingFang SC;
					color: #333333;
					line-height: 18px;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					margin: 6px 0 2px 0;
				}

				.video-date {
					width: 100%;
					box-sizing: border-box;
					padding-left: 8px;
					height: 16px;
					float: left;
					font-size: 12px;
					font-family: PingFang SC;
					color: #52A63A;
					line-height: 16px;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
				}

				.video-mask {
					width: 100%;
					height: 100%;
					position: absolute;
					background-color: rgba(0, 0, 0, 0.3);
					border-radius: 5px;

					.video-mask-horn {
						width: 0;
						height: 0;
						border-bottom: 4px solid #52A63A;
						border-right: 4px solid transparent;
						position: absolute;
						right: -4px;
						top: 8px;
					}

					.video-mask-text {
						position: absolute;
						right: -4px;
						top: 12px;
						width: 52px;
						height: 18px;
						border-radius: 9px 0 0 9px;
						background: #52A63A;
						line-height: 18px;
						font-size: 10px;
						font-family: PingFang SC;
						color: #FFFFFF;
						text-align: center;
					}
				}
			}

			.video-col:nth-child(odd) {
				margin-left: 15px;
				margin-right: 3px;
			}

			.video-col:nth-child(even) {
				margin-left: 3px;
				margin-right: 15px;
			}
		}
	}
</style>
