<template>
	<el-dialog :title="titleMap" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-input type="hidden" v-model="form.code"></el-input>
            <el-form-item :label="$t('login.adminOldPwd')" prop="oldPassword">
                <el-input type="password" v-model="form.oldPassword" clearable show-password></el-input>
            </el-form-item>
			<el-form-item :label="$t('login.newAdminPwd')" prop="password">
                <el-input type="password" v-model="form.password" clearable show-password></el-input>
            </el-form-item>
            <el-form-item :label="$t('login.confirmAdminPwd')" prop="password2">
                <el-input type="password" v-model="form.password2" clearable show-password></el-input>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >{{$t('dialogBox.cancelButtonText')}}</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">{{$t('dialogBox.saveButtonText')}}</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				titleMap: this.$t('login.editPassword'),
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					code:"",
					oldPassword: "",
					password: "",
				},
				//验证规则
				rules: {
					code: [
						{required: true, message: this.$t('login.inputAdminCode')}
					],
                    oldPassword: [
						{required: true, message: this.$t('login.inputAdminOldPwd')},
					],
					password: [
						{required: true, message: this.$t('login.inputAdminPwd')},
						{validator: (rule, value, callback) => {
                            let reg= /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,20}$/
                            if(!reg.test(value)){
								callback(new Error(this.$t('login.inputPwdValidate')))
                            }
							else{
                                callback()
                            }
						}, trigger: 'blur'}
					],
					password2: [
						{required: true, message: this.$t('login.againInputPwd')},
						{validator: (rule, value, callback) => {
							if (value !== this.form.password) {
								callback(new Error(this.$t('login.pwdDiff')));
							}else{
								callback();
							}
						}}
					]
				}
			}
		},
		methods: {
			//显示
			open(userCode = ''){
				this.form.code = userCode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
                        var res = await this.$API.user.modifyUserPassword.post(this.form);
						this.isSaveing = false;
						if(res){
							this.visible = false;
							this.$message.success(this.$t('response.operateSuccess'))
						}else{
							this.$alert(res.message, this.$t('dialogBox.confirmWarningTitle'), {type: 'error'})
						}
					}else{
						return false;
					}
				})
			}
		}
	}
</script>

<style>
</style>
