<!-- 选择城市页面 -->
<template>
	<view>
		<view class="input">
			<input placeholder="输入城市名或拼音查询" placeholder-style="font-size: 13px" :value="inputName" @input="bindKeyInput"
				@blur="bindBlur" />
		</view>

		<view class="container-inner">
			<view class="searchLetter touchClass">
				<view class="thishotText" @click="hotCity">
					<view style="margin-top:0;">当前</view>
					<view style="margin-top:0;">热门</view>
				</view>
				<view v-for="(item, idx) in searchLetter" :key="idx" style="color:#8BC34A;font-size:20upx;"
					:data-letter="item.name" @click="clickLetter">
					{{ item.name }}
				</view>
			</view>

			<view class="container">

				<view v-if="isShowLetter">
					<view class="showSlectedLetter">
						{{ toastShowLetter }}
					</view>
				</view>

				<scroll-view scroll-y="true" v-bind:style="{height: winHeight + 'px'}" :scroll-into-view="scrollTopId">
					<view class="ul" id="completelist">
						<view class="li" v-for="(item, idx) in completeList" :key="idx" :data-city="item.city"
							:data-code="item.code" @click="bindCity">
							{{ item.city }}
						</view>
					</view>


					<view class="selectCity">
						<view class="hotcity-common" @click="reGetLocation" id="currentcity">定位城市</view>
						<view class="thisCityName" @click="bindCity" :data-code="currentCityCode"
							:data-city="currentCity">{{ currentCity }}</view>

						<view class="hotcity-common">热门城市</view>
						<view class="weui-grids">
							<view class="weui-grid" :data-code="110100" data-city="北京市" @click="bindCity">
								<view class="weui-grid__label">北京市</view>
							</view>
							<view class="weui-grid" :data-code="310100" data-city="上海市" @click="bindCity">
								<view class="weui-grid__label">上海市</view>
							</view>
							<view class="weui-grid" :data-code="440100" data-city="广州市" @click="bindCity">
								<view class="weui-grid__label">广州市</view>
							</view>
						</view>
						<view class="weui-grids">
							<view class="weui-grid" :data-code="440300" data-city="深圳市" @click="bindCity">
								<view class="weui-grid__label">深圳市</view>
							</view>
							<view class="weui-grid" :data-code="330100" data-city="杭州市" @click="bindCity">
								<view class="weui-grid__label">杭州市</view>
							</view>
							<view class="weui-grid" :data-code="320100" data-city="南京市" @click="bindCity">
								<view class="weui-grid__label">南京市</view>
							</view>
						</view>
						<view class="weui-grids">
							<view class="weui-grid" :data-code="420100" data-city="武汉市" @click="bindCity">
								<view class="weui-grid__label">武汉市</view>
							</view>
							<view class="weui-grid" :data-code="120100" data-city="天津市" @click="bindCity">
								<view class="weui-grid__label">天津市</view>
							</view>
							<view class="weui-grid" :data-code="610100" data-city="西安市" @click="bindCity">
								<view class="weui-grid__label">西安市</view>
							</view>
						</view>
					</view>

					<view class="selection" v-for="(item, idx) in cityList" :key="idx">
						<view class="item_letter" :id="item.initial">{{ item.initial }}</view>
						<view class="item_city" v-for="(cityItem, index) in item.cityInfo" :key="index"
							:data-code="cityItem.code" :data-city="cityItem.city" @click="bindCity">
							{{cityItem.city}}
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import commonData from '../../commonData.js'
	import commonFun from '../../commonFun.js'

	import city from '../../common/city.js';
	import district from '../../common/district.js';
	// console.log(district)
	export default {
		data() {
			return {
				currentCityCode: '120100',
				searchLetter: [],
				showLetter: '',
				winHeight: 0,
				cityList: [],
				isShowLetter: false,
				// 置顶id
				scrollTopId: '',
				hotcityList: [],
				inputName: '',
				completeList: [],
				condition: false,
				detailAddress: '',
				toastShowLetter: '',
				currentCity: '北京市'
			};
		},
		onLoad() {
			const searchLetter = city.searchLetter;
			const cityList = city.cityList();
			const sysInfo = uni.getSystemInfoSync();
			// console.log(sysInfo);
			const winHeight = sysInfo.windowHeight;
			const itemH = winHeight / searchLetter.length;
			let tempArr = [];

			searchLetter.map(
				(item, index) => {
					let temp = {};
					temp.name = item;
					temp.tHeight = index * itemH;
					temp.bHeight = (index + 1) * itemH;
					tempArr.push(temp);
				}
			);

			this.winHeight = winHeight;
			this.itemH = itemH;
			this.searchLetter = tempArr;
			this.cityList = cityList;
			this.getLocation();
		},
		methods: {
			getLocation() {
				commonFun.getPositionLocal(res => {
					this.currentCity = res.city || '北京市'
				})
			},
			clickLetter(e) {
				const showLetter = e.currentTarget.dataset.letter;
				this.toastShowLetter = showLetter;
				this.isShowLetter = true;
				this.scrollTopId = showLetter;

				const self = this;
				setTimeout(() => {
					self.isShowLetter = false;
				}, 500);
			},
			reGetLocation() {
				// this.getLocation();
			},
			// 选择城市触发
			bindCity(e) {
				this.condition = true;
				//缓存选择的城市
				uni.setStorageSync('currentSelectCity', e.currentTarget.dataset.city);
				// let code = e.currentTarget.dataset.code
				let city = e.currentTarget.dataset.city
				let code = ''
				console.log(city)
				for (let i in district) {
					if (i.length == 8) {
						for (let j in district[i]) {
							if (district[i][j] == city) {
								code = j
							}
						}
					}
				}
				for (let i in district) {
					if (i.indexOf(code) != -1 && i.indexOf(0) == 0) {
						uni.setStorageSync('currentSelectDistrict', JSON.stringify(district[i]));
						uni.reLaunch({
							url: './index?city = ' + city
						})
						// uni.navigateBack({
						// 	delta: 1
						// });
						return
					}
				}
				uni.navigateBack({
					delta: 1
				});
				return
			},
			hotCity() {
				this.scrollTopId = 'currentcity';
			},
			bindScroll(e) {
				// console.log(e.detail);
			},
			bindBlur(e) {
				this.inputName = '';
			},
			bindKeyInput(e) {
				this.inputName = e.mp.detail.value;
				// 空搜索框时 取消匹配显示
				if (this.inputName.length < 1) {
					this.completeList = [];
				}
				this.scrollTopId = 'completelist';
				this.auto();
			},
			auto() {
				let inputSd = this.inputName.trim();
				let sd = inputSd.toLowerCase();
				let num = sd.length;
				const cityList = city.cityObjs;
				let finalCityList = [];

				let temp = cityList.filter(
					item => {
						let text = item.short.slice(0, num).toLowerCase();
						// eslint-disable-next-line
						return (text && text == sd);
					}
				);

				// 在城市数据中，添加简拼到“shorter”属性，就可以实现简拼搜索
				let tempShorter = cityList.filter(
					itemShorter => {
						if (itemShorter.shorter) {
							let textShorter = itemShorter.shorter.slice(0, num).toLowerCase();
							// eslint-disable-next-line
							return (textShorter && textShorter == sd);
						}
					}
				);

				let tempChinese = cityList.filter(
					itemChinese => {
						let textChinese = itemChinese.city.slice(0, num);
						// eslint-disable-next-line
						return (textChinese && textChinese == sd);
					}
				);

				if (temp[0]) {
					temp.map(
						item => {
							let testObj = {};
							testObj.city = item.city;
							testObj.code = item.code;
							finalCityList.push(testObj);
						}
					);
					this.completeList = finalCityList;
				} else if (tempShorter[0]) {
					tempShorter.map(
						item => {
							let testObj = {};
							testObj.city = item.city;
							testObj.code = item.code;
							finalCityList.push(testObj);
						}
					);
					this.completeList = finalCityList;
				} else if (tempChinese[0]) {
					tempChinese.map(
						item => {
							let testObj = {};
							testObj.city = item.city;
							testObj.code = item.code;
							finalCityList.push(testObj);
						}
					);
					this.completeList = finalCityList;
				}
			}
		}
	};
</script>

<style>
	@import url("city.css");
</style>
