<template>
	<view class="content">
		<u-sticky offset-top="0"><my-locationback></my-locationback></u-sticky>
		<u-list @scrolltolower="scrolltolower">

			<view class="headModel">
				<u-cell-group :border="false">
					<u-cell :border="false" title="热门活动" @click="navigateToPage('hotActivity')" value="全部"
						:isLink="true"></u-cell>
				</u-cell-group>
				<view class="swiperModel">
					<u-swiper :list="list" keyName="imgUrl" height="225" circular :autoplay="false" radius="5"
						bgColor="#ffffff"></u-swiper>
				</view>
				<!-- 轮播图 -->
				<view class="baseModel">
					<view class="indexTypeList">
						<view class="indexType" @click="navigateToPage('redemptionCode')">
							<view class="indexTypeIcon">
								<image class="init_image" src="http://amy.yyinhong.cn/uploads/wximg/index_zgm.png"
									mode="">
								</image>
							</view>
							<view class="indexTypeName">
								直购码兑换
							</view>
						</view>
						<view class="indexType" @click="navigateToPage('companyProfile')">
							<view class="indexTypeIcon">
								<image class="init_image" src="http://amy.yyinhong.cn/uploads/wximg/index_gsjs.png"
									mode="">
								</image>
							</view>
							<view class="indexTypeName">
								公司介绍
							</view>
						</view>
						<view class="indexType" v-if="is_examine != 1" @click="navigateToPage('healthStation')">
							<view class="indexTypeIcon">
								<image class="init_image" src="http://amy.yyinhong.cn/uploads/wximg/index_ysxz.png"
									mode="">
								</image>
							</view>
							<view class="indexTypeName">
								养生小站
							</view>
						</view>
						<view class="indexType" @click="navigateToPage('inviteFriends')">
							<view class="indexTypeIcon">
								<image class="init_image" src="http://amy.yyinhong.cn/uploads/wximg/index_yqhy.png"
									mode="">
								</image>
							</view>
							<view class="indexTypeName">
								邀请好友
							</view>
						</view>
					</view>
				</view>
			</view>

			<view class="recommendModel">
				<u-cell-group :border="false">
					<u-cell :border="false" title="推荐商品"></u-cell>
				</u-cell-group>

				<u-list-item v-for="(item, index) in pageList" :key="index">
					<view class="productModel" @click="$utils.goProductDetails(item.id)">
						<view class="productImg">
							<image class="init_image" :src="item.cover_img" mode=""></image>
						</view>
						<view class="pruductInfo">
							<view class="productName">
								<span class="certifiedIcon"
									v-show="item.tags.field1 && item.tags.field1 != ''">{{item.tags.field1}}</span>
								{{item.goods_name}}
							</view>
							<view class="productEvaluate">
								<span v-show="item.tags.field2 && item.tags.field2 != ''">{{item.tags.field2}}</span>
								<span v-show="item.tags.field3 && item.tags.field3 != ''"
									style="margin-left: 5px;">{{item.tags.field3}}</span>
							</view>
							<view class="productPrice">
								<span class="productPriceIcon">¥</span>{{item.goods_price}}
								<span
									style="text-decoration-line: line-through;font-size: 10px;margin-left: 5px;color: #999999;"
									v-show="priceFlag">零售价
									¥{{item.market_price}}</span>
								<span class="productByeNum" style="font-size: 10px;margin-left: 5px;"
									v-show="item.tags.field4 && item.tags.field4 != ''">{{item.tags.field4}}</span>
							</view>
							<view class="productTagList">
								<view class="productTag"
									style="background-image: url('http://amy.yyinhong.cn/uploads/wximg/sptop.png');background-repeat: no-repeat;background-size: 100% 100%;background-position: 0 0;color: #ffffff;border: none;padding: 0 2px"
									v-if="item.tags.field5 && item.tags.field5 != ''&&item.tags.field6 && item.tags.field6 != ''">
									<span style="margin-left: 5px;"></span>{{item.tags.field5.substring(0, 4)}}<span
										style="margin-left: 15px;"></span> {{item.tags.field6.substring(0, 2)}}
								</view>
								<view class="productTag" v-show="item.tags.field7 && item.tags.field7 != ''">
									{{item.tags.field7}}
								</view>
								<view class="productTag" v-show="item.tags.field8 && item.tags.field8 != ''">
									{{item.tags.field8}}
								</view>
							</view>
							<view class="pruductfrom"
								v-if="item.tags.field9 && item.tags.field9 != '' && item.tags.field10">
								<span class="pruductfromLeft">{{item.tags.field9}}</span>
								<view class="pruductfromRight">
									{{item.tags.field10}}
								</view>
							</view>
						</view>
					</view>
				</u-list-item>

			</view>
		</u-list>
		<!-- <my-privacy-popup ref="privacyComponent" position="center"></my-privacy-popup> -->
		<u-toast ref="uToast"></u-toast>
		<my-tabbar :currentTab='0' />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				pageList: [],
				islogin: 0,
				is_examine: null,
				priceFlag: false,
				buycode: '',
				userInfo: ''
			}
		},
		onLoad(option) {
			if (option.q || uni.getStorageSync('inviteCode')) {
				let code = uni.getStorageSync('inviteCode')
				if (option.q) {
					console.log(option.q)
					let url = decodeURIComponent(option.q);
					code = url.split("=")[1];
					uni.setStorageSync('inviteCode', code);
				}
				this.$utils.user_share(code);
			}
		},
		onShow() {
			if (uni.getStorageSync('codeTime')) {
				console.log(uni.getStorageSync('codeTime'))
				let beforTime = uni.getStorageSync('codeTime')
				if (this.$utils.isTimeMoreThanOneDayFromNow(beforTime)) {
					console.log('时间超过一天')
					this.priceFlag = false
				} else {
					console.log('时间未超过一天')
					this.priceFlag = true
				}
			}
			this.getCarousel()
			this.getCommondList();
		},
		methods: {
			//获取轮播图
			getCarousel() {
				this.$request('/carousel', 'GET', {}).then(res => {
					console.log('请求成功');
					if (res.data.info) {
						this.list = res.data.info;
						this.is_examine = res.data.is_examine;
					} else {
						this.list = res.data;
					}

				}).catch(err => {
					uni.showToast({
						icon: 'error',
						title: '' + err.message
					})
				})
			},
			//获取用户信息
			async getUserInfo(url) {
				let _this = this
				await _this.$request('/user-info', 'GET', {}).then(res => {
					console.log(res.data.buycode);
					if (_this.priceFlag) {
						_this.buycode = res.data.buycode
					}
					_this.$request(url, 'GET', {
						buycode: _this.buycode
					}).then(res => {
						console.log(res.data)
						_this.pageList = res.data.list;
					}).catch(err => {
						uni.showToast({
							icon: 'error',
							title: '' + err.message
						})
					})
				}).catch(err => {
					uni.showToast({
						icon: 'error',
						title: '' + err.message
					})
				})
			},
			//获取推荐商品列表
			getCommondList() {
				let param = {}
				let url = uni.getStorageSync('token') ? "/recommend-good-auth" : '/recommend-good';
				if (url == '/recommend-good-auth') {
					this.getUserInfo('/recommend-good-auth');
				} else {
					this.$request(url, 'GET', param).then(res => {
						console.log(res.data)
						this.pageList = res.data.list;
					}).catch(err => {
						uni.showToast({

							icon: 'error',

							title: '' + err.message

						})
					})
				}
			},
			scrolltolower() {
				// console.log('+10')
				// this.pageList += 10;
			},
			// 通用页面跳转方法
			navigateToPage(page) {
				if (!uni.getStorageSync('token')) {
					uni.showToast({
						title: "请先登录！"
					});
					uni.reLaunch({
						url: '/pages/login/login'
					})
					return
				}
				let url = '';
				switch (page) {
					case 'hotActivity':
						url = '/pageSub-index/pages/activityList/activityList';
						break;
					case 'redemptionCode':
						url = '/pageSub-index/pages/redemptionCode/redemptionCode';
						break;
					case 'companyProfile':
						url = '/pageSub-index/pages/companyProfile/companyProfile';
						break;
					case 'healthStation':
						url = '/pageSub-index/pages/healthStation/healthStation';
						break;
					case 'inviteFriends':
						url = '/pageSub-index/pages/inviteFriends/inviteFriends';
						break;
					default:
						console.warn('Unknown page:', page);
						return;
				}
				uni.navigateTo({
					url: url
				});
			}
		}
	}
</script>
<style lang="scss" scoped>
	.content {
		width: 100vw;
		height: 100vh;
		overflow-y: scroll;
		background: #ffffff;
	}

	::v-deep .u-swiper {
		background-color: inherit !important;
	}

	.headModel {
		width: 100%;
		min-height: 700rpx;
		padding-bottom: 30rpx;
		background: linear-gradient(to bottom, #ffffff, #F7F8FA);
	}

	.swiperModel {
		width: 100%;
		height: 225px;
	}

	.baseModel {
		width: calc(100% - 140rpx);
		margin: 0 40rpx;
		margin-top: 20rpx;
		padding: 30rpx;
		background-color: #ffffff;
		border-radius: 32rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
	}


	.indexTypeList {
		width: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		margin-top: 20rpx;
	}

	.indexType {
		width: calc(100% / 4);
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
	}

	.indexTypeIcon {
		width: 72rpx;
		height: 72rpx;
	}

	.indexTypeName {
		width: 100%;
		height: 34rpx;
		text-align: center;
		font-size: 24rpx;
		color: #444444;
	}

	.recommendModel {
		width: 100%;
		padding: 20rpx 0;
		background-color: #ffffff;
		border-radius: 32rpx;
		padding-bottom: 186rpx;
	}
</style>