<template>
  <div id="navBar">
    <el-menu class="navbar" mode="horizontal">
      <div class="user-profile-container">
        <div class="user-profile-content">
          <!-- <div class="menu-icons">
          <span class="menu-icon"><i class="el-icon-search icon"></i></span>
          <span class="menu-icon"><i class="el-icon-message icon"></i></span>
          <span class="menu-icon">
            <el-badge is-dot class="item">
              <i class="el-icon-bell icon"></i>
            </el-badge>
          </span>
        </div> -->
          <el-dropdown @command="handleCommand" style="z-index:99">
            <div class="user-profile-body">
              <img class="user-avatar" src="https://img.alicdn.com/tfs/TB1ONhloamWBuNjy1XaXXXCbXXa-200-200.png" />
              <span class="user-name" v-if="user && user.userid">欢迎,{{ (user && user.name) || '' }}</span>
              <span class="user-name" v-else @click="toLogin">请登录</span>
            </div>
            <el-dropdown-menu class="user-dropdown" slot="dropdown" v-if="user && user.userid">
              <el-dropdown-item :divided="true" command="weixin"> <img :src="weixin" style="zoom:0.1" /> 绑定/更绑微信 </el-dropdown-item>
              <el-dropdown-item :divided="true" icon="el-icon-key" command="passwd">
                修改密码
              </el-dropdown-item>
              <el-dropdown-item :divided="true" icon="el-icon-guide" command="logout">
                退出
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <div>
        <def-sel></def-sel>
      </div>
    </el-menu>
    <el-dialog title="修改密码" :visible.sync="dialog" center @close="toClose" :destroy-on-close="true">
      <data-form :data="info" :fields="fields" :rules="rules" @save="handleSave" :isNew="true"> </data-form>
    </el-dialog>
    <el-dialog :visible.sync="wxDialog" title="绑定微信" width="30%" center :destroy-on-close="true">
      <el-row type="flex" justify="center">
        <el-col :span="24" style="text-align:center">
          <qrcode exchange="qrcode.bind" :uri="qrUri" :config="config" :qrcode="qrcode" @toReturn="toReturn"></qrcode>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import Vue from 'vue';
import defSel from '@frame/layout/admin/navBar/default-select.vue';
import '@frame/plugins/setting';
import qrcode from '@frame/components/qrcode.vue';
import dataForm from '@frame/components/form';
import { mapState, mapMutations, createNamespacedHelpers } from 'vuex';
import _ from 'lodash';
const { mapActions } = createNamespacedHelpers('login');
export default {
  name: 'navBar',
  props: {},
  components: { dataForm, qrcode, defSel },
  data: () => ({
    weixin: require('@frame/assets/wechat.png'),
    dataUrl: '',
    info: {},
    dialog: false,
    wxDialog: false,
    fields: [{ label: '密码', required: true, model: 'passwd', type: 'password', tip: '密码至少6位' }],
    rules: {
      passwd: [
        { required: true, message: '请输入密码' },
        { min: 6, message: '密码最少6位', trigger: 'blur' },
      ],
    },
    qrUri: '',
    config: {},
    qrcode: '',
  }),
  created() {},
  computed: {
    ...mapState(['user']),
  },
  methods: {
    ...mapMutations(['deleteUser', 'changeOption']),
    ...mapActions(['login', 'update', 'getQrcode']),
    async toLogout() {
      this.deleteUser();
      this.$notify({
        title: '注销成功',
        type: 'success',
      });
      this.toLogin();
    },
    handleCommand(type) {
      if (type === 'logout') this.toLogout();
      if (type === 'passwd') this.toPasswd();
      if (type === 'weixin') this.bind();
    },
    async toLogin() {
      this.$router.push({ name: 'login' });
    },
    toPasswd() {
      this.dialog = true;
    },
    toClose() {
      this.info = {};
      this.dialog = false;
    },
    async handleSave({ data }) {
      data.id = this.user.id;
      let res = await this.update(data);
      if (this.$checkRes(res, '修改成功', '修改失败')) this.toClose();
    },
    async bind() {
      let res = await this.getQrcode();
      this.$set(this, `qrcode`, res);
      this.$set(this, `config`, { weixin: Vue.config.weixin, stomp: Vue.config.stomp });
      let uri = `/api/train/auth?redirect_uri=${Vue.config.weixin.target}/confirm?uid=${this.user.id}&type=1&qrcode=${res}`;
      this.$set(this, `qrUri`, uri);
      this.wxDialog = true;
    },
    toReturn(message) {
      this.$notify({
        type: 'success',
        message: message.body,
      });
    },
  },
};
</script>

<style lang="less" scoped>
.navbar {
  height: 4rem;
  box-shadow: 0 0.0625rem 0.25rem rgba(0, 21, 41, 0.08);
  .user-profile-container {
    position: absolute;
    right: 1.25rem;
    cursor: pointer;
    .user-profile-content {
      display: flex;
      padding: 1.25rem 0;
    }
    .menu-icons {
      display: flex;
      align-items: center;
      .menu-icon {
        padding: 0 0.75rem;
        .icon {
          display: inline-block;
          font-size: 1.125rem;
          text-align: center;
        }
      }
    }
    .user-profile-body {
      position: relative;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
      text-align: center;
      padding-right: 0.875rem;
    }
    .user-avatar {
      width: 1.5rem;
      height: 1.5rem;
      margin: 0 0.5rem 0 0.75rem;
      border-radius: 0.25rem;
    }
    .user-name {
      color: rgba(0, 0, 0, 0.65);
    }
    .user-department {
      font-size: 0.75rem;
      color: rgba(102, 102, 102, 0.65);
    }
    .el-icon-caret-bottom {
      position: absolute;
      right: 0;
      top: 0.8125rem;
      font-size: 0.75rem;
    }
  }
}
</style>
