<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
					<view class="list-scroll-view">
						<view class="one_1">
							<image class="image" :src="topUrl" mode="aspectFit"></image>
						</view>
						<view class="one_2">
							<view class="list" v-for="(item,index) in list" :key="index" @tap="toBuy(item)">
								<view class="list_1">
									<image class="image" :src="item.file&&item.file.length>0?item.file[0].url:''" mode=""></image>
								</view>
								<view class="name textOver">
									{{item.name}}
								</view>
							</view>
						</view>
						<view class="is_bottom" v-if="is_bottom">
							<text>{{config.bottom_title}}</text>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				// 系统设置
				config: {},
				// 路由参数
				type: '1',
				tags: '',
				act_tags: '',
				topUrl: '',
				list: [],
				total: 0,
				page: 0,
				skip: 0,
				limit: 10,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
			};
		},
		onLoad(e) {
			const that = this;
			if (e && e.tags) {
				that.$set(that, `type`, '1')
				that.$set(that, `tags`, e.tags || '');
			} else if (e && e.act_tags) {
				that.$set(that, `type`, '2')
				that.$set(that, `act_tags`, e.act_tags || '');
			}
			that.searchConfig();
			that.searchOther();
			that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage()
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			// 查询基本设置
			searchConfig() {
				const that = this;
				uni.getStorage({
					key: 'config',
					success: function(res) {
						if (res.data) that.$set(that, `config`, res.data)
					}
				})
			},
			async search() {
				const that = this;
				let info = {
					skip: that.skip,
					limit: that.limit
				};
				if (that.type == '1') info.tags = that.tags;
				else if (that.type == '2') info.act_tags = that.act_tags;
				let res = await that.$api(`/goods`, 'GET', {
					...info,
					status: '1'
				});
				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.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);
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let type = that.type;
				let act_tags = that.act_tags;
				let tags = that.tags;
				let topUrl = [];
				let res;
				if (type == '1') {
					res = await that.$api(`/goodsTags/getData`, 'GET', {
						code: tags
					})
				} else if (type == '2') {
					res = await that.$api(`/actTags/getData`, 'GET', {
						value: act_tags
					})
				}
				if (res.errcode == '0') {
					let data = res.data;
					uni.setNavigationBarTitle({
						title: data.label
					});
					if (that.type == '1' && data.tags_file.length > 0) topUrl = data.tags_file
					else if (that.type == '2' && data.file.length > 0) topUrl = data.file
				}
				if (topUrl.length > 0) {
					that.$set(that, `topUrl`, topUrl[0].url)
				} else {
					that.$set(that, `topUrl`, that.config.config.logo[0].url)
				}
			},
			// 购买
			toBuy(e) {
				const that = this;
				uni.navigateTo({
					url: `/pagesHome/order/detail?id=${e._id}`
				})
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 6)
				that.$set(that, `page`, 0)
			}
		},

	}
</script>

<style lang="scss">
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			.one_1 {
				padding: 2vw;
				background-color: #ffffff;

				.image {
					width: 100%;
					height: 200px;
					box-shadow: 0 0 5px #f1f1f1;
					border-radius: 10px;
				}
			}

			.one_2 {
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;
				padding: 0 2vw;

				.list {
					width: 43vw;
					margin: 0 0 2vw 0;
					box-shadow: 0 0 5px #f1f1f1;
					padding: 2vw;
					border-radius: 5px;

					.list_1 {
						margin: 0 0 1vw 0;

						.image {
							width: 100%;
							height: 30vh;
							border-radius: 10px;
							box-shadow: 0 0 5px #f1f1f1;
						}
					}

					.name {
						font-size: 16px;
					}
				}
			}
		}

		// .one {
		// 	padding: 2vw;
		// 	background-color: #ffffff;

		// 	.image {
		// 		width: 100%;
		// 		height: 200px;
		// 		box-shadow: 0 0 5px #f1f1f1;
		// 		border-radius: 10px;
		// 	}
		// }

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

		// 	// .pubu {
		// 	// 	// column-count: 2;
		// 	// 	// column-gap: 3vw;
		// 	// 	display: flex;
		// 	// 	justify-content: space-between;
		// 	// 	flex-wrap: wrap;

		// 	// 	.list {
		// 	// 		width: 43vw;
		// 	// 		// break-inside: avoid;
		// 	// 		margin: 0 0 2vw 0;
		// 	// 		box-shadow: 0 0 5px #f1f1f1;
		// 	// 		padding: 2vw;
		// 	// 		border-radius: 5px;

		// 	// 		.list_1 {
		// 	// 			margin: 0 0 1vw 0;

		// 	// 			.image {
		// 	// 				width: 100%;
		// 	// 				height: 30vh;
		// 	// 				border-radius: 10px;
		// 	// 				box-shadow: 0 0 5px #f1f1f1;
		// 	// 			}
		// 	// 		}

		// 	// 		.name {
		// 	// 			font-size: 16px;
		// 	// 		}
		// 	// 	}
		// 	// }
		// }
	}

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

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}


	.is_bottom {
		text-align: center;

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