<template>
  <el-dialog :visible.sync="dialogFormVisible" :close-on-click-modal="close" :close-on-press-escape="close" :show-close="close" title="修改密码" width="450px" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="userForm" label-position="right" label-width="80px">
      <el-form-item label="原密码" prop="oldpassword">
        <el-input v-model.trim="userForm.oldpassword" type="password" auto-complete="off" placeholder="必填"/>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model.trim="userForm.password" type="password" auto-complete="off" placeholder="必填"/>
      </el-form-item>
      <el-form-item label="重复密码" prop="repassword">
        <el-input v-model.trim="userForm.repassword" type="password" auto-complete="off" placeholder="必填"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">保存</el-button>
      <el-button v-show="close" @click="dialogFormVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import SelectTree from '@/components/SelectTree/singleSelect'
import { changePwd } from '../../../api/user'
import { RSAencrypt } from '../../../utils/security'

export default {
  name: 'ResetPwd',
  components: { SelectTree },
  props: {
    close: {
      type: Boolean,
      default: true
    } // 是否显示关闭按钮
  },
  data() {
    const validateOldPass = (rule, value, callback) => {
      console.log('验证旧密码')
      if (value === '') {
        callback(new Error('旧密码不能为空'))
      }
      callback()
    }
    const validatePass = (rule, value, callback) => {
      console.log('验证密码')
      if (value === '') {
        callback(new Error('密码不能为空'))
      } else {
        // var reg = /^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$/
        var reg = /^(?![a-zA-Z]+$)(?![a-z\d]+$)(?![a-z!@#\$%^&_=]+$)(?![A-Z\d]+$)(?![A-Z!@#\$%^&_=]+$)(?![\d!@#\$%]+$)[a-zA-Z\d!@#\$%^&_=]+$/
        if (value.match(reg)) { // 样式匹配
          if (value.length < 12 || value.length > 30) {
            callback(new Error('密码长度至少12位'))
          } else {
            if (this.userForm.repassword !== '') {
              this.$refs.dataForm.validateField('repassword')
            }
          }
        } else {
          callback(new Error('须包含数字、大小写字母、特殊字符中至少三种'))
        }
        callback()
      }
    }
    const validateRepass = (rule, value, callback) => {
      console.log('验证重复密码')
      if (value === '') {
        callback(new Error('重复密码不能为空'))
      } else if (value !== this.userForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      userForm: {
        oldpassword: '', // 原密码
        password: '', // 密码
        repassword: '' // 重复密码
      }, // 表单
      rules: {
        oldpassword: [{ required: true, validator: validateOldPass, trigger: ['blur', 'change'] }],
        password: [{ required: true, validator: validatePass, trigger: ['blur'] }],
        repassword: [{ required: true, validator: validateRepass, trigger: ['blur'] }]
      } // 前端校验规则
    }
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogFormVisible) {
      this.dialogFormVisible = dialogFormVisible
      this.resetForm()
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 重置表单
    resetForm() {
      this.userForm = {
        oldpassword: '',
        password: '', // 密码
        repassword: '' // 重复密码
      }
    },
    // 保存数据
    saveData: function() {
      this.$refs['dataForm'].validate((valid) => {
        const userForm = {
          oldPwd: this.userForm.oldpassword,
          newPwd: this.userForm.password
        }
        userForm.oldPwd = RSAencrypt(userForm.oldPwd)
        userForm.newPwd = RSAencrypt(userForm.newPwd)
        if (valid) {
          changePwd(userForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改密码成功')
              this.dialogFormVisible = false
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

  .el-dialog{
    width:400px
  }
  .el-select{
    width: 100%;
  }
</style>
