<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="formData" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-position="top" label-width="120px">
            <el-form-item label="登录账号">
				<el-input v-model="formData.username" disabled></el-input>
			</el-form-item>
            <el-form-item label="账号昵称">
				<el-input v-model="formData.truename" disabled></el-input>
			</el-form-item>
            <el-form-item label="新密码" prop="password">
                <el-input type="password" v-model="formData.password" clearable show-password></el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="password2">
                <el-input type="password" v-model="formData.password2" clearable show-password></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="isSaveing" @click="submit()">保 存</el-button>
		</template>
    </el-dialog>
</template>

<script>
import scPasswordStrength from '@/components/scPasswordStrength'
export default{
    components: {
        scPasswordStrength
    },
    data(){
        return {
            loading: false,
            mode:"edit",
            titleMap:{
                edit:"修改密码"
            },
            visible: false,
            isSaveing: false,
            formData:{},
            rules:{
                password: [
                    {required: true, message: '请输入登录密码'}
                ],
                password2: [
                    {required: true, message: '请再次输入密码'},
                    {validator: (rule, value, callback) => {
                        if (value !== this.formData.password) {
                            callback(new Error('两次输入密码不一致!'));
                        }else{
                            callback();
                        }
                    }}
                ],
            }
        }
    },
    methods:{
        open(mode = 'edit'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        setData(data){
            data.password = "";
            data.password2 = "";
            this.formData = data;
        },
        submit(){
            this.isSaveing = true;
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    let password = this.$TOOL.crypto.MD5(this.formData.password);
                    var result = await this.$API.user.pwd.post({password:password,id:this.formData.id});
                    if(result.code !== 1){
                        this.$message.warning(result.msg)
                        this.isSaveing = false;
                        return false;
                    }
                    this.$message.success(result.msg)
                    this.isSaveing = false;
                    this.visible = false;
                    this.formData = {};
                }else{
                    this.isSaveing = false;
                    return false;
                }
            });
        }
    }
}
</script>

<style lang="less" scoped>

</style>