<template>
	<view class="scenic">
		<zs-skeleton type="scenic" :loading="loading"></zs-skeleton>
		<zs-header :background="background" title="景区门票"></zs-header>
		<view class="bg-box">
			<image class="bg" src="https://hyxhsh.oss-cn-chengdu.aliyuncs.com/63b7c68b71a69169d1b33f92/store/bdb/user/avatar/AHQWEtDgezfk6e611290c7c9ba152e04c5fe7c339cca.jpg/1.jpg" mode=""></image>
			<view class="fixed-box">
				<view class="search-box" @click="handleSearch">
					<view class="city-box">
				  <image class="icon" src="@/static/blue-position.png" mode=""></image>	
				  <view class="city">
					{{city}}
				  </view>
					</view>
				  <input class="search" disabled v-model="value" type="text" placeholder="景点名称" />
				</view>
				
			</view>
		</view>
			<view class="more-box">
				<view class="title">
					猜你喜欢
				</view>
				<!-- <view class="btn-box">
					更多 <image class="btn" src="@/static/right.png" mode=""></image>
				</view> -->
			</view>
		
		<zs-list class="list" mt="20rpx" @load="loadMore" :status="status">
			<view class="left">
				<view class="item" v-for="item in list" :key="item.tripartiteJson.scenicId" @click="goDetail(item)">
					<zs-img :src="item.logoPath" width="340rpx" height="340rpx" mode="widthFix"></zs-img>
					<view class="info">
						<view class="title">
							{{item.tripartiteJson.scenicName}}
						</view>
						<view class="address-box">
						<view class="price-box">
							<view class="unit">
								￥
							</view>
							<view class="price">
								<!-- {{item.tripartiteJson.ticketList.length?item.tripartiteJson.ticketList[0].salePrice:0}} -->
								{{item.tripartiteJson.ticketList | filterMinPrice}}
							</view>
							<view class="text">
								起
							</view>
						</view>
							<!-- <view class="address">
								
							</view> -->
							<view class="distance">
								{{(item.shopVo.distance/1000).toFixed(2)}} KM
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="right">
				<view class="item" v-for="item in list1" :key="item.tripartiteJson.scenicId" @click="goDetail(item)">
					<zs-img :src="item.logoPath" width="340rpx" height="340rpx" mode="widthFix"></zs-img>
					<view class="info">
						<view class="title">
							{{item.tripartiteJson.scenicName}}
						</view>
						<view class="address-box">
							<view class="price-box">
								<view class="unit">
									￥
								</view>
								<view class="price">
								<!-- {{item.tripartiteJson.ticketList.length?item.tripartiteJson.ticketList[0].salePrice:0}} -->
								{{item.tripartiteJson.ticketList | filterMinPrice}}
								</view>
								<view class="text">
									起
								</view>
							</view>
							<!-- <view class="address">
								
							</view> -->
							<view class="distance">
								{{(item.shopVo.distance/1000).toFixed(2)}} KM
							</view>
						</view>
					</view>
				</view>
			</view>
	
		</zs-list>
	</view>
</template>

<script>
	import {getScenicList} from '@/api/scenic.js'
	import { search } from '@/api/shop.js';
	export default {
		data() {
			return {
				background:false,
				city: uni.getStorageSync('city'),
				value:'',
				status:'more',
				// query:{
				//   key: '黄山风景区',
				//   page: 1,
				//   pageSize: 20
				// },
				query:{
					queryName:'',
					menuId: 1,
					'location.lat':0,
					'location.lon':0,
					pageCurrent:1,
					pageSize:10
				},
				list:[],
				list1:[],
				loading:false,
			}
		},
		filters: {
			filterDistance: function(item) {
				let blocation = item.blocation.split(',')
				let location = JSON.parse(uni.getStorageSync('location'))
				/*参数：两地的经纬度数值*/
				    var radLat1 = location.latitude * Math.PI / 180.0;
				    var radLat2 = location.longitude* Math.PI / 180.0;
				    var a = radLat1 - radLat2;
				    var b = location.longitude* Math.PI / 180.0 - blocation[0]* Math.PI / 180.0;
				    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
				    Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
				    s = s * 6378137.0;//地球半径 单位米;
				    s = Math.round(s * 10000) / 10000 /1000 //输出为千米
				    s = s.toFixed(0)
				    return s
				
			},
			filterMinPrice(list){
				if(list.length){
					let arr = JSON.parse(JSON.stringify(list))
					arr.sort(function(a,b){ return a.salePrice - b.salePrice})
					return arr[0].salePrice
					console.log(arr);
				}else{
					return 0
				}
			}
		},
		methods: {
			handleSearch() {
				uni.navigateTo({
					url:'/scenic/search'
				})
			},
			goDetail(item){
				uni.navigateTo({
					url:`/scenic/detail?id=${item.tripartiteJson.scenicId}&epId=${item.shopVo.shopId}`
				})
			},
			loadMore(){
				// this.getScenicList()
				this.search()
			},
			getScenicList(){
				this.status = 'loading'
				getScenicList(this.query).then(res=>{
					this.loading = false
					if(res.state == 'Success'){
						let list = []
						let list1 = []
						res.content.data.rows.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)
						console.log(this.list,this.list1);
						if((this.list.length + this.list1.length)>= res.content.data.totalCount){
							this.status = 'noMore'
						}else{
							this.status = 'more'
							this.query.page++
						}
					}
				})
			},
			search() {
				if(this.status == 'noMore' || this.status == 'loading' || !this.query['location.lat']) return
				this.status = 'loading'
				search(this.query).then(res=>{
					if(res.state == 'Success'){
						this.loading = false
						let list = []
						let list1 = []
						let data = res.content.records
						data.map((item,index)=>{
							item.tripartiteJson = JSON.parse(item.tripartiteJson)
							if(index%2 == 0){
								list.push(item)
							}else{
								list1.push(item)
							}
						})
						
						// 存储原始数据
						this.list = this.list.concat(list)
						this.list1 = this.list1.concat(list1)
						console.log('加载数据',this.status,this.list,this.list1,this.query.pageCurrent);
						if(this.query.pageCurrent == res.content.pages){
							this.status = 'noMore'
						}else{
							this.status = 'more'
							this.query.pageCurrent++
						}
					}
				})
			}
		},
		onPageScroll(e) {
			if (e.scrollTop >= 50) {
				this.background = true
			} else {
				this.background = false
			}
		},
		onLoad() {
			this.loading = true
			uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						// 解析地址
						this.query['location.lat'] = res.latitude
						this.query['location.lon'] = res.longitude
						console.log(this);
						// 存储经纬度
						uni.setStorageSync('location',JSON.stringify({latitude:res.latitude,longitude:res.longitude}))
						this.search()
					}
			})
		}
	}
</script>

<style lang="scss" >
.scenic{
	background: #F9F9F9;
	min-height: 100vh;
	// padding: 0 24rpx ;
	.bg-box{
		width: 100%;
		height: 624rpx;
		position: relative;
		.bg{
			width: 100%;
			height: 624rpx;
		}
		.fixed-box{
			position: absolute;
			top: 150rpx;
			left: 0;
			padding: 20rpx 24rpx;
			z-index: 9;
			// background: #F9F9F9;
			.search-box{
				width: 702rpx;
				height: 72rpx;
				// padding: 16rpx;
				box-sizing: border-box;
				position: relative;
				z-index: 9;
				.city-box{
					position: absolute;
					left: 0;
					top: 50%;
					transform: translateY(-50%);
					display: flex;
					align-items: center;
					padding: 0 28rpx;
					border-right: 1rpx solid #F0F0F0;
					.icon{
						width: 30rpx;
						height: 30rpx;
					}
					.city{
						font-weight: 600;
						font-size: 28rpx;
						color: #222222;
						margin-left: 6rpx;
					}
				}
				.search{
					width: 702rpx;
					height: 72rpx;
					background: #FFFFFF;
					border-radius: 40rpx 40rpx 40rpx 40rpx;
					padding-left: 230rpx;
					padding-right: 100rpx;
					box-sizing: border-box;
				}
			}
		}
	}
		.more-box{
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 20rpx;
			padding: 0 24rpx;
			.title{
				font-weight: 600;
				font-size: 32rpx;
				color: #222222;
			}
			.btn-box{
				display: flex;
				align-items: center;
				font-weight: 300;
				font-size: 24rpx;
				color: #AAAAAA;
				.btn{
					width: 48rpx;
					height: 48rpx;
				}
			}
		}
	.zs-list{
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		margin: 0 24rpx;
		.item{
			background: #FFFFFF;
			border-radius: 16rpx;
			margin-bottom: 20rpx;
			.info{
				padding: 16rpx 16rpx 20rpx;
				width: 100%;
				box-sizing: border-box;
				.title{
					font-weight: 400;
					font-size: 24rpx;
					color: #222222;
					width: 300rpx;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
				.price-box{
					display: flex;
					align-items: flex-end;
					margin-top: 12rpx;
					.unit{
						font-weight: 600;
						font-size: 20rpx;
						color: $uni-color-primary;
					}
					.price{
						font-weight: 600;
						font-size: 36rpx;
						color: $uni-color-primary;
					}
					.text{
						font-weight: 300;
						font-size: 24rpx;
						color: #AAAAAA;
						margin-left: 6rpx;
					}
				}
				.address-box{
					display: flex;
					align-items: flex-end;
					justify-content: space-between;
					font-weight: 300;
					font-size: 20rpx;
					color: #AAAAAA;
					margin-top: 12rpx;
				}
			}
		}
	}
}
</style>


