<template>
	<view class="home">
		<view class="login-home">
			<image class="img" src="https://oss.dev.zonelife.cn/static/guida/img/life/hold_on2.png"></image>
			<view class="text">
				印象生活，尽请期待...
			</view>
		</view>
		<zs-tab-bar :value="2"></zs-tab-bar>
	</view>
</template>

<script>
	import {
		getUserDetail,
		getMenu,
		getAdv
	} from '@/api/common.js'
	import { search } from '@/api/shop.js';
	// 腾讯地图
	var QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
	var qqmapsdk = new QQMapWX({key:'KX5BZ-B64RC-RO62W-AMWAZ-VVTC3-YAFXF'});
	export default {
		data() {
			return {
				background:false,
				loading: false,
				userId: '',
				city:'定位中',
				status: 'more',
				current:0,
				bannerList:[],
				discountsList: [],//菜单
				advList:[],
				advList1:[],
				holdList:[
					{
						advertsImg:'https://oss.dev.zonelife.cn/static/guida/img/life/hold_on.jpg',
						id:1
					}
				],
				list: [],
				list1: [],
				query:{
					queryName:'',
					'location.lat':0,
					'location.lon':0,
					pageCurrent:1,
					pageSize:10
				},
				
			}
		},
		
		methods: {
			// swiper变动
			swiperChange(val) {
				this.current = val.target.current
			},
			jump(url){
				uni.reLaunch({
					url
				})
			},
			// 点击搜索
			handleSearch(){
				if(!uni.getStorageSync('token')){
					return uni.showModal({
						title:'请登录',
						confirmText:'去登录',
						success(res){
							console.log(res);
							if(res.confirm){
								uni.navigateTo({
									url:'/login/login/login?redirect=/pages/index/index'
								})
							}
						}
					})
				}
				uni.reLaunch({
					url:'./search'
				})
				// if(!val) return
				// this.query.queryName = val
				// this.query.pageCurrent = 1
				// this.list = []
				// this.list1 = []
				// this.status = 'more'
				// this.search()
			},
			// 获取当前城市
			getCity(){
				let that = this
				return new Promise((resolve,reject)=>{
					uni.getLocation({
							type: 'gcj02',
							success: (res) => {
								// 解析地址
								that.query['location.lat'] = res.latitude
								that.query['location.lon'] = res.longitude
								// 存储经纬度
								uni.setStorageSync('location',JSON.stringify({latitude:res.latitude,longitude:res.longitude}))
								qqmapsdk.reverseGeocoder({
									location: {
										latitude: res.latitude,
										longitude: res.longitude
									},
									success: function(res) {
										// 市
										that.city = res.result.address_component.city
										console.log(9999,that.city,res);
										if(uni.setStorageSync('districtList')&&uni.getStorageSync('city')==that.city) return
										uni.setStorageSync('city',res.result.address_component.city)
										// 获取市区的行政区
										qqmapsdk.getDistrictByCityId({
										  // 传入对应省份ID获得城市数据，传入城市ID获得区县数据,依次类推
										  id: res.result.ad_info.city_code.substr(3), //对应接口getCityList返回数据的Id，如：北京是'110000'
										  success: function(res) {//成功后的回调
										    console.log('对应城市ID下的区县数据：', res.result[0]);
											let data = res.result[0]
											data.map(item=>{
												item.label = item.fullname
											})
											uni.setStorageSync('districtList',JSON.stringify(data))
										  },
										  fail: function(error) {
										    console.error(error);
										  },
										  complete: function(res) {
										    console.log(res);
										  }
										});
										resolve()
									},
									fail: function(res) {
										that.city = '定位失败'
									},
								})
								  
								
							},
							fail: () => {
								console.log("获取经纬度失败");
							},
						})
				})
			},

			// 跳转优惠详情
			goDetail(url,id,banner,title) {
				uni.navigateTo({
					url: `${url}?id=${id}&title=${title}`,
					success(res) {
						res.eventChannel.emit('banner', banner)
					}
				})
			},
			goGoodsDetail(item) {
				uni.setStorageSync('shopInfo', JSON.stringify(item))
				uni.reLaunch({
					url: `../../detail/goodsDetail/index?id=${item.goodsVos[0].goodsId}`
				})
			},

			// 更多套餐
			handleMore() {
				uni.reLaunch({
					// url:'../../login/login/login'
					url: '../../combo/combo/combo'
				})
			},

			// 获取用户详情
			getUserDetail() {
				getUserDetail().then(res => {
					uni.setStorageSync('userInfo', JSON.stringify(res.content))
				})
			},
			loadMore() {
				if(this.query['location.lat']&&this.query['location.lon']){
					this.search()
				}
			},
			search() {
				if(this.status == 'noMore') return
				this.status = 'loading'
				search({
					...this.query,
					belongType:2,
					isExceptGZU:true
				}).then(res=>{
					if(res.state == 'Success'){
						this.loading = false
						let list = []
						let list1 = []
						let data = res.content.records
						data.map((item,index)=>{
							if(index%2 == 0){
								list.push(item)
							}else{
								list1.push(item)
							}
						})
						this.list = this.list.concat(list)
						this.list1 = this.list1.concat(list1)
						if(this.query.pageCurrent == res.content.pages){
							this.status = 'noMore'
						}else{
							this.status = 'more'
							this.query.pageCurrent++
						}
						if(!this.advList1.length){
							this.getGoodsAdv().then(()=>{
								this.advList1.map(item=>{
									if(item.showSort<= (this.list.length + this.list1.length)){
										if((item.showSort-1)%2 == 0){
											this.list.splice(Math.ceil(item.showSort/2)-1,0,{isAdv:true,...item})
										}else{
											this.list1.splice(Math.ceil(item.showSort/2)-1,0,{isAdv:true,...item})
										}
									}
								})
							}
						  )
						}else{
							this.advList1.map(item=>{
								if(item.showSort<= (this.list.length + this.list1.length)){
									if((item.showSort-1)%2 == 0){
										this.list.splice(Math.ceil(item.showSort/2)-1,0,{isAdv:true,...item})
									}else{
										this.list1.splice(Math.ceil(item.showSort/2)-1,0,{isAdv:true,...item})
									}
								}
							})
						}
						// 获取商品里面的广告
						// getAdv({currentPage:1,pageSize:99,advertsType:2,status:1}).then(res1=>{
						// 	if(res.state == 'Success'){
						// 		res1.content.records.map(item=>{
						// 			data.splice(item.showSort-1,0,{isAdv:true,...item})
						// 		})
						// 		console.log(data);
						// 		data.map((item,index)=>{
						// 			if(index%2){
						// 				list.push(item)
						// 			}else{
						// 				list1.push(item)
						// 			}
						// 		})
						// 		this.list = this.list.concat(list)
						// 		this.list1 = this.list1.concat(list1)
						// 		// let total = this.list.length+this.list1.length
						// 		// if(total>=res.content.total){
						// 		if(this.query.pageCurrent == res.content.pages){
						// 			this.status = 'noMore'
						// 		}else{
						// 			this.status = 'more'
						// 			this.query.pageCurrent++
						// 		}
						// 	}
						// })
						
						
					}
				})
			},
			// 金刚区
			getMenu(){
				return new Promise((resolve,reject)=>{
					getMenu({belongType: 1,currentPage:1,pageSize:10,status:2}).then(res=>{
						if(res.state == 'Success'){
							this.discountsList = res.content.records
							resolve(1)
						}
					})
				})
			},
			// 广告位
			getAdv(){
				return new Promise((resolve,reject)=>{
					getAdv({belongType:2,currentPage:1,pageSize:99,advertsType:1,status:1}).then(res=>{
						if(res.state == 'Success'){
							this.advList = res.content.records
							// this.advList = res.content.records.filter((item,index)=>index<2)
							// this.advList1 = res.content.records.filter((item,index)=>index>=2)
							resolve(2)
						}
					})
				})
			},
			// 获取商品列表内广告位
			getGoodsAdv(){
				return new Promise((resolve,reject)=>{
					getAdv({belongType:2,currentPage:1,pageSize:99,advertsType:2,status:1}).then(res=>{
						if(res.state == 'Success'){
							this.advList1 = res.content.records
							resolve(2)
						}
					})
				})
			},
		},
		onPageScroll(e) {
				if(e.scrollTop >= 50){
					this.background = true
				}else{
					this.background = false
				}
		},
		onShow() {
			if (uni.getStorageSync('token')&&!uni.getStorageSync('userInfo')) {
				this.getUserDetail()
			}
		},
		onHide() {},
		onLoad(query){
			
			 uni.setStorageSync('inviteCode',decodeURIComponent(query.scene))
		},
		created() {
			this.loading = true
			Promise.all([this.getMenu(),this.getAdv(),this.getCity()]).then(res=>{
				this.search()
			})
			
		}
	}
</script>

<style lang="scss">
	.home {
		// padding-bottom:120rpx;
		// background: linear-gradient(180deg, #FFFFFF 0%, #DCE8FF 100%);
		background: #F9F9F9;

		.card-title {
			display: flex;
			justify-content: center;

			.title {
				font-size: 32rpx;
				text-align: center;
				color: #fff;
				width: 210rpx;
				line-height: 64rpx;
				background: linear-gradient(360deg, #3074F8 0%, #568FFF 100%);
				border-radius: 0rpx 0rpx 24rpx 24rpx;
			}

		}

		.login-home {
			display: flex;
			flex-direction: column;
			align-items: center;
			// justify-content: center;
			height: 100vh;
			.img {
				margin-top: 524rpx;
				width: 252rpx;
				height: 252rpx;
			}

			.text {
				font-size: 32rpx;
				color: #CCCCCC;
				margin-top: 28rpx;
			}
		}

	}
</style>