<template>
	<view class="content" v-if="isPage">
		<far-bottom></far-bottom>
		<view class="history-main dis-flex flex-y-center p-left-right-30">
			<view class="city onelist-hidden dis-flex flex-y-center" @click="navBack">
				<view class="location-icon iconfont icon-locationfill"></view>
				<view class="history-btn col-3 f-28" v-if="!!addressData">{{ addressData['ad_info']['city'] }}</view>
				<view class="select-up-icon iconfont icon-unfold"></view>
			</view>
			<view class="history-input dis-flex flex-y-center p-left-right-30 p-r">
				<view class="history-icon iconfont icon-search"></view>
				<input type="text" value="" placeholder="请输入当前位置" class="f-24" v-model="searchText" />
			</view>
		</view>
		<view class="current-address-main dis-flex flex-x-between">
			<view class="current-address f-28 col-3" v-if="!!addressData">{{ addressData['formatted_addresses']['recommend'] }}</view>
			<view class="again-lot dis-flex">
				<view class="again-icon iconfont icon-focus"></view>
				<view class="again-lot-title f-28" @click="againCurrentlat">重新定位</view>
			</view>
		</view>
		<view class="nearby-main">
			<view class="nearby-address dis-flex flex-y-center m-left-right-30">
				<view class="nearby-icon iconfont icon-dibiao"></view>
				<view class="f-28 col-9">附近地址</view>
			</view>
			<view class="address-list">
				<view class="list-item" v-if="!!addressData && addressData['pois']['length'] > 0" v-for="(item, index) in addressData['pois']"
				 :key="index" @click="navGate(item)">
					<view class="list-title col-3 f-30 m-btm20">{{ item.title }}</view>
					<view class="list-title col-9 f-24">{{ item.address }}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import App from '@/common/js/app.js';
	// #ifdef H5
	import wxApi from '@/common/js/wxApi.js';
	// #endif
	export default {
		components: {},
		data() {
			return {
				city_name: '', //城市名,
				city_id: '', //城市ID,
				city_lat: '', //{城市经纬度}
				city_lng: '',
				isCurentAdres: '', //是否当前城市 1:是 0:不是
				//addressData: null, //城市数据
				skip_addressData: null, //跳转城市数据
				LocaTion_addressData: null, //定位城市数据
				isPage: false, //页面加载完成
				searchText: '', //搜索内容
				navgateTourl: '', //跳转地址,
				agencyData: ""
			};
		},
		computed: {
			addressData() {
				if (!!this.LocaTion_addressData || !!this.skip_addressData) {
					if (!!this.LocaTion_addressData) {
						if (this.LocaTion_addressData['ad_info']['city_code'] === this.skip_addressData['ad_info']['city_code']) {
							return this.LocaTion_addressData;
						} else {
							return this.skip_addressData;
						}
					} else {
						return this.skip_addressData;
					}
				}
			}
		},
		onLoad(options) {
			uni.showLoading({
				title: '定位中...'
			});
			let _this = this,
				{
					lat,
					lng,
					city,
					id,
					isCurrentAddress,
					url,
					loca
				} = options,
				city_lat = lat ? lat : '',
				city_lng = lng ? lng : '',
				city_name = city ? city : options.name,
				city_id = id ? id : '',
				isCurentAdres = isCurrentAddress ? isCurrentAddress : '0',
				agencyData = uni.getStorageSync('agencyData'),
				navgateTourl = url ? url : '';
			_this.setData({
				city_lat,
				city_lng,
				city_name,
				city_id,
				isCurentAdres,
				navgateTourl,
				agencyData
			});
			if (loca === 'now') {
				// #ifdef H5
				wxApi.wxRegister(_this.againCurrentlat);
				// #endif
				// #ifdef MP
				App.getLocation().then(res => {
					_this.getCurrentcity(res.latitude, res.longitude, 'again');
				});
				// #endif
			} else {
				//选择城市
				_this.getCurrentlat(city_lat, city_lng, city_id);
			}
		},
		methods: {
			//城市
			getCurrentlat(city_lat="", city_lng="", city_id) {
				let _this = this,
					requestData = {};
				if (!!city_lat && !!city_lng) {
					requestData = {
						lat: city_lat,
						lng: city_lng,
						area_code: '1'
					};
				} else {
					requestData = {
						citycode: city_id,
						area_code: '1'
					};
				}
				App._post_form(
					'&do=cityLocation',
					requestData,
					res => {
						_this.setData({
							skip_addressData: res.data,
							isPage: true
						});
					},
					false,
					() => {
						uni.hideLoading();
					}
				);
			},
			wxApiCallback() {
				let _this = this;
				jWeixin.ready(() => {
					jWeixin.getLocation({
						type: 'gcj02',
						success: function(res) {
							_this.getCurrentcity(res.latitude, res.longitude);
						}
					});
				});
			},
			againCurrentlat() {
				let _this = this;
				uni.showLoading({
					title: '定位中...'
				});
				// #ifdef H5
				jWeixin.ready(() => {
					jWeixin.getLocation({
						type: 'gcj02',
						success(res) {
							_this.getCurrentcity(res.latitude, res.longitude);
						}
					});
				});
				// #endif
				// #ifndef H5
				App.getLocation().then(res => {
					_this.getCurrentcity(res.latitude, res.longitude);
				});
				// #endif
			},
			getCurrentcity(lat="", lng="", type = '') {
				let _this = this,
					requestData = {
						lat,
						lng,
						area_code: '1'
					};
				App._post_form(
					'&do=cityLocation',
					requestData,
					res => {
						let addressData = res.data;
						_this.setData({
							LocaTion_addressData: addressData
						});
					},
					false,
					() => {
						uni.hideLoading();
					}
				);
			},
			citySearch(searchText) {
				let _this = this;
				if (searchText === '') return;
				App._post_form(
					'&do=citySearch', {
						city_name: _this.addressData['ad_info']['city'],
						keyword: searchText
					},
					res => {
						_this.addressData['pois'] = res.data;
					},
					false,
					() => {}
				);
			},
			navBack() {
				uni.navigateBack();
			},
			cityTaptime(curCityInfo) {
				let _this = this,
					currentCity = _this.currentCity,
					agencyData = _this.agencyData,
					timestamp = new Date().getTime();
				if (agencyData.areaid !== curCityInfo.ad_info.adcode) {
					uni.setStorageSync('cityTimestamp', timestamp);
				}
			},
			navGate(locationInfo) {
				let pages = getCurrentPages(),
					locationArray = uni.getStorageSync('locationArray'),
					navType = pages.length > 1 ? 'reLaunch' : 'rediRect',
					_this = this,
					newLocationArray = [];
					console.log(locationInfo);
				 App.getAreaId(locationInfo.location,locationInfo.title,"", (options) => {
					console.log(options,'2')
					_this.cityTaptime(locationInfo);
					App.navigationTo({
						url: 'pages/mainPages/index/index',
						navType: navType
					});
					uni.setStorageSync('agencyData', options.data);
				});
				if (locationArray) {
					let haveArray = locationArray.filter(item => (item.ad_info ? item.ad_info.city : item.name) === locationInfo.ad_info
						.city).length !== 0;
					if (haveArray) {
						return;
					} else {
						newLocationArray.push(locationInfo, ...locationArray);
						uni.setStorageSync(`locationArray${App.siteInfo.uniacid}`, newLocationArray);
					}
				} else {
					newLocationArray.push(locationInfo);
					uni.setStorageSync(`locationArray${App.siteInfo.uniacid}`, newLocationArray);
				}
			}
		},
		watch: {
			searchText(newValue, oldValue) {
				this.citySearch(newValue);
			}
		}
	};
</script>

<style lang="scss" scoped>
	.history-main {
		height: 120upx;

		.city {
			width: 170upx;
			margin-right: 26upx;
		}

		.history-input {
			flex: 1;
			background: #f6f6f6;
			border-radius: 38upx;
			height: 80upx;

			.history-icon {
				font-size: 30upx;
				margin-right: 20upx;
				color: #959595;
			}
		}

		.history-btn {
			margin: 0 12upx;
		}

		.select-up-icon {
			font-size: 24upx;
		}
	}

	.current-address-main {
		border-bottom: 1px solid #f0f0f0;
		padding: 40upx 30upx;

		.again-lot {
			.again-icon {
				margin-right: 8upx;
				color: #fe504f;
				font-size: 28upx;
			}

			.again-lot-title {
				color: #fe504f;
			}
		}
	}

	.nearby-address {
		padding: 40upx 0;
		border-bottom: 1px solid #f0f0f0;

		.nearby-icon {
			color: #333333;
			font-size: 28upx;
			margin-right: 14upx;
		}
	}

	.address-list {
		.list-item {
			padding: 40upx 0;
			margin: 0 68upx;
			border-bottom: 1px solid #f0f0f0;
		}
	}
</style>
