/**
* 头部菜单
*/ 
<template>
<div class="header-menu">
  <el-menu class="el-menu-demo" mode="horizontal" background-color="#334157" text-color="#fff" active-text-color="#fff">
    <el-button class="buttonimg">
      <img class="showimg" :src="collapsed?imgsq:imgshow" @click="toggle(collapsed)">
    </el-button>
    <el-submenu index="2" class="submenu">
      <!-- <template slot="title">{{user.userRealName}}</template> -->
      <template slot="title">
        <span>
          <img :src="user.avatar" alt="" class="img-avatar">
          </span>
        {{user.username}}
        </template>
      <el-menu-item @click="handleSetInfo()" index="2-2">个人中心</el-menu-item>
      <el-menu-item @click="exit()" index="2-3">退出</el-menu-item>
    </el-submenu>
  </el-menu>
  <el-dialog title="个人中心" :visible.sync="updateFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="updateForm" :rules="rules" ref="updateForm" label-position="top">
        <el-form-item label="设置头像" prop="expires">
          <img :src="updateForm.avatar" alt="" style="border-radius:50%;width:80px">
        </el-form-item>
        <el-form-item label="修改账号" prop="expires">
         <el-input   v-model="updateForm.account" :disabled="updateForm.setaccount === 0" auto-complete="off" placeholder="请输入新账号"></el-input>
          <p class="error-tip">当前用户可修改账号次数为 0</p>
        </el-form-item>
        <el-form-item label="设置新密码" prop="password">
          <el-input type="password"  v-model="updateForm.password" label="请输入新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="password">
          <el-input type="password"  v-model="psw" label="请确认新密码"></el-input>
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button   @click="closeDialog">取消</el-button>
        <el-button
          type="primary"
          :loading="loading"
          class="title"
          @click="submitUpdateForm('updateForm')"
        >保存</el-button>
      </div>
    </el-dialog>
</div>
</template>

<script>
import { updateSelf } from '../api/basisMG'
export default {
  name: 'navcon',
  data() {
    return {
      updateFormVisible:false,
      updateForm:{},
      psw:"",
      collapsed: true,
      imgshow: require('../assets/img/show.png'),
      imgsq: require('../assets/img/sq.png'),
      user: {},
      // rules表单验证
      rules: {
        password: [
          { required: false, message: "请输入新密码", trigger: "blur" }
        ]
      },
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
    this.user = JSON.parse(localStorage.getItem('user'))
    this.updateForm = this.user
  },
  methods: {
    handleSetInfo(){
      this.updateForm.password = ''
      this.updateFormVisible = true
    },
    // 退出登录
    exit() {
      this.$confirm('退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          setTimeout(() => {
            this.$store.commit('logout', 'false')
            this.$router.push({ path: '/login' })
            this.$message({
              type: 'success',
              message: '已退出登录!'
            })
          }, 1000)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    // 更新授权信息
    submitUpdateForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          if(this.updateForm.password === this.psw){
          updateSelf(this.updateForm)
            .then(res => {
              this.updateFormVisible = false;
              this.loading = false;
              if (!res.error) {
                this.$message({
                  type: "success",
                  message: "更新成功"
                });
                localStorage.setItem('user',JSON.stringify(this.updateForm))
              } else {
                this.$message({
                  type: "info",
                  message: res.error.message
                });
              }
            })
            .catch(err => {
              this.updateFormVisible = false;
              this.loading = false;
              this.$message.error("更新失败，请稍后再试！");
            });
          }else{
            this.$message.error("两次密码输入不一致");
          }
        } else {
          return false;
        }
      });
    },
    // 切换显示
    toggle(showtype) {
      this.collapsed = !showtype
      this.$root.Bus.$emit('toggle', this.collapsed)
    }
  }
}
</script>
<style scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  border: none;
}

.header-menu /deep/ .el-submenu__title{
  border: none !important;
}
.submenu {
  float: right;
}
.buttonimg {
  height: 60px;
  background-color: transparent;
  border: none;
}
.showimg {
  width: 26px;
  height: 26px;
  position: absolute;
  top: 17px;
  left: 17px;
}
.showimg:active {
  border: none;
}
.img-avatar{
  width: 35px;
  height: 35px;
  border-radius: 50%;
  margin-right: 10px;
  margin-bottom: 5px;
}

.error-tip{
  font-size: 12px;
  color: rgb(236, 104, 104);
  margin-left: 10px;
}
</style>