<template>
  <a-modal :title="title" :visible="open" @ok="submitForm" @cancel="cancel">
    <a-form :form="form" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }" labelAlign="right">
      <a-form-item label="用户名">
        <a-input
          placeholder="请输入用户名"
          v-decorator="[
            'userName',
            {
              initialValue: (detail && detail.userName) || undefined,
              rules: [{ required: true, message: '请输入用户名' }],
            },
          ]"
          disabled
        />
      </a-form-item>
      <a-form-item has-feedback label="新密码">
        <a-input-password
          placeholder="请输入新密码"
          v-model="newPassword"
          v-decorator="[
            'newPassword',
            {
              initialValue:  undefined,
              rules: [{ required: true, message: '请输入新密码' }],
            },
          ]"
        />
      </a-form-item>
      <a-form-item has-feedback label="确认密码">
        <a-input-password
          placeholder="请确认密码"
          v-decorator="[
            'confirmPassword',
            {
              initialValue: undefined,
              rules: [{ required: true, message: '请确认密码' },{ validator: validateConfirmPass }],
            },
          ]"
        />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { resetUserPwd } from '@/api/system/user'
import md5 from 'md5'
import formMixin from '@/utils/form-mixin'
export default {
  name: 'ResetPassword',
  mixins: [formMixin],
  data() {
    return {
      title: '重置密码',
      open: false,
      childrenDrawer: false,
      detail: undefined,
      newPassword:undefined
    }
  },
  methods: {
    // validateNewPass (rule, value, callback){
    //   if (value === '') {
    //     callback(new Error('请输入新密码'))
    //   } else if (!/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$)([^\u4e00-\u9fa5\s]){6,20}$/.test(value)) {
    //     callback(new Error('请输入6-20位英文字母、数字或者符号（除空格），且字母、数字和标点符号至少包含两种'))
    //   } else {
    //     if (this.form.confirmPassword !== '') {
    //       this.form.validateField('confirmPassword')
    //     }
    //     callback()
    //   }
    // },
    validateConfirmPass (rule, value, callback){
    if (value == '' || value == undefined || value == null) {
        callback()
      } else if (value !== this.newPassword) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.detail = undefined
      this.form.resetFields()
    },
    handleResetPwd(row) {
      this.detail = {
        userId: row.userId,
        userName: row.userName,
      }
      this.open = true
    },
    /** 重置密码按钮操作 */
    submitForm: function () {
      this.form.validateFields((errors, values) => {
        if (!errors) {
          resetUserPwd(this.detail.userId, md5(values.newPassword)).then((response) => {
            this.$message.success('重置成功', 3)
            this.cancel()
          })
        } else {
          return false
        }
      })
    },
  },
}
</script>
