<template>
	<div>
		<common @asynCallBack="asynCallBack"></common>

		<div class="mui-content vongi-fx">
			<div class="mui-content-padded">
				<h1 class="mui-text-center color4fc5f7">返乡人员登记</h1>
				<p>目前，疫情防控形势仍严峻复杂,您家或亲戚朋友家有亲人欲返荆过春节,请您尽量劝其减少流动风险、留在原地过春节。请您出行一定记得戴口罩、与他人保持安全距离(1米以上)
					:非必要不出境、不去国内中、高风险区域若邻居家有外地返荆人员,请帮忙提醒他与社区联系报备。请您记住:少聚集、少聚餐、勤洗手、红事缓办、白事简办，聚会不办。
				</p>
			</div>
			<form class="mui-input-group vongi-editme margin10">
				<div class="mui-input-row">
					<label><i class="colorfe616c">*</i>&nbsp;您的姓名</label>
					<input v-model="subForm.name" name="name" type="text" class="mui-input-clear" placeholder="请填写您的姓名" autocomplete="off">
				</div>
				<div class="mui-input-row">
					<label><i class="colorfe616c">*</i>&nbsp;身份证号码</label>
					<input v-model="subForm.idCard" name="idCard" type="text" class="mui-input-clear" placeholder="请填写身份证号码 "
					 autocomplete="off" maxlength="18">
				</div>
				<div class="mui-input-row">
					<label><i class="colorfe616c">*</i>&nbsp;手机号码</label>
					<input v-model="subForm.phone" name="phone" type="number" maxlength="11" class="mui-input-clear" placeholder="请输入手机号码  "
					 autocomplete="off">
				</div>
				<div class="mui-input-row vongi-fx-yzm">
					<label><i class="colorfe616c">*</i>&nbsp;验证码</label>
					<input v-model="subForm.code" name="code" type="text" class="mui-input-clear" placeholder="请填写验证码  " autocomplete="off">
					<a @click="sendMsg" v-text="sendMsgWz"></a>
				</div>
				<div @click="selectLiveRegionId" class="mui-input-row">
					<label style="width:50%"><i class="colorfe616c">*</i>&nbsp;居住所在地</label>
					<button v-text="liveRegionIdName" class="mui-btn mui-btn-block mui-navigate-right" type='button' style="width:50%">
					</button>
				</div>
				<div class="mui-input-row">
					<label style="width:100%"><i class="colorfe616c">*</i>&nbsp;详细地址</label>
					<textarea v-model="subForm.liveDetailAddress" name="liveDetailAddress" rows="2" placeholder="请填写详细地址信息"
					 autocomplete="off"></textarea>
				</div>
			</form>
			<form class="mui-input-group vongi-editme margin10">
				<div @click="selectType" class="mui-input-row" style="margin-top: 10px;">
					<label><i class="colorfe616c">*</i>&nbsp;返乡类型</label>
					<button v-text="typeName" class="mui-btn mui-btn-block mui-navigate-right" type='button' style="width:65%">
					</button>
				</div>
				<div v-if="subForm.type=='3'" class="mui-content-padded color4fc5f7 mui-h5" v-text="typeDec"></div>
			</form>
			<form class="mui-input-group vongi-editme margin10">
				<div @click="selectOriginId" class="mui-input-row">
					<label><i class="colorfe616c">*</i>&nbsp;始发地</label>
					<button v-text="originIdName" class="mui-btn mui-btn-block mui-navigate-right" type='button' style="width:65%">
					</button>
				</div>
				<div class="mui-input-row">
					<label style="width:100%"><i class="colorfe616c">*</i>&nbsp;始发地详细地址</label>
					<textarea v-model="subForm.originDetailAddress" name="originDetailAddress" rows="2" placeholder="请填写始发地详细地址"
					 autocomplete="off"></textarea>
				</div>
				<div class="mui-input-row">
					<label><i class="colorfe616c">*</i>&nbsp;途径地</label>
					<textarea v-model="subForm.avenuePlace" name="avenuePlace" rows="2" placeholder="请填写详细途径地信息" autocomplete="off"></textarea>
				</div>
				<div class="mui-input-row">
					<label style="width:100%"><i class="colorfe616c">*</i>&nbsp;返乡乘坐（或使用）交通工具情况（飞机航班、动车车次、汽车车牌号）</label>
					<textarea v-model="subForm.vehicle" name="vehicle" rows="2" placeholder="请填写详细返乡交通工具信息" autocomplete="off"></textarea>
				</div>
				<div @click="selectDate" class="mui-input-row">
					<label style="width:60%"><i class="colorfe616c">*</i>&nbsp;返乡时间（已到/预计）</label>
					<button v-text="subForm.arriveTime?subForm.arriveTime:'请选择'" class="mui-btn mui-btn-block mui-navigate-right" type='button'
					 style="width:40%"></button>
				</div>
				<div class="mui-input-row vongi-fx-yzm">
					<label style="width:70%;padding:11px 0 11px 15px"><i class="colorfe616c">*</i>&nbsp;是否有7日内核酸检测阴性证明</label>
					<div class="mui-radio mui-left">
						<label>是</label>
						<input v-model="subForm.haveCheckStr" name="haveCheckStr" type="radio" value="1">
					</div>
					<div class="mui-radio mui-left" style="margin-right:15px">
						<label>否</label>
						<input v-model="subForm.haveCheckStr" name="haveCheckStr" type="radio" value="0">
					</div>
				</div>
				<div class="mui-input-row">
					<label>备注</label>
					<textarea v-model="subForm.remark" name="remark" rows="2" placeholder="可输入备注" autocomplete="off"></textarea>
				</div>
				<div class="mui-input-row">
					<label style="width:70%;padding:11px 0 11px 15px"><i v-if="subForm.type!='4'" class="colorfe616c">*&nbsp;</i>上传7日内核酸检测报告照片</label>
					<button @click="chooseImage('checkImageUrl')" class="mui-btn mui-btn-block mui-navigate-right" type='button' style="width:30%">
						<img v-if="subForm.checkImageUrl" :src="subForm.checkImageUrl" />
						<span v-else>点击上传</span>
					</button>
				</div>
				<div class="mui-input-row">
					<label style="width:70%"><i v-if="subForm.type!='4'" class="colorfe616c">*&nbsp;</i>上传“行程卡”截图</label>
					<button @click="chooseImage('travelImageUrl')" class="mui-btn mui-btn-block mui-navigate-right" type='button'
					 style="width:30%">
						<img v-if="subForm.travelImageUrl" :src="subForm.travelImageUrl" />
						<span v-else>点击上传</span>
					</button>
				</div>
				<div class="mui-input-row">
					<label style="width:70%"><i v-if="subForm.type!='4'" class="colorfe616c">*&nbsp;</i>上传本人健康码截图</label>
					<button @click="chooseImage('healthCodeUrl')" class="mui-btn mui-btn-block mui-navigate-right" type='button' style="width:30%">
						<img v-if="subForm.healthCodeUrl" :src="subForm.healthCodeUrl" />
						<span v-else>点击上传</span>
					</button>
				</div>
			</form>
			<div class="vongi-btn vongi-login-btn">
				<button v-text="submitButtonText" @click="submit" :class="'mui-btn '+(subForm.enableUpdate?'mui-btn-primary':'mui-btn-grey')"
				 style="border-radius: 30px;" :disabled="!subForm.enableUpdate">
					保存
				</button>
			</div>
		</div>

		<loading :visible="isLoading"></loading>
	</div>
</template>

<script>
	require('$project/assets/js/mui.picker.min.js');
	import * as API_WeiXin from '$project/apis/weixin'
	import * as API_return from '@/apis/Other/return'
	import Common from '$project/components/Common.vue'
	import Loading from '$project/components/Loading.vue'
	import TopHeader from '$project/components/TopHeader.vue'
	import * as API_Common from '$project/apis/common'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import {
		checkPhone,
		checkIdCard
	} from '$project/utils'
	import * as WxJsApi from '$project/utils/wxJsApi'
	export default {
		name: 'OtherReturnQuestionnaire',
		components: {
			Common,
			Loading,
			TopHeader
		},
		data() {
			return {
				isLoading: false,

				//pageTitle: '填写问卷',

				subForm: {
					openId: '',
					name: '',
					idCard: '',
					phone: '',
					liveRegionId: '',
					liveDetailAddress: '',
					type: '',
					originId: '',
					originDetailAddress: '',
					avenuePlace: '',
					vehicle: '',
					arriveTime: '',
					haveCheckStr: '1',
					code: '',
					remark: '',
					checkImageUrl: '',
					travelImageUrl: '',
					healthCodeUrl: '',
					//是否可以提交更新
					enableUpdate: true,
				},

				liveRegionIdCheckArr: [],
				originIdCheckArr: [],


				sendMsgWz: '发送验证码',

				liveRegionIdList: [],
				originIdList: [],
				typeList: [{
						value: '1',
						text: '境外返乡',
						dec: '',
					},
					{
						value: '2',
						text: '国内中高风险地区',
						dec: '',
					},
					{
						value: '3',
						text: '省内从事冷链行业',
						dec: '注：包含进口冷链食品从业人员、口岸直接接触进口货物从业人员、隔离场所工作人员、交通运输工具从业人员等重点人群。',
					},
					{
						value: '4',
						text: '其他',
						dec: '',
					},
				],

				submitButtonText: '保存',
				//是否在提交确认过程中,0未保存1已保存确认中2已确认
				isSubmitSureIng: 0,
				sureForm: {
					openId: '',
					id: ''
				}

			}
		},
		created() {},
		methods: {
			//选择时间
			selectDate() {
				var _this = this;
				var picker = new mui.DtPicker({
					"type": "date",
					"beginYear": new Date().getFullYear(),
					"endYear": 2021,
					"startDate": new Date(),
					"value": _this.subForm.arriveTime
				});
				picker.show(function(rs) {
					_this.subForm.arriveTime = rs.text;
					picker.dispose();
				});
			},
			//微信选择图片
			chooseImage(field) {
				WxJsApi.chooseImage().then(res => {
					var localData = res.localData;

					if (localData.indexOf('data:image') != 0) {
						//判断是否有这样的头部
						localData = 'data:image/jpeg;base64,' + localData
					}
					localData = localData.replace(/\r|\n/g, '').replace('data:image/jgp', 'data:image/jpeg')
					this.imgBase64 = localData;

					this.uploadpic(field);
				}).catch(error => {
					mui.toast(error);
				})
			},
			//上传图片
			uploadpic(field) {
				this.isLoading = true;
				WxJsApi.uploadPic(this.imgBase64).then(response => {
					this.isLoading = false;

					this.subForm[field] = response;
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//发送验证码检测手机号
			checkPhone() {
				let phoneResult = checkPhone(this.subForm.phone);
				if (typeof phoneResult == 'string') {
					mui.toast(phoneResult);
					return false;
				} else {
					return true;
				}
			},
			//发送验证码
			sendMsg() {
				if (this.checkPhone()) {
					this.isLoading = true;
					API_Common.sendMsg({
						telephone: this.subForm.phone,
					}).then(response => {
						//倒计时
						this.msgTimeInterval();
						this.isLoading = false;
					}).catch(error => {
						this.isLoading = false;
						mui.toast(error);
					})
				}
			},
			//倒计时
			msgTimeInterval() {
				var time = 60;
				var _this = this;
				this.timer = setInterval(() => {
					if (time > 0) {
						_this.sendMsgWz = time-- + '秒';
					} else {
						_this.isSendMsg = true;
						_this.sendMsgWz = '发送验证码';
						clearInterval(_this.timer)
					}
				}, 1000)
			},
			//获取地区列表
			getregionList(name, fun) {
				this.isLoading = true;
				API_return.regionList(name).then(response => {
					this.isLoading = false;

					fun(response.list)
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//检测表单
			checkForm() {
				let phoneResult = checkPhone(this.subForm.phone);
				let idCardResult = checkIdCard(this.subForm.idCard);
				if (!this.subForm.openId) {
					mui.toast('未获取您的微信授权');
					return false;
				} else if (!this.subForm.name) {
					mui.toast('请输入姓名');
					return false;
				} else if (typeof idCardResult == 'string') {
					mui.toast(idCardResult);
					return false;
				} else if (typeof phoneResult == 'string') {
					mui.toast(phoneResult);
					return false;
				} else if (!this.subForm.code) {
					mui.toast('请输入手机验证码');
					return false;
				} else if (!this.subForm.liveRegionId) {
					mui.toast('请选择居住所在地');
					return false;
				} else if (!this.subForm.liveDetailAddress) {
					mui.toast('请填写居住所在地详情地址');
					return false;
				} else if (!this.subForm.originId) {
					mui.toast('请选择始发地');
					return false;
				} else if (!this.subForm.originDetailAddress) {
					mui.toast('请填写始发地详情地址');
					return false;
				} else if (!this.subForm.avenuePlace) {
					mui.toast('请填写途经地');
					return false;
				} else if (!this.subForm.vehicle) {
					mui.toast('请填写返乡乘坐（或使用）交通工具情况');
					return false;
				} else if (!this.subForm.arriveTime) {
					mui.toast('请选择返乡时间');
					return false;
				} else if (this.subForm.type != '4') {
					if (!this.subForm.checkImageUrl) {
						mui.toast('请上传7日内核酸检测报告');
						return false;
					} else if (!this.subForm.travelImageUrl) {
						mui.toast('请上传“行程卡”截图');
						return false;
					} else if (!this.subForm.healthCodeUrl) {
						mui.toast('请上传本人健康码截图');
						return false;
					} else {
						return true;
					}
				} else {
					return true;
				}
			},
			//提交
			submit() {
				if (this.isSubmitSureIng == 0) {
					if (this.checkForm()) {
						this.isLoading = true;
						API_return.submitReturnInfo(this.subForm).then(response => {
							this.isLoading = false;

							this.sureForm.id = response.id;

							var _this = this;
							mui.alert('保存成功，请重新查看并确认数据是否正确，确认后请再次点击提交按钮上传数据', '提示', function() {
								_this.isSubmitSureIng = 1;
								_this.subForm.enableUpdate = false;
								//提交倒计时
								_this.submitInterval();
							});

						}).catch(error => {
							this.isLoading = false;
							mui.toast(error);
						})
					}
				} else if (this.isSubmitSureIng == 2) {
					this.sureSubmit();
				}
			},
			//确认提交
			sureSubmit() {
				this.isLoading = true;
				API_return.sureSubmitReturnInfo(this.sureForm).then(response => {
					this.isLoading = false;

					//恢复初始值
					this.isSubmitSureIng == 0;

					var _this = this;
					mui.alert('提交成功', '提示', function() {
						_this.$router.push({
							name: 'OtherReturnSuccess'
						});
					});

				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//提交倒计时
			submitInterval() {
				var time = 5;
				var _this = this;
				this.sub_timer = setInterval(() => {
					if (time > 0) {
						_this.submitButtonText = '提交（' + time-- + '）';
					} else {
						_this.subForm.enableUpdate = true;
						_this.isSubmitSureIng = 2;
						_this.submitButtonText = '提交';
						clearInterval(_this.sub_timer)
					}
				}, 1000)
			},
			//获取详情
			getInfo() {
				this.isLoading = true;
				API_return.returnInfoDetail(this.subForm.openId).then(response => {
					this.isLoading = false;

					if (response) {
						for (let key in this.subForm) {
							if (response[key] != null) {
								this.$set(this.subForm, key, response[key]);
							}
						}
					}

				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//选择居住地
			selectLiveRegionId() {
				var _this = this;
				var picker = new mui.PopPicker({
					layer: 3
				});
				picker.setData(this.liveRegionIdList);

				//多级筛选设置默认值
				picker.pickers[0].setSelectedValue(this.liveRegionIdCheckArr[0], 0, function() {
					setTimeout(function() {
						picker.pickers[1].setSelectedValue(_this.liveRegionIdCheckArr[1], 0, function() {
							setTimeout(function() {
								picker.pickers[2].setSelectedValue(_this.liveRegionIdCheckArr[2]);
							}, 100);
						});
					}, 100);
				});

				picker.show(function(selectItems) {
					//重置数据集
					_this.liveRegionIdCheckArr = [];
					for (var i = 0; i < 3; i++) {
						if (selectItems[i].value) {
							_this.liveRegionIdCheckArr.push(selectItems[i].value);
							_this.subForm.liveRegionId = selectItems[i].value;
						}
					}
				})
			},
			//选择始发地
			selectOriginId() {
				var _this = this;
				var picker = new mui.PopPicker({
					layer: 3
				});
				picker.setData(this.originIdList);

				//多级筛选设置默认值
				picker.pickers[0].setSelectedValue(this.originIdCheckArr[0], 0, function() {
					setTimeout(function() {
						picker.pickers[1].setSelectedValue(_this.originIdCheckArr[1], 0, function() {
							setTimeout(function() {
								picker.pickers[2].setSelectedValue(_this.originIdCheckArr[2]);
							}, 100);
						});
					}, 100);
				});

				picker.show(function(selectItems) {
					//重置数据集
					_this.originIdCheckArr = [];
					for (var i = 0; i < 3; i++) {
						if (selectItems[i].value) {
							_this.originIdCheckArr.push(selectItems[i].value);
							_this.subForm.originId = selectItems[i].value;
						}
					}
				})
			},
			//选择返乡类型
			selectType() {
				var _this = this;
				var picker = new mui.PopPicker();
				picker.setData(this.typeList);
				picker.pickers[0].setSelectedValue(_this.subForm.type);
				picker.show(function(selectItems) {
					_this.subForm.type = selectItems[0].value;
				})
			},
			asynCallBack() {
				//赋值
				this.subForm.openId = this.openId;
				this.sureForm.openId = this.openId;
				//获取详情
				this.getInfo();
			},
		},
		mounted() {
			//获取微信配置
			WxJsApi.getWxConfig();
			var _this = this;
			//获取居住地
			this.getregionList('1', function(list) {
				_this.liveRegionIdList = list;
			})
			//获取始发地
			this.getregionList('2', function(list) {
				_this.originIdList = list;
			})
		},
		destroyed() {},
		computed: {
			liveRegionIdName: function() {
				var nameArr = [];
				for (var i = 0; i < this.liveRegionIdList.length; i++) {
					nameArr[0] = this.liveRegionIdList[i]['text'];
					if (this.liveRegionIdList[i]['value'] == this.subForm.liveRegionId) {
						//return this.liveRegionIdList[i]['text'];
						return nameArr.join('/');
					}
					for (var j = 0; j < this.liveRegionIdList[i]['children'].length; j++) {
						nameArr[1] = this.liveRegionIdList[i]['children'][j]['text'];
						if (this.liveRegionIdList[i]['children'][j]['value'] == this.subForm.liveRegionId) {
							//return this.liveRegionIdList[i]['children'][j]['text'];
							return nameArr.join('/');
						}
						for (var s = 0; s < this.liveRegionIdList[i]['children'][j]['children'].length; s++) {
							nameArr[2] = this.liveRegionIdList[i]['children'][j]['children'][s]['text'];
							if (this.liveRegionIdList[i]['children'][j]['children'][s]['value'] == this.subForm.liveRegionId) {
								//return this.liveRegionIdList[i]['children'][j]['children'][s]['text'];
								return nameArr.join('/');
							}
						}
					}
				}
				return '点击选择';
			},
			originIdName: function() {
				var nameArr = [];
				for (var i = 0; i < this.originIdList.length; i++) {
					nameArr[0] = this.originIdList[i]['text'];
					if (this.originIdList[i]['value'] == this.subForm.originId) {
						//return this.originIdList[i]['text'];
						return nameArr.join('/');
					}
					for (var j = 0; j < this.originIdList[i]['children'].length; j++) {
						nameArr[1] = this.originIdList[i]['children'][j]['text'];
						if (this.originIdList[i]['children'][j]['value'] == this.subForm.originId) {
							//return this.originIdList[i]['children'][j]['text'];
							return nameArr.join('/');
						}
						for (var s = 0; s < this.originIdList[i]['children'][j]['children'].length; s++) {
							nameArr[2] = this.originIdList[i]['children'][j]['children'][s]['text'];
							if (this.originIdList[i]['children'][j]['children'][s]['value'] == this.subForm.originId) {
								//return this.originIdList[i]['children'][j]['children'][s]['text'];
								return nameArr.join('/');
							}
						}
					}
				}
				return '点击选择';
			},
			typeName: function() {
				for (var i = 0; i < this.typeList.length; i++) {
					if (this.typeList[i]['value'] == this.subForm.type) {
						return this.typeList[i]['text'];
					}
				}
				return '点击选择';
			},
			typeDec: function() {
				for (var i = 0; i < this.typeList.length; i++) {
					if (this.typeList[i]['value'] == this.subForm.type) {
						return this.typeList[i]['dec'];
					}
				}
				return '';
			},
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				person_data: 'person_data',
				person_popedom: 'person_popedom',
				menu_list: 'menu_list',
				common_menu_list: 'common_menu_list',
			})
		},
		watch: {
			'subForm.type': {
				handler(newValue, oldValue) {
					if (newValue == '1') {
						this.subForm.originId = '100000';
					} else if (newValue != '' && this.subForm.originId == '100000') {
						this.subForm.originId = '';
					}
				},
				immediate: true,
				deep: true
			},
			'subForm.originId': {
				handler(newValue, oldValue) {
					if (newValue == '100000') {
						this.subForm.type = '1';
					} else if (newValue != '' && this.subForm.type == '1') {
						this.subForm.type = '';
					}
				},
				immediate: true,
				deep: true
			}
		}
	}
</script>

<style scoped src="$project/assets/css/xpwyfyy.css"></style>
<style src="$project/assets/css/iconfont.css"></style>
<style src="$project/assets/css/mui.picker.min.css"></style>
<style>
</style>
