<template>
  <div>
    <div v-if="!changePsd">
      <div class="accountmanage-tittle">
        <p>账号管理</p>
      </div>  
      <div class="accountmanage-content">
        <div class="changeheader flex justify-center relative"  >
          <span ><img @mouseover="showEdit = true" @mouseenter="showEdit = true" @mouseout = "showEdit = false" class="cursor-pointer"
             src="http://user-file.17pdf.com/avatars/2020/11/16/0577b907be980556bdafa23fb0be0732-jpg.jpg"></span>
          <div class="absolute headers" v-show="showEdit">
            <span class="absolute z-1"><img src="~/assets/images/common/edit.png"></span>
            <input type="file" accept="image/*" class="left z-10 w-64px h-64px rounded-35px bg-[#000] opacity-0" @change="addFile($event)">
          </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="原始密码" @input="changeErr">
        <br/>
        <input  v-model="form.newPassword"  class="login_input_password new" type="password" placeholder="新密码（6-16个字符，区分大小写）" @keydown="keydown($event)" @input="changeErr">
        <br/>
        <input  v-model="form.newPasswordConfirm"  class="login_input_password confirm" type="password" placeholder="确认新密码" @keydown="keydown($event)" @input="changeErr">
        <br/>
        <div v-show="isRight">
          <div class="error-mess error-mess-signup mb-5px" >
            <span id="error-message error-tip ">{{errMsg}}</span>
          </div>
        </div>

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

<script>
import { Message } from 'element-ui';

export default {
  data() {
    return {
      changePsd: false,
      username: "",
      form: {
        password:'',
        newPassword: '',
        newPasswordConfirm:''
      },
      errMsg:'',
      isRight: false,
      showEdit: false
    }
  },
  watch:{
    dialogVisiable(){
        this.isRight = false
        this.errMsg = ''
    }
  },
  methods: {
    // 点击注册前置校验
    vailidate() {
      const reg = new RegExp(/\s/)
      if(this.form.newPassword !== this.form.newPasswordConfirm) {
        this.isRight=true
        this.errMsg = '密码不一致'
      }else if(this.form.newPassword.length < 6) {
        this.isRight=true
        this.errMsg = '密码位数不能少于6位'
      }else if(this.form.newPassword.length> 16){
        this.isRight=true
        this.errMsg = '密码位数不能大于16位'
      }else if(reg.test(this.form.newPassword)){
        this.isRight=true
        this.errMsg = '新密码中不能存在空格'
      }else {
        this.errMsg = ''
        this.handlerResetPsw()
      }
    },
    handlerResetPsw() {
      this.$axios.post('members/modifyPassword', this.form).then((res)=> {
        if(res.code === 200) {
          // console.log(this.form)
          this.$emit('close')
          this.isRight = false
          this.errMsg = ''
        }else{
          this.isRight = true
          this.errMsg = res.data.msg
        }
      })
    },
    changepwdModal() {
      this.changePsd = true
    },
    handlerChangeUserName() {
      this.$axios.get(`/members/modifyNickname?name=${this.username}`).then((res)=> {
        if(res.code === 200) {
          this.getUserInfo()
        }else{
          Message.error(res.data.msg)
          this.getUserInfo()
          this.username = this.$store.state.userInfo.name
        }       
      })
    },
    getUserInfo()  {
      this.$axios.get('members/getMemberInfo').then((res)=> {
        if(res.code === 200) {
          this.$store.commit('setUser',res.result.memberInfo)
        }
      })
    },
    resetInterfaceStatus() {
      this.changePsd = false
      this.form.password = ''
      this.form.newPassword = ''
      this.form.newPasswordConfirm = ''
    },
    //不允许输入空格
    keydown(event){
	    if(event.keyCode == 32){
		    event.returnValue = false
        this.errMsg = '请勿输入空格'
        this.isRight = true
        }
    },
    changeErr(){
      this.errMsg = ''
      this.isRight = false
    },
    //上传图片
    addFile(event){
      console.log(event,event.target.files)
    }
  },
  props:["dialogVisiable"],
  mounted() {
    this.username = this.$store.state.userInfo.name
  },
}
</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 {
      width: 64px;
      height: 64px;
      margin: 0 24px;
      padding-bottom: 24px;
      display:flex;
      justify-content:center;
      align-items:center;
      border-radius: 35px;
      background: #979797e6;
      img {
        width: 20px;
        margin-top: 22px;
        border-radius: 35px;
      }
    }

  }

  .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: 50px;
      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: rgba(0, 0, 0, 0.4);
    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>