<template>
	<mobile-frame :frameStyle="frameStyle" @toPath="toPath">
		<view class="main">
			<view class="one">
				<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage">
					<view class="list-scroll-view">
						<view class="list" v-for="(item,index) in list" :key="index" @tap="toShop(item)">
							<view class="list_1">
								<view class="l">
									<image class="image" :src="item.logo&&item.logo.length>0?item.logo[0].url:''" mode=""></image>
								</view>
								<view class="c">
									<view class="name">
										{{item.name}}
									</view>
									<view class="other">
										<text>宝贝数<text>{{item.market_num}}</text></text>
										<text>关注数<text>{{item.follow_num}}</text></text>
									</view>
								</view>
								<view class="r" @tap.stop="toFolllow(item)">
									<text class="iconfont icon-yduishoucangshixin-copy" v-if="item.is_follow==true"></text>
									<text class="iconfont icon-yduishoucangkongxin-copy" v-else></text>
								</view>
							</view>
							<view class="list_2">
								<view class="market" v-for="(tag,indexs) in item.market" :key="indexs" @tap.stop="toBuy(tag)">
									<image class="image" :src="tag.file&&tag.file.length>0?tag.file[0].url:''" mode=""></image>
									<view class="money">
										¥{{tag.sell_money}}
									</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				frameStyle: {
					useBar: true
				},
				user: {},
				list: [],
				total: 0,
				page: 0,
				skip: 0,
				limit: 6,
			};
		},
		onLoad: async function() {
			const that = this;
			// 监听用户是否登录
			await that.watchLogin();
			// 查询列表
			await that.search();
		},
		methods: {
			// 监听用户是否登录
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						let user = that.$jwt(res.data);
						that.$set(that, `user`, user)
					}
				})
			},
			// 查询列表
			async search() {
				const that = this;
				let info = {
					skip: that.skip,
					limit: that.limit
				}
				const res = await that.$api(`/viewShop/microIndex`, `GET`, {
					...info,
				})
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					that.$set(that, `list`, list)
					that.$set(that, `total`, res.total)
				} else {
					uni.showToast({
						title: res.errmsg,
					});
				}
			},
			// 分页
			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 uni.showToast({
					title: '没有更多数据了'
				});
			},
			// 商铺
			toShop(e) {
				const that = this;
				uni.navigateTo({
					url: `/pagesHome/shop/index?id=${e._id}`
				})
			},
			// 购买
			toBuy(e) {
				const that = this;
				uni.navigateTo({
					url: `/pagesHome/order/detail?id=${e._id}`
				})
			},
			// 关注
			async toFolllow(e) {
				const that = this;
				let user = that.user;
				if (user && user._id && e && e._id) {
					let res = await that.$api(`/storeShop`, `POST`, {
						customer: user._id,
						shop: e._id
					});
					if (res.errcode == '0') {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
						that.clearPage();
						that.search()
					}
				} else {
					uni.showToast({
						title: '缺少必要信息，无法收藏',
						icon: 'none'
					})
				}
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 5)
				that.$set(that, `page`, 0)
			},
			toPath(e) {
				const that = this;
				if (e && e.route && e.type == '0') {
					uni.redirectTo({
						url: `/${e.route}`
					})
				} else {
					uni.navigateTo({
						url: `/${e.route}`
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	.main {
		.one {
			.list {
				margin: 2vw 2vw 0 2vw;
				padding: 2vw;
				border-radius: 5px;
				background-color: #f5f5f5;

				.list_1 {
					display: flex;
					flex-direction: row;
					margin: 0 0 1vw 0;

					.l {
						width: 20vw;

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

					.c {
						padding: 0 0 0 2vw;
						width: 60vw;

						.name {
							font-size: 16px;
							font-weight: bold;
							margin: 0 0 1vw 0;
						}

						.other {
							font-size: 14px;

							text {
								padding: 0 2vw 0 0;
							}
						}
					}

					.r {
						width: 10vw;
						text-align: right;

						text {
							font-size: 30px;
						}
					}

				}

				.list_2 {
					display: flex;
					flex-direction: row;
					flex-wrap: wrap;

					.market {
						position: relative;
						margin: 0 2vw 2vw 0;
						width: 29vw;

						.image {
							width: 100%;
							height: 20vw;
						}

						.money {
							position: absolute;
							bottom: 0;
							font-size: 10px;
							width: 100%;
							background-color: #0000005f;
							color: #fff;
							padding: 0.2vw 0;
							text-align: center;
							font-size: 14px;
						}
					}

					.market:nth-child(3n) {
						margin: 0 0 2vw 0;
					}
				}
			}
		}
	}

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

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}
</style>
