<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<image class="image" :src="topUrl" mode=""></image>
			</view>
			<view class="two">
				<view class="pubu">
					<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">
							{{item.name}}
						</view>
					</view>
				</view>
			</view>
			<view class="is_bottom" v-if="is_bottom">
				<text>数据到底了！！</text>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				// 系统设置
				config: {},
				// 路由参数
				type: '1',
				tags: '',
				act_tags: '',
				topUrl: '',
				// 数据列表
				list: [],
				is_bottom: true
			};
		},
		onLoad(e) {
			const that = this;
			if (e && e.tags) {
				that.$set(that, `type`, '1')
				that.$set(that, `tags`, e.tags || '');
			} else {
				that.$set(that, `type`, '2')
				that.$set(that, `act_tags`, e.act_tags || '');
			}
		},
		onShow: function() {
			const that = this;
			that.searchConfig();
			that.searchOther();
			that.search();
		},
		methods: {
			// 查询基本设置
			searchConfig() {
				const that = this;
				uni.getStorage({
					key: 'config',
					success: function(res) {
						if (res.data) that.$set(that, `config`, res.data)
					},
					fail: function(err) {
						console.log(err);
					}
				})
			},
			async search() {
				const that = this;
				let info = {};
				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
				});
				if (res.errcode == '0') {
					that.$set(that, `list`, res.data)
				}
			},
			// 查询其他信息
			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`, 'GET', {
						code: tags
					})
				} else if (type == '2') {
					res = await that.$api(`/actTags`, 'GET', {
						value: act_tags
					})
				}
				if (res.errcode == '0' && res.total > 0) {
					let data = res.data[0];
					uni.setNavigationBarTitle({
						title: data.label
					});
					if (data.file.length > 0) topUrl = data;
				}
				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}`
				})
			},
		}
	}
</script>

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

		.one {
			padding: 2vw;

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

		.two {
			padding: 0 2vw;
			margin: 0 0 2vw 0;

			.pubu {
				column-count: 2;
				column-gap: 3vw;

				.list {
					margin: 0 0 2vw 0;
					-webkit-column-break-inside: avoid;
					break-inside: avoid;
					box-shadow: 0 0 5px #858585;
					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;
					}
				}
			}
		}
	}

	.is_bottom {
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
		}
	}
</style>
