<template>
	<el-dialog :title="titleMap[mode]" 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 v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">{{$t('dialogBox.saveButtonText')}}</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: this.$t('login.addPassword'),
					edit: 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()
                            }
							// if (this.form.password2 !== '') {
							// 	this.$refs.dialogForm.validateField('password2');
							// }
							// 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();
							}
						}}
					]
				}
			}
		},
		// mounted() {
		// 	this.getGroup()
		// },
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
                        var res = await this.$API.authentication.modifyPwdProcess.post(this.form);
						//var res = await this.$API.demo.post.post(this.form);
						this.isSaveing = false;
						if(res){
							//this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success(this.$t('response.operateSuccess'))
						}else{
							this.$alert(res.message, this.$t('dialogBox.confirmWarningTitle'), {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.code = data
				// this.form.userName = data.userName
				// this.form.avatar = data.avatar
				// this.form.name = data.name

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
