<template>
	<view class="page" :style="themeColor">
		<view class="flex benben-position-layout flex flex-wrap align-center activityList_flex_0"
			:style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx'}">
			<view class='flex flex-wrap align-center justify-between flex-sub activityList_fd0_0'>
				<view class='flex flex-wrap align-stretch justify-center flex-sub'>
					<text class='activityList_fd0_0_c0_c0'>活动商家</text>
				</view>
			</view>

		</view>
		<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>
		<view v-if="isHandLocation && !lat" class="kaiqiweizhi flex align-center justify-between"
			:style="{top:(70 + StatusBar ) + 'px'}">
			<view class="flex align-center">
				<view class="" style="font-size: 32upx;font-weight: 550;">开启定位权限,显示周边商家及商品</view>
				<view class="kaiqiweizhi-button" @click="_getPermission">
					去开启
				</view>
			</view>
			<view class="text-df flex align-center">
				<image src="/static/close.png" @click="isHandLocation = false" style="width:32rpx;height: 32rpx;"
					mode="aspectFill"></image>
			</view>
		</view>
		<!---flex布局flex布局开始-->
		<block v-if="permissionStatus == 1">
			<fu-loading v-if="isShowLoading"></fu-loading>
			<fu-empty-ui v-if="list.length === 0 && isInit"></fu-empty-ui>
			<view class="flex flex-direction flex-wrap align-stretch benben-flex-layout activityList_flex_1"
				@tap.stop="handleJumpDiy" data-type="navigateTo" :data-url="`/`">
				<view class='flex flex-wrap align-start activityList_fd1_0' v-for="(item, index) in list" :key="index"
					@tap.stop="handleJumpDiy" data-type="navigateTo"
					:data-url="`/pages/hd/eventDetails/eventDetails?id=${item.aid}&name=${item.store_name}`" style="position: relative;">
					<view class="point-bg">
						<view class="point-bg-txt">积分{{item.score_rate}}%</view>
					</view>
					<view class='flex flex-direction flex-wrap align-start' style="position: relative;">
						<!-- <view class='flex flex-wrap align-center justify-center activityList_fd1_0_c0_c0'>
							<text class='activityList_fd1_0_c0_c0_c0'>积分</text>
							<text class='activityList_fd1_0_c0_c0_c1'>{{item.score_rate}}</text>
							<text class='activityList_fd1_0_c0_c0_c2'>%</text>
						</view> -->
						<view class='flex flex-wrap align-center'>
							<image class='activityList_fd1_0_c0_c1_c0' mode="aspectFill" :src='item.logo'></image>
						</view>
						<image style="width: 50rpx; height: 50rpx; position: absolute; top: 0; right: 0; z-index: 10;"
							v-if="item.storelevel_thumb" :src="item.storelevel_thumb" mode="aspectFill"></image>
					</view>
					<view class='flex flex-direction flex-wrap align-start flex-sub activityList_fd1_0_c1'>
						<view class='flex flex-direction flex-wrap align-start'>
							<view class='flex flex-wrap align-center'>
								<text class='activityList_fd1_0_c1_c0_c0_c0'>{{item.store_name}}</text>
							</view>
							<view class='flex align-center activityList_fd1_0_c1_c0_c1'>
								<image class='activityList_fd1_0_c1_c0_c1_c0' mode="aspectFit"
									:src='STATIC_URL+"163.png"'></image>
								<view class='activityList_fd1_0_c1_c0_c1_c1'>
									{{item.address_province + item.address_city + item.address_district + item.address_address}}
								</view>
							</view>
							<text class='storeDetails_fd2_0_c1_c1_c1'
								style="margin: 20rpx 20rpx 0 0; color: #FF9D00;font-size: 26rpx;"
								v-if="item.store_type">{{item.store_type}}</text>
						</view>
						<view class='flex flex-wrap align-center activityList_fd1_0_c1_c1'>
							<text class='activityList_fd1_0_c1_c1_c0'>{{item.distance}}</text>
							<text class='activityList_fd1_0_c1_c1_c1'>km</text>
						</view>
					</view>
				</view>

			</view>
			<uni-load-more v-if="list.length !=0 " :status="status"></uni-load-more>
		</block>
		<!---flex布局flex布局结束-->

		<block v-if="permissionStatus != 1">
			<fu-empty-ui @permission="handlePermission" v-if="permissionStatus == 0" emptyImage="addr"
				pagingListNoListDataText="暂无开启位置权限"></fu-empty-ui>
			<fu-empty-ui @permission="handlePermission" v-if="permissionStatus == -1" emptyImage="permission"
				pagingListNoListDataText="您已永久拒绝位置权限"></fu-empty-ui>
		</block>
	</view>
</template>
<script>
	import amap from '@/common/amap-wx.js';
	import permision from '@/common/permission.js'
	export default {
		components: {},

		data() {
			return {
				"data": [],
				lat: '',
				lng: '',
				list: [],
				isShowLoading: false,
				isInit: false,
				status: 'more', // 表示是否有数据可以请求
				page: 1,
				amapPlugin: null, //new地图
				key: '886bfe2d6c32bd8eac97c2e6d821ed0b',
				permissionStatus: 1,
				isHandLocation: false
			};
		},
		computed: {
			themeColor() {
				return this.$store.getters.themeColor
			},

		},
		watch: {},
		onLoad(options) {
			this.amapPlugin = new amap.AMapWX({
				key: this.key
			});
			this.judgeIsLocation()
			// uni.$on('LOCATION_SUCCESS', () => {
			//   this.judgeIsLocation()
			// })
			// if (global.lat && global.lng) {
			//   this.lat = global.lat
			//   this.lng = global.lng
			//   this.status = 'more';
			//   this.page = 1;
			//   this.isInit = false;
			//   this.list = [];
			//   this.getRecommendList(true);
			// } else {
			//   // #ifdef APP-PLUS
			//   this._getPermission();
			//   // #endif

			//   // #ifndef APP-PLUS
			//   this._getLocation()
			//   // #endif
			// }

		},
		onUnload() {

		},
		onReady() {

		},
		onShow() {
			// if (this.permissionStatus == -1) {
			//   this.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION');
			// }
			// let lat = uni.getStorageSync('LVTAI_LAT') || '';
			// let lng = uni.getStorageSync('LVTAI_LNG') || '';
			// // 定位功能
			// if (!lat) {
			// 	// #ifndef MP-WEIXIN
			// 	let platform = uni.getSystemInfoSync().platform == 'ios' ? 1 : 2
			// 	console.log(platform)
			// 	if (platform == 2) {
			// 		this.isHandLocation = true;
			// 	} else {
			// 		this._getLocation()
			// 		this.isHandLocation = false;
			// 	}
			// 	//  #endif
			// 	// #ifdef MP-WEIXIN 
			// 	this._getLocation()
			// 	this.toppop = false;
			// 	//  #endif
			// } else {
			// 	this._getLocation()
			// }
			// if (global.lat && global.lng) {
			//   this.lat = global.lat
			//   this.lng = global.lng
			//   this.status = 'more';
			//   this.page = 1;
			//   this.isInit = false;
			//   this.list = [];
			//   this.getRecommendList(true);
			// } else {
			//   this._getLocation()
			// }
		},
		onHide() {

		},
		onResize() {

		},
		onPullDownRefresh() {
			let lat = uni.getStorageSync('LVTAI_LAT') || '';
			let lng = uni.getStorageSync('LVTAI_LNG') || '';
			if (!lat) {
				// #ifndef MP-WEIXIN
				let platform = uni.getSystemInfoSync().platform == 'ios' ? 1 : 2
				console.log(platform)
				if (platform == 2) {
					this.isHandLocation = true;
				} else {
					this._getLocation()
					this.isHandLocation = false;
				}
				//  #endif
				// #ifdef MP-WEIXIN 
				this._getLocation()
				this.toppop = false;
				//  #endif
			} else {
				this._getLocation()
			}
			// if(this.lat){
			// 	this.status = 'more';
			// 	this.page = 1;
			// 	this.isInit = false;
			// 	this.list = [];
			// 	this.getRecommendList(true);
			// }else{
			// 	this._getLocation()
			// }




			// this.$refs.guess && this.$refs.guess.refresh();
			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onReachBottom(e) {
			if (this.lat && this.lng) {
				this.getRecommendList();
			}
		},
		onPageScroll(e) {

		},
		methods: {
			judgeIsLocation() {
				let lat = uni.getStorageSync('LVTAI_LAT') || '';
				let lng = uni.getStorageSync('LVTAI_LNG') || '';
				// 定位功能
				if (!lat) {
					// #ifndef MP-WEIXIN
					let platform = uni.getSystemInfoSync().platform == 'ios' ? 1 : 2
					console.log(platform)
					if (platform == 2) {
						this.isHandLocation = true;
					} else {
						this._getLocation()
						this.isHandLocation = false;
					}
					//  #endif
					// #ifdef MP-WEIXIN 
					this._getLocation()
					this.toppop = false;
					//  #endif
				} else {
					this._getLocation()
				}
			},
			handlePermission(type) {
				console.log(123)
				if (type == 1) {
					this.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION');
				} else {
					permision.gotoAppPermissionSetting()
				}
			},
			async requestAndroidPermission(permisionID) {
				console.log(123)
				var result = await permision.requestAndroidPermission(permisionID);
				var strStatus
				if (result == 1) {
					this._getLocation();
				}
				this.permissionStatus = result;
			},
			_getPermission() {
				const that = this;
				uni.getSystemInfo({
					success: (res) => {
						if (res.platform == 'android') {
							let isLocation = uni.getStorageSync('LVTAI_USER_LOCATION') || false;
							if (!isLocation) {
								that.$util.showModal({
									title: '提示',
									content: '获取您的定位权限以展示附近活动商家?',
									success: res => {
										if (res.confirm) {
											that.requestAndroidPermission(
												'android.permission.ACCESS_FINE_LOCATION');
										} else {
											that.permissionStatus = 0;
										}
									}
								});
							} else {
								this._getLocation();
							}
						} else {
							this._getLocation();
						}
					}
				});
			},

			/**
			 * 获取附近商家列表
			 * @param {Object} value
			 */
			getRecommendList(value) {
				if (this.status != 'more') return;
				this.status = 'loading';
				let data = {
					list_rows: 20,
					page: this.page,
					lat: this.lat,
					lng: this.lng
				};
				// console.log('请求的数据', data);
				if (this.page == 1 && value) {
					this.isShowLoading = true;
				}
				this.$api
					.post(global.apiUrls.getPromotionList, data)
					.then(res => {
						console.log('订单列表', res);
						this.isShowLoading = false;
						if (res.data.code == 1) {
							var curPageData = res.data.data.data;
							if (this.page == 1) this.list = [];
							this.list = this.list.concat(curPageData);
							console.log('上拉加载', curPageData.length, this.size);
							if (res.data.data.last_page > this.page) {
								this.status = 'more';
								this.page++;
							} else {
								this.status = 'noMore';
							}
						} else {
							this.$message.info(res.data.msg);
						}
						this.isInit = true;
						uni.stopPullDownRefresh();
					})
					.catch(err => {
						this.isShowLoading = false;
						uni.stopPullDownRefresh();
					});
			},




			_getLocation() {
				// #ifdef H5
				this.lat = '34.738695';
				this.lng = '113.64328';
				uni.setStorageSync('LVTAI_LAT', this.lat)
				uni.setStorageSync('LVTAI_LNG', this.lng)
				this.status = 'more';
				this.page = 1;
				this.isInit = false;
				this.list = [];
				this.address = '华城国际中心'
				this.getRecommendList(true);
				// #endif
				// #ifndef H5
				this.amapPlugin.getRegeo({
					success: res => {
						console.log(res[0]);
						// uni.$off('LOCATION_SUCCESS', ()=>{})
						// uni.$emit('LOCATION_SUCCESS', {})
						uni.setStorageSync('LVTAI_USER_LOCATION', true);
						this.address = res[0].regeocodeData.addressComponent.city;
						this.lat = res[0].latitude;
						this.lng = res[0].longitude;
						uni.setStorageSync('LVTAI_LAT', this.lat)
						uni.setStorageSync('LVTAI_LNG', this.lng)
						this.status = 'more';
						this.page = 1;
						this.isInit = false;
						this.list = [];
						this.getRecommendList(true);
					},
					fail: err => {

					}
				});
				// #endif

			}

		}
	};
</script>
<style lang="scss" scoped>
	.kaiqiweizhi-button {
		padding: 8rpx 10rpx;
		line-height: 1;
		background-color: #64BF1D;
		color: #fff;
		font-size: 24rpx;
		border-radius: 12rpx;
		margin-left: 18rpx;
	}

	.kaiqiweizhi {
		position: fixed;
		left: 24rpx;
		right: 24rpx;
		width: calc(100% - 48rpx);
		height: 80rpx;
		line-height: 80rpx;
		border-radius: 18rpx;
		padding: 0 24rpx;
		background: rgba(0, 0, 0, 0.8);
		z-index: 999;
		font-size: 24rpx;
		color: #fff;
	}

	.page {
		width: 100vw;
		overflow-x: hidden;
		min-height: calc(100vh - var(--window-bottom));
		background: rgba(246, 247, 249, 1);
		background-size: 100% auto !important;
	}

	.activityList_flex_0 {
		background: #fff;
		width: 750rpx;
		height: 88rpx;
		overflow: hidden;
		z-index: 10;
		top: 0rpx;
		background-size: 100% auto !important;
	}

	.activityList_fd0_0_c0_c0 {
		font-size: 36rpx;
		font-weight: 500;
		color: #333;
		line-height: 88rpx;
	}

	.activityList_fd0_0 {
		margin: 0rpx 32rpx 0rpx 32rpx;
	}

	.activityList_flex_1 {
		padding: 24rpx 24rpx 0rpx 24rpx;
	}

	.activityList_fd1_0_c1_c1_c1 {
		color: #333333;
		font-size: 24rpx;
		font-weight: 400;
		line-height: 24rpx;
	}

	.activityList_fd1_0_c1_c1_c0 {
		color: #333333;
		font-size: 24rpx;
		font-weight: 400;
		line-height: 24rpx;
	}

	.activityList_fd1_0_c1_c1 {
		margin: 20rpx 0rpx 0rpx 0rpx;
	}

	.activityList_fd1_0_c1_c0_c1_c1 {
		// width: 0;
		flex: 1;
		line-height: 30rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: #333333;
		-webkit-line-clamp: 1;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		// max-width: 475rpx;
	}

	.activityList_fd1_0_c1_c0_c1_c0 {
		width: 20rpx;
		height: 24rpx;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		margin: 0rpx 8rpx 0rpx 0rpx;
	}

	.activityList_fd1_0_c1_c0_c1 {
		margin: 16rpx 0rpx 0rpx 0rpx;
	}

	.activityList_fd1_0_c1_c0_c0_c0 {
		color: #333333;
		font-size: 32rpx;
		font-weight: 600;
		line-height: 50rpx;
	}

	.activityList_fd1_0_c1 {
		margin: 0rpx 0rpx 0rpx 24rpx;
	}

	.activityList_fd1_0_c0_c1_c0 {
		width: 180rpx;
		height: 180rpx;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
	}

	.activityList_fd1_0_c0_c0_c2 {
		color: #FFFFFF;
		font-size: 20rpx;
		font-weight: 500;
		line-height: 36rpx;
	}

	.activityList_fd1_0_c0_c0_c1 {
		color: #FFFFFF;
		font-size: 20rpx;
		font-weight: 500;
		line-height: 36rpx;
	}

	.activityList_fd1_0_c0_c0_c0 {
		color: #FFFFFF;
		font-size: 20rpx;
		font-weight: 500;
		line-height: 36rpx;
	}

	.activityList_fd1_0_c0_c0 {
		background: url(https://lvtai-files.oss-cn-beijing.aliyuncs.com/mini/23.png) no-repeat, rgba(255, 162, 31, 1);
		width: 128rpx;
		height: 36rpx;
		background-size: 100% auto !important;
		border-radius: 16rpx 0rpx 24rpx 0rpx;
		position: absolute;
		z-index: 10;
	}

	.activityList_fd1_0 {
		background: rgba(255, 255, 255, 1);
		margin: 0rpx 0rpx 24rpx 0rpx;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		background-size: 100% auto !important;
		padding: 24rpx 24rpx 24rpx 24rpx;
	}

	.point-bg {
		background: url(https://lvtai-files.oss-cn-beijing.aliyuncs.com/mini/point_bg.png) no-repeat;
		background-size: cover;
		position: absolute;
		top: -32rpx;
		left: -32rpx;
		z-index: 10;
		width: 112rpx;
		height: 112rpx;
	}

	.point-bg-txt {
		transform: rotate(315deg);
		font-size: 20rpx;
		color: #fff;
		margin-left: 12rpx;
		margin-top: 32rpx;
	}
</style>