<template>
  <div class="navbar">
    <hamburger :toggle-click="toggleSideBar" :is-active="sidebar.opened" class="hamburger-container"/>

    <breadcrumb class="breadcrumb-container"/>

    <div class="right-menu">

      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <div class="avatar-wrapper">
          <!--img :src="avatar+'?imageView2/1/w/80/h/80'" class="user-avatar"-->
          <span>退出系统</span>
          <i class="el-icon-caret-bottom"/>
        </div>
        <el-dropdown-menu slot="dropdown">
          <router-link to="/">
            <el-dropdown-item>
              {{ $t('navbar.dashboard') }}
            </el-dropdown-item>
          </router-link>
          <el-dropdown-item>
            <label style="display:block;" @click="btn_changepw">修改密码</label>
          </el-dropdown-item>
          <el-dropdown-item divided>
            <span style="display:block;" @click="logout">{{ $t('navbar.logOut') }}</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>

    <el-dialog :visible.sync="chPasswrdFormVisible" title="密码更改">
      <el-form>
        <el-form-item :label-width="formLabelWidth" label="原密码">
          <el-input v-model="oldpassword" placeholder="原密码" type="password"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="新密码">
          <el-input v-model="newpassword" placeholder="新密码" type="password"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="确认密码">
          <el-input v-model="newpasswordconfirm" placeholder="确认密码" type="password"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btn_cancel">取 消</el-button>
        <el-button type="primary" @click="btn_submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import axios from 'axios'
import md5 from 'js-md5'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'

export default {
  components: {
    Breadcrumb,
    Hamburger
  },
  data() {
    return {
      chPasswrdFormVisible: false,
      formLabelWidth: '100px', // 弹出框form宽
      oldpassword: '',
      newpassword: '',
      newpasswordconfirm: ''
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'name',
      'avatar',
      'device'
    ])
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('toggleSideBar')
    },
    logout() {
      this.$store.dispatch('LogOut').then(() => {
        location.reload()// In order to re-instantiate the vue-router object to avoid bugs
      })
    },
    btn_cancel() {
      this.chPasswrdFormVisible = false
    },
    btn_submit() {
      const vm = this
      if (this.newpassword !== this.newpasswordconfirm) {
        this.$message.error('新密码两次输入不一致')
        return
      }
      if (vm.newpassword === '') {
        this.$message.error('新密码不可以为空')
        return
      }
      var params = new URLSearchParams()
      params.append('oldpassword', md5(vm.oldpassword))
      params.append('newpassword', md5(vm.newpassword))

      axios.post(process.env.BASE_API + '/user/changepw', params).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
        } else {
          vm.oldpassword = ''
          vm.newpassword = ''
          vm.newpasswordconfirm = ''
          vm.chPasswrdFormVisible = false
          vm.$message(obj.message)
        }
      }).catch(function(error) {
        alert(error)
      })
    },
    btn_changepw() {
      this.chPasswrdFormVisible = true
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.navbar {
  height: 50px;
  overflow: hidden;

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background .3s;

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

  .breadcrumb-container {
    float: left;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;

    &: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>
