<template>
  <el-dialog
    title="修改密码"
    :visible.sync="pwdConfig"
    :append-to-body=true
    top="15%"
    width="600px"
    :before-close="handleClose" class="pwd-change">
    <table style="width: 420px;">
      <tbody>
      <tr>
        <td class="text-general-color" style="width:125px;">旧口令</td>
        <td>
          <span class="text-important-color">* </span><el-input size="small" v-model="pwd1" id="pwd1" placeholder="" name="pwd1" auto-complete="new-password" type="password" style="width: 240px; margin-bottom: 5px;"/>
        </td>
        <td></td>
      </tr>
      <tr>
        <td class="text-general-color">新口令</td>
        <td>
          <span class="text-important-color">* </span><el-input size="small" v-model="pwd2" placeholder="" id="pwd2" name="pwd2" auto-complete="new-password" type="password" style="width: 240px; margin-bottom: 5px;"/>
        </td>
        <td></td>
      </tr>
      <tr>
        <td class="text-general-color">确认口令</td>
        <td>
          <span class="text-important-color">* </span><el-input size="small" v-model="pwd3" id="pwd3" name="pwd3" placeholder="" auto-complete="new-password" type="password" style="width: 240px; margin-bottom: 5px;"/>
        </td>
        <td></td>
      </tr>
      </tbody>
    </table>
    <span slot="footer" class="dialog-footer">
              <awsui-button class="button-general-color" type="primary" @click="saveUserPwd()">确定</awsui-button>
              <awsui-button @click="clearPwd(true)">取消</awsui-button>
            </span>
  </el-dialog>
</template>

<script>
    import awsuiAxios from "../../awsuiAxios";

    export default {
      name: "PwdChange",
      data() {
        return {
          pwdConfig: false,
          pwd1:'',
          pwd2:'',
          pwd3:''
        }
      },
      methods: {
        saveUserPwd() {
          const that = this;
          let type = 'success';
          let msg = '';
          if (that.pwd1 == '') {
            type = 'warning';
            msg = '[旧口令]不允许为空';
          } else if (that.pwd2 == '') {
            type = 'warning';
            msg = '[新口令]不允许为空';
          } else if (that.pwd3 == '') {
            type = 'warning';
            msg = '[确认口令]不允许为空';
          } else if (that.pwd1 == that.pwd2) {
            type = 'warning';
            msg = '新口令和旧口令不能相同';
          } else if (that.pwd2 != that.pwd3) {
            type = 'warning';
            msg = '新口令和确认口令不一致';
          } else if (that.pwd2.indexOf(" ") > -1) {
            type = 'warning';
            msg = '[新口令]不能包含空格';
          } else if (that.pwd3.indexOf(" ") > -1) {
            type = 'warning';
            msg = '[确认口令]不能包含空格';
          }
          if (type != 'success') {
            that.$message({
              message: msg,
              duration: 2000,
              type: type
            });
            return false;
          }
          if (that.validateUpdateLoginpassword(that.pwd2, "新口令")) {
            return false;
          }

          const params1 = {
            url:'jd',
            data:{
              cmd: 'CLIENT_P_PERSON_CONFIG_PW_ISRIGHT',
              oldpwd: that.pwd1
            }
          };
          awsuiAxios.post(params1).then(function (msg) {
            if(msg== '-1'){
              that.$message.error("旧口令输入错误,请重新输入");
              return false;
            }
            const params2 = {
              url:'jd',
              data:{
                cmd: "CLIENT_P_PERSON_CONFIG_PW_SAVE",
                oldpwd: that.pwd1,
                newpwd: that.pwd2
              }
            }
            awsuiAxios.post(params2).then(function (ro) {
              if (ro.result == 'ok') {
                // 关闭密码框
                that.$message({
                  message: ro.msg,
                  type: 'success'
                });
                that.clearPwd(true);
              } else {
                that.$message.error(ro.msg);
              }
            }).catch(error=>{
              console.log(error);
            })
          }).catch(error=>{
            console.log(error);
          })
        },
        /**
         * 校验portal/console 端口令修改；
         * 页面需要有securityMinPwdLength(aws-portal.xml安全配置minPwdLength)、securityMaxPwdLength(aws-portal.xml安全配置maxPwdLength)、isSecurityPwdComplexity(aws-portal.xml安全配置pwdComplexity)参数
         * @param {} value  输入的口令
         * @param {} value  口令的label
         * @return {Boolean}
         */
        validateUpdateLoginpassword(value, labelName) {
          // 允许账户口令最小长度，0表示无限制
          if (securityMinPwdLength > 0) {
            // 允许账户口令最小长度
            if (value.length < securityMinPwdLength) {
              this.$message({
                message: '[' + labelName + ']不得少于' + securityMinPwdLength + '个字符',
                type: 'warning'
              });
              return true;
            }
            // 允许账户口令最大长度，最多32位长度
            if (value.length > securityMaxPwdLength) {
              this.$message({
                message: '[' + labelName + ']不允许超过' + securityMaxPwdLength + '个字符',
                duration: 2000,
                type: 'warning'
              });
              return true;
            }
          }
          // 是否要求强度口令
          if (isSecurityPwdComplexity) {
            //自定义密码强度  参数判断在后台实现
            // var reg = new RegExp(pwdComplexityRegular);
            // if (!reg.test(value)) {
            // 	$.simpleAlert(labelName + 必须包含字母大小写和数字);
            // 	return true;
            // }
          }
          return false;
        },
        //判断口令长度
        checkPasswordLength(pwd1, pwd2) {
          var minLength = jQuery("#minLength").val();
          if (length2(pwd1) < minLength || length2(pwd2) < minLength) {
            $.simpleAlert("口令长度不能少于" + minLength + "位", "error", 2000);
            return false;
          } else {
            return true;
          }
        },
        handleClose(done) {
          this.clearPwd(false);
          done();
        },
        clearPwd(close) {
          const that = this;
          that.pwd1 = '';
          that.pwd2 = '';
          that.pwd3 = '';
          if(close) {
            that.pwdConfig = false;
          }
        }
      }
    }
</script>

<style scoped>
  #pwd1,#pwd2,#pwd3 {
    display: inline-block;
  }
  .pwd-change >>> .el-dialog__body {
    padding: 10px 20px;
    color: #606266;
    font-size: 14px;
    word-break: break-all;
  }
</style>
