<template>
    <el-dialog :title="$t(`user.resetPassword`)" :close-on-click-modal="false"
        :close-on-press-escape="false" :visible.sync="visible" lock-scroll
        class="JNPF-dialog JNPF-dialog_center" width="600px">
        <el-form ref="dataForm" v-loading="formLoading" :model="dataForm" :rules="dataRule"
            label-width="100px">
            <el-form-item label="账户" prop="account">
                <el-input v-model="dataForm.account" placeholder="账户" readonly />
            </el-form-item>
            <el-form-item label="新密码" prop="userPassword">
                <el-input v-model="dataForm.userPassword" type="password" show-password
                    autocomplete="off" placeholder="输入新密码" />
            </el-form-item>
            <el-form-item label="确认新密码" prop="validatePassword">
                <el-input v-model="dataForm.validatePassword" type="password" show-password
                    autocomplete="off" placeholder="确认新密码" />
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">{{$t('common.cancelButton')}}</el-button>
            <el-button type="primary" :loading="btnLoading" @click="dataFormSubmit()">
                {{$t('common.confirmButton')}}</el-button>
        </span>
    </el-dialog>
</template>

<script>
import {
    resetUserPassword
} from '@/api/permission/user'
import md5 from 'js-md5'
import { getSystemConfig } from '@/api/system/sysConfig'

export default {
    data() {
        var validatePass = (rule, value, callback) => {
            // const passwordreg = /(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{6,16}/
            //是否包含数字
            const containsNumbers = /[0-9]+/
            //是否包含小写字符
            const includeLowercaseLetters = /[a-z]+/
            //是否包含大写字符
            const includeUppercaseLetters = /[A-Z]+/
            //是否包含字符
            const containsCharacters = /\W/
            const containsCharacters2 = /_/

            if (value === '') {
                callback(new Error('新密码不能为空'));
            } else if (this.baseForm.passwordStrengthLimit == 1) {
                if (this.baseForm.passwordLengthMin) {
                    if (value.length < this.baseForm.passwordLengthMinNumber) {
                        callback(new Error('新密码长度不能小于' + this.baseForm.passwordLengthMinNumber + '位'));
                    }
                }
                if (this.baseForm.containsNumbers) {
                    if (!containsNumbers.test(value)) {
                        callback(new Error('新密码必须包含数字'));
                    }
                }
                if (this.baseForm.includeLowercaseLetters) {
                    if (!includeLowercaseLetters.test(value)) {
                        callback(new Error('新密码必须包含小写字母'));
                    }
                }
                if (this.baseForm.includeUppercaseLetters) {
                    if (!includeUppercaseLetters.test(value)) {
                        callback(new Error('新密码必须包含大写字母'));
                    }
                }
                if (this.baseForm.containsCharacters) {
                    if (!containsCharacters.test(value) && !containsCharacters2.test(value)) {
                        callback(new Error('新密码必须包含字符'));
                    }
                }
                if (this.dataForm.validatePassword !== '') {
                    this.$refs.dataForm.validateField('validatePassword');
                }
                callback();
            } else {
                if (this.dataForm.validatePassword !== '') {
                    this.$refs.dataForm.validateField('validatePassword');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value !== this.dataForm.userPassword) {
                callback(new Error('两次密码输入不一致'));
            } else {
                callback();
            }
        };
        return {
            visible: false,
            formLoading: false,
            btnLoading: false,
            dataForm: {
                id: '',
                account: '',
                userPassword: '',
                validatePassword: ''
            },
            dataRule: {
                userPassword: [
                    { required: true, validator: validatePass, trigger: 'blur' }
                ],
                validatePassword: [
                    { validator: validatePass2, trigger: 'blur' }
                ]
            },
            baseForm: {
                passwordStrengthLimit: 0,
                passwordLengthMin: false,
                passwordLengthMinNumber: 0,
                containsNumbers: false,
                includeLowercaseLetters: false,
                includeUppercaseLetters: false,
                containsCharacters: false,
            },
        }
    },
    methods: {

        init(id, account) {
            this.visible = true
            this.formLoading = true
            this.$nextTick(() => {
                this.$refs['dataForm'].resetFields()
                this.dataForm.id = id
                this.dataForm.account = account

                this.$nextTick(() => {
                    getSystemConfig().then(res => {
                        this.baseForm = res.data
                        this.baseForm.passwordLengthMin = this.baseForm.passwordLengthMin ? true : false
                        this.baseForm.containsNumbers = this.baseForm.containsNumbers ? true : false
                        this.baseForm.includeLowercaseLetters = this.baseForm.includeLowercaseLetters ? true : false
                        this.baseForm.includeUppercaseLetters = this.baseForm.includeUppercaseLetters ? true : false
                        this.baseForm.containsCharacters = this.baseForm.containsCharacters ? true : false
                        this.formLoading = false
                    }).catch(() => {
                        this.formLoading = false
                    })
                })
            })
        },
        dataFormSubmit() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    this.btnLoading = true
                    const formData = {
                        id: this.dataForm.id,
                        userPassword: md5(this.dataForm.userPassword),
                        validatePassword: md5(this.dataForm.validatePassword)
                    }
                    resetUserPassword(formData).then(res => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.visible = false
                                this.btnLoading = false
                                this.$emit('refreshDataList')
                            }
                        })
                    }).catch(() => {
                        this.btnLoading = false
                    })
                }
            })
        }
    }
}
</script>
