<template>
	<view class="main">
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">头像</view>
				<image class="cu-avatar radius bg-gray" @click="ChooseImage" :src='imgList[0]' mode='aspectFill'>
				</image>
			</view>
			<view class="cu-form-group">
				<view class="title">就诊人姓名:</view>
				<input v-model="name" placeholder-style="text-align:right" style="text-align: right;" placeholder="请输入姓名"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">出身年月:</view>
				<picker mode="date" :value="date" :end="time" @change="bindDateChange">
					<view class="picker">
						{{date}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group" v-if="jsGetAge(date)<18&&date!='请选择出生日期'">
				<view class="title">监护人姓名:</view>
				<input v-model="jianhuname" placeholder-style="text-align:right" style="text-align: right;" placeholder="请输入监护人姓名"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">联系电话:</view>
				<input type="number" v-model="phone" placeholder-style="text-align:right" style="text-align: right;" placeholder="请输入联系电话"></input>
			</view>
			<!-- <view class="cu-form-group">
				<view class="title">出生医院:</view>
				<input v-model="birthhospital" placeholder-style="text-align:right" style="text-align: right;" placeholder="请输入出生医院"></input>
			</view> -->
			<view class="cu-form-group" @click="loncationTap">
				<view class="title">地址:</view>
				<input v-model="address" placeholder-style="text-align:right" :disabled="true" style="text-align: right;"></input>
				<u-icon name="map-fill" color="#2979ff" size="32"></u-icon>
			</view>
			<view class="cu-form-group">
				<view class="title">详细住址:</view>
				<input v-model="infoaddress" placeholder-style="text-align:right" style="text-align: right;" placeholder="请输入详细住址"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">就诊人性别:</view>
				<u-radio-group v-model="value" @change="radioGroupChange">
					<u-radio v-for="(item, index) in list" :key="index" :name="item.name" :disabled="item.disabled">
						{{item.name}}
					</u-radio>
				</u-radio-group>
			</view>
			<view class="cu-form-group" @click="show=!show">
				<view class="title">与就诊人关系:</view>
				<picker :value="guanxi" @change="confirm" :range="guanxilist" range-key="label">
					<view class="picker">
						{{guanxi}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">就诊信息(非必填)</view>
			</view>
			<view class="cu-form-group">
				<view class="title">就诊人身份证:</view>
				<input v-model="idcrad" placeholder-style="text-align:right" style="text-align: right;" placeholder="请输入就诊人身份证号"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">电子邮箱:</view>
				<input v-model="email" placeholder-style="text-align:right" style="text-align: right;" placeholder="请输入电子邮箱"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">社保卡号:</view>
				<input v-model="social" placeholder-style="text-align:right" style="text-align: right;" placeholder="请输入社保卡号"></input>
			</view>
			<view class="padding-sm bg-white" style="color: #C0C0C0;">
				注：就诊人信息用于平台审核和医疗业务使用，不会外传。请正却确填写就诊人信息，如需修改身份信息可电话联系客服修改（13330431369）
			</view>
		</form>
		<view class="cu-bar bg-white tabbar" v-if="info==''" style="position: fixed;bottom: 0;width: 100%;z-index: 22;">
			<view class="submit" style="background-color: #0B73B9;color: white;" @click="addarchives">
				提交
			</view>
		</view>
		<view class="cu-bar bg-white tabbar" v-else style="position: fixed;bottom: 0;width: 100%; z-index: 22;">
			<view class="submit" style="background-color: #0B73B9;color: white;" @click="editarchives">
				修改
			</view>
		</view>
		<view class="cu-tabbar-height"></view>
		<view class="cu-tabbar-height"></view>
	</view>
</template>

<script>
	import {
		getLocation
	} from "../../common/authorize.js"
	export default {
		onLoad(options) {
			let date = new Date();
			let nian = date.getFullYear()
			let yue = date.getMonth() + 1
			let ri = date.getDate() >= 10 ? date.getDate() : '0' + date.getDate()
			this.time = nian + "-" + yue + "-" + ri
			if (options.info) {
				this.info = JSON.parse(options.info)
			}
			if (this.info != "") {
				uni.setNavigationBarTitle({
					title: "修改档案"
				})
				this.isaddress = false
				this.name = this.info.name
				this.imgList = [this.info.avatar]
				this.touxiang = this.info.avatar
				this.date = this.info.birthday
				this.value = this.list[this.info.sex - 1].name
				this.guanxi = this.guanxilist[this.info.relationship_type].label
				this.guanxiindex = this.info.relationship_type
				this.idcrad = this.info.card_number
				this.jianhuname = this.info.guardian_name
				this.phone = this.info.phone
				this.email = this.info.email
				// this.birthhospital = this.info.born_hospital
				this.address = this.info.address.split(' ')[0]
				this.infoaddress = this.info.address.split(' ')[1]
				this.social = this.info.social_card_number
			}
			this.GetCurrentAddress()
		},
		onShow() {

		},
		data() {
			return {
				infoaddress: "",
				address: "定位中...",
				birthhospital: "",
				email: "",
				phone: "",
				jianhuname: "",
				imgList: ['https://zhengda.oss-cn-chengdu.aliyuncs.com/baoma/static/%E5%A4%B4%E5%83%8F.png'],
				name: "",
				list: [{
						name: '男',
						disabled: false
					},
					{
						name: '女',
						disabled: false
					},
				],
				value: '男',
				date: "请选择出生日期",
				show: false,
				guanxi: "请选择",
				guanxiindex: -1,
				guanxilist: [{
						value: '1',
						label: '父子'
					},
					{
						value: '2',
						label: '母子'
					},
					{
						value: '3',
						label: '祖孙'
					},
					{
						value: '4',
						label: '亲属'
					},
					{
						value: '5',
						label: '本人'
					},
					{
						value: '6',
						label: '其他'
					},
				],
				idcrad: "",
				touxiang: "",
				time: "",
				info: "",
				social: "", //社保卡号
				isaddress: true,
				latitude: 0,
				longitude: 0
			}
		},
		methods: {
			jsGetAge(strBirthday) {
				let returnAge;
				let strBirthdayArr = strBirthday.split("-");
				let birthYear = strBirthdayArr[0];
				let birthMonth = strBirthdayArr[1];
				let birthDay = strBirthdayArr[2];
				let d = new Date();
				let nowYear = d.getFullYear();
				let nowMonth = d.getMonth() + 1;
				let nowDay = d.getDate();
				if (nowYear == birthYear) {
					returnAge = 0; //同年 则为0岁  
				} else {
					let ageDiff = nowYear - birthYear; //年之差  
					if (ageDiff > 0) {
						if (nowMonth == birthMonth) {
							let dayDiff = nowDay - birthDay; //日之差  
							if (dayDiff < 0) {
								returnAge = ageDiff - 1;
							} else {
								returnAge = ageDiff;
							}
						} else {
							let monthDiff = nowMonth - birthMonth; //月之差  
							if (monthDiff < 0) {
								returnAge = ageDiff - 1;
							} else {
								returnAge = ageDiff;
							}
						}
					} else {
						returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天  
					}
				}
				return returnAge; //返回周岁年龄  
			},
			ChooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: (res) => {
						this.imgList = res.tempFilePaths
						this.$request.upload("/api/v1/common/uploadFile", this.imgList[0]).then(data => {
							let re = JSON.parse(data)
							if (re.status == 0) {
								this.touxiang = re.data.url
								console.log(this.touxiang)
							}
						})

					}
				});
			},
			loncationTap() {
				uni.chooseLocation({
					latitude: this.latitude,
					longitude: this.longitude,
					success: (res) => {
						console.log(res)
						if (res.name !== "" || res.address !== "") {
							this.address = res.address
							this.latitude = res.latitude
							this.longitude = res.longitude
						} else {
							uni.showToast({
								title: "请选择位置",
								icon: "none"
							})
							return false
						}
					}
				})
			},
			GetCurrentAddress() {
				if (this.isaddress) {
					getLocation().then(res => {
						this.address = res.address.address
						this.latitude = res.latitude
						this.longitude = res.longitude
						console.log(res)
					}).catch(res => {})
				}
			},
			// 选中任一radio时，由radio-group触发
			radioGroupChange(e) {
				this.value = e
			},
			bindDateChange({
				detail
			}) {
				this.date = detail.value
			},
			confirm(e) {
				this.guanxiindex = e.detail.value
				this.guanxi = this.guanxilist[e.detail.value].label
			},
			addarchives: async function() {
				let num = 0
				if (this.value == "男") {
					num = 1
				} else {
					num = 2
				}
				if (this.imgList[0] == undefined) {
					uni.showToast({
						title: "请上传头像",
						icon: "none"
					})
					return false
				}
				if (this.name == "") {
					uni.showToast({
						title: "请填写名称",
						icon: "none"
					})
					return false
				}
				// if (this.jianhuname == "") {
				// 	uni.showToast({
				// 		title: "请输入监护人姓名",
				// 		icon: "none"
				// 	})
				// 	return false
				// }
				if (!this.$util.isPhoneNumber(this.phone)) {
					uni.showToast({
						title: "请输入正确手机号",
						icon: "none"
					})
					return false
				}
				// if (!this.$util.isEmail(this.email)) {
				// 	uni.showToast({
				// 		title: "请填写正确的邮箱",
				// 		icon: "none"
				// 	})
				// 	return false
				// }
				// if (this.birthhospital == "") {
				// 	uni.showToast({
				// 		title: "请填写正确的医院",
				// 		icon: "none"
				// 	})
				// 	return false
				// }
				if (this.infoaddress == "") {
					uni.showToast({
						title: "请填写正确的地址",
						icon: "none"
					})
					return false
				}
				if (this.date == "请选择出生日期") {
					uni.showToast({
						title: "请填写日期",
						icon: "none"
					})
					return false
				}
				if (this.guanxiindex == -1) {
					uni.showToast({
						title: "请选择被保人关系",
						icon: "none"
					})
					return false
				}
				let res = await this.$request.post("/api/v1/patient/createPatient", {
					name: this.name,
					sex: num,
					avatar: this.touxiang == '' ? this.imgList[0] : this.touxiang,
					birthday: this.date,
					relationship_type: this.guanxiindex,
					card_type: 1,
					card_number: this.idcrad,
					email: this.email,
					phone: this.phone,
					guardian_name: this.jianhuname,
					address: this.address + " " + this.infoaddress,
					// born_hospital: this.birthhospital,
					social_card_number: this.social
				})
				if (res.status == 0) {
					uni.showToast({
						title: "提交成功",
						icon: "none",
						duration: 1500
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 1500)
				} else {
					uni.showModal({
						title: "错误",
						content: res.message,
						showCancel: true,
					})
				}
			},
			editarchives: async function() {
				let num = 0
				if (this.value == "男") {
					num = 1
				} else {
					num = 2
				}
				if (this.imgList[0] == undefined) {
					uni.showToast({
						title: "请上传头像",
						icon: "none"
					})
					return false
				}
				if (this.name == "") {
					uni.showToast({
						title: "请填写名称",
						icon: "none"
					})
					return false
				}
				// if (this.jianhuname == "") {
				// 	uni.showToast({
				// 		title: "请输入监护人姓名",
				// 		icon: "none"
				// 	})
				// 	return false
				// }
				if (!this.$util.isPhoneNumber(this.phone)) {
					uni.showToast({
						title: "请输入正确手机号",
						icon: "none"
					})
					return false
				}
				// if (!this.$util.isEmail(this.email)) {
				// 	uni.showToast({
				// 		title: "请填写正确的邮箱",
				// 		icon: "none"
				// 	})
				// 	return false
				// }
				// if (this.birthhospital == "") {
				// 	uni.showToast({
				// 		title: "请填写正确的医院",
				// 		icon: "none"
				// 	})
				// 	return false
				// }
				if (this.infoaddress == "") {
					uni.showToast({
						title: "请填写正确的地址",
						icon: "none"
					})
					return false
				}
				if (this.date == "请选择出生日期") {
					uni.showToast({
						title: "请填写日期",
						icon: "none"
					})
					return false
				}
				if (this.guanxiindex == -1) {
					uni.showToast({
						title: "请选择被保人关系",
						icon: "none"
					})
					return false
				}
				let res = await this.$request.post("/api/v1/patient/updatePatient", {
					id: this.info.id,
					name: this.name,
					sex: num,
					avatar: this.touxiang,
					birthday: this.date,
					relationship_type: this.guanxiindex,
					card_type: 1,
					card_number: this.idcrad,
					email: this.email,
					phone: this.phone,
					guardian_name: this.jianhuname,
					address: this.address + " " + this.infoaddress,
					// born_hospital: this.birthhospital,
					social_card_number: this.social
				})
				if (res.status == 0) {
					uni.showToast({
						title: "修改成功",
						icon: "none",
						duration: 1500
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 1500)
				}
			}
		}
	};
</script>

<style scoped lang="scss">
	.main {}
</style>
