<template>
	<div class="container">
		<!-- <view class="title-back">
			<view class="left-father" @click="goBack">
				<image class="back-img" src="https://www.focusnu.com/media/directive/index/left.png" />
				<view style="font-size: 30rpx;">监护人信息</view>
			</view>
		</view> -->
		<view class="title-back" :style="{height:`${uni.getStorageSync('moveHeight')+35}px`}">
			<view class="left-father" @click="goBack">
				<image class="back-img" src="https://www.focusnu.com/media/directive/index/left.png" />
				<view style="font-size: 30rpx;">监护人信息</view>
			</view>
		</view>
		<view :style="{height:`${uni.getStorageSync('moveHeight') + 30}px`}"></view>
		<view class="white-content">
			<view class="content-title">
				<view class="shu"></view>
				<view class="content-weight">监护人基本信息</view>
			</view>
			<view class="one">
				<view class="one-left">与长者关系</view>
				<input class="one-right" maxlength="10" placeholder="请输入与长者关系" v-model="form.relationship" />
			</view>
			<view class="one">
				<view class="one-left">姓名</view>
				<input class="one-right" maxlength="5" placeholder="请输入姓名" v-model="form.name" />
			</view>

			<view class="one">
				<view class="one-left">电话</view>
				<input class="one-right" type="number" maxlength="11" placeholder="请输入电话" v-model="form.tel" />
			</view>
			<view class="one">
				<view class="one-left">身份证号</view>
				<input class="one-right" type="number" maxlength="18" placeholder="请输入身份证号" v-model="form.idCard" />
			</view>
			<view class="one">
				<view class="one-left">家庭住址</view>
				<input class="one-right" maxlength="30" placeholder="请输入家庭住址" v-model="form.homeAddress" />
			</view>
			<view class="one">
				<view class="one-left">工作单位</view>
				<input class="one-right" maxlength="40" placeholder="请输入工作单位" v-model="form.workUnit" />
			</view>
		</view>
		<view style="display: flex;width: 100%;position: fixed;bottom: 20rpx;left: 0;">
			<view class="finish-button" @click="next">
				提交
			</view>
		</view>
	</div>
</template>

<script setup>
	import {
		ref,
		reactive
	} from 'vue'
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app';
	import {
		base_url
	} from '@/request/index.js';
	import {
		savePayer,
		changePayer
	} from './api/api.js'
	import {
		getMessage
	} from '@/api/loginApi.js'

	const form = reactive({
		name: "",
		tel: "",
		advisoryType: 1,
		status: 2,
		idCard: "",
		homeAddress: "",
		workUnit: "",
		izJs: 1,
		openId: uni.getStorageSync('openid'),
		relationship: ""
	})

	function isValid11DigitNumber(val) {
		return /^(\d{11})$/.test(val);
	}

	function isValid18DigitNumber(val) {
		return /^(\d{18})$/.test(val);
	}
	const ukey = ref(
		`${uni.getStorageSync('openid') || ''}jhbg${String(Date.now()).slice(-5)}${String(Math.floor(Math.random() * 900) + 100)}`
	)
	const next = () => {

		if (!form.relationship) {
			uni.showToast({
				title: '请填写与长者关系',
				icon: 'error'
			})
		} else if (!form.name) {
			uni.showToast({
				title: '请填写姓名',
				icon: 'error'
			})
		} else if (!form.tel) {
			uni.showToast({
				title: '请填写电话',
				icon: 'error'
			})
		} else if (!isValid11DigitNumber(form.tel)) {
			uni.showToast({
				title: '电话格式错误',
				icon: 'none'
			})
			return
		} else if (!form.idCard) {
			uni.showToast({
				title: '请填写身份证号',
				icon: 'error'
			})
		} else if (!isValid18DigitNumber(form.idCard)) {
			uni.showToast({
				title: '身份证号格式错误',
				icon: 'none'
			})
			return
		} else if (!form.homeAddress) {
			uni.showToast({
				title: '请填写家庭住址',
				icon: 'error'
			})
		} else if (!form.workUnit) {
			uni.showToast({
				title: '请填写工作单位',
				icon: 'error'
			})
		} else {
			if (uni.getStorageSync('allinfo').name) {
				let data = uni.getStorageSync('allinfo');
				data.guardianName = form.name
				data.guardianPhone = form.tel
				data.guardianIdCard = form.idCard
				data.guardianHomeAddress = form.homeAddress
				data.guardianWorkUnit = form.workUnit
				data.relationship = form.relationship
				data.modifyType = "jhr"
				const urlpost = `${base_url}/api/elderInfo/updateElderInfo?ukey=${ukey.value}`;
				uni.request({
					url: urlpost,
					method: 'POST',
					header: {
						'Content-Type': 'application/json',
						'X-Access-Token': uni.getStorageSync('token') || '',
					},
					data: data,
					success: (res) => {
						if (res.data.result == '重复提交') {
							uni.showToast({
								title: `重复提交`,
								icon: 'error'
							})
							return
						}
						if (res.data.result == '未做变更') {
							uni.showToast({
								title: `当前无变更，无需提交`,
								icon: 'none'
							})
							return
						}
						if (res.data.success) {
							getMessage(uni.getStorageSync('openid')).then(res => {
								uni.setStorageSync('allinfo', res.result);
								uni.reLaunch({
									url: '/pages/oldmanindex/oldmansuccess?type=1&jianhu=1'
								})
							})
						}
					},
					fail: (e) => {
						console.log("????", e)
					}
				});
			} else {
				let data = {}
				data.guardianName = form.name
				data.guardianPhone = form.tel
				data.guardianIdCard = form.idCard
				data.guardianHomeAddress = form.homeAddress
				data.guardianWorkUnit = form.workUnit
				data.relationship = form.relationship
				const urlpost = `${base_url}/api/elderInfo/addElder?ukey=${ukey.value}`;
				uni.request({
					url: urlpost,
					method: 'POST',
					header: {
						'Content-Type': 'application/json',
						'X-Access-Token': uni.getStorageSync('token') || '',
					},
					data: data,
					success: (res) => {

						if (res.data.result == '重复提交') {
							uni.showToast({
								title: `重复提交`,
								icon: 'error'
							})
							return
						}
						if (res.data.result == '未做变更') {
							uni.showToast({
								title: `当前无变更，无需提交`,
								icon: 'none'
							})
							return
						}
						if (res.data.code != 200) {
							uni.showToast({
								title: res.data.message,
								icon: 'none', // 'success' 成功图标，'none' 无图标
								duration: 2000 // 显示时长 ms
							})
							return
						} else {
							uni.reLaunch({
								url: '/pages/oldmanindex/oldmansuccess?type=1&jianhu=1'
							})
						}

						//  return
						// 		const connectinon = {
						// 			nuId: data.nuId,
						// 			openId: uni.getStorageSync('openid'),
						// 			elderId: r.data.result,
						// 			sysOrgCode: data.sysOrgCode,

						// 		}
						// 		const urlpost = `${payurl.value}/api/nuBaseInfo/bindNu`;
						// 		uni.request({
						// 			url: urlpost,
						// 			method: 'POST',
						// 			header: {
						// 				'Content-Type': 'application/json',
						// 				'X-Access-Token': uni.getStorageSync('token') || '',
						// 			},
						// 			data: connectinon,
						// 			success: (r) => {
						// 				uni.reLaunch({
						// 					url: '/pages/oldmanindex/oldmansuccess'
						// 				})

						// 				// console.log("????", r)
						// 			},
						// 			fail: (e) => {
						// 				// console.log("????", e)
						// 			}
						// 		});

						// console.log("????", r)
					},
					fail: (e) => {
						// console.log("????", e)
					}
				});
				/* savePayer(form).then((res) => {
					// if (res.code == 401) {
					// 	savePayer(form).then((res) => {
					// 		if (res.success) {
					// 			getMessage(uni.getStorageSync('openid')).then(res => {
					// 				uni.setStorageSync('allinfo', res.result);
					// 				uni.reLaunch({
					// 					url: "/pages/login/callback",
					// 				})
					// 			})
					// 		}
					// 	})
					// }
					if (res.success) {
						getMessage(uni.getStorageSync('openid')).then(res => {
							uni.setStorageSync('allinfo', res.result);
							uni.reLaunch({
								url: "/pages/login/callback",
							})
						})
					}
				}) */
			}
		}
	}
	const goBack = () => {
		uni.navigateBack()
	}

	onLoad(() => {
		if (uni.getStorageSync('allinfo').name) {
			let data = uni.getStorageSync('allinfo');
			const keys = [
				"openId",
				"name",
				"tel",
				"advisoryType",
				"status",
				"idCard",
				"homeAddress",
				"workUnit",
				"izJs",
				"relationship"
			]

			keys.forEach(key => {
				form[key] = data[key] || ""
			})
			console.log("????", uni.getStorageSync('allinfo'))
		}

	})
</script>

<style lang="scss" scoped>
	.container {
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		width: 100%;
		background-color: #F7F7F7;
		position: relative;

		.white-content {
			width: 90%;
			margin-left: 5%;
			border-radius: 35rpx;

			.content-title {
				display: flex;
				align-items: center;
				height: 100rpx;
				position: relative;
				font-weight: 600;

				.content-weight {
					font-size: 32rpx;
				}

				.content-img {
					position: absolute;
					right: 0;
					top: 0;
					width: 400rpx;
					height: 100%;
				}
			}
		}
	}


	.finish-button {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 80%;
		height: 90rpx;
		margin: 0rpx auto;
		margin-bottom: 80rpx;
		margin-top: 20rpx;
		background: linear-gradient(to bottom, #e7f4ff, #c5e5ff);
		border: 2rpx solid #9AD1FF;
		color: #007CFF;
		border-radius: 35rpx;
		font-size: 33rpx;
	}

	.title-back {
		background-color: #EFF6F9;
		width: 100%;
		height: 70rpx;
		display: flex;
		justify-content: space-between;
		align-items: flex-end;
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid #cbd1d2;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 9999;
	}

	.left-father {
		display: flex;
		align-items: center;

		.back-img {
			width: 45rpx;
			height: 40rpx;
			margin-left: 40rpx;
			margin-right: 15rpx;
		}
	}

	.shu {
		width: 14rpx;
		height: 36rpx;
		background-color: #0097FF;
		border-radius: 10rpx;
		margin: 3rpx 20rpx 0 30rpx;
	}

	.one {
		width: 90%;
		margin-left: 5%;
		height: 100rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 10rpx;

		.one-left {
			margin-left: 10rpx;
			font-size: 30rpx;
		}

		.one-right {
			font-size: 30rpx;
			height: 100%;
			color: #999999;
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;
			width: 350rpx;
			display: flex;
		}

	}
</style>