<template>
	<el-dialog title="重置用户密码" v-model="visible" draggable :width="500">
		<el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="100px" label-position="right">
			<el-form-item label="新密码" prop="new_pwd">
				<el-input v-model="form.new_pwd" type="password" show-password placeholder="请输入新密码"></el-input>
				<sc-password-strength v-model="form.new_pwd"></sc-password-strength>
				<div class="el-form-item-msg">请输入包含英文、数字的8位以上密码</div>
			</el-form-item>
			<el-form-item label="确认新密码" prop="conf_pwd">
				<el-input v-model="form.conf_pwd" type="password" show-password
						  placeholder="请再次输入新密码"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!=='show'" type="primary" :loading="isSave" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import scPasswordStrength from '@/components/scPasswordStrength'

export default {
	emits: ['success', 'closed'],
	components: {
		scPasswordStrength
	},
	data() {
		return {
			form: {
				old_pwd:"",
				new_pwd: "",
				conf_pwd: "",
				id: ""
			},
			mode: "add",
			show: false,
			isSave: false,
			visible: false,
			rules: {
				new_pwd: [
					{required: true, message: '请输入新密码'}
				],
				conf_pwd: [
					{required: true, message: '请再次输入新密码'},
					{
						validator: (rule, value, callback) => {
							if (value !== this.form.new_pwd) {
								callback(new Error('两次输入密码不一致'));
							} else {
								callback();
							}
						}
					}
				]
			}
		}
	},
	methods: {
		open(mode = 'add', id) {
			this.form.id = id;
			this.mode = mode;
			this.visible = true;
			this.show = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					const reqData = {
						id: this.form.id,
						new_pwd: this.form.conf_pwd
					};
					const res = await this.$API.user.resetPass.post(reqData);
					if (res.code === 200) {
						this.visible = false;
						this.$message.success("密码重置成功")
					}
				} else {
					return false;
				}
			})
		}
	}
}
</script>

<style>
</style>
