<template>
  <el-form>
    <el-form-item :label="$t('profile.password0')">
      <el-input v-model.trim="formData.old" type="password" maxlength="16" />
    </el-form-item>
    <el-form-item :label="$t('profile.password1')">
      <el-input v-model.trim="formData.new" type="password" maxlength="16" />
    </el-form-item>
    <el-form-item :label="$t('profile.password2')">
      <el-input v-model.trim="formData.new2" type="password" maxlength="16" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">{{
        $t("profile.update")
      }}</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { mapGetters } from 'vuex'
import { userInfoChange } from '@/api/index.js'
import { encryptData } from '@/utils/encryption/entrypt'

// import { encryptData } from '@/utils/encryption/entrypt.js'
export default {
  data() {
    return {
      formData: {
        old: '',
        new: '',
        new2: ''
      }
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  methods: {
    submit() {
      if (!this.formData.old) {
        this.$message({
          message: this.$i18n.t('profile.oldPassword'),
          type: 'warning'
        })
        return
      }
      if (!this.formData.new) {
        this.$message({
          message: this.$i18n.t('profile.newPassword'),
          type: 'warning'
        })
        return
      }
      if (!this.formData.new2) {
        this.$message({
          message: this.$i18n.t('profile.newPassword2'),
          type: 'warning'
        })
        return
      }
      if (this.formData.new !== this.formData.new2) {
        this.$message({
          message: this.$i18n.t('profile.passwordDifferent'),
          type: 'warning'
        })
        return
      }
      // 大写小写数字特殊符号四选三
      const reg = /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$%^&*`~()-+=]+$)(?![0-9\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\W_!@#$%^&*`~()-+=]{8,16}$/
      // 大写小写数字特殊符号四种都有
      // const reg = /^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,16}$/
      if (this.formData.new.length < 8) {
        this.$message({
          message: this.$i18n.t('profile.passwordLengthError'),
          type: 'warning'
        })
        return
      }
      if (!reg.test(this.formData.new)) {
        this.$message({
          message: this.$i18n.t('profile.passwordStrongError'),
          type: 'warning'
        })
        return
      }
      // const param = {
      //   old: encryptData(this.formData.old + this.formData.old),
      //   new: encryptData(this.formData.new + this.formData.new)
      // }
      var param = {
        old: this.formData.old,
        new: this.formData.new
        // username: this.name
      }
      param.old = encryptData(param.old)
      param.new = encryptData(param.new)
      userInfoChange(param).then(res => {
        if (res.code === 200) {
          this.$message({
            message: this.$i18n.t('profile.updatePasswordSuccess'),
            type: 'success'
          })
          setTimeout(() => {
            this.$store.dispatch('user/logout')
            this.$router.push(`/login?redirect=${this.$route.fullPath}`)
          }, 1000)
        } else {
          this.$message({
            message: res.message,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
