<template>
	<view>
		<view class="infoItem">
			<view class="tip">
				<u-text text="姓名："></u-text>
			</view>
			<u-input placeholder="请输入姓名" border="surround" v-model="name" clearable></u-input>
		</view>
		<view class="infoItem">
			<view class="tip">
				<u-text text="身份证："></u-text>
			</view>
			<u-input placeholder="请输入身份证号" border="surround" v-model="idNumber" type="idcard" clearable></u-input>
		</view>
		<view class="btn">
			<u-button text="确定" color="#7896F5" @click="updateNameAndIdNumber"></u-button>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				idNumber: '',
				name: '',
			};
		},
		computed: {
			...mapState('m_user', ['token', 'userInfo'])
		},
		methods: {
			...mapMutations('m_user', ['updateUserInfo']),
			async updateNameAndIdNumber() {
				if (this.idNumber.length < 18) {
					uni.$showMsg('身份证格式错误！')
					return;
				}
				if (this.name.length < 2) {
					uni.$showMsg('姓名格式错误！')
					return;
				}
				let res = await uni.$http.put('/userInfo/' + this.userInfo.id, {
					idNumber: this.idNumber,
					name: this.name
				});

				if (res.data.code === 200 && res.data.success === true) {
					uni.$showMsg('修改成功！');
					let info = this.userInfo;
					info.idNumber = this.idNumber;
					info.name = this.name;
					this.updateUserInfo(info);
					var pages = getCurrentPages(); // 当前页面
					var beforePage = pages[pages.length - 2]; // 前一个页面
					if (beforePage.route == "subpkg/myAccount/myAccount") {
						setTimeout(() => {
							uni.navigateBack();
						}, 1000);
					} else {
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/home/home'
							});
						}, 1000);
					}

				} else {
					uni.$showMsg('修改失败：' + value.data.msg);
				}
			}
		},
		onLoad() {
			var pages = getCurrentPages(); // 当前页面
			var beforePage = pages[pages.length - 2]; // 前一个页面
			console.log(beforePage)
			this.name = this.userInfo.name
			this.idNumber = this.userInfo.idNumber
		}
	}
</script>

<style lang="scss">
	.top-tip {
		padding-left: 20rpx;
		font-size: 12px;
		color: darkgray
	}

	.infoItem {
		height: 100rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 0 40rpx;
		border-bottom: black 1px solid;
	}

	.get-code-text,
	.tip {
		margin: 0 20rpx;
		width: 120rpx;
	}

	.u-input {
		width: 70vw;
	}

	.btn {
		margin: 40rpx auto;
		width: 80vw;
		color: #FFFFFF;
	}

	::v-deep .is-disabled {
		color: black !important;
	}
</style>