<template>
	<view>
		<!-- 门店列表 -->
		<view class="shop-list">
			<u-card v-for="(item, index) in shopList" :key="index" @click="onSelectedShop(item)"
				class="shop-item dis-flex flex-y-center" :border="false" :box-shadow="true" :show-head="false"
				:head-border-bottom="false">
				<view class="" slot="body">
					<view class="shop-item__content flex-box">
						<view class="u-body-item u-flex u-col-between u-p-t-0">
							<u-avatar :src="item.logoUrl" :size="120" mode="square" :show-level="true" ></u-avatar>
							<view class="shop-info">
								<view class="title">{{item.shopName}}</view>
								<view class="about">联系电话：{{item.phone}}</view>
								<view class="address">地址：{{ item.region.province }}{{ item.region.city }}{{ item.region.region }}{{ item.address }}</view>
								<view v-if="item.distance" class="shop-item__distance">
									<text class="iconfont icon-dingwei"></text>
									<text class="f-24">{{ item.distanceUnit }}</text>
								</view>
							</view>
						</view>
					</view>
					<!-- 选中状态 -->
					<!-- <view v-if="item.shop_id == selectedId" class="shop-item__right">
						<text class="iconfont icon-check1"></text>
					</view> -->
				</view>
			</u-card>
		</view>
		<!-- 定位按钮 -->
		<view v-if="!isAuthor" class="widget-location dis-flex flex-x-center flex-y-center" @click="onAuthorize()">
			<text class="iconfont icon-locate"></text>
		</view>
		<empty v-if="!shopList.length" :isLoading="isLoading" tips="亲，暂无关注的站点哦" />

		<!-- <view class="shop-list">
			<u-card v-for="(item,index) in list" :key="index" :border="false" :box-shadow="true" :show-head="false"
				:head-border-bottom="false">
				<view class="" slot="body">
					<view class="u-body-item u-flex u-col-between u-p-t-0">
						<image class="shop-avatar"
							:src="item.logo_url"
							mode="widthFix"></image>
						<view class="shop-info">
							<view class="title">{{item.shop_name}}</view>
							<view class="about">{{item.summary}}</view>
						</view>



					</view>
				</view>
			</u-card>
		</view> -->
	</view>
</template>

<script>
	import Search from '@/components/page/diyComponents/search/index.vue'
	import {
		getMoreListData
	} from '@/core/app'
	import * as ShopApi from '@/api/shop.js'
	import Empty from '@/components/empty'

	export default {
		components: {
			Search,
			Empty
		},
		data() {
			return {
				title: '国内馆',
				// 正在加载中
				isLoading: true,
				// 是否授权了定位权限
				isAuthor: true,
				// 当前选择的门店ID
				selectedId: null,
				// 订单列表数据
				shopList: [],

			}
		},
		onLoad({
			selectedId
		}) {
			const app = this
			// 记录当前选择的门店ID
			app.selectedId = selectedId ? selectedId : null
			// 获取默认门店列表
			app.getShopList()
			// 获取用户坐标
			app.getLocation(res => {
				app.getShopList(res.longitude, res.latitude)
			})
		},
		methods: {
			// 获取门店列表
			getShopList(longitude, latitude) {
				const app = this
				app.isLoading = true
				ShopApi.list({
						isCheck: 1,
						longitude,
						latitude,
						follow: true
					})
					.then(result => app.shopList = result.data.list)
					.finally(() => app.isLoading = false)
			},

			// 获取用户坐标
			// 参考文档：https://uniapp.dcloud.io/api/location/location?id=getlocation
			getLocation(callback) {
				const app = this
				uni.getLocation({
					type: 'wgs84',
					success: callback,
					fail() {
						app.$toast('获取定位失败，请点击右下角按钮重新尝试定位')
						app.isAuthor = false
					}
				})
			},

			// 授权启用定位权限
			onAuthorize() {
				const app = this
				// #ifdef MP
				uni.openSetting({
					success(res) {
						if (res.authSetting['scope.userLocation']) {
							console.log('定位权限授权成功')
							app.isAuthor = true
							setTimeout(() => {
								// 获取用户坐标
								app.getLocation((res) => {
									app.getShopList(res.longitude, res.latitude)
								})
							}, 1000)
						}
					}
				})
				// #endif
				// #ifdef H5
				// 获取用户坐标
				app.getLocation((res) => {
					app.getShopList(res.longitude, res.latitude)
				})
				// #endif
			},

			/**
			 * 选择门店
			 */
			onSelectedShop(item) {
				this.$navTo('package/shops/detail/index',item)
			},

			changeCity() {
				this.$toast('切换区域')
			}
		}
	}
</script>

<style lang="scss" scoped>
	.bg {
		background-color: #F5F5F8
	}

	.shop-list {
		.shop-item {
		}
	}

	.header-box {
		padding: 20rpx 36rpx 0 36rpx;
		display: flex;
		justify-content: space-between;

		.location {
			display: flex;
			justify-content: center;
			align-items: center;

			image {
				width: 60rpx;
				height: 60rpx
			}
		}

		.u-subsection {
			width: 260rpx;
			margin-top: 7rpx
		}
	}

	.search-wrapper {
		display: flex;
		width: 80%;
		height: 64rpx
	}

	// 搜索输入框
	.search-input {
		width: 90%;
		background: #fff;
		border-radius: 10rpx 0 0 10rpx;
		box-sizing: border-box;
		overflow: hidden;

		.search-input-wrapper {
			display: flex;

			.left {
				display: flex;
				width: 60rpx;
				justify-content: center;
				align-items: center;

				.search-icon {
					display: block;
					color: #b4b4b4;
					font-size: 28rpx
				}
			}

			.right {
				flex: 1;

				input {
					font-size: 28rpx;
					height: 64rpx;
					display: flex;

					.input-placeholder {
						color: #aba9a9
					}
				}
			}
		}
	}

	.shop-avatar {
		width: 200rpx;
		margin-right: 24rpx
	}

	// 搜索按钮
	.search-button {
		width: 25%;
		box-sizing: border-box;

		.button {
			height: 64rpx;
			font-size: 28rpx;
			border-radius: 0 5px 5px 0;
			background: #2C71C7
		}
	}

	.shop-info {
		width: 100%;
		margin-left: 20rpx;

		.title {
			font-size: 38rpx;
			font-weight: bold;
			padding-bottom: 10rpx;
		}
	}

	.u-body-item {
		align-items: stretch !important
	}
</style>
