<template>
  <div>
    <el-dialog
      title="重置密码"
      :visible.sync="visible"
      width="500px"
      :before-close="close"
      :append-to-body="true"
      :close-on-click-modal="false">

      <el-form :model="form" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item label="账号" prop="personnelUserName">
          <el-input v-model="form.personnelUserName" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="personnelEmail">
          <el-input v-model="form.personnelEmail" placeholder="请输入账号绑定的邮箱"></el-input>
        </el-form-item>
      </el-form>
      
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="sure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    const errorPersonnelEmail = (rule, value, callback) => {
      if (rule.errorMessage) {
        callback(new Error(rule.errorMessage))
      } else {
        if (!value) {
          callback(new Error('请输入账号绑定的邮箱'))
        } else {
          let reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9_\.\-])+\.)+([a-zA-Z0-9]{2,4})+$/
          let re = new RegExp(reg)
          if (re.test(value)) {
            callback()
          } else {
            callback(new Error('请输入正确的邮箱格式'))
          }
        }
      }
    }
    const errorPersonnelUserName = (rule, value, callback) => {
      if (rule.errorMessage) {
        callback(new Error(rule.errorMessage))
      } else {
        if (!value) {
          callback(new Error('请输入账号'))
        } else {
          callback()
        }
      }
    }
    return {
      form: {},
      visible: false,
      rules: {
        personnelUserName: [{ required: true,  validator:errorPersonnelUserName, trigger: 'blur' },],
        personnelEmail: [
          { required: true, validator:errorPersonnelEmail, trigger: "blur" },
        ]
      },
    }
  },
  mounted() {

  },
  methods: {
    
    open() {
      this.visible = true
    },
    sure() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.$api.reSetPasswordBeforeLogin(this.form).then(res => {
            if (res.code == 200) {
              this.$message.success('密码重置成功，请您到邮箱中查看新密码')
              this.close()
            }
          }).catch(error => {
            if (error.message.indexOf('账号') != -1) {
              var personnel = 'personnelUserName'
            }else if (error.message.indexOf('邮箱')!=-1) {
              var personnel = 'personnelEmail'
            }
            this.rules[personnel][0].errorMessage = error.message
            this.$refs.ruleForm.validateField(personnel)
            this.rules[personnel][0].errorMessage = ''
          })
        } else {
          return false;
        }
      });
    },
    close() {
      this.visible = false
      this.$refs.ruleForm.resetFields()
    },
  },
}
</script>

<style lang="scss" scoped>

</style>