<template>
  <div class="navbar sticky flex-between" >
    <div class="flex-center">
      <div class="logo flex-center" style="height: 69px">
        <span style="font-size: 18px">数据服务门户</span>
      </div>
      <!--      <hamburger :toggle-click="toggleSideBar" :is-active="sidebar.opened" class="hamburger-container"/>-->
      <breadcrumb class="breadcrumb-container"/>
    </div>
    <div v-if="roles[0] !== '1'" class="flex-between nav">
      <div class="text-center mr-25">
        <router-link to="/" class="font-14 gesture mt-5">
          <i class="el-icon-pie-chart font-24"/>
          <div class="font-14 gesture mt-5">数据地图</div>
        </router-link>
      </div>
      <div class="text-center mr-25">
        <router-link to="/apply/apply" class="font-14 gesture mt-5">
          <i class="el-icon-files font-24"/>
          <div class="font-14 gesture mt-5">我的申请</div>
        </router-link>
      </div>
      <div class="text-center mr-25">
        <router-link to="/collect/index" class="font-14 gesture mt-5">
          <i class="el-icon-collection font-24"/>
          <div class="font-14 gesture mt-5">
            我的收藏
          </div>
        </router-link>
      </div>
    </div>
    <div class="right-menu">
      <el-dropdown class="avatar-container right-menu-item hover-effect" placement="bottom" trigger="hover">
        <div class="avatar-wrapper">
          <!--img :src="avatar+'?imageView2/1/w/80/h/80'" class="user-avatar"-->
          <el-button type="text" class="font-14 white">
            {{ name }}，您好
            <i class="el-icon-caret-bottom"/>
          </el-button>
        </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" :close-on-click-modal="false" append-to-body title="密码更改">
      <el-form>
        <el-form-item :label-width="formLabelWidth" label="原密码">
          <el-input v-model="oldpassword" show-password placeholder="原密码" type="password"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="新密码">
          <el-input v-model="newpassword" show-password placeholder="新密码 最小长度为8，包含字母(大小写)和数字" type="password"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="确认密码">
          <el-input
            v-model="newpasswordconfirm"
            show-password
            placeholder="确认密码 最小长度为8，包含字母(大小写)和数字"
            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'
const APP_CONFIG = require('@/../public/config')
export default {
  components: {
    Breadcrumb,
    Hamburger
  },
  data() {
    return {
      chPasswrdFormVisible: false,
      formLabelWidth: '100px', // 弹出框form宽
      oldpassword: '',
      newpassword: '',
      newpasswordconfirm: '',
      bg:APP_CONFIG.THEME_COLOR,
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'name',
      'avatar',
      'device',
      'roles'
    ])
  },
  mounted() {
    document.documentElement.style.setProperty('--THEME_COLOR', APP_CONFIG.THEME_COLOR);
  },
  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
      }
      if (vm.newpassword.length < 8) {
        this.$message.info('密码最小长度为8')
        return
      }
      // 必须包含数字、大小写字母和特殊符号
      // (?=.*[\W])   必须有特殊符号
      // (?=.*[A-Z]) 必须有小写字母
      const repw = /^(?=.*[A-Z])(?=.*[a-z])(?=.*[1-9]).{8,}$/
      if (!repw.test(vm.newpassword)) {
        this.$message.info('密码需是8-32位的英文大小写加数字组合')
        return
      }
      const 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: 70px;
  border-bottom: 1px solid #ebeef5;
  background: var(--THEME_COLOR);;
  z-index: 999;
  color: #ffffff;

  .nav {
    &:hover {
      background: var(--THEME_COLOR);
    }
  }

  .hamburger-container {
    height: 100%;
    cursor: pointer;
    transition: background .3s;
    color: white;

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

  .logo {
    background: var(--THEME_COLOR);
    color: #ffffff;
    margin-left: 40px;

    span {
      font-size: 20px;
      font-weight: 700;
    }
  }

  .breadcrumb-container {
  }

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

  .right-menu {

    &:focus {
      outline: none;
    }

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

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

      .avatar-wrapper {
        .el-icon-caret-bottom {
          cursor: pointer;
          font-size: 12px;
          //动画
          transition: all .3s;

          &:hover {
            //放大
            transform: scale(1.2);
          }
        }

        &:hover {
          button {
            color: #ffffff;
          }
        }

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