<template>
	<view class="page">
		<view class="content">
			<view class="base-person">
				<view class="thick-divider"></view>
				<view class="item-box">
					<view class="item-title">姓名</view>
					<input @input="inputBaseName" :value="name" placeholder="请输入您的姓名" class="item-content"
						placeholder-class="item-input" />
				</view>
				<view style="width: 90%;background: #f2f2f2;height: 1rpx;align-self: center;"></view>
				<view class="item-box">
					<view class="item-title">联系电话</view>
					<input @input="inputBasePhone" :value="phone" placeholder="请输入常用手机号码" class="item-content"
						placeholder-class="item-input" />
				</view>
				<view style="width: 90%;background: #f2f2f2;height: 1rpx;align-self: center;"></view>
				<view class="item-box">
					<view class="item-title">身份证号码</view>
					<input @input="inputBaseIdCard" :value="idcard" placeholder="请输入身份证号码" class="item-content"
						placeholder-class="item-input" />
				</view>
			</view>
			<view class="btn-box">
				<view class="save-btn" @click="doSave">确认提交</view>
			</view>
		</view>
		<pageLoading v-if="showPageLoading"></pageLoading>
	</view>
</template>

<script>
	import pageLoading from '@/components/loading/pageLoading.vue';
	export default {
		components: {
			pageLoading
		},
		data() {
			return {
				token: '',
				name: "",
				phone: "",
				idcard: "",
				showPageLoading: true,
				userInfo: {}
			};
		},
		methods: {
			inputBaseName(event) {
				this.name = event.target.value
			},
			inputBasePhone(event) {
				this.phone = event.target.value
			},
			inputBaseIdCard(event) {
				this.idcard = event.target.value
			},
			doSave() {
				var _self = this;
				if (this.checkParams()) {
					uni.showLoading({
						title: "保存中..."
					})
					_self.$app.request({
						url: _self.$api.user.doUpdateUserInfo,
						method: 'PUT',
						header: {
							token: getApp().globalData.token
						},
						data: {
							"avatar": _self.userInfo.avatar,
							"email": _self.userInfo.email,
							"name": _self.name,
							"phone": _self.phone
						},
						success: res => {
							uni.hideLoading()
							uni.showToast({
								icon: "success",
								title: "保存成功"
							})
							let pages = getCurrentPages(); // 当前页面
							let beforePage = pages[pages.length - 2]; // 前一个页面
							beforePage.$vm.isRefresh = true;
							uni.navigateBack()
						},
						fail: res => {
							uni.hideLoading()
							uni.showToast({
								icon: "error",
								title: "系统错误",
								duration: 2000
							})
						},
						complete: res => {
							uni.hideLoading()
						}
					})
				}
			},
			checkParams() {
				var _self = this;
				if (_self.name == '') {
					uni.showToast({
						title: '请输入姓名'
					})
					return false;
				}
				if (_self.phone == '') {
					uni.showToast({
						title: '请输入电话'
					})
					return false;
				}
				if (_self.email == '') {
					uni.showToast({
						title: '请输入邮箱'
					})
					return false;
				}
				return true;
			},
			getUserInfo() {
				var _self = this;
				_self.$app.request({
					url: _self.$api.user.getUserInfo,
					method: 'GET',
					header: {
						token: getApp().globalData.token
					},
					success: res => {
						if (res) {
							getApp().globalData.userInfo = res;
							_self.userInfo = res;
							_self.name = _self.userInfo.name;
							_self.phone = _self.userInfo.phone;
						}
					},
					complete: res => {
						_self.showPageLoading = false;
					}
				})
			}
		},
		onLoad() {
			var _self = this;
			_self.token = getApp().globalData.token;
			_self.getUserInfo()
		},
		onUnload() {},
		onShow() {
			var _self = this;
			uni.getStorage({
				key: 'token',
				success: res => {
					_self.token = res.data;
				}
			});
		},
	};
</script>

<style lang="scss">
	page {
		height: 100%;

	}

	.page {
		display: flex;
		flex: 1;
		flex-direction: column;
		overflow: hidden;
		height: 100%;
		background: white;
	}

	.content {
		flex: 1;
		width: 100%;
		height: 100rpx;
		display: flex;
		flex-direction: column;
	}

	.base-person {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: flex-start;
		font-size: 20rpx;
		margin-bottom: 20rpx;
		width: 100%;

		.thick-divider {
			height: 5rpx;
			background: #f2f2f2;
			width: 100%;
		}

		.hint-txt {
			padding: 10rpx 40rpx;
			color: darkred;
			font-size: 20rpx;
			background: #f2f2f2;
		}

		.item-box-v {
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			margin-top: 10rpx;
			background: white;
			align-items: flex-start;
			width: 100%;
			padding-bottom: 50rpx;

			.item-title {
				font-size: 28rpx;
				padding: 10rpx 30rpx;
			}

			.item-content {
				display: flex;
				flex-direction: row;
				justify-content: flex-end;
				align-items: center;
				padding: 10rpx 30rpx;
				font-size: 28rpx;

				input {
					font-size: 28rpx;
				}
			}

			.item-content-bg {
				display: flex;
				flex-direction: column;
				justify-content: flex-start;
				align-items: center;
				padding: 20rpx;
				width: 85%;
				background: #f3f3f3;
				border-radius: 10rpx;
				align-self: center;
				height: 220rpx;

				textarea {
					font-size: 28rpx;
					text-align: left;
					width: 100%;
				}

				.hint-num {
					align-self: flex-end;
				}
			}

		}

		.item-box {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			margin-top: 10rpx;
			background: white;
			align-items: center;
			width: 100%;

			.item-input {
				font-size: 28rpx;
			}

			.item-title {
				font-size: 28rpx;
				flex: 0.5;
				padding: 15rpx 30rpx;
			}

			.item-content {
				display: flex;
				flex-direction: row;
				justify-content: flex-end;
				align-items: center;
				flex: 1.5;
				padding: 15rpx 30rpx;
			}
		}

		.item {
			display: flex;
			flex-direction: row;
			margin: 20rpx;
			justify-content: flex-start;
			align-items: center;
			width: 95%;

			.item-title {
				font-size: 25rpx;
				width: 100%;
				background: #e4f3f2;
				padding: 10rpx;
			}

			.item-title-box {
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
				background: #f2f2f2;
				padding: 10rpx;
				width: 100%;

				.item-title-name {
					font-size: 20rpx;
				}

				.item-title-del {
					color: darkgreen;
				}

			}

			input {
				margin-top: 20rpx;
				padding: 10rpx;
				width: 95%;
				font-size: 25rpx;
				border: 1rpx #d3d3d3 solid;
			}
		}
	}

	.add-person {
		color: green;
		border: 1rpx #c28127 solid;
		border-radius: 10rpx;
		text-align: center;
		align-self: flex-start;
		margin-top: 10rpx;
		border-radius: 10rpx;
		width: 25%;
		padding: 10rpx;
	}

	.btn-box {
		width: 100%;
		align-items: center;
		display: flex;
		flex-direction: row;
		justify-content: center;
		background: white;
		bottom: 0;
		position: fixed;

		.save-btn {
			color: white;
			background-color: green;
			width: 95%;
			border-radius: 10rpx;
			text-align: center;
			align-self: center;
			margin-top: 5rpx;
			border-radius: 10rpx;
			padding: 10rpx;
		}
	}


	.type-change-box {
		margin-top: 20rpx;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: flex-start;
	}
</style>
