<template>
	<view>
		<headers :showBack="true">
			<view class="headerName">
				修改密码
			</view>
		</headers>
		<view  class="userLogin">
			<u-form style="width: 100%;" :rules="rules" :error-type="['message']" :model="formData" ref="uForm">
			<u-form-item prop="account" class="uni-form-item" :border-bottom="false">
				<view>当前账号</view>
				<input class="uni-input" name="userInfo" readonly :disabled="true" v-model="userInfo.realName"
					placeholder="请输入您的账号" />
			</u-form-item>
			<u-form-item prop="pw" class="uni-form-item" :border-bottom="false">
				<view>当前密码</view>
				<input class="uni-input" name="pw" v-model="formData.pw" :password="showPassword"
					placeholder="请输入您的密码" />
				<!-- <u-icon :name="showPassword ? 'eye-off' : 'eye-fill'" @click="showPassword = !showPassword"></u-icon> -->
			</u-form-item>
			<u-form-item prop="newpw" class="uni-form-item" :border-bottom="false">
				<view>新密码</view>
				<input class="uni-input" name="newpw" :password="showPassword" v-model="formData.newpw"
					placeholder="请输入新密码" />
			</u-form-item>
			<u-form-item prop="newpwaff" class="uni-form-item" :border-bottom="false">
				<view>确认新密码</view>
				<input class="uni-input" name="newpwaff" :password="showPassword" v-model="formData.newpwaff"
					placeholder="请输入再次新密码" />
			</u-form-item>
			<!-- 	<u-form-item prop="email" class="uni-form-item" :border-bottom="false">
				<image class="icon" mode="widthFix" src="@/static/login/email.png"></image>
				<input class="uni-input" type="email" name="email" v-model="formData.email" placeholder="请输入您的电子邮箱" />
			</u-form-item> -->
			<view class="uni-form-item submit">
				<button type="primary" class="btn" @click="formSubmit">完成</button>
			</view>
		</u-form>
		</view>
	</view>
</template>

<script>
	import headers from '../../../components/headers/headers.vue';
	import { exitApp } from "@/utils/tool.js"
	
	export default {
		components: {
			headers
		},
		data() {
			return {
				showPassword: true,
				userInfo:{},
				formData: {
					account: '',
					pw: '',
					newpw: "",
					newpwaff: "",
					email: ''
				},
				rules: {
					pw: [{
							required: true,
							message: '请输入密码',
							trigger: ['change', 'blur'],
						},
						{
							pattern: /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+~`\-={}[\]:";'<>?,./])[a-zA-Z\d!@#$%^&*()_+~`\-={}[\]:";'<>?,./]{8,}$/,
							// 正则检验前先将值转为字符串
							transform(value) {
								return String(value);
							},
							message: '密码必须包含至少一个字母、一个数字、一个特殊字符以及长度至少为8位数'
						}
					],
					newpw: [{
							required: true,
							message: '请输入密码',
							trigger: ['change', 'blur'],
						},
						{
							pattern: /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+~`\-={}[\]:";'<>?,./])[a-zA-Z\d!@#$%^&*()_+~`\-={}[\]:";'<>?,./]{8,}$/,
							// 正则检验前先将值转为字符串
							transform: (value) => {
								return String(value);
							},
							message: '密码必须包含至少一个字母、一个数字、一个特殊字符以及长度至少为8位数'
						}
					],
					newpwaff: [{
						required: true,
						message: '请输入密码',
						trigger: ['change', 'blur'],
					}, {
							pattern: /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+~`\-={}[\]:";'<>?,./])[a-zA-Z\d!@#$%^&*()_+~`\-={}[\]:";'<>?,./]{8,}$/,
							// 正则检验前先将值转为字符串
							transform: (value) => {
								// console.log(value,this.formData);
								if(value != this.formData.newpw){
									return false
								}
								return String(value);
							},
							message: '两次密码不一致'
						}],
					email: [{
							required: true,
							message: '请输入邮箱',
							trigger: ['change', 'blur'],
						},
						{
							type: "email",
							required: true,
							message: '邮箱格式错误',
							trigger: ['change', 'blur'],
						}
					],
				}
			};
		},
		onLoad() {
			this.userInfo = JSON.parse(uni.getStorageSync('userInfo'));
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			formSubmit() {
				// 表单验证 -> 下一步
				this.$refs.uForm.validate(valid => {
					if (valid) {
						// 修改密码
						this.sendRequest({
							url: "xmgl/systemUser/xz/updatePw",
							data: {
								userId:this.userInfo.userId,
								oldPassword:this.formData.pw,
								showPassword:this.formData.newpwaff,
								isCheckCode:0
							},
							method: "POST",
							success: (res) => {
								console.log(res);
								if (res.code == 200) {
									uni.showToast({
										title:'修改成功！'
									})
									this.logout();
								}
							}
						})
					}
				});
			},
			logout() {
				this.sendRequest({
					url: "xmgl/base/logout",
					data: {
						id: this.userInfo.userId
					},
					method: "POST",
					success() {
						exitApp()
					},
					fail() {
						exitApp()
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.userLogin {
		padding: 30rpx;

		.uni-form-item {
			display: flex;
			padding: 20rpx 10rpx;
			width: 100%;
			border-bottom: 1rpx solid #ddd;

			&.submit {
				border: none;
				margin-top: 40rpx;
			}

			.icon {
				width: 40rpx;
				height: auto;
				margin: auto 0;
			}

			.uni-input {
				width: 70%;
				padding: 15rpx;
			}

			.btn {
				width: 100%;
			}
		}
	}
</style>