<template>
	<view>
		<view class="flex content1">
			<view @click='placeSelect' class='address flex'>
				<view class='detailedAddress'>{{detailedAddress1}}</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  align-item-center">
						<image class="dSLComVueTopLeftImg" style="width: 30rpx;height: 30rpx;flex: none;"
							src="@/static/image/card/search.png"></image>
						<view class="search-val"> {{searchVal?searchVal:'搜索店铺或服务 '}}</view>
					</view>

					<view class="right">
						<image style='width:32rpx;height:32rpx;' v-if="searchVal" @click.stop="delSearchVal"
							src="@/static/image/card/searchdel.png" mode="widthFix"></image>
					</view>
				</view>
			</view>
		</view>
		<view>
			<mescroll-uni :up="upOption" :down="downOption" ref="mescrollRef" @init="mescrollInit" @up="upCallback"
				@down="downCallback" :top="108">
				<u-swiper :list="fileList" autoplay :height='height' imgMode='aspectFill' radius='0' interval='5000'
					:circular="true" @change="e => current = e.current" @click="swiperClick">
					<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 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="86px"
									height="86px" @click="click"></u--image>
							</view>
						</view>
						<view class="right">
							<view class='flex'>
								<view class="main-body">
									{{item.mainBody=="商铺"?'商铺':'个人'}}
								</view>
								<!-- <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="3" expandText="展开"
									foldHint="收起" />
							</view>
						</view>
					</view>
					<view class='address flex justify-space-between'>
						<view class='flex'>
							<view class='iconfont applet-dizhi'></view>
							<view @click.stop="toShopDetail(item)">{{item.detailedAddress}}</view>
						</view>
						<view>{{item.distance1>1000?item.distance:item.distance1}}
							<text style='font-weight:600;'>{{item.distance1>1000?'km':'m'}}</text>
						</view>
					</view>
				</view>
			</mescroll-uni>
			<!-- <mescroll-body ref="mescrollRef" :up="upOption" :down="downOption" @init="mescrollInit" @up="upCallback" @down="downCallback"></mescroll-body> -->
		</view>
		<login-pop-up ref='loginpopup' :content='"手机登录后才能发布内容哦~"'></login-pop-up>
		<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 GDMapWX = require('@/js_sdk/js-amap/amap-wx.130.js');
	import {
		authorizedLocation
	} from '@/util/util.js'
	import MoteLinesDivide from "@/components/text-over-flow/text-over-flow.vue"
	import loginPopUp from "@/components/loginPopUp/index.vue"
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		components: {
			MoteLinesDivide,
			loginPopUp
		},
		mixins: [MescrollMixin],
		data() {
			return {
				searchVal: uni.getStorageSync("search_val") ? uni.getStorageSync("search_val") : "",
				current: 0,
				mescroll: null,
				fileList: [],
				findList: [],
				handleList: [],
				height: '',
				width: '',
				longitude: "122.084565",
				latitude: '40.220547',
				curPageLen: 0,
				totalPage: 0,
				detailedAddress: '正在定位...',
				detailedAddress1: '',
				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() {
			if (uni.getStorageSync("search_val")) {
				this.searchVal = uni.getStorageSync("search_val")
				this.mescroll.resetUpScroll()
			}
			this.height = uni.getSystemInfoSync().windowWidth / 2.35;
			this.width = uni.getSystemInfoSync().windowWidth;
			if (this.detailedAddress == '正在定位...') {
				this.isdingwei()
			}
		},
		onShareAppMessage(res) {
			debugger
			let path = `/pages/find/find?id=${this.searchVal}`
			if (res.from === 'button') {
				return {
					title: `为您找到${this.totalPage}条“${this.searchVal}”服务 `,
					path: path,
				};
			}
			if (res.from === 'menu') {
				return {
					title: `为您找到${this.totalPage}条“${this.searchVal}”服务 `,
					path: path,
				};
			}
		},
		methods: {
			swiperClick(e){
				console.log(e)
				if(e==0){
					uni.navigateTo({
						url:"/pageA/cxTable"
					})
				}
				
			},
			toShopDetail(val){
				console.log(val)
				uni.navigateTo({
					url: "/pageA/food/detailMap?val="+JSON.stringify(val)
				})
				
			},
			delSearchVal() {
				this.searchVal = ''
				uni.removeStorageSync("search_val")
				that.mescroll.resetUpScroll()
			},
			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 = res.name ? res.name : _address.Village
						that.detailedAddress1 = that.detailedAddress.length > 4 ? that.detailedAddress.slice(0,
							4) + '...' : that.detailedAddress
						console.log(that.detailedAddress, that.detailedAddress1)
						uni.setStorageSync("detailedAddress", that.detailedAddress)
						that.$forceUpdate()
						that.upCallback({
							num: 1,
							size: 10
						})
					}
				});
			},
			isdingwei() {
				authorizedLocation().then(res => {
					let _obj = {}
					if (res == '取消授权') {
						//获取上一次，无上一次山海广场
						let _place = uni.getStorageSync("LocationPlace")
						if (_place && _place.latitude) {
							_obj = {
								latitude: _place.latitude,
								longitude: _place.longitude
							}
						} else {
							_obj = {
								latitude: 40.22086204872,
								longitude: 122.08338497727
							}
						}

					} else {
						_obj = {
							latitude: res.latitude,
							longitude: res.longitude
						}
					}
					this.longitude = _obj.longitude
					this.latitude = _obj.latitude
					var amapPluginInstance = new GDMapWX.AMapWX({
						key: '6bafe91754a563ff2b7c02542c1ef4e8'
					});
					amapPluginInstance.getRegeo({
						success: function(res) {
							console.log(res)
							that.detailedAddress = res[0].desc
							that.detailedAddress1 = that.detailedAddress.length > 4 ? that
								.detailedAddress.slice(0, 4) + '...' : that.detailedAddress
							console.log(that.detailedAddress, that.detailedAddress1)
							that.mescroll.resetUpScroll()
							//成功回调
						},
						fail: function(info) {
							//失败回调
							console.log(info)
						}
					})
				})
			},
			mescrollInit(mescroll) {
				// this.$refs.loginpopup.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: '数据加载中'
				})

				var finddata = await that.getfindList(page)
				if (page.num == 1) that.findList = [], that.handleList = [];
				that.curPageLen = finddata.data.items.length;
				that.handleList = finddata.data.items
				that.totalPage = finddata.data.total;
				if (that.handleList.length > 0) {
					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]
						that.handleList[i].distance1 = JSON.parse(JSON.stringify(that.handleList[i].distance))
						that.handleList[i].distance = (that.handleList[i].distance / 1000).toFixed(1)
						console.log(that.handleList)
						that.findList = that.handleList
						that.loading = false
					}
				}
				uni.hideLoading()
				that.$nextTick(() => {
					that.mescroll.endBySize(that.curPageLen, that.totalPage)
				});

				// }
			},
			add() {
				if (!uni.getStorageSync("userInfo").phone) {
					this.$refs.loginpopup.open()
				} else {
					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
				})
			},
			getfindList(page) {
				return new Promise((resolve, reject) => {
					that.$request.baseRequest('admin.tourism.productManagement', 'list', {
						page: page.num,
						limit: page.size,
						userLongitude: that.longitude,
						userLatitude: that.latitude,
						searchKeyWord: this.searchVal,
						// classify: this.typeName
					}, failres => {
						uni.showToast({
							icon: "none",
							title: failres.errmsg,
							duration: 3000
						});
						uni.hideLoading()
					}).then(res => {
						resolve(res)
					})
				})

			},
		}
	}
</script>

<style lang="scss" scoped>
	.content1 {
		position: fixed;
		top: 0;
		left: 0;
		padding: 20rpx;
		background-color: #fff;
		z-index: 99;
		width: 100vw;

		.search {
			// color: #9199af;
			// background: #f9d27d;
			border-radius: 50rpx;
			padding: 10rpx 0 10rpx 30rpx;
			box-sizing: border-box;
			margin-right: 20rpx;
		}

		.left {
			width: 80%;

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

		.right {
			// width:14%;
			margin-right: 20rpx
		}
	}

	.search-wrap {
		width: 76%;
		background: #f9f9f9;
		// border: 1px solid #f9d27d;
		border-radius: 50rpx;
	}

	.address {
		width: 24%;
		margin-right: 20rpx;
	}

	.findItem {
		background: #fff;
		margin: 10rpx;
		border-radius: 20rpx;
		padding: 20rpx;
		font-size: 32rpx;

		.main-body {
			color: #E95700;
			background: #FDF2E5;
			font-size: 12px;
			padding: 6rpx 14rpx;
			box-sizing: border-box;
			border-radius: 10rpx;
		}

		.left {
			margin-right: 20rpx;

			.cover {
				border-radius: 8rpx;
			}
		}

		.name {
			font-size: 30rpx;
			color: #333;
			margin-left: 10rpx;
			overflow-x: scroll;
			white-space: nowrap;
			width: 55vw;
			font-weight: 600;
			letter-spacing: 3rpx;
		}

		.description {
			color: #333;
			margin-top: 10rpx;
			width: 67vw;
			font-size: 28rpx;
		}

		.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;
		z-index: 2;
	}

	.applet-colors-tianjia2 {
		font-size: 100px;
	}
</style>