<template>
	<view>
		<view class="flex content1">
			<view @click='placeSelect' class='address flex'>
			<view class='detailedAddress'>{{detailedAddress}}</view>
			<view class='iconfont applet-xiala'></view>
			</view>
			<view class=" search-wrap">
				<view class="search flex justify-space-between align-item-center flex-between" @click="search">
					<view class="left flex">
						<uni-icons type="search" size="24" color="#9199af"></uni-icons>
						<text class="search-val"> {{searchVal?searchVal:'搜索店铺或服务 '}}</text>
					</view>
			
					<view class="right">
						<!-- <image style='width:32rpx;height:32rpx;' v-if="searchVal" @click.native.stop="delSearchVal"
							src="../../static/imgs/card/searchdel.png" mode="widthFix"></image> -->
					</view>
				</view>
			</view>
		</view>
		<view style='margin-top:112rpx;'>
			<u-swiper :list="fileList"
			autoplay
			:height='height'
			imgMode='aspectFill'
			radius='0'
			:circular="true"
			@change="e => current = e.current" @click="click">
				<view slot="indicator" class="indicator">
					<view
			            class="indicator__dot"
			            v-for="(item, index) in fileList"
			            :key="index"
			            :class="[index === current && 'indicator__dot--active']">
			        </view>
				</view>
			</u-swiper>
		</view>
		<view>
			<mescroll-uni :up="upOption" :down="downOption" ref="mescrollRef" @init="mescrollInit"
				@up="upCallback" @down="downCallback" height="960">
				<view class='findItem' v-for='item in findList'>
					<view style='align-items: flex-start;' class='flex'>
						<view class="left">
							<view class='cover'>
								<u--image radius='4' :showLoading="true" :src="item.coverImage" width="80px" height="80px" @click="click"></u--image>
							</view>
						</view>
						<view class="right">
							<view class='flex'>
								<view v-if='item.mainBody=="商铺"' class='iconfont applet-dianpu1'></view>
								<u--image v-if='item.mainBody=="个人"'  :showLoading="true" src="/static/image/find/geren.png" width="20px" height="20px"></u--image>
								<view  class='name'>{{item.shopNames}}</view>
							</view>
							<view class='description'>
								<mote-lines-divide :dt="item.serviceDescription" :line="4" expandText="展开" foldHint="收起" />
								</view>
						</view>
					</view>
					<view class='address flex justify-space-between'>
						<view class='flex'>
							<view class='iconfont applet-dizhi'></view>
							<view>{{item.province}}{{item.city}}{{item.area}}</view>
						</view>
						<view>{{item.distance}}
						<text style='font-weight:600;'>km</text></view>
					</view>
				</view>
			</mescroll-uni>
			<!-- <mescroll-body ref="mescrollRef" :up="upOption" :down="downOption" @init="mescrollInit" @up="upCallback" @down="downCallback"></mescroll-body> -->
		</view>
		<view class='add' @click='add'>
			<u--image   :showLoading="true" src="/static/image/find/add.png" width="60px" height="60px"></u--image>
		</view>
	</view>
</template>

<script>
	var that;
	var QQMapWX = require('@/js_sdk/qqmap-wx-jssdk.min.js');
	 var qqmapsdk = new QQMapWX({
	        key: 'HQ6BZ-RMALQ-TGF5H-BXJQB-UFYN3-Q5BYM'
	    });
	import MoteLinesDivide from "@/components/text-over-flow/text-over-flow.vue"
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		components: {
			MoteLinesDivide
		},
		mixins: [MescrollMixin],
		data() {
			return {
				searchVal:uni.getStorageSync("search_val")?uni.getStorageSync("search_val"):"",
				current:0,
				mescroll:null,
				fileList:[],
				findList:[],
				handleList:[],
				height:'',
				width:'',
				longitude:uni.getStorageSync("findlongitude")?uni.getStorageSync("findlongitude"):"122.084565",
				latitude:uni.getStorageSync("findlatitude")?uni.getStorageSync("findlatitude"):'40.220547',
				curPageLen:0,
				totalPage:0,
				detailedAddress:uni.getStorageSync("detailedAddress")?uni.getStorageSync("detailedAddress"):'山海广场',
				downOption: {
					auto: false,
					textColor: '#bbb'
				},
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					auto: false,
					noMoreSize: 1,
					textNoMore: '没有更多了~',
					textColor: '#bbb'
				},
			};
		},
		onLoad() {
			that = this
		this.getList()
		// this.mescroll.resetUpScroll();
		},
		onShow(){
			this.height = uni.getSystemInfoSync().windowWidth/2;
			
			this.width = uni.getSystemInfoSync().windowWidth;
			console.log(this.height,this.width,11111111)
			
			
			if(!this.detailedAddress){
				// this.placeSelect()
			}else{
				that.upCallback({
					num:1,
					size:10
				})
			}
		},
		methods: {
			search() {
				// if (this.userInfo.phone) {
					uni.navigateTo({
						url: "/pageA/find/search"
					})
				// } else {
				// 	this.showAuthorizePhone = true
				// }
			},
			placeSelect() {
				uni.chooseLocation({
					success: function(res) {
						console.log(res);
						that.latitude = res.latitude
						that.longitude = res.longitude
						uni.setStorageSync("findlatitude",res.latitude)
						uni.setStorageSync("findlongitude",res.longitude)
						let _address = that.$helper.formatLocation(res.address)
						that.detailedAddress = _address.Village
						console.log(that.detailedAddress)
						uni.setStorageSync("detailedAddress",_address.Village)
						that.$forceUpdate()
						that.upCallback({
							num:1,
							size:10
						})
					}
				});
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			downCallback() {
				if (uni.getStorageSync("userInfo").phone) {
					this.mescroll.resetUpScroll()
				} else {
					that.mescroll.endBySize(0, 0)
					this.showAuthorizePhone = true
				}
			
			},
			getDistance(latitude,longitude){
				console.log(latitude,longitude,that.latitude,that.longitude)
				return new Promise((resolve, reject) => {
					qqmapsdk.direction({
					                    mode: 'driving', //可选值：'driving'（驾车）  trucking 货车
					                    //from参数不填默认当前地址
					                    // latitude纬度    longitude 经度
					                    from: {
					                        latitude: that.latitude,
					                        longitude: that.longitude
					                    },
					                    to: {
					                        latitude: latitude,
					                        longitude:longitude
					                    },
					                    size: 4, // 车型 1: 微型车  2: 轻型车 3: 中型车 4: 重型车
					                    policy: 'LEAST_TIME', //'9',  //参考实时路况，高速优先，尽量躲避拥堵
					                    height: 4,
					                    width: 2.5,
					                    length: 13,
					                    weight: 6.8,
					                    axle_weight: 34,
					                    axle_count: 6,
					                    is_trailer: 1,
					                 success: function(res1, data) {
										 console.log(res1,data[0].distance,data)
										 resolve(data[0])
										 
									 }
					})
				})
			},
			async upCallback(page) {
					// if (uni.getStorageSync("userInfo").id) {
						uni.showLoading({
							title: '数据加载中'
						})
						await that.$request.baseRequest('admin.tourism.productManagement', 'list', {
							page: page.num,
							limit: page.size,
							// searchContent: this.searchVal,
							// classify: this.typeName
						}, failres => {
							uni.showToast({
								icon: "none",
								title: failres.errmsg,
								duration: 3000
							});
							uni.hideLoading()
						}).then(res => {
							if (page.num == 1) that.findList = [],that.handleList = [];
							that.curPageLen = res.data.items.length;
							that.handleList = res.data.items
							that.totalPage = res.data.total;
							
						})
						for(var i=0;i<that.handleList.length;i++){
							that.handleList[i].latitude=that.handleList[i].location.split(',')[0]
							that.handleList[i].longitude=that.handleList[i].location.split(',')[1]
							var data=await that.getDistance(that.handleList[i].latitude,that.handleList[i].longitude) 
							that.handleList[i].distance=(data.distance/1000).toFixed(2)
							console.log(data)
							if(i==that.handleList.length-1){
								setTimeout(()=>{
									console.log(that.handleList)
									that.findList = that.handleList
									that.loading = false
									that.$nextTick(() => {
										that.mescroll.endBySize(that.curPageLen, that.totalPage)
									});	
									uni.hideLoading()
								},100)
							}
						}
			
					// }
			},
		 add(){
			uni.navigateTo({
				url: '/pageA/find/createLifeService'
			})
		 },
		 getList(){
			 this.$request.baseRequest('admin.tourism.carouselManagement', 'list', {
			 	page: 1,
			 	limit: 9999,
				classify:'发现'
			 }, failres => {
			 	uni.showToast({
			 		icon: "none",
			 		title: failres.errmsg,
			 		duration: 3000
			 	});	
			 }).then(res => {
			 	this.fileList=res.data.items
			 })
		 }
		}
	}
</script>

<style lang="scss" scoped>
	
	.content1 {
		position: fixed;
		top:0;
		left:0;
		padding: 20rpx;
		background-color: #fbb612;
		z-index:99;
		width:95vw;
		.search {
			// color: #9199af;
			// background: #f9d27d;
			border-radius: 50rpx;
			padding: 10rpx 0 10rpx 30rpx;
			box-sizing: border-box;
			margin-right: 20rpx;
		}

		.left {
			width: 80%;

			text {
				height: 46rpx;
				white-space: nowrap;
				overflow: scroll;
				position: relative;
				margin-left: 20rpx;
				color:#9199af;
			}
		}

		.right {
			// width:14%;
			margin-right: 20rpx
		}
	}
	.search-wrap{
		width:78%;
		background: #ffff;
		border: 1px solid #f9d27d;
		border-radius: 50rpx;
	}
	.address{
		width:22%;
	}
	.findItem{
		background:#fff;
		margin:10rpx;
		border-radius:20rpx;
		padding:20rpx;
		font-size:32rpx;
		.left{
			margin-right:20rpx;
			.cover{
				border-radius: 8rpx;
			}
		}
		.name{
			font-size:36rpx;
			color:#333;
			margin-left:10rpx;
		}
		.description{
			color:#666;
			margin-top:10rpx;
		}
		.address{
			margin-top:20rpx;
			width:100%;
			font-size:28rpx;
			color:#999;
		}
	}
	.applet-dianpu1{
		font-size:44rpx;
		color:#eaad1a;
	}

	.applet-dizhi{
		font-size:38rpx;
		color:#393733;
	}
	.add{
		// background:#fff;
		border-radius:50%;
		position: fixed;
		right:0;bottom:10px;
	}
	.applet-colors-tianjia2{
		font-size:100px;
	}
</style>
