<!--
 * @Author: hisense.liangjunhua
 * @Date: 2022-06-23 16:41:46
 * @LastEditors: hisense.liangjunhua
 * @LastEditTime: 2022-06-24 17:51:42
 * @Description: 告诉大家这是什么
-->
<template>
  <el-dialog
    :visible.sync="visible"
    :title="$t('updatePassword.title')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :append-to-body="true">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
      <el-form-item :label="$t('updatePassword.username')">
        <span>{{ $store.state.user.name }}</span>
      </el-form-item>
      <el-form-item prop="password" :label="$t('updatePassword.password')">
        <el-input v-model="dataForm.password" type="password" :placeholder="$t('updatePassword.password')"></el-input>
      </el-form-item>
      <el-form-item prop="newPassword" :label="$t('updatePassword.newPassword')">
        <el-input v-model="dataForm.newPassword" type="password" :placeholder="$t('updatePassword.newPassword')" @input="changeInput"></el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword" :label="$t('updatePassword.confirmPassword')">
        <el-input v-model="dataForm.confirmPassword" type="password" :placeholder="$t('updatePassword.confirmPassword')"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import { Encrypt } from '@/utils/crypto'
import { clearLoginInfo } from '@/utils'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        password: '',
        newPassword: '',
        confirmPassword: ''
      }
    }
  },
  computed: {
    dataRule () {
      var validatePassword = (rule, value, callback) => {
        if (!value) {
          this.showLevel = false
          return callback(new Error(this.$t('validate.pwdStrength')))
        }
        if (value.length < 8) {
          this.showLevel = false
          return callback(new Error(this.$t('validate.pwdStrength')))
        }
        const num = /^.*[0-9]+.*/
        const low = /^.*[a-z]+.*/
        const up = /^.*[A-Z]+.*/
        const spe = /^.*[^a-zA-Z0-9]+.*/
        console.log('包含数字', num.test(value), '包含字母', low.test(value), up.test(value), '包含特殊符号', spe.test(value))
        if (!(num.test(value) && (low.test(value) || up.test(value)) && spe.test(value))) {
          return callback(new Error(this.$t('validate.pwdStrength')))
        }
        // if (!this.dataForm.id && !/\S/.test(value)) {
        //   return callback(new Error(this.$t('validate.required')))
        // }
        callback()
      }
      var validateConfirmPassword = (rule, value, callback) => {
        if (this.dataForm.newPassword !== value) {
          return callback(new Error(this.$t('updatePassword.validate.confirmPassword')))
        }
        callback()
      }
      return {
        password: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    changeInput (value) {
      if (value !== value.replace(/ /g, '')) {
        this.$message({
          message: '密码中不允许存在空格！',
          type: 'warning'
        })
        this.dataForm.newPassword = value.replace(/ /g, '')
        // console.log('密码发生变化=============>', value)
      }
    },
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs.dataForm.validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http.put('/sys/user/password', {
          confirmPassword: Encrypt(this.dataForm.confirmPassword),
          newPassword: Encrypt(this.dataForm.newPassword),
          password: Encrypt(this.dataForm.password)
        }).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              clearLoginInfo()
              this.$router.replace({ name: 'login' })
            }
          })
        }).catch(() => {})
      })
    }, 1000, { leading: true, trailing: false })
  }
}
</script>
