<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" @mouseout = "showEdit = false" class="cursor-pointer userManageAvatarUrl" :src="userInfo.avatarUrl">
          </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="cursor-pointer left z-10 w-64px h-64px rounded-35px bg-[#000] opacity-0" title="请选择合适图片"
            @change="addFile($event)" @mouseover="showEdit = true" @mouseout = "showEdit = false">
          </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" @keydown.enter="vailidate">
        <br/>
        <input  v-model="form.newPassword"  class="login_input_password new" type="password" placeholder="新密码（6-16个字符，区分大小写）" @keydown="keydown($event)" @input="changeErr" @keydown.enter="vailidate">
        <br/>
        <input  v-model="form.newPasswordConfirm"  class="login_input_password confirm" type="password" placeholder="确认新密码" @keydown="keydown($event)" @input="changeErr" @keydown.enter="vailidate">
        <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';
import { mapState } from 'vuex'

export default {
  data() {
    return {
      changePsd: false,
      username: "",
      form: {
        password:'',
        newPassword: '',
        newPasswordConfirm:''
      },
      errMsg:'',
      isRight: false,
      showEdit: false
    }
  },
  watch:{
    dialogVisiable(){
        this.isRight = false
        this.errMsg = ''
    }
  },
  computed:{
    ...mapState(["userInfo"])
  },
  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){
      const formData = new FormData()
      formData.append("file",event.target.files[0])
      this.$axios.post('avatar/picUpload',formData).then((res)=> {
        if(res.code === 200) {
          this.getUserInfo()
        }
      })
    }
  },
  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;
    }
    .userManageAvatarUrl{
      object-fit: cover;
      width: 64px;
      height: 64px;
      display: inline-block;
      border: none;
      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: #979797cc;
      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>