<template>
  <div class="navbar">
    <hamburger id="hamburger-container" :is-active="sidebar.opened" class="hamburger-container"
      @toggleClick="toggleSideBar" />

    <breadcrumb id="breadcrumb-container" class="breadcrumb-container" v-if="!topNav" />
    <top-nav id="topmenu-container" class="topmenu-container" v-if="topNav" />

    <div class="right-menu">
      <!--  -->
      <div class="flex-row mr10 height100 align-items">
        <el-button type="text" class="mr10" @click="go2Home" icon="el-icon-s-home">
          首页
        </el-button>
        <el-button type="text" class="mr10 qrcode-box" @click="checkQrCodeImg">
          <svg-icon icon-class="qrCode2" class-name="icon" style="width:25px;height:25px;" />
          <span>医院二维码</span>
        </el-button>
      </div>
    </div>
    <Popup ref="popup" title="" width="400px" :is-crud="false">
      <div class="flex-column justify-content" v-loading="loading">
        <img :src="qrCode" style="width:200px;height:200px;margin: 0 auto 20px;">
        <el-button type="text" @click="downloadPng">点击下载二维码</el-button>
      </div>
    </Popup>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import Breadcrumb from "@/components/Breadcrumb";
import TopNav from "@/components/TopNav";
import Hamburger from "@/components/Hamburger";
import Screenfull from "@/components/Screenfull";
import Popup from "@/components/Popup";
import { hospitalQrcode } from "@/api/hospital";

export default {
  components: {
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull,
    Popup,
  },
  computed: {
    ...mapGetters(["sidebar", "avatar", "device", "name"]),
    setting: {
      get() {
        return this.$store.state.settings.showSettings;
      },
      set(val) {
        this.$store.dispatch("settings/changeSetting", {
          key: "showSettings",
          value: val,
        });
      },
    },
    topNav: {
      get() {
        return this.$store.state.settings.topNav;
      },
    },
  },
  data() {
    return {
      qrCode: "",
      loading: false,
    };
  },
  methods: {
    checkQrCodeImg() {
      this.$refs.popup.open();
      this.loading = true;
      hospitalQrcode()
        .then((res) => {
          this.qrCode = res.data;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 下载图片
    async downloadPng() {
      const aEl = document.createElement("a");
      //设置href指向
      aEl.href = this.qrCode;
      //设置图片下载名字
      aEl.download = "二维码";
      aEl.click();
    },
    toggleSideBar() {
      this.$store.dispatch("app/toggleSideBar");
    },
    go2Home() {
      location.href = process.env.VUE_APP_ADMIN_HOME_URL;
    },
    async logout() {
      this.$confirm("确定注销并退出系统吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$store.dispatch("LogOut").then(() => {
            location.href = process.env.VUE_APP_BASE_PATH;
          });
        })
        .catch(() => { });
    },
  },
};
</script>

<style lang="scss" scoped>
.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background 0.3s;
    -webkit-tap-highlight-color: transparent;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .topmenu-container {
    position: absolute;
    left: 50px;
  }

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

  .right-menu {
    float: right;
    height: 100%;
    // line-height: 50px;
    flex-direction: row;
    align-items: center;

    .height100 {
      height: 100%;
    }

    &: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 0.3s;

        &:hover {
          background: rgba(0, 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>
