<template>
	<view class="container">
		<!-- #ifdef MP -->
		<view class="" style="height: 44rpx;">
			
		</view>
		<!-- #endif -->
		<view class="topNav flex">
			<image @click="backIndex" class="back" src="../../static/icon/back.png" mode="widthFix"></image>
			<view class="inputbox flex">
				<image class="search" src="../../static/icon/search.png" mode="widthFix"></image>
				<input class="input" confirm-type='搜索' @confirm='searchData' placeholder="请输入城市或者门店" type="text"
					v-model="search" />
			</view>
		</view>
		<!-- <pickerAddress class="" @change="onCityClick">
			<view class="address flex">
				<view class="flex">
					<text class="text">{{province}}</text>
					<image class="tipButtom" src="../../static/icon/dom.png" mode="widthFix"></image>
				</view>
				<view class="flex">
					<text class="text">{{city}}</text>
					<image class="tipButtom" src="../../static/icon/dom.png" mode="widthFix"></image>
				</view>
				<view class="flex">
					<text class="text">{{district}}</text>
					<image class="tipButtom" src="../../static/icon/dom.png" mode="widthFix"></image>
				</view>
			</view>
		</pickerAddress> -->
		<scroll-view class="list-scroll-content" scroll-y @scrolltolower="storeList">
			<!-- 空白页 -->
			<!-- <empty v-if="loaded === true && orderList.length === 0"></empty> -->
			<!-- 订单列表 -->
			<view class="itemList">
				<view class="item flex" v-for="(item,index) in orderList" :key='index' @click="onChecked(item)">
					<view class="imgBox">
						<image class="logo" :src="item.image" mode="aspectFit"></image>
					</view>
					<view class="padding-l-20 flex-grow-true">
						<view class="title clamp flex">
							<view class="name">
								{{item.name}}
							</view>
							<view class="km" v-if="item.range">
								{{(item.range<1?item.distance+'m':item.range+'km') || index + 1 + 'km'}}
							</view>
						</view>
						<view class="flex addressBox margin-t-20">
							<image class="iconA margin-r-10" src="../../static/icon/shop.png" mode="">
							</image>
							<text class="clamp">
								{{item.detailed_address}}
							</text>
						</view>
						<view class="addressBox flex">
							<image class="iconA margin-r-10" src="../../static/icon/shopPhone.png" mode="">
							
							<text class="clamp">
								{{item.phone}}
							</text>
						</view>
						<view class="yysj">
							营业时间：{{item.day_time}}
						</view>
						
					</view>
					<view class="dh-wrap" @click.stop="todh(item)">
						<image src="../../static/icon/dh.png" mode=""></image>
					</view>
				</view>
			</view>
			<uni-load-more :status="loadingType"></uni-load-more>
		</scroll-view>
	</view>
</template>

<script>
	// #ifdef MP-WEIXIN
	import {
		openMap
	} from '@/utils/rocessor.js';
	// #endif
	import {
		storeList,
	} from '@/api/product.js';
	import {
		mapMutations,
		mapState
	} from 'vuex';
	export default {
		computed: {
			...mapState('user', ['address'])
		},
		data() {
			return {
				search: '', //查询内容
				loadingType: 'more',
				orderList: [],
				page: 1, //当前页数
				limit: 10, //每次信息条数
				province: '请选择', //省
				city: '请选择', //市
				district: '请选择', //区
				loaded: false,
				type: 0 ,//默认为切换商店，1为不切换
				select: 0,//0为不选择商店，1为选择商店

			};
		},
		onLoad: function(option) {
			console.log(option.type);
			if (option.type) {
				this.type = option.type;
			}
			if(option.select) {
				this.select = option.select;
			}
			this.getlocation()
		},
		methods: {
			...mapMutations('shop', ['setShopInfo']),
			...mapMutations('user',['setAddress']),
			// 获取经纬度
			getlocation() {
				let obj = this;
				// #ifdef H5 || APP
				uni.getLocation({
					type: 'gcj02',
					success(e) {
						obj.getlocationSetInit(e)
					},
					fail(e) {
						if(e.errCode==22){
							uni.showModal({
								title: '定位开启错误',
								content: '请查看是否已经开启定位服务',
								showCancel: false,
							});
						}
						console.log(e, 'cw');
					}
				})
				// #endif
				// #ifdef MP-WEIXIN
				openMap().then((e) => {
					console.log("授权");
					wx.getLocation({
						type: 'gcj02',
						success: (e) => {
							console.log(e);
							// 调用鉴定位置变化事件
							this.getlocationSetInit(e)
						},
						fail() {
						}
					})
				})
				// #endif
			},
			// 获取经纬度完毕后回调处理
			getlocationSetInit(data) {
				const obj = this;
				try {
					console.log(data, '位置数据');
					// 保存当前经纬度
					obj.setAddress({
						latitude: data.latitude,
						longitude: data.longitude
					})
					this.storeList();
				} catch (e) {
					console.log(e, 'cww');
					this.storeList();
				}
			},
			toJSON() {
				return this;
			},
			backIndex() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			// 查询方法
			searchData() {
				// 初始化数据重新搜索
				this.loaded = false;
				this.page = 1;
				this.orderList = [];
				this.loadingType = 'more';
				this.storeList()
			},
			// 存储当前的门店
			onChecked(item) {
				// if (this.type == 0) {
				// 	this.setShopInfo(item);
				// 	uni.showToast({
				// 		title: '切换成功',
				// 		mask: true,
				// 		duration: 1500
				// 	});
				// 	setTimeout((e) => {
				// 		uni.switchTab({
				// 			url: '/pages/index/index'
				// 		})
				// 	}, 1500)
				// }
				// console.log();
				// if (+this.type == 1) {
				// 	this.setShopInfo(item);
				// 	uni.navigateTo({
				// 		url: '/pages/shop/shopIndex?id=' + item.id
				// 	})
				// }
				let that = this
				if(that.select == 1) {
					let prepage = that.$api.prePage();
					prepage.selctStore = item
					uni.navigateBack()
				}
			},
			storeList(source) {
				//这里是将订单挂载到tab列表下
				let navItem = this;
				if (source === 'tabChange' && navItem.loaded === true) {
					//tab切换只有第一次需要加载数据
					return;
				}
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return;
				}
				if (navItem.loadingType === 'noMore') {
					//防止重复加载
					return;
				}
				// 修改当前对象状态为加载中
				navItem.loadingType = 'loading';
				let city = `${this.province},${this.city},${this.district}`;
				if (this.province == '请选择') {
					city = '';
				}
				storeList({
						city,
						page: navItem.page,
						limit: navItem.limit,
						key: this.search,
						latitude: this.address.latitude,
						longitude: this.address.longitude,
					})
					.then(({
						data
					}) => {
						let arr = data.list.map(e => {
							return e;
						});
						navItem.orderList = navItem.orderList.concat(arr);
						// console.log(navItem.orderList);
						navItem.page++;
						if (navItem.limit == arr.length) {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'more';
							return;
						} else {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'noMore';
						}
						uni.hideLoading();
						this.$set(navItem, 'loaded', true);
						console.log(this, '结果');
					})
					.catch(e => {
						console.log(e);
					});
			},
			// 内容改变事件
			onCityClick(item) {
				const arr = item.data;
				this.province = arr[0];
				this.city = arr[1];
				this.district = arr[2];
				// 开始查询
				console.log(this.province, this.city, this.district);
				console.log(arr);
				this.searchData()
			},
			todh(item) {
				wx.openLocation({
					latitude: item.latitude * 1,
					longitude: item.longitude * 1,
					name: item.name,
					address: item.detailed_address
				})
			}
		},

	};
</script>

<style lang="scss">
	page,
	.container {
		height: 100%;
	}
	/* #ifdef APP */
	.container {
		padding-top: var(--status-bar-height);
	}
	/* #endif */

	.list-scroll-content {
		height: calc(100% - 44px);
		position: relative;
		overflow: hidden;
		z-index: 100;
	}

	.itemList {
		padding: 30rpx $page-row-spacing;

		.item {
			background-color: #FFFFFF;
			margin-bottom: 30rpx;
			padding: 30rpx;
			border-radius: 20rpx;
			position: relative;
			.list,.km{
				color: $font-color-light;
				font-size: $font-sm;
			}
			.logo {
				height: 200rpx;
				width: 200rpx;
				border-radius: 20rpx;
			}

			.title {
				font-size: 36rpx;
				color: $font-color-dark;
			}

			.iconR {
				height: 24rpx;
			}

			.iconA {
				width: 24rpx;
				height: 24rpx;
			}

			.addressBox {
				font-size: 24rpx;
				color: $font-color-light;
				justify-content: flex-start;
				padding: 10rpx 0;
			}
			.yysj {
				display: inline-block;
				// width: 239rpx;
				padding: 0 10rpx;
				height: 32rpx;
				background: #ddf8f1;
				border-radius: 16rpx 16rpx 16rpx 0rpx;
				font-size: 20rpx;
				font-weight: bold;
				color: #5FCCA7;
				text-align: center;
				line-height: 32rpx;
				margin-top: 10rpx;
			}
		}
	}

	.address {
		background-color: #FFFFFF;
		justify-content: space-around;
		height: 40px;

		.text {
			font-size: 22rpx;
			color: $font-color-dark;
			margin-right: 5px;
		}

		.tipButtom {
			width: 11px;
		}
	}

	.topNav {
		height: 44px;
		background-color: #FFFFFF;
		padding: 0 15px;
		/* #ifdef MP */
		height:calc(44px +  var(--status-bar-height)) ;
		padding-top: calc(var(--status-bar-height) - 15px);
		/* #endif */
		.back {
			width: 13px;
		}

		.inputbox {
			background-color: $page-color-base;
			height: 15px;
			flex-grow: 1;
			margin-left: 15px;
			border-radius: 50px;
			height: 30px;
			padding-left: 20px;
			/* #ifdef MP */
			margin-right: 200rpx;
			/* #endif */
			font-size: $font-base;

			.search {
				width: 18px;
			}

			.input {
				height: 100%;
				width: 100%;
				text-align: left;
				padding-left: 10px;

			}
		}
	}
	.dh-wrap {
		width: 40rpx;
		height: 40rpx;
		position: absolute;
		bottom:50rpx;
		right: 50rpx;
		image {
			width: 100%;
			height: 100%;
		}
	}
</style>
