<template>
	<div class="wrapper" :style="'top:'+statusBarHeight+'px'">
		<div class="header">
			<view class="back_div" v-if="inputActive">
				<image class="back_img" @click="back_city()" src="https://img.agrimedia.cn/chwl%2Fv2%2Fsearch.png" mode=""></image>
			</view>
			<input :class="inputActive? 'input':'input inputActive'" @input="onInput" placeholder="搜索城市名" v-model="searchValue" />
		</div>
		<scroll-view class="calendar-list" scroll-y="true" :scroll-into-view="scrollIntoId">
			<view v-if="disdingwei" id="hot">
				<!-- 定位模块 -->
				<view class="dingwei">
					<view class="dingwei_Tips">
						当前城市
					</view>
					<view class="dingwei_city">
						<view class="dingwei_city_one" @click="AtCity">
							{{UserFilmArae['cityName']?UserFilmArae['cityName']:UserArae2['city']}}
						</view>
					</view>
				</view>

				<!-- 最近模块 -->
				<view class="dingwei" v-if="Visit.length>=0">
					<view class="dingwei_Tips">
						最近搜索
					</view>
					<view class="dingwei_city dingwei_city_zuijin">
						<view class="dingwei_city_one toright" v-for="(item,index) in Visit" :key="index" v-if="index<2" @click="back_city(item)">
							{{item.cityName}}
						</view> 
					</view>
				</view>

			</view>


			<!-- 城市列表 -->
			<view v-if="searchValue == ''" v-for="(item, index) in list" :id="getId(index)" :key="index">
				<view class="letter-header">{{ getId(index) }}</view>
				<view class="city-div" v-for="(city, i) in item" :key="i">
					<view class="regionName" @click="back_city(city)">
						{{city.cityName}}
					</view>
				</view>
			</view>
			<!-- 搜索结果 -->
			<view class="city-div" v-for="(item, index) in searchList" :key="item.cityId">
				<view class="regionName" @click="back_city(item)">
					{{item.cityName}}
				</view>
			</view>
			</view>
		</scroll-view>

		<!-- 右侧字母 -->
		<view class="letters" v-if="searchValue == ''">
			<view class="letters-item" v-for="item in letter" :key="item" @click="scrollTo(item)">{{ item }}</view>
		</view>

		<!-- 选中之后字母 -->
		<view class="mask" v-if="showMask">
			<view class="mask-r">{{selectLetter}}</view>
		</view>
	</div>
</template>

<script>
	import { getFilmCity} from '@/request/film/index.js';
	import { mapGetters} from 'vuex';
	import Citys from '../city.js';
	import { mapMutations } from 'vuex';
	export default {
		components: {},
		props: {},

		data() {
			return {
				statusBarHeight: this.statusBarHeight,
				ImgUrl: this.ImgUrl,
				letter: [],
				selectLetter: '',
				searchValue: '',
				scrollIntoId: '',
				list: [],
				tId: null,
				searchList: [],
				showMask: false,
				disdingwei: true,
				Visit: [], //最近访问
				position: '',
				longitude: '', //经度
				latitude: '', //纬度
				seconds: 3,
				po_tips: '重新定位',
				citys:[],
				hotCity:[],
				inputActive: true
			}
		},
		
		computed: {
			...mapGetters({
				// 获取用户自主选择的电影城市信息
				UserFilmArae: 'GetFilmData',
				// 获取用户设备定位的地址
				UserArae2: 'GetcityInfor',
			}),
		},

		created() {
			getFilmCity().then(data=>{
				this['citys'] = data['data'];
				
				//获取存储的最近访问
				var that = this
				uni.getStorage({
					key: 'Visit_key',
					success: function(res) {
						that.Visit = res.data
					}
				});
				
				// this.position = this['getCityName'];
				//获取定位 经度纬度
				// that.getWarpweft()
				//获取city.js 的程序字母
				var mu = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'w', 'x', 'y', 'z'];
				var tmp = [];
				for (var i = 0; i < mu.length; i++) {
					var item = mu[i];
					for (var j = 0; j < this.citys.length; j++) {
						var py = this.citys[j].firstLetter;
						if (py.toLowerCase().substring(0, 1) == item) {
							if (tmp.indexOf(item) == -1) {
								this.list[i] = [this.citys[j]];
								tmp.push(item);
								this.letter.push(item.toUpperCase());
							} else {
								this.list[i].push(this.citys[j]);
							}
						}
					}
				}
			})
			
		},
		methods: {
			...mapMutations({
				SETFILMDATA: 'SETFILMDATA',
			}),
			
			getId(index) {
				return this.letter[index];
			},

			scrollTo(letter) {
				this.showMask = true
				this.selectLetter = letter == 'hot' ? '最' : letter
				setTimeout(() => {
					this.showMask = false
				}, 300);
				this.scrollIntoId = letter;
			},

			query(source, text) {
				let res = [];
				var self = this;
				res = source.filter(item => {
					const arr = [];
					let isHave = false;
					Object.keys(item).forEach(prop => {
						const itemStr = item[prop];
						self.isString(itemStr) &&
							itemStr.split(',').forEach(val => {
								arr.push(val);
							});
					});
					arr.some(val => {
						isHave = new RegExp('^' + text).test(val);
						return isHave;
					});
					return isHave;
				});
				console.log(JSON.stringify(res));
				return res;
			},

			isString(obj) {
				return typeof obj === 'string';
			},

			onInput(e) {
				const value = e.target.value;
				console.log(value, '值');
				if (value !== '' && this.citys && this.citys.length > 0) {
					const queryData = this.query(this.citys, String(value).trim());
					this.searchList = queryData;
					this.disdingwei = false;
					this.inputActive = false;
				} else {
					this.searchList = [];
					this.disdingwei = true;
					this.inputActive = true;
				}
			},

			back_city(item, ind) {
				if (item) {
					const FilmCity = {
						cityId: item.cityId,
						cityName: item.cityName,
						regionName: "",
						regionId: ""
					}
					
					//unshift 把数据插入到首位，与push相反
					if(this.Visit  == null){
						this.Visit = []
					}

					this.Visit.unshift(FilmCity)
					this.searchValue = "";
					this.disdingwei = true
					var arr = this.Visit
					//数组去重
					function distinct(arr) {
						let newArr = []
						for (let i = 0; i < arr.length; i++) {
							if (newArr.indexOf(arr[i]) < 0) {
								newArr.push(arr[i])
							}
						}
						return newArr
					}
					this.Visit = distinct(arr)
					console.log(this.Visit, "---最近访问")
					uni.setStorage({
						key: 'Visit_key',
						data: this.Visit
					});

					this.SETFILMDATA(FilmCity);
					
					// 保存之后退页
					uni.navigateBack({
						delta: 1
					});
				}
			},
			AtCity() {
				uni.navigateBack({
					delta: 1
				});
			},
			getWarpweft() {
				this.po_tips = '定位中'
				this.$store.dispatch('location/getLocation').then(res => {
					console.log(res,'locationinfo')
					if(res != undefined){
						this.$store.dispatch('location/getCityName2').then(res => {
							this.po_tips = '重新定位';
							// this.position = this['getCityName'];
						})
					}else{
						this.po_tips = '定位失败'
					}
				})
				
				return
				var that = this
				that.po_tips = '定位中...'
				let qqmapsdk = new qqMap({
					key: 'DABBZ-UVV33-TN63X-3YGRM-2L562-NHFGO' // 自己申请的key
				})
				this.$store.dispatch('location/getLocation').then(res => {
					qqmapsdk.reverseGeocoder({
						location: {
							latitude: res.latitude,
							longitude: res.longitude
						},
						success(res) {
							console.log(res)
							that.position = res.result.address_component.city
							that.po_tips = '重新定位'
						},
						fail(err) {
							console.log(err)
						}
					})
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.wrapper {
		position: fixed;
		z-index: 999999;
		background: #F5F5F5;
		height: 100%;
		width: 100%;
		top: 0px;
		left: 0px;
	}

	.mask {
		position: absolute;
		bottom: 0upx;
		top: 83upx;
		left: 0upx;
		right: 0upx;
		width: 750upx;
		display: flex;
		justify-content: center;
		align-items: center;
		background: rgba(0, 0, 0, 0);
	}

	.mask-r {
		height: 120upx;
		width: 120upx;
		border-radius: 60upx;
		display: flex;
		background: rgba(0, 0, 0, 0.5);
		justify-content: center;
		align-items: center;
		font-size: 40upx;
		color: #FFFFFF
	}

	.content {
		height: 100%;
		width: 100%;
		background-color: #ffffff;
	}

	.header {
		height: 85upx;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		display: flex;
	}


	.back_div {
		width: 35rpx;
		height: 35rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		position: absolute;
		top: 24rpx;
		left: 280rpx;
	}
	.back_img {
		width: 35rpx;
		height: 35rpx;
	}
	.input {
		font-size: 24upx;
		width: 720rpx;
		height: 60rpx;
		border-radius: 40upx;
		background-color: #FFFFFF;
		padding-left: 20upx;
		padding-right: 20upx;
		box-sizing: border-box;
		border: 2rpx solid #DEDEDE;
		text-align: center;
		margin: 0 auto;
		color: #999;
	}
	
	.inputActive {
		text-align: left !important;
	}

	.title {
		font-size: 30upx;
		color: white;
	}

	.show {
		left: 0;
		width: 100%;
		transition: left 0.3s ease;
	}

	.hide {
		left: 100%;
		width: 100%;
		transition: left 0.3s ease;
	}


	.title {
		font-size: 30upx;
		color: white;
	}

	.calendar-list {
		position: absolute;
		top: 83upx;
		bottom: 0upx;
		width: 100%;
	}

	.letters {
		position: absolute;
		right: 30upx;
		bottom: 0px;
		width: 50upx;
		top: 260upx;
		color: #2f9bfe;
		text-align: center;
		font-size: 24upx;
	}

	.letters-item {
		margin-bottom: 5upx;
		color: #666666;
	}

	.letter-header {
		height: 45upx;
		font-size: 22upx;
		color: #333333;
		padding-left: 24upx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		/* background-color: #ebedef; */

	}

	.city-div {
		width: 100%;
		padding: 0rpx 50rpx 0rpx 20rpx;
		border-bottom-width: 0.5upx;
		border-bottom-color: #ebedef;
		border-bottom-style: solid;
		align-items: center;
		margin-right: 35upx;
		.regionName {
			width: 100%;
			padding: 20rpx 30rpx;
			margin: 0upx 24upx;
			align-items: center;
			font-size: 24rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #666666;
			line-height: 34rpx;
		}
	}

	.city {
		padding-left: 30upx;
		font-size: 24rpx;
		font-family: PingFangSC-Semibold, PingFang SC;
		font-weight: 600;
		color: #333333;
		line-height: 34rpx;
	}

	.dingwei {
		width: 100%;
		padding-top: 25upx;
		box-sizing: border-box;
		margin-bottom: 26upx;
	}

	.dingwei_Tips {
		margin-left: 24upx;
		margin-bottom: 24upx;
		font-size: 24upx;
		color: #A5A5A5;
	}
	
	.dingwei_refresh {
		width: 44rpx;
		height: 44rpx;
	}
	.dingwei_city {
		width: 100%;
		padding-left: 55upx;
		padding-right: 70upx;
		box-sizing: border-box;
		display: flex;
	}

	.dingwei_city_one {
		width: 218rpx;
		height: 80rpx;
		line-height: 80rpx;
		font-size: 24rpx;
		color: #333333;
		display: flex;
		justify-content: center;
		align-items: center;
		background: #DEDEDE;
		border-radius: 44rpx;
		margin-right: 6rpx;
		display: inline-block;
		white-space: nowrap; 
		overflow: hidden;
		text-overflow:ellipsis;
		text-align: center;
		padding: 0rpx 20rpx;
	}

	.dingweis_div {
		display: flex;
		align-content: flex-end;
		align-items: center;
		font-size: 24upx;
		color: #FD5745;
	}

	.dingweis {
		width: 32upx;
		height: 32upx;
	}

	.dingwei_city_zuijin {
		display: flex;
		justify-content: flex-start;
	}

	.toright {
		margin-right: 25upx;
	}
</style>
