<template>
	<view class="page">
		<view class="content">
			<view class="base-person">
				<view class="item">
					<view class="item-title">姓名</view>
					<input @input="inputBaseName" :value="name" placeholder="请输入您的姓名" />
				</view>
				<view class="item">
					<view class="item-title">联系电话</view>
					<input @input="inputBasePhone" :value="phone" placeholder="请输入常用手机号码" />
				</view>
				<view class="item">
					<view class="item-title">身份证号码</view>
					<input @input="inputBaseIdCard" :value="idcard" placeholder="请输入身份证号码" />
				</view>
			</view>
			<view class="save-btn" @click="doSave">确认提交</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: auto;
		height: 100%;
	}

	.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: 32rpx;
		background: white;
		margin-bottom: 20rpx;
		margin-top: 30rpx;
		width: 100%;

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

			.item-title {
				font-size: 32rpx;
				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: 32rpx;
				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;
	}

	.save-btn {
		color: white;
		background-color: green;
		border-radius: 10rpx;
		text-align: center;
		align-self: center;
		margin: 30rpx 40rpx;
		width: 500rpx;
		padding: 20rpx;
		font-size: 32rpx;
	}
</style>
