Vue.config.devtools = true;
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if(pair[0] == variable){return pair[1];}
	}
	return(false);
}
var v = new Vue({
	el:".container",
	data:{
		memToken:'',
		//提交信息
		form:{
			objType:1,
			objId:1,
			patientName:'',
			sex:'',
			phone:'',
			dateOfBirth:'',
			age:'',
			doctorName:'',
		},
		//判断显示
		isFinish:0,
		//问卷
		isFirst:1,
		paper: '',
		questionList: [],
		optionArr: [],
		paper2: '',
		questionList2: [],
		optionArr2: [],
		//提交信息后的id
		id:null,
		//得分
		scoreAll:'',
		scoreAll2:'',
		expireTimeOPtion: {
			disabledDate(time) {
				return time.getTime() > Date.now() - 8.64e6;  //如果没有后面的-8.64e6就是不可以选择今天的
			}
		}
	},
	created:function (){
		this.memToken = getQueryVariable("memToken") + '==';
		this.form.objId = getQueryVariable("id");
		this.infoDoctor()
	},
	mounted:function (){

	},
	methods: {
		//获取医生信息
		infoDoctor(){
			var that = this;
			$.ajax({
				type : 'GET',
				url : serverHttp+"/miniapp/user/findUserBaseInfo",
				data : {},
				headers:{
					"memToken":that.memToken,
				},
				success:function (res) {
					that.form.doctorName = res.data.memNickname
				}
			})
		},
		//提交信息
		submitInfo(){
			var that = this;
			if (that.isEmptyStr(that.form.patientName.trim()) ||
				that.isEmptyStr(that.form.sex) ||
				that.isEmptyStr(that.form.phone.trim()) ||
				that.isEmptyStr(that.form.dateOfBirth.trim()) ||
				that.isEmptyStr(that.form.age.trim()) ||
				that.isEmptyStr(that.form.doctorName.trim())
			) {
				alert("请完善必填信息");
				return false;
			}
			const regex = /^1[3456789]\d{9}$/;
			if (!regex.test(that.form.phone)) {
				alert("请填写正确的手机号")
				return false;
			}
			$.ajax({
				type : 'POST',
				url : serverHttp+"/app/PatientApp/patientMes",
				data : JSON.stringify(that.form),
				headers:{
					"memToken":that.memToken,
					"Content-Type":"application/json;charset=UTF-8"
				},
				success:function (res) {
					if (res.code == 200) {
						that.id = res.data;
						that.isFinish = 1;
						that.getQuestion();
						// window.location.href = serverHttp + '/app/PatientApp/jumpPage/question?memToken=' + that.memToken +'&objId=' + that.form.objId + '&id=' + res.data
					} else {
						alert(res.message);
					}
				}
			})
		},
		// 年龄
		getAge(birthday){
			console.log(birthday)
			if(birthday){
				birthday=birthday.split('-');
				// 新建日期对象
				let date = new Date();
				// 今天日期，数组，同 birthday
				let today = [date.getFullYear(), date.getMonth() + 1, date.getDate()];
				// 分别计算年月日差值
				let age = today.map((val, index) => {
					return val - birthday[index]
				})
				// 当天数为负数时，月减 1，天数加上月总天数
				if (age[2] < 0) {
					// 简单获取上个月总天数的方法，不会错
					let lastMonth = new Date(today[0], today[1], 0)
					age[1]--
					age[2] += lastMonth.getDate()
				}
				// 当月数为负数时，年减 1，月数加上 12
				if (age[1] < 0) {
					age[0]--
					age[1] += 12
				}
				// console.log(age[0]+'岁'+age[1]+'月'+age[2]+'天');
				this.form.age = age[0]+'岁';
			}
		},
		//为空判断
		isEmptyStr (str) {
			if ( str == "" ) return true;
			var regu = "^[ ]+$";
			var re = new RegExp(regu);
			return re.test(str);
		},
		//获取问卷答案
		getAnswer(id,key){
			for (var i = 0; i < this.optionArr.length; i++) {
				if (this.optionArr[i].quId == id) {
					this.optionArr[i].optionId = key.optionId
					this.optionArr[i].answerContent = key.optionTitle
					this.optionArr[i].score = key.score
				}
			}
			console.log(this.optionArr)
		},
		getAnswer2(id,key){
			for (var i = 0; i < this.optionArr2.length; i++) {
				if (this.optionArr2[i].quId == id) {
					this.optionArr2[i].optionId = key.optionId
					this.optionArr2[i].answerContent = key.optionTitle
					this.optionArr2[i].score = key.score
				}
			}
			console.log(this.optionArr2)
		},
		//获取问卷
		getQuestion(){
			var that = this;
			$.ajax({
				type : 'get',
				url : serverHttp+"/app/QuestionnaireApp/findQuestions",
				data :{
					type:that.form.objType,
					objId:that.form.objId
				},
				headers:{
					"memToken":that.memToken
				},
				success:function (res) {
					console.log(res)
					// 获取键值对  第几套试题
					let values = Object.values(res.data)
					let keys = Object.keys(res.data)
					that.paper =  keys[0];
					that.questionList =  values[0];
					that.paper2 =  keys[1];
					that.questionList2 =  values[1];
					// 设置数组，用来判断是否全部答题完成  提交
					var newArr = [];
					for (var i = 0; i < that.questionList.length; i++) {
						var optionId = '';
						var answerContent = '';
						var score = '';
						var answerArr = that.questionList[i].option
						for (var j = 0; j < answerArr.length; j++) {
							if (answerArr[j].checked == true) {
								optionId = answerArr[j].optionId
								answerContent = answerArr[j].optionTitle
								score = answerArr[j].score
							}
						}
						newArr.push({
							patientId: that.id,
							type: that.form.objType,
							objId: that.form.objId,
							quId: that.questionList[i].quId,
							quTitle: that.questionList[i].quTitle,
							optionId: optionId,
							answerContent: answerContent,
							score: score,
							frequency:2
						})
					}
					that.optionArr = newArr
					//问卷2
					var newArr2 = [];
					for (var i = 0; i < that.questionList2.length; i++) {
						var optionId2 = '';
						var answerContent2 = '';
						var score2 = '';
						var answerArr2 = that.questionList2[i].option
						for (var j = 0; j < answerArr2.length; j++) {
							if (answerArr2[j].checked == true) {
								optionId2 = answerArr2[j].optionId
								answerContent2 = answerArr2[j].optionTitle
								score2 = answerArr2[j].score
							}
						}
						newArr2.push({
							patientId: that.id,
							type: that.form.objType,
							objId: that.form.objId,
							quId: that.questionList2[i].quId,
							quTitle: that.questionList2[i].quTitle,
							optionId: optionId2,
							answerContent: answerContent2,
							score: score2,
							frequency:3
						})
					}
					that.optionArr2 = newArr2
				}
			})
		},
		//提交
		submit(){
			var that = this;
			var check = true;
			for (var i = 0; i < that.optionArr.length; i++) {
				if (that.optionArr[i].optionId == '') {
					check = false;
					break;
				}
			}
			if (!check) {
				alert("请完成问卷")
				return false;
			}
			$.ajax({
				type : 'POST',
				url : serverHttp+"/app/QuestionAnswer/addQuestionAnswer",
				data : JSON.stringify(that.optionArr),
				headers:{
					"memToken":that.memToken,
					"Content-Type":"application/json;charset=UTF-8"
				},
				success:function (res) {
					if (res.code == 200) {
						//切换第二个问卷
						$('html,body,.container').animate({ scrollTop: 0 }, 0);
						that.isFirst = 2
					} else {
						alert(res.message);
					}
				}
			})
		},
		submit2(){
			var that = this;
			var check = true;
			for (var i = 0; i < that.optionArr2.length; i++) {
				if (that.optionArr2[i].optionId == '') {
					check = false;
					break;
				}
			}
			if (!check) {
				alert("请完成问卷")
				return false;
			}
			$.ajax({
				type : 'POST',
				url : serverHttp+"/app/QuestionAnswer/addQuestionAnswer",
				data : JSON.stringify(that.optionArr2),
				headers:{
					"memToken":that.memToken,
					"Content-Type":"application/json;charset=UTF-8"
				},
				success:function (res) {
					if (res.code == 200) {
						that.getFinish();
					} else {
						alert(res.message);
					}
				}
			})
		},
		//获取结果
		getFinish(){
			var that = this;
			$.ajax({
				type : 'GET',
				url : serverHttp+"/app/QuestionAnswer/findQuestionScore",
				data : {
					patientId: that.id,
					objType: that.form.objType,
					objId: that.form.objId
				},
				headers:{
					"memToken":that.memToken,
				},
				success:function (res) {
					that.scoreAll = res.data[0].score;
					that.scoreAll2 = res.data[1].score;
					that.isFinish = 2;
					console.log(res)
				}
			})
		}
	}
})
