<template>
  <div class="navbar">
    <svg-icon icon-class="navLogo" class="nav-logo" />
    <!-- <hamburger :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" /> -->

    <div class="right-menu">
      <span class="name">{{ name }}</span>
      <el-dropdown class="avatar-container" trigger="click">
        <div class="avatar-wrapper">
          <!-- <img :src="avatar+'?imageView2/1/w/80/h/80'" class="user-avatar"> -->
          <svg-icon icon-class="out" style="width: 18px; height: 18px;" />
          <!-- <i class="el-icon-caret-bottom" /> -->
        </div>
        <el-dropdown-menu slot="dropdown" class="user-dropdown">
          <el-dropdown-item @click.native="openPwdDialog">
            <span style="display:block;">修改密码</span>
          </el-dropdown-item>
          <el-dropdown-item @click.native="logout">
            <span style="display:block;">退出</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <cy-dialog ref="updatePwd" title="修改密码" :top="'20vh'" :append="true">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="150px" label-position="top" class="rule-form-orange">
        <el-form-item label="新密码：" prop="newPwd" style="width: 100%;">
          <el-input v-model="formData.newPwd" maxlength="20" type="password" placeholder="请输入新密码" />
        </el-form-item>
        <el-form-item label="确认新密码：" prop="confirmPwd" style="width: 100%;">
          <el-input v-model="formData.confirmPwd" maxlength="20" type="password" placeholder="请确认新密码" />
        </el-form-item>
      </el-form>
      <div class="submit-btn">
        <el-button @click="$refs.updatePwd.show = false;">关闭</el-button>
        <el-button type="primary" @click="confirmSubmit">确定</el-button>
      </div>
    </cy-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import request from '@/utils/request'
import md5 from 'js-md5'

// import Hamburger from '@/components/Hamburger'

export default {
  components: {
    // Hamburger
  },
  data() {
    return {
      formData: {
        newPwd: '',
        confirmPwd: ''
      },
      rules: {
        newPwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        confirmPwd: [
          { required: true, message: '请确认新密码', trigger: 'blur' }
        ]
      },
      showDialog: false
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'sidebar',
      'avatar'
    ])
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      // this.$router.push(`/login?redirect=${this.$route.fullPath}`)
      this.$router.push(`/login`)
    },
    openPwdDialog() {
      this.formData.newPwd = ''
      this.formData.confirmPwd = ''
      // this.showDialog = true
      this.$refs.updatePwd.show = true
    },
    confirmSubmit() {
      this.$refs.ruleForm.validate(flag => {
        if (flag) {
          this.submitPwd()
        }
      })
    },
    submitPwd() {
      if (this.formData.confirmPwd !== this.formData.newPwd) {
        this.$message.error('两次输入的密码不一致')
        return
      }
      request({
        url: '/user/updatePassword/' + md5(this.formData.newPwd),
        method: 'put',
        params: { Loading: true }
      }).then(() => {
        this.$message.success('修改成功')
        // this.showDialog = false
        this.$refs.updatePwd.show = false
        this.logout()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.navbar {
  height: 80px;
  overflow: hidden;
  position: relative;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .nav-logo {
    width: 136px;
    height: 42px;
    line-height: 80px;
    margin-left: 40px;
  }
  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background .3s;
    -webkit-tap-highlight-color:transparent;

    &:hover {
      background: rgba(0, 0, 0, .025)
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .right-menu {
    height: 100%;
    line-height: 50px;
    margin-top: 30px;
    .name {
      font-size: 16px;
      color: #e0e0e0;
      margin-right: 20px;
    }
    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }

    .avatar-container {
      margin-right: 30px;

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }

}
</style>
