<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='5' />
			</view>
			<!-- <view class="item">
				<span>电话:</span>
				<u-input :clearable='false' v-model="query.phone" maxlength='11' type="text" placeholder='' />
			</view> -->
			<view class="item">
				<span>身份证号:</span>
				<u-input :clearable='false' v-model="query.cardNo" type="text" placeholder='' maxlength='18' />
			</view>
			<view class="item" @tap='areashow=true'>
				<span>所属区域:</span>
				<view class="address">{{address}}</view>
			</view>
			<view class="item">
				<span>详细地址:</span>
				<u-input :clearable='false' v-model="query.address" maxlength='40' type="text"
					placeholder='小区、单元、门牌号' />
			</view>
			<view class="item" @tap='getAddress()'>
				<span>所在位置:</span>
				<view class="address" style="overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">
					{{chooseLocation}}
				</view>
			</view>
		</view>
		<u-select v-model="areashow" mode="mutil-column-auto" label-name='areaName' value-name='areaCode'
			:list="arealist" @confirm="areaconfirm"></u-select>
		<view class="info">
			<view class="service">
				<view class="title">
					所属服务
				</view>
				<view class="Multiplechoice">
					<view :class="query.nurseTypeIdList.find(e => e == item.id)?'choices':'choice'"
						v-for="(item,index) in getNurseTypelist" :key="item.id" @tap="addnurseType(item)">
						{{item.nurseTypeName}}
					</view>
					<view @tap="godisease(item)"
						:class="query.nurseTypeIdList.find(e => e == item.id)?'choices':'choice'"
						v-for="(item,index) in medicalcarelist" :key="item.id">
						{{item.nurseTypeName}}
					</view>
				</view>
			</view>
			<u-button type="primary" style='width:30%;float: right;margin-top: 50rpx;' @tap='informationinfo'>提交
			</u-button>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		getRegionAndStreetInfo,
		getNurseType,
		information
	} from '@/api/information/index.js'
	export default {
		data() {
			return {
				timer: null,
				arealist: [], //区街道list
				areashow: false, //经纬度开关
				getNurseTypelist: [], //护理类型数组
				medicalcarelist: [], //医疗护理数组
				address: '', //页面所属区域
				chooseLocation: '',
				query: { //信息请求数组
					patientName: "",
					cardNo: "",
					phone: "",
					address: "",
					areaCode: "",
					nurseTypeIdList: [],
					diseaseInfoList: [],
					patientId: '',
				}
			};
		},
		methods: {
			//提交信息
			informationinfo() {
				let that = this
				try {
					const value = uni.getStorageSync('phone');
					if (value) {
						that.query.phone = value
						information(that.query).then(res => {
							if (res.code == 200) {
								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',
								})
							}
						})
					}
				} catch (e) {}
			},
			//获取所在位置
			getAddress() {
				var that = this;
				uni.chooseLocation({
					success: function(res) {
						that.query.locationName = res.name
						that.chooseLocation = res.name
						that.query.homeLongitude = res.longitude;
						that.query.homeLatitude = res.latitude;
					}
				});
			},
			//护理类型请求
			getNurseTypeInfo() {
				getNurseType().then(res => {
					this.medicalcarelist = res.data.filter(e => e.nurseTypeName == '医疗护理')
					this.getNurseTypelist = res.data.filter(e => e.nurseTypeName != '医疗护理')
				})
			},
			//区街道
			areaInfo() {
				getRegionAndStreetInfo().then(res => {
					//区下无街道添加一个空街道
					res.data.forEach(e => {
						if (e.children.length == 0) {
							e.children.push({
								areaCode: "",
								areaName: "暂无街道",
								children: null,
								id: '',
							})
						}
					})
					this.arealist = res.data;
				})
			},
			//区街道选择
			areaconfirm(e) {
				this.query.areaCode = e[e.length - 1].value
				this.address = e[0].label + '-' + e[1].label
			},
			//点击所需服务
			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: `/pages/disease/disease?diseaseInfoList=${JSON.stringify(this.query.diseaseInfoList)}`
				})
			},
		},
		onUnload() {
			// 页面卸载时设置插件选点数据为null，防止再次进入页面，geLocation返回的是上次选点结果
			chooseLocation.setLocation(null);
		},
		//进入界面加载
		onLoad() {
			let that = this
			try {
				const value = uni.getStorageSync('patientId');
				if (value) {
					that.query.patientId = value
				}
			} catch (e) {
				// error
			}
			// uni.getStorage({
			// 	key: 'patientId',
			// 	success: function(res) {
			// 		that.query.patientId = res.data
			// 	}
			// });
			this.areaInfo()
			this.getNurseTypeInfo();
		},
		//带参返回
		onShow() {
			let that = this
			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 {
					console.log(that.query.nurseTypeIdList)
					that.medicalcarelist.forEach(e => {
						that.query.nurseTypeIdList = that.query.nurseTypeIdList.filter(el => el != e.id)
					})
				}
			})
		}
	}
</script>
<style lang="scss">
	@import './information.scss';
</style>
