<template>
	<view class="">
		<!-- 金刚区 -->
		<view class="zw-pages-content">
			<!-- 宜格优选 -->
			<!-- 			<view class="service-boxs " v-if="adVal && yiSever">
				<view class="service-title">
					<text>{{adVal.adName}}</text>
					<text class="moer-text" @click.stop="$openPage(adVal)">更多 <text
							class="moer-icon iconfont_yige">&#xe65f;</text> </text>
				</view>
				<view class="service-content">
					<scroll-view class="district-service" scroll-x="true">
						<div class="district-service-items">
							<block v-for="item in yiSever.themeModuleVoList">
								<view class="district-service-item" @click.stop="$openPage(item)">
									<image :src="item.moduleImage" mode="aspectFit"></image>
								</view>
							</block>
						</div>
					</scroll-view>
				</view>
			</view> -->

			<!-- 宜格优选 -->
			<view class="zw-service-box theme-service zw-interval" v-if="adVal && yiSever">
				<view class="service-title">
					<text>{{adVal.adName}}</text>
					<text class="moer-text" @click.stop="$openPage(adVal)">更多 <text
							class="moer-icon iconfont_yige">&#xe65f;</text> </text>
				</view>
				<view class="service-content">
					<u-row gutter="20">
						<u-col span="6" v-for="item in yiSever.themeModuleVoList">
							<view class="service-item lazy-img" @click.stop="$openPage(item)">
								<u-image class="service-img" :src="item.moduleImage" mode="aspectFit"
									:lazy-load="true"></u-image>
							</view>
						</u-col>

					</u-row>
				</view>
			</view>

			<!-- 广告banner -->
			<view class="banner" v-show="adBanner">
				<zw-swiper @getBanner="onAdBanner" ref="adBanner" :autoLoad="false" :height="200"
					:keyword="$keys.SH_ACTIVITY" :StorageKey="$keys.SH_ACTIVITY" />
				<!-- <image class="life-img" src="./life_banner.png" mode="aspectFill"></image> -->
			</view>

			<!-- 人气热卖 -->
			<view class="hot-box" v-if="hotList && hotList.length > 0">
				<view class="hot-title">
					人气热卖
				</view>
				<view class="hot-goods">
					<scroll-view class="scroll-box" scroll-x :show-scrollbar="false" @scroll="scrollGoods">
						<block v-for="item in hotList">
							<view class="hot-goods-item" :key="item.id"
								@click="goProductDetails('/pages/product/goods/goods?id=' + item.id)">
								<!-- <image :src="item.cover" mode="aspectFit"></image> -->
								<view class="hot-goods-img lazy-img">
									<u-image class="service-img" :src="item.cover" mode="aspectFit" :lazy-load="true" />
								</view>
								<text class="hot-goods-price">
									<rich-text :nodes="$mUtil.priceBigSmall(item.min_sale_price)"></rich-text>
								</text>
							</view>
						</block>
					</scroll-view>
					<view class="indicator">
						<view class="line-bar"
							:style="{'transform':'translateX('+translateX + 'px)', 'width': lineBarWidth +'px'}">
						</view>
					</view>
				</view>
			</view>

			<!-- 商品 -->
			<view class="goods-box" v-if="goodsList && goodsList.length > 0">
				<view class="goods-item" v-for="item in goodsList"
					@click="goProductDetails('/pages/product/goods/goods?id=' + item.id)">
					<!-- <image :src="item.cover" mode="aspectFill"></image> -->
					<view class="goods-img lazy-img">
						<u-image class="service-img" :src="item.cover" mode="aspectFit" :lazy-load="true" />
					</view>
					<view class="goods-info">
						<view class="goods-title">{{item.title}}</view>
						<view class="goods-price">
							<rich-text :nodes="$mUtil.priceBigSmall(item.min_sale_price)"></rich-text>
							<text class="line-price">¥ {{ item.max_market_price }}</text>
						</view>
					</view>
				</view>
			</view>

			<EmptyDate v-else-if="status !== 'loading'" />
			<loadMore v-if="goodsList.length > 0 || status === 'loading'" :status="status"></loadMore>
		</view>
	</view>
</template>

<script>
	import Mixin from "./module/Mixin.js";
	import kongKim from "./module/kong-kim.vue";
	import {
		getBannerList,
		getThemeModule
	} from "@/api/government.js"
	export default {
		mixins: [Mixin],
		components: {
			kongKim
		},
		data() {
			return {
				adBanner: false,
				activityAd: [],
				translateX: 0,
				lineBarWidth: 0,
				params: {
					page: 1,
					limit: 6
				},
				hotList: [], // 人气热卖
				goodsList: [], // 猜你喜欢
				status: "more",
				adVal: null,
				yiSever: null,
			}
		},
		created() {
			// this.hotGoods()
			// this.guessLike()
		},
		mounted() {},
		methods: {
			init() {
				try {
					this.$nextTick(() => {
						this.$refs.adBanner.init()
					})
				} catch (e) {
					//TODO handle the exception
				}
				this.params = {
					page: 1,
					limit: 6
				};
				this.hotGoods();
				this.getAD();
				this.guessLike();
			},
			getAD() {
				getBannerList(this.$keys.YigeShop).then(res => {
					const data = res.data || []
					this.adVal = data[0] || null;
					this.getTheme();
				});
			},
			onAdBanner(data) {
				if (data && data.length > 0) {
					this.adBanner = true
				} else {
					this.adBanner = false
				}
			},
			getTheme() {
				getThemeModule(this.$keys.YI_SERVE).then(res => {
					this.yiSever = res.data || null;
				});
			},
			goProductDetails(url) {
				uni.navigateTo({
					url: url,
				});
			},
			scrollGoods(e) {
				// 获取滚动条所在位置
				let scrollLeft = e.detail.scrollLeft;
				// 获取当前屏幕宽度
				const screenWidth = uni.upx2px(660)
				// 获取item的宽度(px)
				let itemWidth = uni.upx2px(160);
				// 根据滚动条所在位置计算当前所在的item
				let index = scrollLeft / itemWidth;
				// 移动滚动条
				this.translateX = index * (uni.upx2px(124) / this.hotList.length); // 滚动条的宽度 / item的总数量
			},
			// 获取人气热卖商品
			hotGoods() {
				this.$http.get('/goods/month', {
					limit: 10,
					num: 1
				}).then(res => {
					if (res && res.code == 200) {
						this.hotList = res.list
						this.lineBarWidth = 4 / this.hotList.length * uni.upx2px(124)
					}
				})
			},
			/**猜你喜欢 */
			guessLike() {
				this.status = 'loading';
				this.$http.get(`/goods/smart`, {
					page: this.params.page,
					limit: this.params.limit,
				}).then((res) => {
					if (res && res.code == 200) {
						this.goodsList = this.params.page == 1 ? res.page.list : this.goodsList.concat(res.page
							.list);
						if (res.page.totalPage <= res.page.currPage) {
							this.status = "noMore";
						} else {
							this.status = "more";
						}
						// console.log(this.goodsList)
					}
				}).catch(err => {
					this.status = "noMore";
				})
			},
			moreGoods() {
				if (this.status == 'more') {
					this.params.page++;
					this.guessLike();
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import './sever.scss';

	.banner {
		margin-top: $zw-gap;
		width: 100%;
		height: 200rpx;

		.life-img {
			width: 100%;
			height: 100%;
		}
	}


	.service-box {
		margin-top: $zw-gap;
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: stretch;

		.service-item {
			width: calc(50% - 10rpx);
			height: 200rpx;
			background-color: red;
			border-radius: 10rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			image {
				width: 80rpx;
				height: 50rpx;
			}

			text {
				font-size: 28rpx;
			}
		}
	}

	.discount-box {
		margin-top: $zw-gap;
		border-radius: 20rpx;
		overflow: hidden;
		width: 100%;
		height: 400rpx;
		background-color: skyblue;
		display: flex;
		justify-content: space-between;
		align-items: stretch;

		.discount-max {
			width: 400rpx;
			height: 100%;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.discount-min {
			width: calc(100% - 420rpx);
			height: 100%;
			margin-left: 20rpx;

			.discount-min-item {
				width: 100%;
				height: calc(50% - 10rpx);
				// height:190rpx;

				image {
					width: 100%;
					height: 100%;
				}

				&:last-child {
					margin-top: 20rpx;
				}
			}
		}

	}

	.hot-box {
		margin-top: $zw-gap;
		background-color: #fff;
		border-radius: 16rpx;
		box-shadow: 0 2rpx 12rpx 0 rgba(0, 0, 0, 0.1);

		.hot-title {
			width: 100%;
			height: 100rpx;
			padding: 0 20rpx;
			font-size: 38rpx;
			font-weight: bold;
			line-height: 100rpx;
			border-bottom: 1rpx solid #EDEDED;
		}

		.hot-example {
			width: 100%;
			padding: 20rpx;
			display: flex;
			justify-content: space-between;
			align-items: stretch;
			border-bottom: 1rpx solid #EDEDED;

			image {
				width: 160rpx;
				height: 160rpx;
			}

			.example-info {
				width: calc(100% - 160rpx);
				padding-left: 20rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.example-name {
					font-size: 32rpx;
					font-weight: bold;
				}

				.example-hint {
					font-size: 26rpx;
				}

				.example-price {
					display: flex;
					justify-content: space-between;
					align-items: flex-end;

					text {
						color: red;
						font-size: 28rpx;
						font-weight: bold;
					}

					.example-btn {
						color: #fff;
						background-color: red;
						width: 180rpx;
						height: 50rpx;
						font-size: 34rpx;
						text-align: center;
						line-height: 50rpx;
						border-radius: 25rpx;
					}
				}
			}
		}

		.hot-goods {
			width: 100%;
			// display: flex;
			// flex-direction: column;
			// align-items: stretch;
			padding: 40rpx 30rpx;

			.scroll-box {
				width: 100%;
				margin-bottom: 36rpx;
				white-space: nowrap;

				// ::v-deep ::-webkit-scrollbar {
				//   // 滚动条整体样式
				//   display: block;
				//   width: 124rpx !important;
				//   height: 14rpx !important;
				//   // -webkit-appearance: auto !important;
				//   background: #EDEDED;
				//   // overflow: auto !important;
				// }

				// ::v-deep ::-webkit-scrollbar-thumb {
				//   // 滚动条里面小方块
				//   // width: 124rpx !important;
				//   // height: 14rpx !important;
				//   border-radius: 10rpx !important;
				//   // box-shadow: inset 0 0 5rpx rgba(0, 0, 0, 0.2) !important;
				//   background-color: #F5D680 !important;
				// }

				// ::v-deep ::-webkit-scrollbar-track {
				//   // 滚动条
				//   // width: 124rpx !important;
				//   // height: 14rpx !important;
				//   border-radius: 10rpx !important;
				//   // box-shadow: inset 0 0 5rpx rgba(0, 0, 0, 0.2) !important;
				//   background-color: #EDEDED !important;
				// }
			}

			.indicator {
				display: flex;
				margin: 0 auto;
				width: 124rpx; // 滚动条宽度
				height: 14rpx;
				background-color: #EDEDED;
				border-radius: 10rpx;

				.line-bar {
					width: 50rpx; // (显示的item数量 / item的总数量) * 滚动条宽度
					background-color: #F5D680;
					transition-duration: .3s;
					border-radius: 10rpx;
				}
			}

			.hot-goods-item {
				display: inline-block;
				width: 140rpx;
				margin-right: 23rpx;

				.hot-goods-img {
					width: 140rpx;
					height: 140rpx;
				}

				.hot-goods-name {
					width: 100%;
					font-size: 26rpx;
					font-weight: bold;
				}

				.hot-goods-price {
					font-size: 30rpx;
					text-align: center;
				}
			}
		}
	}

	.goods-box {
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		margin-top: $zw-gap;

		.goods-item {
			width: 336rpx;
			height: 512rpx;
			margin-right: 18rpx;
			margin-bottom: 38rpx;
			background: #ffffff;
			border-radius: 18rpx;
			overflow: hidden;
			box-shadow: 0 2rpx 12rpx 0 rgba(0, 0, 0, 0.1);

			&:nth-child(2n) {
				margin-right: 0;
			}

			.goods-img {
				width: 336rpx;
				height: 336rpx;
				border-radius: 18rpx 18rpx 0 0;
				vertical-align: middle;
			}

			.goods-info {
				padding: 0 20rpx 20rpx;
				padding-top: 14rpx;
				height: calc(100% - 336rpx);
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.goods-title {
					// height: 90rpx;
					margin-bottom: 10rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
				}

				.goods-price {
					display: flex;
					align-items: baseline;
					color: #FF6600;
					font-size: 36rpx;

					.line-price {
						font-size: 22rpx;
						color: #999999;
						margin-left: 16rpx;
						text-decoration: line-through;
					}
				}
			}
		}
	}

	.service-boxs {

		.service-content {
			width: 100%;
			padding: 0 20rpx;
			height: calc(100% - 100rpx);
			display: flex;
			justify-content: center;
			align-items: center;

			// padding: 30rpx;
			.district-service {
				width: 100%;
				white-space: nowrap;
				height: 222rpx;

				.district-service-items {
					display: flex;
					align-items: stretch;

					.district-service-item {
						width: 145rpx;
						height: 222rpx;
						border-radius: 20rpx;
						margin-right: 25rpx;

						&:last-child {
							margin-right: 0;
						}

						image {

							width: 145rpx;
							height: 222rpx;
							border-radius: 20rpx;
						}

					}
				}
			}

			// <scroll-view class="district-service" scroll-x="true">
			// 	<div class="district-service-items">
			// 		<block v-for="item in yiSever.themeModuleVoList">
			// 			<view class="district-service-item" @click.stop="$openPage(item)">
			// 				<image :src="item.moduleImage" mode="aspectFit"></image>
			// 			</view>
			// 		</block>
			// 	</div>
			// </scroll-view>
		}
	}
</style>