<template>
	<view class="app">
		<view class="" style="background-color: #F4F5F7;height: 20rpx;width:100%">
		</view>
		<view class="userinfo info">
			<view class="item">
				<span>姓名：</span>
				<u-input :clearable='false' v-model="query.patientName" type="text" placeholder='请输入' maxlength='30' />
			</view>
			<view class="item">
				<span>性别：</span>
				<u-radio-group v-model="query.sex" size='44'>
					<u-radio @change='sexchange' v-for="(item, index) in sexlist" :key="index" :name="item.name"
						:disabled="item.disabled">
						{{item.name}}
					</u-radio>
				</u-radio-group>
			</view>
			<!-- <view class="item">
				<span>电话:</span>
				<u-input :clearable='false' v-model="query.phone" maxlength='11' type="text" placeholder='请输入' />
			</view> -->
			<view class="item" @tap='timeshow=true'>
				<span>出生日期：</span>
				<view class="address" style="">
					{{query.birthDate}}
				</view>
			</view>
			<view class="item">
				<span>身份证号：</span>
				<u-input :clearable='false' v-model="query.cardNo" type="text" placeholder='请输入身份证号' maxlength='18' />
			</view>
			<view class="item" @tap='showPicker'>
				<span>所属区域：</span>
				<view class="address">{{address}}</view>
			</view>
			<view class="item">
				<span>详细地址：</span>
				<u-input :clearable='false' v-model="query.address" type="text" placeholder='小区、单元、门牌号'
					maxlength='40' />
			</view>
			<view class="item" @tap='getAddress()'>
				<span>所在位置：</span>
				<view class="address" style="">
					{{query.locationName}}
				</view>
			</view>
		</view>
		<view class="">
			<m-city style='z-index: 10076;' :provinceData="list" headTitle="地区选择" ref="cityPicker"
				@funcValue="getpickerParentValue" pickerSize="4">
			</m-city>
		</view>
		<view class="info">
			<view class="disease" style="border: none;" @tap="godisease">
				<view class="title" style="">基础疾病:</view>
				<image class="pictureA" src="../../static/huijiantou.png" mode=""></image>
				<view style="padding-right: 10rpx;display: inline-block;" v-for="(item,index) in query.diseaseInfoList">
					{{item.diseaseName}}
				</view>
			</view>
		</view>
		<view class="radio-content">
			<view class="radio-right" @tap="changeRadio">
				<view class="radio" :class="radio == 2 ? 'radio-default':''">
					<view :class="radio == 2 ? 'radio-active':''"></view>
				</view>
			</view>
			<view class="agreement">我已阅读并同意<text @tap='maskshow=true'
					style="color: #000000;border-bottom: 1rpx solid #000000;">《用户协议》</text>
			</view>
		</view>
		<view class="btn" @tap='informationinfo'>
			提交
		</view>
		<u-toast ref="uToast" />
		<!-- //用户协议 -->
		<u-mask :show="maskshow" class='mask' @click='maskshow=false'>
			<view class="Agreement">
				<view class="title">
					用户协议与隐私政策
				</view>
				<scroll-view scroll-y="true" class="scroll-Y" style="">
					<contenttext></contenttext>
				</scroll-view>
				<view class="cancel" @tap='maskshow=false'>
					取消
				</view>
				<view class="determine" @tap='tapradio'>
					确定并同意
				</view>
			</view>
		</u-mask>
		<u-picker mode="time" v-model="timeshow" :params="params" @confirm='timechange' @canel='timeshow=false'>
		</u-picker>
	</view>
</template>

<script>
	import {
		AppIdentification
	} from '@/api/pagesB/AppIdentification/index.js'
	import {
		getRegionAndStreetInfo,
		getNurseType,
		information
	} from '@/api/pagesB/information/index.js'
	import contenttext from '../text.vue';
	import {
		appPersonal,
	} from '@/api/pages/myinformation/myinformation.js';
	import {
		getSubordinateRegions,
	} from '@/api/pagesB/modifyAddress/modifyAddress.js';
	import gkcity from "../../components/m-city/m-city.vue";
	export default {
		components: {
			contenttext,
			"m-city": gkcity
		},
		data() {
			return {
				timeshow: false, //出生日期
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				sexlist: [{
						name: '男',
						disabled: false
					},
					{
						name: '女',
						disabled: false
					}
				],
				radio: 1,
				// chooseLocation: '', //地图选址
				arealist: [], //区街道list
				list: [{
					id: "",
					localName: "请选择",
					children: [],
				}, ],
				areashow: false, //经纬度开关
				maskshow: false, //用户协议
				getNurseTypelist: [], //护理类型数组
				medicalcarelist: [], //医疗护理数组
				address: '', //页面所属区域
				query: { //信息请求数组
					couponId: null,
					patientName: "",
					cardNo: "",
					phone: "",
					address: "",
					areaCode: "",
					homeLongitude: null,
					homeLatitude: null,
					nurseTypeIdList: [],
					diseaseInfoList: [],
					patientId: '',
					locationName: '',
					sex: '',
					birthDate: '',
				},
				timer: null,
				addresslength: null,
				patientDiseaseInfoList: [], //获取个人信息
			};
		},
		methods: {
			//提交信息
			informationinfo() {
				var that = this
				if (that.query.sex == '男') {
					that.query.sex = 'MALE';
				} else if (that.query.sex == '女') {
					that.query.sex = 'FEMALE';
				}
				if (this.addresslength) {
					if (this.addresslength.length > 2) {
						const value = uni.getStorageSync('phone');
						if (value) {
							that.query.phone = value
							if (that.radio == 1) {
								that.$refs.uToast.show({
									title: '请审核并同意用户协议',
									type: 'error'
								})
							} else {
								const patientId = uni.getStorageSync('patientId');
								AppIdentification(patientId).then(resp => {
									if (resp.code == 200) {
										if (resp.data.loginFlag) {
											that.$refs.uToast.show({
												title: '您已完善',
												type: 'success',
											})
											if (that.timer) {
												clearTimeout(that.timer)
											}
											that.timer = setTimeout(e => {
												uni.navigateBack({
													delta: 1
												})
											}, 1500)
										} else {
											information(that.query).then(res => {
												if (res.code == 200) {
													uni.removeStorageSync('invitationPatientId');
													that.$refs.uToast.show({
														title: '完善信息成功',
														type: 'success',
														duration: '1500'
													})
													if (that.timer) {
														clearTimeout(that.timer)
													}
													that.timer = setTimeout(e => {
														uni.navigateBack({
															delta: 1
														})
													}, 1500)
												} else if (res.code == 500) {
													that.$refs.uToast.show({
														title: res.msg,
														type: 'error',
													})
												}
											})
										}
									}
								})
							}
						}
					} else {
						that.$refs.uToast.show({
							title: '所属区域应选择所在的区或街道,请重新选择!',
							type: 'error'
						})
					}
				} else {
					that.$refs.uToast.show({
						title: '所属区域应选择所在的区或街道,请重新选择!',
						type: 'error'
					})
				}
			},
			//性别
			sexchange(e) {
				if (e == '男') {
					this.query.sex = 'MALE';
				} else if (e == '女') {
					this.query.sex = 'FEMALE';
				}
			},
			//选择出生日期
			timechange(e) {
				this.query.birthDate = e.year + '-' + e.month + '-' + e.day
			},
			//获取所在位置
			getAddress() {
				var that = this;
				uni.chooseLocation({
					success: function(location) {
						that.query.locationName = location.address
						that.query.homeLongitude = location.longitude;
						that.query.homeLatitude = location.latitude;
					}
				});
			},
			// getAddress() {
			// 	uni.getLocation({
			// 		type: 'wgs84',
			// 		success: function(res) {
			// 			const key = '3VABZ-6LZWK-YPAJ3-AMQED-D7RUK-VPB37'; //使用在腾讯位置服务申请的key
			// 			const referer = '新医路健康服务平台'; //调用插件的app的名称
			// 			const location = JSON.stringify({
			// 				latitude: 39.89631551,
			// 				longitude: 116.323459711
			// 			});
			// 			wx.navigateTo({
			// 				url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer
			// 			});
			// 		}
			// 	});
			// },
			//护理类型请求
			getNurseTypeInfo() {
				getNurseType().then(res => {
					this.medicalcarelist = res.data.filter(e => e.nurseTypeName == '医疗护理')
					this.getNurseTypelist = res.data.filter(e => e.nurseTypeName != '医疗护理')
				})
			},
			//区街道
			areaInfo() {
				getSubordinateRegions().then(res => {
					this.list = res.data;
				})
			},
			// 三级地址联动回调
			getpickerParentValue(e) {
				e = e.filter(ele => ele.localName != '暂不选择')
				e = e.filter(ele => ele.localName != '暂无需选择')
				this.addresslength = e
				this.address = ''
				this.query.areaCode = ''
				if (e && e.length >= 1) {
					e.forEach(el => {
						this.address = this.address + el.localName
					})
					this.query.areaCode = e[e.length - 1].id
				} else {
					this.address = ''
					this.query.areaCode = ''
				}
			},
			// 显示三级地址联动
			showPicker() {
				this.areashow = true
				this.$refs.cityPicker.show();
			},
			//点击所需服务
			addnurseType(item) {
				if (this.query.nurseTypeIdList.findIndex(e => e == item.id) == -1) {
					this.query.nurseTypeIdList.push(item.id)
				} else {
					this.query.nurseTypeIdList = this.query.nurseTypeIdList.filter(e => e != item.id)
				}
			},
			godisease(item) {
				uni.navigateTo({
					url: `/pagesB/disease/disease?diseaseInfoList=${JSON.stringify(this.query.diseaseInfoList)}`
				})
			},
			changeRadio() {
				if (this.radio == 1) {
					this.radio = 2;
				} else {
					this.radio = 1;
				}
			},
			tapradio() {
				this.radio = 2;
				this.maskshow = false;
			},
		},
		//进入界面加载
		onLoad(options) {
			let that = this
			const value = uni.getStorageSync('userinfo');
			if (value) {
				that.query.patientId = value.id
			}

			if(options.updata){
				// this.baseurl = baseurl
				// var that = this
				const value = uni.getStorageSync('userinfo');
				// if (value) {
					const patientid =value.id
					appPersonal(patientid).then(Response => {
						if (Response.code == 200) {
							that.query = Response.data
							Number(that.query.homeLatitude) > 0 ? that.query.homeLatitude = Number(
								that.query.homeLatitude) : that.query.homeLatitude = null
							Number(that.query.homeLongitude) > 0 ? that.query.homeLongitude =
								Number(that.query.homeLongitude) : that.query.homeLongitude = null
							if (that.query.headPictureUrl) {
								that.img = baseurl + that.query.headPictureUrl
							}
							if (that.query.sex == 'MALE') {
								that.query.sexname = '男';
							} else if (that.query.sex == 'FEMALE') {
								that.query.sexname = '女';
							}
							if (that.query.areaName) {
								that.address = that.query.areaName
							}
						} else if (Response.code == 9999) {} else {
							uni.navigateBack({
								delta: 1
							})
						}
					})
				// } else {}
				// this.listinfo = JSON.parse(options.updata)
				// console.log(this.listinfo, '888')
			}else{
				this.query.couponId = Number(options.couponId)
			}
			this.areaInfo()
			this.getNurseTypeInfo();
		},
		//带参返回
		// 从地图选点插件返回后，在页面的onShow生命周期函数中能够调用插件接口，取得选点结果对象
		onShow() {
			var that = this
			const invitationPatientId = uni.getStorageSync('invitationPatientId')
			if (invitationPatientId) {
				that.query.source = 'FRIEND_INVITATION'
				that.query.invitationPatientId = Number(invitationPatientId)
			} else {
				that.query.source = 'REGISTER_YOURSELF'
				that.query.invitationPatientId = null
			}
			// const chooseLocation = requirePlugin('chooseLocation');
			// const location = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null
			// if (location) {
			// 	that.query.locationName = location.address
			// 	that.chooseLocation = location.address
			// 	that.query.homeLongitude = location.longitude;
			// 	that.query.homeLatitude = location.latitude;
			// }
			uni.$on('disease', function(data) {
				that.query.diseaseInfoList = JSON.parse(data.disease)
				if (that.query.diseaseInfoList.length > 0) {
					that.medicalcarelist.forEach(e => {
						if (that.query.nurseTypeIdList.findIndex(el => el == e.id) == -1) {
							that.query.nurseTypeIdList.push(e.id)
						} else {}
					})
				} else {
					that.medicalcarelist.forEach(e => {
						that.query.nurseTypeIdList = that.query.nurseTypeIdList.filter(el => el != e.id)
					})
				}
				uni.$off('disease')
			})
		},
	}
</script>
<style lang="scss">
	@import './information.scss';
</style>
