<template>
	<home-frame @toPath="toPath">
		<view class="main">
			<view class="one">
				<view class="list" v-for="(item,index) in typeList" :key="index" @tap="typeChange(item,index)">
					<span :style="{background:tActive==index?'var(--rgbfa4)':''}" class="textOver">{{item.title}}</span>
				</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>
						<view class="two_2">
							<view class="list" v-for="(item,index) in recomList" :key="index">
								<image class="image"
									:src="item.img_url&&item.img_url.length>0?item.img_url[0].url:'../../static/error.jpg'"
									mode="">
								</image>
								<span class="name" v-if="item.title">{{item.title}}</span>
							</view>
						</view>
						<view class="two_1">
							全部
						</view>
						<view class="two_2">
							<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:'../../static/error.jpg'"
									mode="">
								</image>
								<span class="name" v-if="item.title">{{item.title}}</span>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</home-frame>
</template>

<script>
	import homeFrame from "../components/home-frame.vue";
	export default {
		components: {
			homeFrame
		},
		data() {
			return {
				typeList: [],
				tActive: 0,
				type_id: '',
				recomList: [],
				list: [],
				total: 0,
				page: 0,
				skip: 0,
				limit: 20,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0
			};
		},
		onLoad() {

		},
		onShow() {
			const that = this;
			that.search()
		},
		onHide() {
			const that = this;
			that.clearPage()
		},
		methods: {
			async search() {
				const that = this;
				let res = await that.$api('scenetype', 'GET', {
					is_use: '0'
				})
				if (res.errcode == '0') {
					that.$set(that, `typeList`, res.data);
					if (res.total > 0) {
						that.$set(that, `type_id`, res.data[0]._id);
						that.searchScene()
						that.searchData()
					}
				}
			},
			// 类型选择
			typeChange(e, index) {
				const that = this;
				that.$set(that, `tActive`, index);
				that.$set(that, `type_id`, e._id);
				that.clearPage();
				that.searchScene();
				that.searchData();
			},
			// 查询推荐
			async searchScene() {
				const that = this;
				let res = await that.$api('scenedata', 'GET', {
					skip: 0,
					limit: 6,
					type_id: that.type_id,
					is_scene: '1',
					is_use: '0',
				})
				if (res.errcode == '0') {
					that.$set(that, `recomList`, res.data);
				}
			},
			// 查询数据
			async searchData() {
				const that = this;
				let res = await that.$api('scenedata', 'GET', {
					skip: that.skip,
					limit: that.limit,
					type_id: that.type_id,
					is_use: '0',
				})
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data]
					that.$set(that, `list`, list);
					that.$set(that, `total`, res.total);
				}
			},
			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.searchData();
					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`, 20)
				that.$set(that, `page`, 0)
			},
			// 跳转页面
			toPath(e) {
				let url = `/${e.route}`;
				if (e.type == '0') uni.navigateTo({
					url
				})
				else if (e.type == '1') uni.redirectTo({
					url
				})
				else if (e.type == '2') uni.reLaunch({
					url
				})
				else if (e.type == '3') uni.switchTab({
					url
				})
			}
		},
	};
</script>

<style lang="scss">
	.main {
		background-color: var(--rgb111);
		display: flex;
		flex-direction: row;
		width: 96vw;
		height: 92vh;
		padding: 0 2vw;

		.one {
			width: 24vw;
			color: var(--rgbfff);
			margin: 0 10px 0 0;
			background-color: var(--rgb000);
			overflow-y: auto;

			.list {
				span {
					display: inline-block;
					width: 100%;
					height: 40px;
					line-height: 40px;
					text-align: center;
					font-size: 14px;
				}
			}

		}

		.two {
			flex-grow: 1;
			color: var(--rgbfff);
			background-color: var(--rgb000);
			padding: 0 2vw;
			overflow-y: auto;
			position: relative;

			.two_1 {
				padding: 1vw 0;
			}

			.two_2 {
				display: flex;
				// justify-content: space-around;
				flex-wrap: wrap;
				position: relative;

				.list {
					width: 30%;
					height: 70px;
					overflow: hidden;
					position: relative;
					margin: 0 10px 10px 0;
					box-shadow: 0 0 5px var(--rgbf1f);
					border-radius: 5px;

					.image {
						width: 100%;
						height: 100%;
						border-radius: 5px;
					}

					.name {
						position: absolute;
						bottom: 10px;
						width: 100%;
						text-align: center;
						left: 0;
						color: var(--rgb000);
						font-size: 12px;
						font-family: monospace;
					}
				}

				.list:nth-child(3n) {
					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>
