<template>
  <a-modal
    :title="title"
    :width="modalWidth"
    :visible="visible"
    :confirm-loading="confirmLoading"
    cancel-text="关闭"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="旧密码"
        >
          <a-input
            v-decorator="[ 'oldPassword', validatorRules.oldPassword]"
            type="password"
            placeholder="请输入旧密码"
          />
        </a-form-item>

        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="新密码"
        >
          <a-input
            v-decorator="[ 'newPassword', validatorRules.newPassword]"
            type="password"
            placeholder="请输入新密码"
          />
        </a-form-item>

        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="确认新密码"
        >
          <a-input
            v-decorator="[ 'reNewPassword', validatorRules.reNewPassword]"
            type="password"
            placeholder="请确认新密码"
          />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { putAction } from '@/api/request'
import { Base64 } from 'js-base64'

export default {
  name: 'UserPassword',
  data() {
    return {
      title: '修改密码',
      modalWidth: 800,
      visible: false,
      confirmLoading: false,
      validatorRules: {
        oldPassword: {
          rules: [{
            required: true, message: '请输入旧密码!'
          }]
        },
        newPassword: {
          rules: [{
            required: true, message: '请输入新密码!'
          }, {
            validator: this.validateToNextPassword
          }]
        },
        reNewPassword: {
          rules: [{
            required: true, message: '请确认新密码!'
          }, {
            validator: this.compareToFirstPassword
          }]
        }
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      form: this.$form.createForm(this),
      url: 'system/user/profile/updatePwd',
      username: ''
    }
  },
  methods: {
    show(uname) {
      if (!uname) {
        this.$message.warning('当前系统无登录用户!')
        return
      } else {
        this.username = uname
        this.form.resetFields()
        this.visible = true
      }
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.disableSubmit = false
      this.selectedRole = []
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          // { userName: this.username },
          const params = Object.assign(values)
          // params.oldPassword = encodeURIComponent(params.oldPassword)
          // params.newPassword = encodeURIComponent(params.newPassword).
          // params.reNewPassword = encodeURIComponent(params.reNewPassword)
          // delete params.reNewPassword
          console.log('修改密码提交数据', params)
          putAction(this.url, params).then((res) => {
            if (res.code === 200) {
              that.$message.success(res.msg)
              that.close()
            } else {
              that.$message.warning(res.msg)
            }
          })
        }
      })
    },
    validateToNextPassword(rule, value, callback) {
      const pattern = /^(?![A-Za-z0-9]+$)(?![a-z0-9\W]+$)(?![A-Za-z\W]+$)(?![A-Z0-9\W]+$)[a-zA-Z0-9\W]{8,}$/
      if (value && !pattern.test(value)) {
        callback('请输入8位以上大小写字母、数字、特殊符号组合')
      } else {
        callback()
      }
    },
    compareToFirstPassword(rule, value, callback) {
      const form = this.form
      if (value && value !== form.getFieldValue('newPassword')) {
        callback('两次输入的密码不一样！')
      } else {
        callback()
      }
    }
  }
}
</script>

<style scoped>

</style>

