<template>
	<mobile-frame :frameStyle="frameStyle" @toPath="toPath">
		<scroll-view class="scrollView" scroll-with-animation :scroll-into-view="topItem" scroll-y="true" @scroll="handleScroll">
			<view class="main" id="top">
				<view class="zero one">
					<input type="text" placeholder="搜索商品" @tap="toCommon('pagesHome/market/search')" placeholder-class="placss">
				</view>
				<view class="zero two">
					<swiper class="swiper" circular :indicator-dots="true" indicator-color="#ffffff" indicator-active-color="#FB1438" :autoplay="true" :interval="3000" :duration="1000">
						<swiper-item class="list" v-for="(item,index) in bannerList" :key="index">
							<image class="image" :src="item.url&&item.url.length>0?item.url[0].url:''" mode=""></image>
						</swiper-item>
					</swiper>
				</view>
				<view class="zero thr">
					<view class="list" v-for="(item,index) in btnList" :key="index" @tap="toIndexModel(item)">
						<image class="image" :src="item.url&&item.url.length>0?item.url[0].url:''" mode=""></image>
						<view class="textOver name">
							{{item.name}}
						</view>
					</view>
				</view>
				<view class="zero four">
					<view class="recomList" v-for="(item,index) in recomList" :key="index">
						<view class="list" v-for="(tag,indexs) in item.list" :key="indexs" @tap="toBuy(tag)">
							<view class="title">
								<text>{{tag.title||'&nbsp;'}}</text>
							</view>
							<image class="image" :src="tag.url&&tag.url.length>0?tag.url[0].url:''" mode=""></image>
							<view class="textOver name">
								{{tag.name}}
							</view>
						</view>
					</view>
				</view>
				<view class="zero five">
					<view class="list" v-for="(item,index) in marketList" :key="index" @tap="toBuy(item)">
						<image class="image" :src="item.file&&item.file.length>0?item.file[0].url:''" mode=""></image>
						<view class="name">
							{{item.name}}
						</view>
						<view class="other">
							<view class="money">
								<text>¥</text><text>{{item.sell_money||'暂无'}}</text>
							</view>
							<view class="btn">
								<!-- <button type="default" size="mini">购买</button> -->
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<view class="backTop" v-if="isShow==true">
			<text @click="backTop" class="iconfont icon-fanhuidingbu"></text>
		</view>
	</mobile-frame>
</template>

<script>
	import {
		getAddress
	} from '@/common/tool.js'
	export default {
		data() {
			return {
				frameStyle: {
					useBar: true
				},
				bannerList: [ // 轮播图 
				],
				btnList: [ //功能按钮
				],
				recomList: [ //推荐好物
				],
				marketList: [ //商品列表
				],
				// 是否显示返回顶部
				isShow: false,
				topItem: '',
			};
		},
		onLoad: function() {
			const that = this;
		},
		onShow: function() {
			const that = this;
			that.search();
		},
		methods: {
			async search() {
				const that = this;
				let res;
				// 轮播图
				res = await that.$api(`/banner`, 'GET', {
					status: '0'
				});
				if (res.errcode == '0') that.$set(that, `bannerList`, res.data);
				// 首页模块管理
				res = await that.$api(`/indexModule`, 'GET', {});
				if (res.errcode == '0') {
					let data = res.data.sort(function(a, b) {
						return a.sort - b.sort
					});
					that.$set(that, `btnList`, data);
				}
				// 推荐好货
				res = await that.$api(`/viewGoods/iatg`, 'GET', {});
				if (res.errcode == '0') that.$set(that, `recomList`, res.data);
				// 首页产品列表
				res = await that.$api(`/viewGoods/indexGoodsList`, `GET`, {
					limit: 20
				});
				if (res.errcode == '0') {
					that.$set(that, `marketList`, res.data)
				}
			},
			// 公共跳转
			toCommon(e) {
				uni.navigateTo({
					url: `/${e}`
				})
			},
			// 首页模块菜单跳转
			toIndexModel(data) {
				if (data && data.status == '1') {
					uni.showToast({
						title: '模块禁用',
						icon: 'none'
					})
				} else {
					uni.navigateTo({
						url: `/${data.to}`
					})
				}
			},
			// 购买
			toBuy(e) {
				uni.navigateTo({
					url: `/pagesHome/order/detail?id=${e.id||e._id}`
				})
			},
			// 计算高度
			handleScroll(e) {
				const that = this;
				let scrollTop = e.detail.scrollTop;
				that.isShow = scrollTop > 500;
				that.topItem = '';
			},
			// 返回顶部
			backTop() {
				const that = this;
				that.topItem = 'top'
			},
			toPath(e) {
				if (e && e.route && e.type == '0') {
					uni.redirectTo({
						url: `/${e.route}`
					})
				} else {
					uni.navigateTo({
						url: `/${e.route}`
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	.scrollView {
		height: 100vh;
	}

	.main {
		padding: 2vw;

		.zero {
			margin: 0 0 2vw 0;
		}

		.zero:last-child {
			margin: 0
		}

		.one {
			background-color: var(--fFB1Color);
			border-radius: 20px;
			padding: 0 2vw;

			input {
				font-size: var(--font15Size);
				color: var(--fffColor);
				border-radius: 14px;
				width: 100%;
				padding: 1.5vw 0;
			}

			.placss {
				color: var(--fffColor);
			}

		}

		.two {
			swiper {
				height: 50vw;
				border-radius: 5px;
			}

			.list {
				border-radius: 5px;

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

		.thr {
			display: flex;
			flex-direction: row;
			justify-content: space-around;
			flex-wrap: wrap;

			.list {
				width: 17vw;
				text-align: center;
				margin: 0 0 2vw 0;

				.image {
					width: 80%;
					height: 13vw;
					margin: 1.5vw 0 1.5vw 0;
				}

				.name {
					font-size: var(--font13Size);
				}
			}
		}

		.four {
			display: flex;
			flex-direction: row;
			justify-content: space-around;
			flex-wrap: wrap;
			background-color: var(--f2Color);
			padding: 2vw 0 0 0;

			.recomList {
				display: flex;
				flex-direction: row;
				justify-content: space-around;
				width: 41vw;
				margin: 0 0 2vw 0;
				padding: 2vw;
				border-radius: 10px;
				background-image: linear-gradient(to bottom, rgba(250, 216, 213, 1) 5%, rgba(255, 255, 255, 1) 22%);

				.list {
					width: 20vw;
					text-align: center;

					.title {
						text-align: center;
						font-weight: bold;
						font-size: var(--font15Size);
						;
						margin: 0 0 1vw 0;
					}

					.image {
						width: 18vw;
						height: 20vw;
						margin: 0 0 1vw 0;
					}

					.name {
						width: 17vw;
						font-size: var(--font12Size);
						;
						border: 1px solid var(--fFB1Color);
						border-radius: 25px;
						padding: 0 1vw;
						text-align: center;
					}
				}
			}
		}

		.five {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
			background-color: var(--f2Color);
			padding: 2vw 0 0 0;

			.list {
				width: 40vw;
				background-color: var(--fffColor);
				padding: 2vw;
				margin: 0 2vw 2vw 2vw;
				border-radius: 10px;

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

				.name {
					font-size: var(--font14Size);
					margin: 0 0 1vw 0;
				}

				.other {
					display: flex;
					flex-direction: row;
					justify-content: space-between;

					.money {
						color: var(--fFB1Color);

						text:nth-child(1) {
							font-size: var(--font12Size);
						}
					}

					.btn {
						button {
							color: var(--fffColor);
							background-color: var(--fFB1Color);
							padding: 5px 2vw;
							font-size: var(--font14Size);
							line-height: 1;
							border-radius: 90px;

						}
					}
				}
			}

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

	.backTop {
		position: fixed;
		bottom: 20vw;
		right: 5vw;

		text {
			font-size: 30px;
			background-color: #0000005f;
			border-radius: 90px;
		}
	}
</style>
