<template>
	<view class="content">
		<view class="one">
			<input type="text" v-model="searchInfo.title" @blur="toInput" placeholder="请输入视频名称">
		</view>
		<view class="two">
			<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
				<view class="list-scroll-view">
					<view class="two_1">
						<view class="list" v-for="(item,index) in list" :key="index">
							<image class="image" :src="item.img_url&&item.img_url.length>0?item.img_url[0].url:''"
								mode="">
							</image>
							<view class="name textOver">
								{{item.title}}
							</view>
							<view class="other">
								<view class="other_1">
									<uni-icons type="eye" color="var(--rgbfff)" size="12"></uni-icons>
									{{item.view_num}}
								</view>
								<view class="other_2">
									{{item.time_num}}
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchInfo: {},
				list: [],
				total: 0,
				page: 0,
				skip: 0,
				limit: 10,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0
			}
		},
		onLoad(options) {
			const that = this;
			that.$set(that, `searchInfo`, options);
			that.searchOther();
			that.search();
		},
		onHide() {
			const that = this;
			// that.clearPage()
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			async search() {
				const that = this;
				let info = {
					skip: that.skip,
					limit: that.limit,
				}
				let res = await that.$api('videos', 'GET', {
					...info,
					...that.searchInfo,
				})
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data]
					that.$set(that, `list`, list);
					that.$set(that, `total`, res.total);
				}
			},
			async searchOther() {
				const that = this;
				if (that.searchInfo.is_hot) {
					uni.setNavigationBarTitle({
						title: '热播'
					});
				} else {
					let res;
					if (that.searchInfo.firm_id) {
						res = await that.$api(`scenedata/${that.searchInfo.firm_id}`)
					} else if (that.searchInfo.type_id) {
						res = await that.$api(`scenedata/${that.searchInfo.type_id}`)
					} else if (that.searchInfo.place_id) {
						res = await that.$api(`scenedata/${that.searchInfo.place_id}`)
					} else if (that.searchInfo.year) {
						res = await that.$api(`scenedata/${that.searchInfo.year}`)
					} else if (that.searchInfo.head_actor) {
						res = await that.$api(`scenedata/${that.searchInfo.head_actor}`)
					}
					if (res.errcode == '0') {
						uni.setNavigationBarTitle({
							title: res.data.title
						});
					}
				}
			},
			toInput(e) {
				const that = this;
				let title = e.detail.value;
				if (title) that.$set(that.searchInfo, `title`, title);
				else delete that.searchInfo.title;
				that.clearPage();
				that.search()
			},
			toPage() {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.search();
					uni.hideLoading();

				} else that.$set(that, `is_bottom`, true)
			},
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				that.$set(that, `scrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 10)
				that.$set(that, `page`, 0)
				if (!that.searchInfo.title) delete that.searchInfo.title;
			},
		},

	}
</script>

<style lang="scss">
	.content {
		background-color: var(--rgb111);

		.one {
			padding: 2vw;

			input {
				border: 1px solid var(--rgbf1f);
				border-radius: 5px;
				padding: 5px;
				font-size: 14px;
				color: var(--rgbfff);
			}
		}

		.two {
			flex-grow: 1;
			position: relative;

			.two_1 {
				display: flex;
				flex-wrap: wrap;

				.list {
					width: 48%;
					margin: 0 10px 10px 0;

					.image {
						width: 100%;
						height: 100px;
						overflow: hidden;
						border-radius: 5px;
						box-shadow: 0 0 5px var(--rgbf1f);
						margin: 0 0 5px 0;
					}

					.name {
						font-size: 14px;
						color: var(--rgbfff);
						margin: 0 0 5px 0;
					}

					.other {
						display: flex;
						color: var(--rgbfff);
						font-size: 12px;
						justify-content: space-between;
					}
				}

				.list:nth-child(2n) {
					margin: 0 0 10px 0;
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
			padding: 0 2vw;
		}
	}

	.is_bottom {
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: #858585;
			font-size: 14px;
		}
	}
</style>
