// pages/city/index.js
const app = getApp();
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		inputShowed: false,
		inputVal: "",
		hotCity: [
			{ id: 1, name: "郑州" },
			{ id: 2, name: "上海" },
			{ id: 3, name: "昆山" },
			{ id: 4, name: "苏州" },
			{ id: 5, name: "无锡" },
			{ id: 6, name: "深圳" },
			{ id: 7, name: "南京" },
			{ id: 8, name: "东莞" },
			{ id: 9, name: "常州" },
		],
		hotCitySelect: -1,
		toView: "",
		from: "",
		cityList: [],
		letter: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
		selectedQuanGuo: {
			name: "全国",
			shortName: "全国",
			checked: true,
		},
		selectedCityLocal: {
			name: "",
			shortName: "",
			checked: false,
		},
		selectedCity: {
			name: "",
			shortName: "",
			checked: false,
		},
		selectedCityTemp: {
			name: "",
			shortName: "",
			checked: false,
		},
		alpha: "",
		plShow: false,
		pltop: 70,

		selectedQuanGuoStorage: {
			name: "全国",
			shortName: "全国",
			checked: true,
		},
		selectedCityStorage: {
			name: "",
			shortName: "",
			checked: false,
		},
		selectedCityLocalStorage: {
			name: "",
			shortName: "",
			checked: false,
		},
		selectedCityTempStorage: {
			name: "",
			shortName: "",
			checked: false,
		},
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		var that = this;
		console.log(options);
		if (options.from) {
			this.setData({
				from: options.from,
			});
		}
		try {
			var searchCityParamStorage = wx.getStorageSync("searchCityParamStorage");
			console.log("searchCityParamStorage======", searchCityParamStorage);
			var selectedCityTempParamStorage = wx.getStorageSync("selectedCityTempParamStorage");
			console.log("selectedCityTempParamStorage======", selectedCityTempParamStorage);

			var tag = wx.getStorageSync("selectedQuanGuoStorage");
			if (tag && app.isNotEmptyCheck(tag.name)) {
				that.data.selectedQuanGuoStorage = wx.getStorageSync("selectedQuanGuoStorage");
				console.log("selectedQuanGuoStorage======", that.data.selectedQuanGuoStorage);
				that.data.selectedCityStorage = wx.getStorageSync("selectedCityStorage");
				console.log("selectedCityStorage======", that.data.selectedCityStorage);
				that.data.selectedCityLocalStorage = wx.getStorageSync("selectedCityLocalStorage");
				console.log("selectedCityLocalStorage======", that.data.selectedCityLocalStorage);
				that.data.selectedCityTempStorage = wx.getStorageSync("selectedCityTempStorage");
				console.log("selectedCityTempStorage======", that.data.selectedCityTempStorage);
			}

			if (searchCityParamStorage && app.isNotEmptyCheck(searchCityParamStorage.name)) {
				that.setData({
					selectedQuanGuo: JSON.parse(JSON.stringify(that.data.selectedQuanGuoStorage)),
					selectedCity: JSON.parse(JSON.stringify(that.data.selectedCityStorage)),
					selectedCityTemp: JSON.parse(JSON.stringify(that.data.selectedCityTempStorage)),
				});
			} else {
				that.setData({
					selectedQuanGuo: that.data.selectedQuanGuo,
					selectedCityLocal: that.data.selectedCityLocal,
					selectedCity: that.data.selectedCity,
					selectedCityTemp: that.data.selectedCityTemp,
				});
			}
		} catch (e) {
			console.log("获取缓存设置的查询职位列表参数错误：", e);
		}

		this.getAllCityLevel2();
	},
	leftMove(e) {
		this.setData({
			toView: "",
		});
	},
	handlerAlphaTap(e) {
		console.log(e.target.dataset.item);
		this.setData({
			toView: e.target.dataset.item,
			alpha: e.target.dataset.item,
			plShow: true,
		});
	},
	touchEnd(e) {
		this.setData({
			plShow: false,
		});
	},
	touchMove(e) {
		var that = this;
		// console.log(e)
		console.log(e.changedTouches[0].clientY);
		let moveY = e.changedTouches[0].clientY;
		let rY = moveY - 80;

		// toView:'hot'
		// toView:e.target.dataset.item

		if (rY >= 0) {
			let index = Math.ceil(rY / 22);
			// console.log(index);
			if (index == 1) {
				this.setData({ toView: "top", alpha: "查", plShow: true });
			} else if (index == 2) {
				this.setData({ toView: "hot", alpha: "热", plShow: true, pltop: 92 });
			} else {
				let nonwAp = that.data.letter[index - 3];
				let hg = (index - 2) * 22;
				this.setData({ toView: nonwAp, alpha: nonwAp, plShow: true, pltop: 92 + hg });
			}

			// if(0 <= index < list.length) {
			//   let nonwAp = list[index];
			//   nonwAp && this.setData({alpha: nonwAp.alphabet});
			// }
		}

		// let {list} = this.data;
		// console.log(list)
		// console.log(this.data.alphabet)
		// let moveY = e.touches[0].clientY;
		// let rY = moveY - this.offsetTop;
		// if(rY >= 0) {
		//   let index = Math.ceil((rY - this.apHeight)/ this.apHeight);
		//   if(0 <= index < list.length) {
		//     let nonwAp = list[index];
		//     nonwAp && this.setData({alpha: nonwAp.alphabet});
		//   }
		// }
	},
	getLocation() {
		var that = this;
		if (app.isNotEmptyCheck(app.globalData.lng) && app.isNotEmptyCheck(app.globalData.lat)) {
			that.getCityNameByLatLng(app.globalData.lng, app.globalData.lat, 1);
		} else {
			wx.getSetting({
				success: (res) => {
					console.log(res); // res.authSetting.location

					if (res.authSetting["scope.userLocation"]) {
						wx.getLocation({
							type: "gcj02",
							success(res1) {
								console.log("获取位置");
								console.log(res1);
								app.globalData.lng = res1.longitude;
								app.globalData.lat = res1.latitude;
								that.getCityNameByLatLng(res1.longitude, res1.latitude, 1);
							},
							fail() {},
						});
						that.getCityNameByLatLng(app.globalData.lng, app.globalData.lat, 1);
					} else {
						console.log("11111111");
						wx.openSetting({
							success(res) {
								console.log(res.authSetting);
								// res.authSetting = {
								//   "scope.userInfo": true,
								//   "scope.userLocation": true
								// }
							},
						});
						// that.getStoreJobDetailById();
					}
				},
			});
		}
	},
	selectQuanGuo: function (e) {
		var that = this;
		that.data.selectedQuanGuo.checked = true;
		that.data.selectedCity.checked = false;
		that.data.selectedCityLocal.checked = false;
		that.data.selectedCityTemp.checked = false;

		this.setData({
			selectedQuanGuo: that.data.selectedQuanGuo,
			selectedCity: that.data.selectedCity,
			selectedCityTemp: that.data.selectedCityTemp,
			selectedCityLocal: that.data.selectedCityLocal,
		});
	},
	selectOld: function (e) {
		var that = this;
		that.clearAllChecked();
		that.data.selectedCity.checked = true;

		this.setData({
			selectedQuanGuo: that.data.selectedQuanGuo,
			selectedCity: that.data.selectedCity,
			selectedCityTemp: that.data.selectedCityTemp,
			selectedCityLocal: that.data.selectedCityLocal,
		});
	},
	reset: function (e) {
		this.clearAllChecked();
		this.data.selectedQuanGuo.checked = true;
		this.setAllSelected();
	},
	selectCity: function (e) {
		var city = e.currentTarget.dataset.city;
		console.log(city);
		this.data.selectedCityLocal.checked = false;
		var that = this;

		if (this.data.selectedCity.id != city.id) {
			this.data.selectedCity.checked = false;
		}

		this.data.selectedCityTemp = city;
		this.data.selectedCityTemp.checked = true;

		this.data.selectedQuanGuo.checked = false;

		this.setData({
			selectedCity: that.data.selectedCity,
			selectedCityTemp: that.data.selectedCityTemp,
			selectedQuanGuo: that.data.selectedQuanGuo,
			selectedCityLocal: this.data.selectedCityLocal,
		});
	},
	loginOut: function () {
		var that = this;

		var temp;
		if (that.data.selectedCityTemp.checked) {
			that.clearAllChecked();
			that.data.selectedCityTemp.checked = true;

			temp = that.data.selectedCityTemp;
			that.data.selectedCity = that.data.selectedCityTemp;
		} else if (that.data.selectedQuanGuo.checked) {
			that.clearAllChecked();
			that.data.selectedQuanGuo.checked = true;

			temp = that.data.selectedQuanGuo;
		} else if (that.data.selectedCity.checked) {
			that.clearAllChecked();
			that.data.selectedCity.checked = true;

			temp = that.data.selectedCity;
		} else if (that.data.selectedCityLocal.checked) {
			that.clearAllChecked();
			that.data.selectedCityLocal.checked = true;

			temp = that.data.selectedCityLocal;
		}

		try {
			wx.setStorageSync("selectedCityTempParamStorage", that.data.selectedCity);
			wx.setStorageSync("searchCityParamStorage", temp);

			wx.setStorageSync("selectedQuanGuoStorage", that.data.selectedQuanGuo);
			wx.setStorageSync("selectedCityStorage", that.data.selectedCity);
			wx.setStorageSync("selectedCityLocalStorage", that.data.selectedCityLocal);
			wx.setStorageSync("selectedCityTempStorage", that.data.selectedCityTemp);
			wx.setStorageSync("FROMCITY", true);
		} catch (e) {
			console.log("index-设置查询职位列表参数错误", e);
		}
		if (this.data.from == "search") {
			var pages = getCurrentPages();
			var prevPage = pages[pages.length - 2]; // 上一个页面

			prevPage.data.storeJobListSearchForm.cityName = temp.name;
			prevPage.setData({
				storeJobListSearchForm: prevPage.data.storeJobListSearchForm,
				searchCityParamStorage: temp,
			});
			wx.navigateBack({
				delta: 1,
			});
			// wx.reLaunch({
			// 	url: "../jobListSearch/index?fromSearchPage=1",
			// });
		} else {
			var pages = getCurrentPages();
			var prevPage = pages[pages.length - 2]; // 上一个页面

			prevPage.data.storeJobListSearchForm.cityName = temp.name;
			prevPage.setData({
				storeJobListSearchForm: prevPage.data.storeJobListSearchForm,
				searchCityParamStorage: temp,
			});
			wx.navigateBack({
				delta: 1,
			});
			// var pages = getCurrentPages();
			// var prevPage = pages[pages.length - 2]; // 上一个页面
			// console.log(prevPage);
			// console.log(temp);
			// prevPage.data.storeJobListSearchForm.cityName = temp.name;
			// prevPage.setData({
			// 	storeJobListSearchForm: prevPage.data.storeJobListSearchForm,
			// 	searchCityParamStorage: temp,
			// });
			// wx.navigateBack({
			// 	delta: 1,
			// });
			// wx.reLaunch({
			// 	url: "../firstBill/index?fromSearchPage=1",
			// });
		}
	},
	showInput: function () {
		this.setData({
			inputShowed: true,
		});
	},
	hideInput: function () {
		this.setData({
			inputVal: "",
			inputShowed: false,
		});
	},
	clearInput: function () {
		this.setData({
			inputVal: "",
		});
	},
	inputTyping: function (e) {
		this.setData({
			inputVal: e.detail.value,
		});
	},
	clickHot: function (e) {
		this.setData({
			toView: "hot",
		});
	},
	goTop: function (e) {
		this.setData({
			toView: "top",
		});
	},
	chooseLetter: function (e) {
		console.log(e.target.dataset.item);
		this.setData({
			toView: e.target.dataset.item,
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {},

	getAllCityLevel2: function () {
		var that = this;
		wx.request({
			url: app.globalData.ip + "/city/getAllCityLevel2",
			data: {},
			header: app.globalData.header,
			method: "GET",
			success: function (res) {
				console.log(res);
				that.setData({
					cityList: res.data.data,
				});
				console.log(that.data.cityList);

				if (app.isNotEmptyCheck(app.globalData.lng) && app.isNotEmptyCheck(app.globalData.lat)) {
					that.getCityNameByLatLng(app.globalData.lng, app.globalData.lat, 0);
				} else {
					wx.getLocation({
						type: "gcj02",
						success(res) {
							console.log(res);
							app.globalData.lng = res.longitude;
							app.globalData.lat = res.latitude;
							that.getCityNameByLatLng(res.longitude, res.latitude, 0);
						},
					});
				}
			},
		});
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {},

	getCityNameByLatLng: function (longitude, latitude, comeFrom) {
		var that = this;
		wx.request({
			url: app.globalData.ip + "/location/getCityNameByLatLng",
			data: {
				lng: longitude,
				lat: latitude,
			},
			header: app.globalData.headers,
			method: "GET",
			success: function (res) {
				console.log(res);
				if (res.data.status == 200) {
					var cityName = res.data.data;

					var tag = false;

					that.data.cityList.forEach((item) => {
						item.list.forEach((item1) => {
							if (item1.name == cityName) {
								// if (that.data.selectedCityLocalStorage) {}

								// debugger;
								if (comeFrom == 1) {
									//主动点击获取位置

									that.clearAllChecked();
									that.data.selectedCityLocal = item1;
									that.data.selectedCityLocal.checked = true;

									that.setAllSelected();
								} else {
									if (that.data.selectedCityLocalStorage.checked) {
										that.clearAllChecked();
										that.data.selectedCityLocal = item1;
										that.data.selectedCityLocal.checked = true;

										that.setAllSelected();
									} else {
										// that.clearAllChecked();
										that.data.selectedCityLocal = item1;
										that.data.selectedCityLocal.checked = false;

										that.setAllSelected();
									}
								}
							}
						});
					});
				}
			},
		});
	},

	clearAllChecked: function () {
		var that = this;
		that.data.selectedQuanGuo.checked = false;
		that.data.selectedCity.checked = false;
		that.data.selectedCityLocal.checked = false;
		that.data.selectedCityTemp.checked = false;
	},
	setAllSelected: function () {
		var that = this;
		this.setData({
			selectedQuanGuo: that.data.selectedQuanGuo,
			selectedCity: that.data.selectedCity,
			selectedCityTemp: that.data.selectedCityTemp,
			selectedCityLocal: that.data.selectedCityLocal,
		});
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {},

	/**
	 * 用户点击右上角分享
	 */
	// onShareAppMessage1: function () {

	// }
});
