<template>
    <el-dialog title="提示" v-model="visible" width="25%">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
            <el-form-item label="原密码" prop="password"><el-input type="password" v-model="dataForm.password"
                    size="medium" clearable /></el-form-item>
            <el-form-item label="新密码" prop="newPassword"><el-input type="password" v-model="dataForm.newPassword"
                    size="medium" clearable /></el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword"><el-input type="password"
                    v-model="dataForm.confirmPassword" size="medium" clearable /></el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button size="medium" @click="visible = false">取消</el-button>
                <el-button type="primary" size="medium" @click="dataFormSubmit">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script>
    export default {
        data() {
            const validateConfirmPassword = (rule, value, callback) => {
                if (value != this.dataForm.newPassword) {
                    callback(new Error('两次输入的密码不一致'));
                } else {
                    callback();
                }
            };

            return {
                visible: false,
                dataForm: {
                    password: '',
                    newPassword: '',
                    confirmPassword: ''
                },
                dataRule: {
                    password: [{
                        required: true,
                        pattern: '^[a-zA-Z0-9]{6,20}$',
                        message: '密码格式错误'
                    }],
                    newPassword: [{
                        required: true,
                        pattern: '^[a-zA-Z0-9]{6,20}$',
                        message: '密码格式错误'
                    }],
                    confirmPassword: [{
                        required: true,
                        pattern: '^[a-zA-Z0-9]{6,20}$',
                        message: '密码格式错误'
                    }, {
                        validator: validateConfirmPassword,
                        trigger: 'blur'
                    }]
                }
            };
        },
        methods: {
            reset: function() {
                let dataForm = {
                    password: null,
                    newPassword: null,
                    confirmPassword: null
                };
                this.dataForm = dataForm;
            },
            init: function() {
                let that = this;
                that.reset();
                that.visible = true;
                that.$nextTick(() => {
                    that.$refs['dataForm'].resetFields();
                });
            },
            dataFormSubmit: function() {
                let that = this;
                this.$refs['dataForm'].validate(valid => {
                    if (valid) {
                        let json = {
                            "password": that.dataForm.password,
                            "newPassword": that.dataForm.newPassword
                        };
                        that.$http(
                            '/mis_user/updatePassword/', 'POST', json, true,
                            function(resp) {
                                let result = resp.result;
                                let type = "error";
                                if (result == "密码更新成功") {
                                    type = "success";
                                }
                                ElMessage({
                                    message: result,
                                    type: type
                                });
                                if (result == "密码更新成功") {
                                    that.visible = false;
                                    that.$emit('refreshDataList');
                                    type = "success";
                                }
                                
                            }
                        );
                    }
                });
            }
        }
    };
</script>

<style></style>