<template>
  <div>
    <div v-if="!changePsd">
      <div class="accountmanage-tittle">
        <p>账号管理</p>
      </div>  
      <div class="accountmanage-content">
        <div class="changeheader flex justify-center">
          <span><img src="http://user-file.17pdf.com/avatars/2020/11/16/0577b907be980556bdafa23fb0be0732-jpg.jpg"></span>
          <div class="headers">
          </div>
        </div>

        <div class="row-name">
          <span>昵称</span>
          <input v-model="username" @blur="handlerChangeUserName">
          <!-- <p class="err-prompt">用户名已经存在</p> -->
        </div>
        <div class="changepwd" @click="changepwdModal()">
          修改密码
        </div>
      </div>
    </div>
    <div v-else>
      <div class="accountmanage-tittle">
        <p>修改密码</p>
      </div>
      <div class="login_content changepwd-content">
        <input v-model="form.password" class="login_input_password" type="password" placeholder="原始密码">
        <br/>
        <input  v-model="form.newPassword"  class="login_input_password new" type="password" placeholder="新密码(6-16个字符,区分大小写)">
        <br/>
        <input  v-model="form.newPassword_confirm"  class="login_input_password confirm" type="password" placeholder="确认新密码">
        <br/>
        <div class="error-mess error-mess-signup" >
          <span id="error-message error-tip">{{errMsg}}</span>
        </div>

        <button class="logging" @click="vailidate">确认</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      changePsd: false,
      username: this.$store.state.userInfo?.memberInfo?.name,
      form: {
        password:'',
        newPassword: '',
        newPassword_confirm:''
      },
      errMsg:''
    }
  },

  methods: {
    // 点击注册前置校验
    vailidate() {
      if(this.form.newPassword !== this.form.newPassword_confirm) {
        this.errMsg = '密码不一致'
      }else if(this.form.newPassword.length < 6) {
        this.errMsg = '密码位数不能少于6位'
      }else if(this.form.newPassword.length> 16){
        this.errMsg = '密码位数不能大于16位'
      }else {
        this.errMsg = ''
        this.handlerResetPsw()
      }
    },
    handlerResetPsw() {
      this.$axios.post('members/modifyPassword', this.form).then((res)=> {
        if(res.code === 200) {
          this.$emit('close')
        }
      })
    },
    changepwdModal() {
      this.changePsd = true
    },
    handlerChangeUserName() {
      this.$axios.get(`/members/modifyNickname?name=${this.username}`).then((res)=> {
        if(res.code === 200) {
          this.getUserInfo()
        }
      })
    },
    getUserInfo()  {
      this.$axios.get('members/getMemberInfo').then((res)=> {
        if(res.code === 200) {
          this.$store.commit('setUser',res.result)
          localStorage.setItem('userInfo',JSON.stringify(res.result))
        }
      })
    },
    resetInterfaceStatus() {
      this.changePsd = false
      this.form.password = ''
      this.form.newPassword = ''
      this.form.newPassword_confirm = ''
    }
  }
}
</script>

<style lang="scss" scoped>
.accountmanage-tittle {
  color: rgb(255, 79, 79);
  text-align: center;
  margin: 0;

  p {
    font-size: 20px;
    line-height: 64px;
  }
}

.accountmanage-content {
  .changeheader {
    text-align: center;
    margin: 0 24px;

    span img {
      width: 64px;
      border-radius: 35px;
    }

    .headers {
      margin-top: 8px;
      padding-bottom: 24px;
      border-bottom: 1px solid rgb(240, 240, 240);
      img {
        width: 36px;
        border-radius: 35px;
        margin: 8px 2px 0px 2px;
      }
    }

  }

  .row-name {
    border-top: 1px solid rgb(240, 240, 240);
    margin: 0 24px;
    height: 64px;
    line-height: 64px;
    position: relative;
    margin-top: 25px;

    span {
      color: rgb(153, 153, 153);
      font-size: 14px;
      float: left;
      width: 10%;
    }

    input {
      width: 88%;
      float: right;
      height: 30px;
      line-height: 30px;
      text-align: right;
      border: 0px;
      margin-top: 12px;
      border: none;
      outline: medium;

      &:focus {
        text-align: right;
        border: 1px;
      }
    }

    p {
      position: absolute;
      right: 0px;
      bottom: 0px;
      height: 20px;
      width: 100%;
      color: rgb(255, 79, 79);
      font-size: 14px;
      line-height: 20px;
      text-align: right;
      display: none;
    }
  }

  .changepwd {
    color: rgb(51, 51, 51);
    height: 64px;
    font-size: 14px;
    cursor:pointer;
    margin: 0 24px;
    line-height: 64px;
    border-top: 1px solid rgb(240, 240, 240);
    background: url(http://cn-file.17pdf.com/website/account/ic_cebian_normal.png) right 50% no-repeat;
  }
}
// .changepwd-content {
//   margin: 20px 0px;
// }
.login_content {
  text-align: center;
  font-size: 14px;
  .error-mess {
    position: relative;
    font-size: 14px;
    display: none;
    span{
      display: inline-block;
      background: #808080;
      color: rgb(255, 255, 255);
      padding: 5px 15px;
      border-radius: 4px;
    }
  }
  .error-mess-signup {
    position: initial;
    clear: both;
    margin-top: 15px;
  }
  .login_input_password {
    height: 60px;
    width: 360px;
    border-width: 0 0 1px 0;
    border-color: rgb(238, 238, 238);
    padding-left: 42px;
    padding-top: 16px;
    line-height: 44px;
    outline:none;
    background: url(http://cn-file.17pdf.com/website/account/ic_login_inputpassword.png) no-repeat 0px 28px;
    &.new {
      background: url(http://cn-file.17pdf.com/website/account/ic_lock_new.png) no-repeat 0px 28px;
    }
    &.confirm {
      background: url(http://cn-file.17pdf.com/website/account/ic_lock_sure.png) no-repeat 0px 28px;
    }
  }
  .login_input_password:-webkit-autofill {
    box-shadow: 0 0 0px 1000px white inset;
  }
  .logging {
    width: 360px;
    height: 48px;
    font-size: 16px;
    color: rgb(255, 255, 255);
    border: none;
    background-color: rgb(255, 79, 79);
    margin-bottom: 40px;
    border-radius: 4px;
    outline:none;
  }
}
</style>