<template>
  <div >
   <div title="修改密码" class="msg-img user-warp" @click="editPasword" style="display:flex;">
    <div class="user-info">
      <span class="item" style='margin-top:2px;'>{{info.userName}}</span>
      <span class="item">{{info.name}}</span>
    </div>

     <img src="../../imgs/user.png" alt="">
   </div>
    <!-- <span class="btn-text">你好， {{info.name}}</span>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item @click.native="editPasword">
      <d2-icon name="power-edit" class="d2-mr-5"/>
      修改密码
      </el-dropdown-item>
      <el-dropdown-item @click.native="logOff">
        <d2-icon name="power-off" class="d2-mr-5"/>
        注销
      </el-dropdown-item>
    </el-dropdown-menu> -->
    <el-dialog title="修改密码"
               :visible.sync="editPaswDialog" @close="closeEditPasw"
               width="50%">
      <div slot='title' class="title-bold">
        <span>修改密码</span>
      </div>
       <el-form :model="editPasForm" :rules="fromrule" ref="editPasRef">
            <el-form-item label="用户名" :label-width="formLabelWidth">
                <el-input v-model="editPasForm.username" disabled></el-input>
            </el-form-item>
            <el-form-item label="原密码" prop="oldpsw" :label-width="formLabelWidth" >
                <el-input v-model="editPasForm.oldpsw" autocomplete="off" type='password' placeholder='请输入原密码' @input='e => editPasForm.oldpsw = inputMe(e)'></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newpas" :label-width="formLabelWidth" >
                <el-input v-model="editPasForm.newpas" autocomplete="off" type='password' placeholder='请输入新密码' @input='e => editPasForm.newpas = inputMe(e)'></el-input>
            </el-form-item>
            <el-form-item label="密码确认" prop="pwd" :label-width="formLabelWidth" >
                <el-input v-model="editPasForm.pwd" autocomplete="off" type='password' placeholder='请再次输入新密码' @input='e => editPasForm.pwd = inputMe(e)'></el-input>
            </el-form-item>
        </el-form>
      <div slot="footer"   align='center'>
        <el-button type="primary" @click="cancel" size="medium">取 消</el-button>
        <el-button type="primary" @click="saveEditPas" size="medium">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'
export default {
  data() {
    var validatePassword = (rule, value, callback) => {
      if (!/\S/.test(value)) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    var validateComfirmPassword = (rule, value, callback) => {
      if (!/\S/.test(value)) {
        callback(new Error('确认密码不能为空'))
      } else if (this.editPasForm.newpas !== value) {
        callback(new Error('确认密码与密码输入不一致'))
      } else {
        callback()
      }
    }
    return {
      editPaswDialog: false,
      formLabelWidth: '100px',
      editPasForm: {
        username: '',
        oldpsw: '',
        newpas: '',
        pwd: ''
      },
      fromrule: {
        newpas: [
            { validator: validatePassword, trigger: 'blur' },
            { min: 1, max: 30, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        pwd: [
            { validator: validateComfirmPassword, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.editPasForm.username = this.info.name
  },
  computed: {
    ...mapState('d2admin/user', [
      'info'
    ])
  },
  methods: {
    ...mapActions('d2admin/account', [
      'logout'
    ]),
    /**
     * @description 登出
     */
    logOff () {
      this.logout({
        vm: this,
        confirm: true
      })
    },
    inputMe(e) {
      return e.replace(/[<>#?:"：？@,.;']/g, '')
    },
    editPasword() {
      this.editPaswDialog = true
      this.$nextTick(() => {
        this.$refs.editPasRef.resetFields()
      })
    },
    saveEditPas() {
      this.$refs.editPasRef.validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/sys/user/password'),
            method: 'post',
            data: {
              password: this.editPasForm.oldpsw,
              newPassword: this.editPasForm.pwd,
              username: this.editPasForm.username,
              stationId: localStorage.getItem('sysSign')
            }
          }).then((data) => {
            // console.log(data)
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.editPaswDialog = false
                }
              })
            }
          })
        }
      })
    },
    closeEditPasw() {
       this.editPaswDialog = false
    },
    cancel() {
      this.$confirm('确认取消?', '修改密码', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
           this.editPaswDialog = false
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退出'
          })
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.user-warp{

  .user-info{
    width: 84px;
    height: 36px;
    background: url(../../imgs/subwayBg.png);
    background-size: 100% 100%;
    text-align:center;
    opacity: 0;
    float: right;
    z-index: -3;
    margin-right:-84px;
    transition:all .3s ease;
    -webkit-transition: all .3s ease;
    .item{
      display: block;
      padding-right:10px;
      font-size: 12px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: rgba(255, 255, 255, 0.75);
    }
}
}
.user-warp:hover .user-info{
    opacity: 1;
    margin-right:-11px;
    z-index: 1;

}

</style>
