<template>
	<view>
			<!-- 展馆图标 -->
			<view class="header-box">
				<view class="location" @click="changeCity">
					附近站点推荐
				</view>
			</view>
			<!-- 门店列表 -->
			<view class="shop-list">
				<view class="item" v-for="(item, index) in shopList" :key="index"
					@click="onSelectedShop(item.merchantId,item.merchantCode)">
					<image :src="item.logo" mode="aspectFit"></image>
					<view class="info">
						<text class="title">{{item.merchantName}}</text>
						<text class="desc">{{item.address}}</text>
						<view class="tag">
							<view class="mr12">
								<u-tag size="mini" plain text="租车站"></u-tag>
							</view>
							<view class="mr12">
								<u-tag size="mini" type="warning" plain text="换点站"></u-tag>
							</view>
							<view class="mr12">
								<u-tag size="mini" type="success" plain text="买车站"></u-tag>
							</view>
						</view>
						<text class="distance">距离4.2km</text>
					</view>
				</view>
			</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 == 0" :isLoading="isLoading" tips="请先登录" />
	</view>
</template>

<script>
	import store from '@/store/index.js'
	import storage from '../../utils/storage'
	import { ACCESS_TOKEN, USER_ID } from '@/store/mutation-types'
	import { pageMerchant } from '@/websoft/api/merchant.js'
	import Empty from '@/components/empty'
	import { tenantId } from '@/config.js';

	export default {
		components: {
			// Search,
			Empty
		},
		data() {
			return {
				title: '国内馆',
				// 正在加载中 
				isLoading: false,
				// 是否授权了定位权限
				isAuthor: true,
				// 当前选择的门店ID
				selectedId: null,
				// 订单列表数据
				shopList: []
			}
		},
		onLoad() {
			const app = this
			if(!uni.getStorageSync('userId')){
				console.log("未登录1: ");
				return false;
			}
			// 获取用户坐标
			app.getLocation(res => {
				app.getShopList(res.longitude, res.latitude)
			})
		},
		onShow() {
			const app = this
			console.log("app.shopList.length: ",app.$store.getters.token);
			if(app.shopList.length == 0){
				app.getShopList(app.longitude,app.latitude)
			}
		},
		methods: {
			// 获取门店列表
			getShopList(longitude, latitude) {
				const app = this
				app.isLoading = true
				pageMerchant({tenantId})
					.then(result => {
						if(result.code == 0){
							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({
					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
			},

			navTo(merchantId) {
				const navTo = uni.$u.route()
				navTo('pages/merchant/detail', {
					merchantId
				})
			},

			/**
			 * 选择门店
			 */
			onSelectedShop(merchantId,merchantCode) {
				uni.$u.route('pages/merchant/detail', {
					merchantId,merchantCode
				})
			},

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

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

	.shop-list {
		.item {
			padding: 20rpx;
			margin: 20rpx auto;
			background-color: #ffffff;
			width: 660rpx;
			border-radius: 20rpx;
			display: flex;
			align-items: center;

			image {
				width: 180rpx;
				height: 180rpx;
				margin-right: 40rpx;
				background-color: #f9f9f9;
			}

			.info {
				display: flex;
				flex-direction: column;

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

				.desc {
					font-size: 28rpx;
				}

				.tag {
					font-size: 28rpx;
					display: flex;

					.mr12 {
						margin-right: 12rpx;
					}
				}

				.distance {
					font-size: 28rpx;
				}
			}
		}
	}

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

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

			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;
		}

		.about {
			display: flex;

			.mr12 {
				margin-right: 12rpx;
			}
		}
	}

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