<template>
	<view class="content">
		<view class="one">
			<view class="one_1">
				<input type="text" v-model="searchInfo.title" @blur="toInput" placeholder="请输入视频名称">
			</view>
			<view class="one_2">
				<button size="mini" @tap="toFilter()">筛选</button>
			</view>
		</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" @tap="videoView(item)">
							<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>
		<c-drawer :drawer="drawer" @toClose="toClose">
			<view class="drawer_one">
				<view class="drawer_one_1">
					<uni-forms ref="searchInfo" :model="searchInfo" label-width="auto">
						<uni-forms-item>
							<template>
								<view class="label">是否热推</view>
								<view class="value">
									<uni-data-checkbox mode="tag" v-model="searchInfo.is_hot" :localdata="isnoList"
										:map="{text:'label',value:'value'}"></uni-data-checkbox>
								</view>
							</template>
						</uni-forms-item>
						<uni-forms-item>
							<template>
								<view class="label">厂商</view>
								<view class="value">
									<uni-data-checkbox mode="tag" v-model="searchInfo.firm_id" :localdata="firmList"
										:map="{text:'title',value:'id'}"></uni-data-checkbox>
								</view>
							</template>
						</uni-forms-item>
						<uni-forms-item>
							<template>
								<view class="label">类型</view>
								<view class="value">
									<uni-data-checkbox mode="tag" v-model="searchInfo.type_id" :localdata="typeList"
										:map="{text:'title',value:'id'}"></uni-data-checkbox>
								</view>
							</template>
						</uni-forms-item>
						<uni-forms-item>
							<template>
								<view class="label">产地</view>
								<view class="value">
									<uni-data-checkbox mode="tag" v-model="searchInfo.place_id" :localdata="placeList"
										:map="{text:'title',value:'id'}"></uni-data-checkbox>
								</view>
							</template>
						</uni-forms-item>
						<uni-forms-item>
							<template>
								<view class="label">年份</view>
								<view class="value">
									<uni-data-checkbox mode="tag" v-model="searchInfo.year" :localdata="yearList"
										:map="{text:'title',value:'id'}"></uni-data-checkbox>
								</view>
							</template>
						</uni-forms-item>
						<uni-forms-item>
							<template>
								<view class="label">领衔演员</view>
								<view class="value">
									<uni-data-checkbox mode="tag" v-model="searchInfo.head_actor" :localdata="actorList"
										:map="{text:'title',value:'id'}"></uni-data-checkbox>
								</view>
							</template>
						</uni-forms-item>
					</uni-forms>
				</view>
				<view class="drawer_one_2">
					<button size="mini" @tap="toReset()">重置</button>
					<button size="mini" @tap="toSave()">确认</button>
				</view>
			</view>
		</c-drawer>
	</view>
</template>

<script>
	import cDrawer from "../components/c-drawer.vue";
	export default {
		components: {
			cDrawer
		},
		data() {
			return {
				searchInfo: {},
				list: [],
				total: 0,
				page: 0,
				skip: 0,
				limit: 10,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				drawer: {
					title: '筛选',
					show: false,
					mode: 'right'
				},
				// 字典表
				isnoList: [],
				firmList: [],
				typeList: [],
				placeList: [],
				yearList: [],
				actorList: [],

			}
		},
		async onLoad() {
			const that = this;
			await that.searchOther();
			await 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);
				}
			},
			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);
			},
			// 打开弹框
			toFilter() {
				const that = this;
				that.$set(that, `drawer`, {
					title: '筛选',
					show: true,
					mode: 'right'
				})
			},
			// 重置
			toReset() {
				const that = this;
				that.$set(that, `searchInfo`, {})
				that.clearPage();
				that.search()
			},
			toSave() {
				const that = this;
				that.clearPage();
				that.toClose();
				that.search()
			},
			// 关闭弹框
			toClose() {
				const that = this;
				that.$set(that, `drawer`, {
					title: '筛选',
					show: false,
					mode: 'right'
				})
			},
			// 视频查看
			videoView(e) {
				uni.navigateTo({
					url: `/pagesVideo/video/index?id=${e._id}`
				})
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				// 是否
				res = await that.$api('dictdata', 'GET', {
					type: 'is_no'
				})
				if (res.errcode == '0') {
					that.$set(that, `isnoList`, res.data)
				}
				res = await that.$api('scenetype', 'GET', {
					is_use: '0'
				})
				if (res.errcode == '0') {
					for (const val of res.data) {
						let arr;
						let type = '';
						if (val.title == '厂商') type = 'firm';
						else if (val.title == '类型') type = 'type';
						else if (val.title == '产地') type = 'place';
						else if (val.title == '年份') type = 'year';
						else if (val.title == '演员') type = 'actor';
						arr = await that.$api('scenedata', 'GET', {
							is_use: '0',
							type_id: val._id
						})
						if (arr.errcode == '0') {
							that.$set(that, `${type}List`, arr.data);
						}
					}
				}
			},
			// 清空列表
			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 {
			background-color: var(--rgb111);
			display: flex;
			padding: 2vw;

			.one_1 {
				flex-grow: 1;

				input {
					border: 1px solid var(--rgbf1f);
					height: 30px;
					border-radius: 2px;
					padding: 0 5px;
					font-size: 12px;
					color: var(--rgbfff);
				}
			}

			.one_2 {
				button {
					height: 100%;
					background-color: var(--rgbfa4);
					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;
		}
	}

	.drawer_one {
		display: flex;
		flex-direction: column;
		width: 77vw;
		height: 92vh;

		.drawer_one_1 {
			flex-grow: 1;
			overflow-y: auto;

			.uni-forms-item {
				margin: 0;
			}

			.label {
				font-weight: bold;
				margin: 0 0 5px 0;
			}

			.value {}
		}

		.drawer_one_2 {
			text-align: center;
			padding: 2vw 0;

			button {
				width: 30%;
				height: 100%;
				margin: 0 2vw;

			}

			button:nth-child(1) {
				background-color: var(--rgbfa4);
				color: var(--rgbfff);
			}

			button:nth-child(2) {
				background-color: var(--rgb67c);
				color: var(--rgbfff);
			}
		}
	}
</style>
