<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>
				<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">
				<view class="title">家庭住址:</view>
				<input v-model="address" 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">
				<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" @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>
	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.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
				this.social = this.info.social_card_number
			}
		},
		onShow() {

		},
		data() {
			return {
				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: '外祖母'
					},
					{
						value: '7',
						label: '叔侄'
					},
					{
						value: '8',
						label: '其他'
					}
				],
				idcrad: "",
				touxiang: "",
				time: "",
				info: "",
				social: "" //社保卡号
			}
		},
		methods: {
			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)
							}
						})

					}
				});
			},
			// 选中任一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.address == "") {
					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,
					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,
					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.address == "") {
					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,
					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>
