<template>
  <div class="admin-user-bar">
    <el-dropdown class="user panel-item" trigger="click" @command="handleCommand">
      <div class="user-avatar">
        <el-avatar :size="30" :src="require('@/assets/user-avatar.jpeg')"></el-avatar>
        <label>{{ userinfo.name }}</label>
        <div class="el-icon--right"><i class="el-icon-arrow-down"></i></div>
      </div>
      <template slot="dropdown">
        <el-dropdown-menu>
          <!-- <el-dropdown-item command="userinfo">个人信息</el-dropdown-item> -->
          <el-dropdown-item command="changePwd" v-if="$permission('/admin/updatePassword')" >修改密码</el-dropdown-item>
          <el-dropdown-item divided command="doLogout">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>

    <el-dialog title="修改密码" :visible.sync="changePwdDialogVisible" width="500px">
      <el-form :model="changePwdRuleForm" :rules="changePwdRules" ref="changePwdRuleForm" v-if="changePwdDialogVisible">
        <el-form-item label="旧密码" prop="oldPassword">
          <el-input v-model="changePwdRuleForm.oldPassword" type="password" placeholder="输入旧密码" />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="changePwdRuleForm.newPassword" type="password" placeholder="输入新密码" />
        </el-form-item>
        <el-form-item label="再次输入" prop="repeat">
          <el-input v-model="changePwdRuleForm.repeat" type="password" placeholder="再次输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="changePwdDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitChangePwd" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";

export default {
  data() {
    const validateRepeat = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value !== this.changePwdRuleForm.newPassword) {
        callback(new Error("两次输入密码不一致"))
      } else {
        callback()
      }
    }
    return {
      changePwdDialogVisible: false,
      btnLoading: false,
      changePwdRuleForm: {
        oldPassword: '',
        newPassword: '',
        repeat: '',
      },
      changePwdRules: {
        oldPassword: {
          required: true,
          message: '请输入旧密码'
        },
        newPassword: {
          required: true,
          message: '请输入新密码'
        },
        repeat: [{ validator: validateRepeat, trigger: 'blur', required: true }]
      }
    }
  },
  computed: {
    userinfo() {
      return this.$store.state.admin.userinfo
    }
  },
  mounted() {
  },
  methods: {
    resetChangePwdRuleForm() {
      this.changePwdRuleForm = {
        oldPassword: '',
        newPassword: '',
        repeat: '',
      }
    },
    handleCommand(command) {
      switch (command) {
        case 'doLogout':
          this.$api.adminLogout().then(response => {
            this.$router.push({ path: '/admin/login' })
          })
          break;
        case 'changePwd':
          this.resetChangePwdRuleForm()
          this.changePwdDialogVisible = true
          break;
        case 'userinfo':
          break;
      }
    },
    submitChangePwd() {
      this.$refs.changePwdRuleForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true
          this.$api.ChangePassword(this.changePwdRuleForm).then(response => {
            this.$message.success('修改成功')
            this.changePwdDialogVisible = false
            this.btnLoading = false
          }).catch(err => {
            this.btnLoading = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
.admin-user-bar {
  display: flex;
  align-items: center;
  height: 100%;

  .panel-item:hover {
    background: rgba(255, 255, 255, 0.1) !important;
  }

  .user label {
    color: #fff;
  }

  .panel-item {
    padding: 0 10px;
    cursor: pointer;
    height: 100%;
    display: flex;
    align-items: center;
  }

  .panel-item i {
    font-size: 16px;
  }

  .panel-item:hover {
    background: rgba(0, 0, 0, 0.1);
  }

  .user-avatar {
    height: 49px;
    display: flex;
    align-items: center;
  }

  .user-avatar label {
    display: inline-block;
    margin-left: 5px;
    font-size: 12px;
    cursor: pointer;
    color: #383838;
  }

  .bell-bar {
    margin-right: 10px;
    cursor: pointer;
    padding: 10px;
    border-radius: 5px;
    position: relative;
  }
  .bell-bar:hover {
    background-color: rgba(153, 148, 148, 0.35);
  }
  .bell-bar .el-icon-bell {
    font-size: 20px;
    z-index: 20;
  }
  .bell-bar .current-num {
    position: absolute;
    background: red;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    font-size: 13px;
    text-align: center;
    top: 20px;
    left: 20px;
  }
}
</style>
