<template>
  <div class="full-size">
    <div class="content-item">
      <el-radio v-model="policy" label="current">保留当前密码</el-radio><br/>
      <el-radio v-model="policy" label="default">重新自动生成默认密码</el-radio><br/>
      <div class="custom-password-wrapper">
        <el-radio v-model="policy" label="custom">重新设置自定义密码</el-radio>
        <el-input type="password" v-if="policy === 'custom'" style="width: 250px;" v-model="customPassword" auto-complete="new-password"></el-input>
      </div>
      <el-button v-permission="'user:ModifyInstancePassword'"  type="primary" style="margin-top: 15px;" @click="resetLoginPassword">确定</el-button>
    </div>
  </div>
</template>
<script>
export default {
  name: 'UserPasswordSetting',
  props: {
    userId: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      policy: 'current',
      customPassword: ''
    }
  },
  created () {
  },
  mounted () {
  },
  methods: {
    // 设置登录密码
    resetLoginPassword (row) {
      let password = ''
      if (this.policy === 'current') {
        return this.$message.success('重置密码成功')
      } else if (this.policy === 'custom') {
        if (this.customPassword.length < 4) {
          this.$message.warning('登录密码至少4位！')
          return
        }
        password = this.customPassword
      } else {
        password = this.generateRandowPwd(10)
      }

      this.$confirm('请确认是否重置密码?', '重置密码', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$http.put(this.$apiUrl('updatePasswordOfUser'), { id: this.userId, password })
          .then(response => {
            this.$message.success('重置密码成功')
            if (this.policy === 'default') {
              this.$alert(password, '请保存好自动生成的密码', {
                confirmButtonText: '记下了'
              })
            }
            // 恢复默认
            this.policy = 'current'
            this.customPassword = ''
          })
          .catch(error => {
            if (error.response) {
              this.$message.error(`重置密码失败! ${error.response.data.message}`)
            } else {
              this.$message.error(`重置密码失败! ${error.message}`)
            }
          })
      })
    },
    generateRandowPwd (pasLen) {
      const pasArr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', '-', '$', '%', '&', '@', '+', '!']
      let password = ''
      const pasArrLen = pasArr.length
      for (let i = 0; i < pasLen; i++) {
        const x = Math.floor(Math.random() * pasArrLen)
        password += pasArr[x]
      }
      return password
    }
  }
}
</script>
<style lang="scss" scoped>
  @import '../../style/mixin';
  .content-item {
    margin-left: 10px;
    span {
      display: inline-block;
      font-size: $text-font-size;
      color: $color-text-primary;
      font-weight: bold;
      margin-bottom: 10px;
    }
    .el-radio {
      line-height: 30px;
      font-size: $text-font-size;
      color: $color-text-primary;
    }
  }
</style>
