const baseUrl = "https://fare.somnisix.top/";
const authorization = "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6Ijk5YzI3MjI2LTdkOWQtNDhiOC1hYjlhLWM4YjVjYWZiZGNkNyIsInVzZXJuYW1lIjoiYWRtaW4ifQ.zITQOINFkHs19_1RAgHS4I9YMaVxVq0Dsn3t6r_K7rv3rXvJy0sMtodejIMi_PNa_m4uQYxnU-6k3wC2LZht_Q";
const api = "system/sleepEvaluation";

const homeApi = "https://fare.somnisix.top";
const homeApiTest = "http://localhost:8080";
const token = "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6Ijk5YzI3MjI2LTdkOWQtNDhiOC1hYjlhLWM4YjVjYWZiZGNkNyIsInVzZXJuYW1lIjoiYWRtaW4ifQ.zITQOINFkHs19_1RAgHS4I9YMaVxVq0Dsn3t6r_K7rv3rXvJy0sMtodejIMi_PNa_m4uQYxnU-6k3wC2LZht_Q";

let countABCDEF;
Page({
	data: {

		activeIcon: "/subpages/icons/ic_selected_checkbox.svg",
		normalIcon: "/subpages/icons/ic_checkbox.svg",
		activeColor: "color:#0BC3AA;",
		normalColor: "color:#666666;",

		//一、身体综合
		bodySynthesisOptions: [
			{ value: '0', remark: 'A', label: '易疲劳，总感到疲倦' },
			{ value: '1', remark: 'A', label: '好出汗，一活动就冒汗' },
			{ value: '2', remark: 'A', label: '易感冒，恢复慢' },
			{ value: '3', remark: 'A', label: '稍一活动就气喘' },
			{ value: '4', remark: 'A', label: '疲劳后身体就不舒服' },
			{ value: '5', remark: 'A', label: '无食欲' },
			{ value: '6', remark: 'A', label: '手脚发凉' },
			{ value: '7', remark: 'A', label: '肌肉无力' },
			{ value: '8', remark: 'B', label: '腹部易滞气' },
			{ value: '9', remark: 'B', label: '头痛、肩凝、关节痛、胃疼、手脚麻痹' },
			{ value: '10', remark: 'B', label: '紧张、发怒后身体不舒服' },
			{ value: '11', remark: 'B', label: '身体有痛感后，疼痛部位游走不定' },
			{ value: '12', remark: 'C', label: '易眩晕和站起时头晕' },
			{ value: '13', remark: 'C', label: '常感到心慌' },
			{ value: '14', remark: 'C', label: '易头晕眼花' },
			{ value: '15', remark: 'D', label: '有受伤和手术后的旧伤痛' },
			{ value: '16', remark: 'D', label: '感到肩和腰、头有针扎样痛' },
			{ value: '17', remark: 'D', label: '入夜后容易出现肩、腰、头疼痛' },
			{ value: '18', remark: 'E', label: '喉咙易渴，常想喝水' },
			{ value: '19', remark: 'E', label: '感冒愈后常干咳' },
			{ value: '20', remark: 'E', label: '易便秘，大便呈球状' },
			{ value: '21', remark: 'E', label: '尿量少、色浓' },
			{ value: '22', remark: 'E', label: '身体总是发热' },
			{ value: '23', remark: 'E', label: '持续低烧' },
			{ value: '24', remark: 'F', label: '常感到身体和手脚沉重，易浮肿' },
			{ value: '25', remark: 'F', label: '胃部不适、恶心' },
			{ value: '26', remark: 'F', label: '雨天和湿度高时身体状况就很糟糕' },
			{ value: '27', remark: 'F', label: '有时眩晕' },
			{ value: '28', remark: 'F', label: '大便偏软' },
			{ value: '29', remark: 'F', label: '易恶心、呕吐、晕车' },
			{ value: '30', remark: '', label: '以上都没有' },
		],

		bodySynthesis: [],// 一、身体综合

		//二、面部五官
		facialFeaturesOptions: [
			{ value: '0', remark: 'B', label: '喉头部有异物感' },
			{ value: '1', remark: 'C', label: '脸色不好' },
			{ value: '2', remark: 'D', label: '眼圈发黑' },
			{ value: '3', remark: 'E', label: '眼睛经常模糊不清' },
			{ value: '4', remark: 'E', label: '多呈现消极、羸弱的面孔' },
			{ value: '5', remark: 'F', label: '唾液和痰较多，中发黏' },
			{ value: '6', remark: 'F', label: '易犯花粉症和鼻炎，容易淌鼻涕' },
			{ value: '7', remark: '', label: '以上都没有' },
		],

		facialFeatures: [],// 二、面部五官

		//三、皮肤毛发
		skinHairOptions: [
			{ value: '0', remark: 'C', label: '头发脆弱，脱发' },
			{ value: '1', remark: 'C', label: '皮肤和指甲无光泽' },
			{ value: '2', remark: 'D', label: '皮肤干燥易皲裂，色斑、雀斑多' },
			{ value: '3', remark: 'D', label: '皮肤呈青紫色或黑紫色，不光滑' },
			{ value: '4', remark: 'D', label: '肤色暗淡' },
			{ value: '5', remark: 'E', label: '洗浴后皮肤很快干燥，易发痒' },
			{ value: '6', remark: 'E', label: '皮肤纹理粗糙、毛孔张开' },
			{ value: '7', remark: '', label: '以上都没有' },
		],

		skinHair: [],// 三、皮肤毛发

		//四、情绪方面
		emotionOptions: [
			{ value: '0', remark: 'A', label: '经常情绪低落，容易受伤害' },
			{ value: '1', remark: 'B', label: '焦虑、易怒' },
			{ value: '2', remark: 'B', label: '常长吁短叹、嗳气' },
			{ value: '3', remark: '', label: '以上都没有' },
		],

		emotion: [],// 四、情绪方面

		//五、月经方面
		menstruationOptions: [
			{ value: '0', remark: 'B', label: '月经前身体不适' },
			{ value: '1', remark: 'C', label: '经血量少或月经周期过长' },
			{ value: '2', remark: 'D', label: '月经痛加重、经血中有动物肝脏血块' },
			{ value: '3', remark: '', label: '以上都没有' },
		],

		menstruation: [],// 五、月经方面

		//六、性格方面
		characterOptions: [
			{ value: '0', remark: 'A', label: '过度依赖人际关系' },
			{ value: '1', remark: 'B', label: '具有攻击性，不善于体谅别人' },
			{ value: '2', remark: 'C', label: '属冷静、认真型' },
			{ value: '3', remark: 'C', label: '善于制造一个自我世界并沉溺其中' },
			{ value: '4', remark: 'D', label: '忍耐性较强的性格，容易积聚压力' },
			{ value: '5', remark: 'D', label: '接人待物中规中矩，严守礼节' },
			{ value: '6', remark: 'F', label: '以自我为中心，争强好胜，嫉妒心强，爱慕虚荣' },
			{ value: '7', remark: 'F', label: '喜欢奢华、热闹的氛围' },
			{ value: '8', remark: '', label: '以上都没有' },
		],

		character: [],// 六、性格方面

		//七、睡眠
		sleepOptions: [
			{ value: '0', remark: 'B', label: '入睡困难' },
			{ value: '1', remark: 'C', label: '睡眠浅，易做梦' },
			{ value: '2', remark: '', label: '以上都没有' },
		],

		sleep: [],// 七、睡眠

		//八、舌头状态
		tongueStateOptions: [
			{ value: '0', remark: 'A', label: '整体色淡；厚、肿大；边缘呈齿状' },
			{ value: '1', remark: 'B', label: '两侧赤红；中间色淡或偏白；薄薄的黄色舌苔' },
			{ value: '2', remark: 'C', label: '舌体偏小；色淡、偏白，舌苔整体偏薄' },
			{ value: '3', remark: 'D', label: '舌色暗、偏紫色；有黑色斑点；舌下两根静脉粗粗地浮现出来' },
			{ value: '4', remark: 'E', label: '舌色整体发红；舌苔少、甚至没有；舌面有齿痕' },
			{ value: '5', remark: 'F', label: '舌苔厚、黏黏糊糊；舌苔色黄或白；舌体肿胀' },
			{ value: '6', remark: '', label: '以上都没有' },
		],

		tongueState: [],// 八、舌头状态

		comScore: 0,
		comScoreStatus: '',
		comScoreIndex: 0,
		comScoreDesc: "",
		ratingResultsList: [{
			index: 0,
			title: "气虚型",
			icon: "/subpages/icons/ic_triangle_down_1.svg",
			color: "#70B3FE",
			opacity: "0"
		}, {
			index: 1,
			title: "气滞型",
			icon: "/subpages/icons/ic_triangle_down_2.svg",
			color: "#68D89E",
			opacity: "0"
		}, {
			index: 2,
			title: "血虚型",
			icon: "/subpages/icons/ic_triangle_down_3.svg",
			color: "#FFA95A",
			opacity: "0"
		}, {
			index: 3,
			title: "淤血型",
			icon: "/subpages/icons/ic_triangle_down_4.svg",
			color: "#cffc6d",
			opacity: "0"
		}, {
			index: 4,
			title: "水虚型",
			icon: "/subpages/icons/ic_triangle_down_1.svg",
			color: "#80f5ff",
			opacity: "0"
		}, {
			index: 5,
			title: "痰湿型",
			icon: "/subpages/icons/ic_triangle_down_2.svg",
			color: "#ef00b3",
			opacity: "0"
		}],

		hotel: '',//酒店
		room: '',//房间
		mInputId: '',//自动滚动用的
		intervalId1: null,//保存数据
		intervalId2: null,//授权解除定时任务
		intervalId3: null,//导出excel
	},


	// 一、身体综合
	bindBodySynthesis: function (e) {
		console.log(e)
		this.setData({
			bodySynthesis: e.detail
		});
	},

	// 二、面部五官
	bindFacialFeatures: function (e) {
		console.log(e)
		this.setData({
			facialFeatures: e.detail
		});
	},

	// 三、皮肤毛发
	bindSkinHair: function (e) {
		console.log(e)
		this.setData({
			skinHair: e.detail
		});
	},

	// 四、情绪方面
	bindEmotion: function (e) {
		console.log(e)
		this.setData({
			emotion: e.detail
		});
	},

	// 五、月经方面
	bindMenstruation: function (e) {
		console.log(e)
		this.setData({
			menstruation: e.detail
		});
	},

	// 六、性格方面
	bindCharacter: function (e) {
		console.log(e)
		this.setData({
			character: e.detail
		});
	},

	// 七、睡眠
	bindSleep: function (e) {
		console.log(e)
		this.setData({
			sleep: e.detail
		});
	},

	// 八、舌头状态
	bindTongueState: function (e) {
		console.log(e)
		this.setData({
			tongueState: e.detail
		});
	},

	//统计这8个数组，总共包含A,B,C,D,E,F的个数，返回最多的一项（也可能多项）：
	//如果有多个 最多 的情况，返回最多的几个，如果都是0或者空，返回空
	findMostFrequentLetters: function (arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8) {

		console.log('arr1=' + arr1);
		console.log('arr2=' + arr2);
		console.log('arr3=' + arr3);
		console.log('arr4=' + arr4);
		console.log('arr5=' + arr5);
		console.log('arr6=' + arr6);
		console.log('arr7=' + arr7);
		console.log('arr8=' + arr8);
		//判断6个数组是否都为空
		if (this.areAllArraysEmpty(arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8))
			return '';
		// 初始化一个对象来存储每个字母的计数  
		const letterCounts = {
			A: 0,
			B: 0,
			C: 0,
			D: 0,
			E: 0,
			F: 0
		};

		// 定义一个辅助函数来更新计数  
		function updateCounts(arr) {
			if (Array.isArray(arr)) {
				arr.forEach(item => {
					if (typeof item === 'string' && ['A', 'B', 'C', 'D', 'E', 'F'].includes(item)) {
						letterCounts[item]++;
					}
				});
			}
		}

		// 对每个数组调用辅助函数  
		updateCounts(arr1);
		updateCounts(arr2);
		updateCounts(arr3);
		updateCounts(arr4);
		updateCounts(arr5);
		updateCounts(arr6);
		updateCounts(arr7);
		updateCounts(arr8);


		console.log('arr1=' + arr1);
		console.log('arr2=' + arr2);
		console.log('arr3=' + arr3);
		console.log('arr4=' + arr4);
		console.log('arr5=' + arr5);
		console.log('arr6=' + arr6);
		console.log('arr7=' + arr7);
		console.log('arr8=' + arr8);
		countABCDEF = letterCounts;
		// 找出出现次数最多的字母及其计数  
		let maxCount = 0;
		let mostFrequentLetters = [];

		for (let letter in letterCounts) {
			if (letterCounts[letter] == 0) {
				continue;
			}
			if (letterCounts[letter] > maxCount) {
				maxCount = letterCounts[letter];
				mostFrequentLetters = [letter]; // 重置为当前最多的字母  
			} else if (letterCounts[letter] === maxCount) {
				mostFrequentLetters.push(letter); // 如果计数相同，则添加到列表中  
			}
		}

		console.log('letterCounts=' + JSON.stringify(letterCounts) );
		// 如果所有字母的计数都是0或数组为空，则返回空  
		if (mostFrequentLetters.length === 0) {
			return '';
		}

		// 返回出现次数最多的字母或字母集合  
		return mostFrequentLetters;
	},
	// 提交问卷的函数  
	submitSurvey: function () {
		var that = this;
		// 清空表单：
		this.setData({
			ratingResultsList: [{
				index: 0,
				title: "气虚型",
				icon: "/subpages/icons/ic_triangle_down_1.svg",
				color: "#70B3FE",
				opacity: "0"
			}, {
				index: 1,
				title: "气滞型",
				icon: "/subpages/icons/ic_triangle_down_2.svg",
				color: "#68D89E",
				opacity: "0"
			}, {
				index: 2,
				title: "血虚型",
				icon: "/subpages/icons/ic_triangle_down_3.svg",
				color: "#FFA95A",
				opacity: "0"
			}, {
				index: 3,
				title: "淤血型",
				icon: "/subpages/icons/ic_triangle_down_4.svg",
				color: "#cffc6d",
				opacity: "0"
			}, {
				index: 4,
				title: "水虚型",
				icon: "/subpages/icons/ic_triangle_down_1.svg",
				color: "#80f5ff",
				opacity: "0"
			}, {
				index: 5,
				title: "痰湿型",
				icon: "/subpages/icons/ic_triangle_down_2.svg",
				color: "#ef00b3",
				opacity: "0"
			}],
		});
		const { nickName } = wx.getStorageSync('userInfo');
		const unionid = wx.getStorageSync('unionid');
		console.log('nickName==' + nickName);
		console.log('unionid==' + unionid);
		let resultMostFrequentLetters = this.findMostFrequentLetters(
			this.getRemarkValues(this.data.bodySynthesisOptions, this.data.bodySynthesis),
			this.getRemarkValues(this.data.facialFeaturesOptions, this.data.facialFeatures),
			this.getRemarkValues(this.data.skinHairOptions, this.data.skinHair),
			this.getRemarkValues(this.data.emotionOptions, this.data.emotion),
			this.getRemarkValues(this.data.menstruationOptions, this.data.menstruation),
			this.getRemarkValues(this.data.characterOptions, this.data.character),
			this.getRemarkValues(this.data.sleepOptions, this.data.sleep),
			this.getRemarkValues(this.data.tongueStateOptions, this.data.tongueState)
		);
		console.log('resultMostFrequentLetters==' + resultMostFrequentLetters);
		let comScoreIndex = [];
		let _comScoreStatus = "";
		let _comScoreDesc = ""
		if (!resultMostFrequentLetters || !Array.isArray(resultMostFrequentLetters) || !resultMostFrequentLetters.length) {
			comScoreIndex = [];
			_comScoreStatus = "";
			_comScoreDesc = ""
		} else {
			comScoreIndex = [];
			// 遍历输入数组  
			resultMostFrequentLetters.forEach(item => {

				console.log('item=' + item);
				if (item == 'A') {
					if (_comScoreStatus) {
						_comScoreStatus += "\n气虚型";
					} else {
						_comScoreStatus += "气虚型";
					}
					if (_comScoreDesc) {
						_comScoreDesc += "\n气虚型:填写内容";
					} else {
						_comScoreDesc += "气虚型:填写内容";
					}
					comScoreIndex.push(0);
				} else if (item == 'B') {
					if (_comScoreStatus) {
						_comScoreStatus += "\n气滞型";
					} else {
						_comScoreStatus += "气滞型";
					}
					if (_comScoreDesc) {
						_comScoreDesc += "\n气滞型:填写内容";
					} else {
						_comScoreDesc += "气滞型:填写内容";
					}
					comScoreIndex.push(1);
				} else if (item == 'C') {
					if (_comScoreStatus) {
						_comScoreStatus += "\n血虚型";
					} else {
						_comScoreStatus += "血虚型";
					}
					if (_comScoreDesc) {
						_comScoreDesc += "\n血虚型:填写内容";
					} else {
						_comScoreDesc += "血虚型:填写内容";
					}
					comScoreIndex.push(2);
				} else if (item == 'D') {
					if (_comScoreStatus) {
						_comScoreStatus += "\n淤血型";
					} else {
						_comScoreStatus += "淤血型";
					}
					if (_comScoreDesc) {
						_comScoreDesc += "\n淤血型:填写内容";
					} else {
						_comScoreDesc += "淤血型:填写内容";
					}
					comScoreIndex.push(3);
				} else if (item == 'E') {
					if (_comScoreStatus) {
						_comScoreStatus += "\n水虚型";
					} else {
						_comScoreStatus += "水虚型";
					}
					if (_comScoreDesc) {
						_comScoreDesc += "\n水虚型:填写内容";
					} else {
						_comScoreDesc += "水虚型:填写内容";
					}
					comScoreIndex.push(4);
				} else if (item == 'F') {
					if (_comScoreStatus) {
						_comScoreStatus += "\n痰湿型";
					} else {
						_comScoreStatus += "痰湿型";
					}
					if (_comScoreDesc) {
						_comScoreDesc += "\n痰湿型:填写内容";
					} else {
						_comScoreDesc += "痰湿型:填写内容";
					}
					comScoreIndex.push(5);
				}

			});
		}
		console.log('comScoreIndex=' + comScoreIndex.join());
		console.log('_comScoreStatus=' + _comScoreStatus);
		console.log('_comScoreDesc=' + _comScoreDesc);
		this.setData({
			comScoreStatus: _comScoreStatus,
			comScoreIndex: comScoreIndex.join(),
			comScoreDesc: _comScoreDesc,
			promptTextMessageFlag: true,
			ratingResultsList: this.updateOpacity(this.data.ratingResultsList, comScoreIndex),
		})
		console.log('ratingResultsList=' + JSON.stringify(this.data.ratingResultsList));
		let surveyData = {
			hotel: this.data.hotel,
			room: this.data.room,
			userId: nickName,
			id2: unionid,
			bodySynthesis: this.getFormattedString(this.data.bodySynthesis, this.data.bodySynthesisOptions),
			facialFeatures: this.getFormattedString(this.data.facialFeatures, this.data.facialFeaturesOptions),
			skinHair: this.getFormattedString(this.data.skinHair, this.data.skinHairOptions),
			emotion: this.getFormattedString(this.data.emotion, this.data.emotionOptions),
			menstruation: this.getFormattedString(this.data.menstruation, this.data.menstruationOptions),
			character: this.getFormattedString(this.data.character, this.data.characterOptions),
			sleep: this.getFormattedString(this.data.sleep, this.data.sleepOptions),
			//舌头状态是新增加项，赋给value1了，数据库里有value1...value8，以备增加项用
			value1: this.getFormattedString(this.data.tongueState, this.data.tongueStateOptions),
			//value2 保存A、B、C、D、E、F 每个的个数
			value2: JSON.stringify(countABCDEF),
			comScoreStatus: _comScoreStatus ? _comScoreStatus : '健康型',
			comScoreIndex: comScoreIndex,
			comScoreDesc: _comScoreDesc,
		};

		console.log('surveyData=' + JSON.stringify(surveyData));
		var isFinish = true;
		var emptyKey = "";
		let checkList = {
			bodySynthesis: this.data.bodySynthesis,
			facialFeatures: this.data.facialFeatures,
			skinHair: this.data.skinHair,
			emotion: this.data.emotion,
			menstruation: this.data.menstruation,
			character: this.data.character,
			sleep: this.data.sleep,
			tongueState: this.data.tongueState
		}

		for (const key in checkList) {
			if (checkList.hasOwnProperty(key)) {
				const element = checkList[key];
				if (!element || (Array.isArray(element) && element.length <= 0)) { // 检查属性值是否为空  
					// 这里可以添加更复杂的判断，比如仅针对字符串检查是否为空字符串  
					// if (typeof obj[key] === 'string' && obj[key].trim() === '') {  
					isFinish = false;
					emptyKey = key;
					wx.showModal({
						title: '无法提交',
						content: '您还有部分题目未完成，请检查后重新提交',
						showCancel: false,
						confirmColor: '#0BC3AA',
						confirmText: "好的",
						success(res) {
							console.log(emptyKey.toString());
							that.setData({
								mInputId: emptyKey.toString()
							})

						}
					})
					return; // 停止循环  
				}
			}
		}
		if (!isFinish) {
			wx.showModal({
				title: '无法提交',
				content: '您还有部分题目未完成，请检查后重新提交',
				showCancel: false,
				confirmColor: '#0BC3AA',
				confirmText: "好的",
				success(res) {
					console.log(emptyKey.toString());
					that.setData({
						mInputId: emptyKey.toString()
					})

				}
			})
			return;
		}
		// 将surveyData发送到服务器  
		console.log('提交的问卷数据：', surveyData);
		if (!that.data.intervalId1) {
			that.data.intervalId1 = setInterval(() => {
				//提交到数据库
				wx.request({
					url: `${homeApi}/system/constitutionDiagnosis`, //
					method: 'POST',
					header: {
						'content-type': 'application/json', // 默认值
						'Authorization': 'Bearer ' + token // 在头部设置认证信息，例如使用Bearer Token
					},
					data: surveyData,
					success(res) {
						console.log('res===' + JSON.stringify(res));
						console.log('res.code===' + res.code);
						if (res && res.data && res.data.code == 200) {
							if (that.data.intervalId1) {
								clearInterval(that.data.intervalId1);
								that.data.intervalId1 = null;
							}
							wx.showToast({
								title: '问卷提交成功',
								icon: 'success',
								duration: 2000,
								success() {
									setTimeout(() => {
										wx.redirectTo({
											url: `/subpages/detectionResult/detectionResult?comScoreIndex=${comScoreIndex.slice(0, 1)}`,
										})
									}, 1000);
								}
							});

						}
					},
					fail: function (error) {
						console.error('error', error);
					},
					complete: function (e) {
					}
				});
			}, 1000);
		}

		// wx.showToast({
		// 	title: '问卷提交成功',
		// 	icon: 'success',
		// 	duration: 2000
		// });
	},
	// 取出最大值的obj
	findMaxValueKeyAndIndex(obj) {
		let maxValue = -Infinity;
		let maxKey = '';
		let maxIndex = -1;

		const keys = Object.keys(obj);
		keys.forEach((key, index) => {
			const value = obj[key];
			if (value > maxValue) {
				maxValue = value;
				maxKey = key;
				maxIndex = index;
			}
		});

		return {
			key: maxKey,
			value: maxValue,
			index: maxIndex
		};
	},
	export() {
		var that = this;
		const { nickName } = wx.getStorageSync('userInfo');
		const unionid = wx.getStorageSync('unionid');
		let exportData = {
			hotel: this.data.hotel,
			room: this.data.room,
			userId: nickName,
			id2: unionid,
		}
		if (!that.data.intervalId1) {
			that.data.intervalId1 = setInterval(() => {
				wx.request({
					url: `${homeApi}/system/constitutionDiagnosis/export`,
					method: 'POST',
					header: {
						'content-type': 'application/json', // 默认值
						'Authorization': 'Bearer ' + token // 在头部设置认证信息，例如使用Bearer Token
					},
					// 参数
					data: exportData,
					responseType: "arraybuffer", // 该属性处理二进制数据
					success: res => {
						console.log('res==' + JSON.stringify(res));
						if (res.statusCode == 200) {
							if (that.data.intervalId3) {
								clearInterval(that.data.intervalId3);
								that.data.intervalId3 = null;
							}
							const fs = wx.getFileSystemManager(); // 获取全局唯一的文件管理器 
							fs.writeFile({ //写文件
								// wx.env.USER_DATA_PATH是小程序提供的访问用户文件路劲的变量
								filePath: wx.env.USER_DATA_PATH + '/' + '体质问卷调查.xlsx',  // 一般后端回返回文件名
								data: res.data,  // res.data就是获取到的二进制文件流
								encoding: "binary", //二进制流文件必须是 binary
								success(e) {
									wx.openDocument({ // 打开文档
										filePath: wx.env.USER_DATA_PATH + '/' + '体质问卷调查.xlsx',
										showMenu: true, // 显示右上角菜单
										success: function (res) {
											console.log("打开文件", res);
										},
									})
								}
							})
						}

					},
					error(err) {
						console.log('export-err==' + err)
					}
				})
			}, 2000);
		}
	},
	onCloseResultsDialog() {
		this.setData({ promptTextMessageFlag: false });
	},
	onLoad(option) {
		var that = this;
		console.log("constitutionDiagnosis页面");
		// wx.clearStorage();
		// console.log('option.res=' + JSON.stringify(wx.getStorageSync('res')));
		// if (!wx.getStorageSync('res') || !wx.getStorageSync('res').result) {
		// 	wx.showModal({
		// 		title: '提示',
		// 		content: '请先扫描房间二维码',
		// 		showCancel: false
		// 	});

		// 	setTimeout(function () {
		// 		wx.reLaunch({
		// 			url: '/subpages/scan/scan',
		// 		})
		// 	}, 2000) // 设置延时时间，单位为毫秒
		// } else if (wx.getStorageSync('res').result.split('|').length != 2) {
		// 	wx.showModal({
		// 		title: '提示',
		// 		content: '房间二维码不正确，请重新扫码',
		// 		showCancel: false
		// 	});

		// 	setTimeout(function () {
		// 		wx.reLaunch({
		// 			url: '/subpages/scan/scan',
		// 		})
		// 	}, 2000) // 设置延时时间，单位为毫秒
		// } else {
		// const expiresAt = wx.getStorageSync('scanResultExpiresAt');
		// const now = Date.now() 
		// if (expiresAt || expiresAt < now) {
		//   // 扫码结果已过期  
		//   wx.showToast({  
		//     title: '扫码结果失效或已过期，请重新扫码',  
		//     icon: 'none'  
		//   });  
		//   // 清除过期的扫码结果  
		//   wx.removeStorageSync('scanResultExpiresAt');  
		//   setTimeout(function() {
		//       wx.reLaunch({
		//       		url: '/pages/scan/scan',
		//       })
		//     }, 2000) // 设置延时时间，单位为毫秒

		// }else{
		// 	//续期
		// 	wx.setStorageSync('scanResultExpiresAt', Date.now() + 2 * 60 * 60 * 1000)
		// }

		let hotelTemp = '';
		let roomTemp = '';

		hotelTemp = wx.getStorageSync('hotelEmpower');
		roomTemp = wx.getStorageSync('roomEmpower');
		// hotelTemp = wx.getStorageSync('res').result.split('|')[0];
		// roomTemp = wx.getStorageSync('res').result.split('|')[1];
		this.setData({
			hotel: hotelTemp,
			room: roomTemp,
		});
		// }

	},
	areAllArraysEmpty: function (arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8) {
		// 定义一个数组来包含所有传入的数组  
		const arrays = [arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8];

		// 遍历这个数组，检查每一项（即传入的数组）是否为空  
		for (let i = 0; i < arrays.length; i++) {
			// 检查当前数组是否为空（即长度为0）  
			if (arrays[i] && arrays[i].length > 0) {
				// 如果发现任何一个数组不为空，则返回false  
				return false;
			}
		}

		// 如果所有数组都检查过了，且都是空的，则返回true  
		return true;
	},
	//根据第二个数组对应第一个数组的value列，取第一个数组的remark的值，组成新的数组
	getRemarkValues: function (options, values) {

		if (!options || options.length === 0 || !values || values.length === 0) {
			return [];
		}
		// 初始化一个空数组来存储结果  
		let remarkValues = [];

		// 遍历values数组  
		values.forEach(value => {
			// 遍历options数组  
			options.forEach(option => {
				// 如果找到了对应的value，则将该option的remark添加到result数组中  
				if (option.value == value) {
					remarkValues.push(option.remark);
				}
			});
		});
		console.log('remarkValues=' + remarkValues);
		// 返回包含所有remark值的数组  
		return remarkValues;
	},

	// ratingResultsList里的index和list字段相对应，如果两个值相等，将opacity的值改为'1'
	updateOpacity: function (ratingResultsList, list) {
		// 如果ratingResultsList为空，则直接返回  
		if (!ratingResultsList || ratingResultsList.length === 0) {
			return ratingResultsList;
		}

		// 如果list为空，则直接返回原ratingResultsList  
		if (!list || list.length === 0) {
			return ratingResultsList;
		}

		// 否则，进行更新操作  
		return ratingResultsList.map(item => {
			// 如果item的index在list中，则修改opacity为'1'，否则保持原样  
			if (list.includes(item.index)) {
				return {
					...item,
					opacity: '1'
				};
			}
			return item;
		});
	},

	submitDB: function () {
		var that = this;
		if (!that.data.intervalId1) {
			that.data.intervalId1 = setInterval(() => {
				that.submitSurvey();
			}, 1000);
		}
	},
	onShow: function (options) {
		// var that = this;
		// if (!that.data.intervalId2) {
		// 	that.data.intervalId2 = setInterval(() => {
		// 		// 这里执行你的代码  
		// 		console.log('sleepEvaluation页面：'+Date.now());
		// 	}, 10000);
		// }
	},
	// startInterval: function () {
	// 	 var that = this;
	// 	 if (!that.data.intervalId) {
	// 	 	that.data.intervalId = setInterval(() => {
	// 	 		that.submitSurvey();
	// 	 	}, 1000);
	// 	 }  
	// },
	// clearInterval: function () {
	// 	   var that = this;
	//   	   if (that.data.intervalId) {
	//   		 clearInterval(that.data.intervalId);  
	//   		 that.data.intervalId = null;
	//   	   }
	//   },
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {
		var that = this;
		if (that.data.intervalId1) {
			clearInterval(that.data.intervalId1);
			that.data.intervalId1 = null;
		}
		// if (this.data.intervalId2) {
		// 	clearInterval(this.data.intervalId2);
		// 	this.data.intervalId2 = null; // 可选，但建议清除以避免潜在的错误  
		// }
		if (that.data.intervalId3) {
			clearInterval(that.data.intervalId3);
			that.data.intervalId3 = null;
		}
	},
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {
		var that = this;
		if (that.data.intervalId1) {
			clearInterval(that.data.intervalId1);
			that.data.intervalId1 = null;
		}
		// if (this.data.intervalId2) {
		// 	clearInterval(this.data.intervalId2);
		// 	this.data.intervalId2 = null; // 可选，但建议清除以避免潜在的错误  
		// }
		if (that.data.intervalId3) {
			clearInterval(that.data.intervalId3);
			that.data.intervalId3 = null;
		}
	},

	//取出arr2的value和arr1对应的remark和label项，取出的数据组装成一个字符串，加上'char10'
	getFormattedString: function (arr1, arr2) {
		// 检查 arr1 和 arr2 是否为非空数组  
		if (!arr1 || !arr2 || !Array.isArray(arr1) || arr1.length === 0 || !Array.isArray(arr2) || arr2.length === 0) {
			return '';
		}

		let result = '';

		arr1.forEach(value => {
			const valueStr = value ? value.toString() : '';
			const item = arr2.find(obj => obj.value == valueStr);
			if (item) {
				result += ` ${item.remark}、${item.label} char10`;
			}
		});

		return result;
	}
});