<template>
	<view class="page">
		<view class="content">
			<view class="base-person">
				<view class="item">
					<view class="item-title">原密码</view>
					<input @input="inputOrgPwd" :value="ypassword" placeholder="请输入原密码" />
				</view>
				<view class="item">
					<view class="item-title">新密码</view>
					<input @input="inputNewPwd" :value="npassword" placeholder="请输入新密码" />
				</view>
				<view class="item">
					<view class="item-title">确认密码</view>
					<input @input="inputConfirmPwd" :value="rnpassword" placeholder="请确认新密码" />
				</view>
			</view>
			<view class="save-btn" @click="changePassword">确认提交</view>
		</view>
		<pageLoading v-if="showPageLoading"></pageLoading>
	</view>
</template>

<script>
	import pageLoading from '@/components/loading/pageLoading.vue';
	import md5 from "@/common/md5";
	export default {
		components: {
			pageLoading
		},
		data() {
			return {
				token: '',
				ypassword: '',
				npassword: '',
				rnpassword: '',
				showPageLoading: true,
			};
		},
		methods: {
			inputOrgPwd(event) {
				this.ypassword = event.target.value
			},
			inputNewPwd(event) {
				this.npassword = event.target.value
			},
			inputConfirmPwd(event) {
				this.rnpassword = event.target.value
			},
			changePassword() {
				var _self = this
				if (_self.checkParams()) {
					uni.showLoading({
						title: '修改中...',
					})
					_self.$app.request({
						url: _self.$api.user.doChangePwd,
						method: 'PUT',
						data: {
							'newPassword': md5(md5(md5(_self.npassword))),
							'oldPassword': md5(md5(md5(_self.ypassword)))
						},
						success: (res) => {
							uni.hideLoading()
							uni.showToast({
								icon: "success",
								title: '修改成功',
								success() {
									setTimeout(function() {
										let pages = getCurrentPages(); // 当前页面
										let beforePage = pages[pages.length - 2]; // 前一个页面
										beforePage.$vm.loginOut = true;
										uni.navigateBack()
									}, 1000);
								}
							})

						},
						fail(error) {
							uni.hideLoading()
							uni.showToast({
								title: '修改失败',
								duration: 1000
							})
						}
					})
				}
			},
			checkParams() {
				var _self = this;
				if (_self.ypassword == '') {
					uni.showToast({
						title: '请输入原密码',
						duration: 1000
					})
					return false;
				}
				if (_self.npassword == '') {
					uni.showToast({
						title: '请输入新密码',
						duration: 1000
					})
					return false;
				}
				if (_self.rnpassword == '') {
					uni.showToast({
						title: '请输入新密码',
						duration: 1000
					})
					return false;
				}
				if (_self.npassword != _self.rnpassword) {
					uni.showToast({
						title: '新密码不相同',
						duration: 1000
					})
					return false;
				}
				return true;
			}
		},
		onLoad() {
			var _self = this;
			_self.token = getApp().globalData.token

		},
		onUnload() {},
		onShow() {
			var _self = this;
			uni.getStorage({
				key: 'token',
				success: res => {
					_self.token = res.data;
				}
			});
			_self.showPageLoading = false
		},
	};
</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>
