<template>
  <div class="main-layout">
    <header class="header qm-primary-text">
      <Poptip v-if="ifShowQr" trigger="hover" width="280">
        <span class="name-arrow">手机端登录(Android)</span>
        <div slot="content">
          <Tabs type="card">
            <TabPane label="下载安卓apk">
              <div class="qm-primary-text flex-center">
                <qrcode :value="qrValue" :options="{ width: 200 }"></qrcode>
              </div>
            </TabPane>
            <TabPane label="绑定用户">
              <div class="qm-primary-text flex-center">
                <qrcode :value="qrValue" :options="{ width: 200 }"></qrcode>
                <div>请使用“云考”App扫描</div>
              </div>
            </TabPane>
          </Tabs>
        </div>
      </Poptip>
      <span v-if="ifShowQr" style="margin: auto 20px">|</span>
      <Poptip trigger="hover" width="300">
        <span class="name-arrow"
          >{{ user.displayName }} &nbsp;
          <i
            class="ivu-icon ivu-icon-md-arrow-dropdown"
            style="vertical-align: middle;"
          ></i>
        </span>
        <div slot="content">
          <div class="info qm-primary-text">
            <div class="hr info-row">
              <div>底照</div>
              <div>
                <img class="user-avatar" :src="user.photoPath" alt="无底照" />
              </div>
            </div>
            <div class="hr info-row">
              <div>学号</div>
              <div>{{ user.studentCodeList.join(",") }}</div>
            </div>
            <div class="hr info-row">
              <div>身份证号</div>
              <div>{{ user.identityNumber }}</div>
            </div>
            <div class="hr info-row">
              <div>学习中心</div>
              <div>{{ user.orgName }}</div>
            </div>
            <div
              v-if="!isEpcc"
              style="grid-column: span 2; place-self: center; width: 100%; padding-top: 10px"
            >
              <i-button class="qm-primary-button" long @click="goChangePwd">
                修改密码
              </i-button>
            </div>
          </div>
        </div>
      </Poptip>
      <span style="margin: auto 20px">|</span>
      <a
        class="qm-primary-text"
        style="display: inline-block; margin-right: 20px; text-align: center;"
        @click="() => logout('?LogoutReason=正常退出')"
        >退出登录</a
      >
    </header>
    <transition name="fade" appear>
      <main :key="$route.path" class="content">
        <slot></slot>
      </main>
    </transition>
    <nav class="nav">
      <img :src="getLogo" class="qm-logo" />
      <ul>
        <li>
          <router-link class="link" to="/online-exam">
            在线考试
          </router-link>
        </li>
        <li v-if="!isEpcc">
          <router-link class="link" to="/online-practice">
            在线练习
          </router-link>
        </li>
        <li v-if="!isEpcc">
          <router-link class="link" to="/offline-exam">
            离线考试
          </router-link>
        </li>
        <li v-if="!isEpcc">
          <router-link class="link" to="/site-message">
            <Badge :count="messageUnread" :offset="[20, -20]">
              公告通知
            </Badge>
          </router-link>
        </li>
        <li v-if="!isEpcc">
          <router-link class="link" to="/password">
            修改密码
          </router-link>
        </li>
      </ul>
    </nav>
    <footer class="footer">©️2019 启明泰和</footer>

    <SiteMessagePopup />
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import VueQrcode from "@chenfengyuan/vue-qrcode";
import SiteMessagePopup from "./SiteMessagePopup.vue";

export default {
  name: "MainLayout",
  components: {
    qrcode: VueQrcode,
    SiteMessagePopup,
  },
  data() {
    return {
      appDownloadUrl: "fetching...",
    };
  },
  computed: {
    ...mapState(["user", "siteMessages", "QECSConfig"]),
    ...mapGetters(["isEpcc"]),
    messageUnread() {
      return this.siteMessages.filter(v => v.hasRead === false).length;
    },
    qrValue() {
      const { rootOrgId, studentCodeList, identityNumber } = this.user;
      return JSON.stringify({
        rootOrgId,
        studentCode: studentCodeList.join(","),
        identityNumber,
      });
    },
    ifShowQr() {
      const shouldShow = this.QECSConfig.SHOW_STUDENT_CLIENT_APP_QRCODE;
      return shouldShow === "true";
    },
    getLogo() {
      return this.isEpcc
        ? require("./epcc-logo.png")
        : require("./qm-logo.png");
    },
  },
  async created() {
    const r = await this.$http.get(
      "/api/ecs_core/systemProperty/APP_DOWNLOAD_URL"
    );
    this.appDownloadUrl = r.data;
  },
  methods: {
    goChangePwd() {
      this.$router.push("/password");
    },
  },
};
</script>

<style scoped>
.main-layout {
  display: grid;
  grid-template-areas: "nav header" "nav content" "nav footer";
  min-height: 100vh;
  grid-template-columns: 180px 1fr;
  grid-template-rows: auto 1fr auto;
}

.header {
  grid-area: header;
  height: 50px;
  background-color: #ffffff;
  box-shadow: 0px 1px 0px 0px #eeeeee;

  text-align: right;
  line-height: 50px;
}

.user-avatar {
  display: inline-block;
  width: 80px;
  height: 80px;
  object-fit: contain;
}

.info {
  width: 260px;
}

.info > div {
  min-height: 50px;
}

.hr {
  border-bottom: 1px solid #eeeeee;
}
.info-row {
  display: grid;
  grid-template-columns: 1fr 2fr;
  align-items: center;
}
.info-row > div:nth-of-type(odd) {
  justify-self: left;
}

.info-row > div:nth-of-type(even) {
  justify-self: right;
}

.name-arrow:hover .ivu-icon-md-arrow-dropdown::before {
  content: "\F343";
}

.nav {
  grid-area: nav;
  background-color: rgb(113, 113, 113);
  background-repeat: repeat;
  width: 180px;
  min-height: 100vh;

  display: flex;
  flex-direction: column;
}

.qm-logo {
  width: 80px;
  height: 68px;
  margin: 30px auto;
}

.flex-center {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.link {
  background-image: url(./link.png);
  background-repeat: no-repeat;
  background-position: 30px 50%;
  /* margin-left: -40px; */
  padding-right: 20px;
  line-height: 40px;
  width: 100%;
  display: inline-block;

  color: #ffffff;
}
.link >>> .ivu-badge-count {
  z-index: 1;
}

.router-link-active,
.link:hover {
  background-image: url(./link-active.png);
  background-color: rgba(255, 255, 255, 0.5);
  color: #fafafa;
}

.footer {
  grid-area: footer;
  margin-bottom: 10px;
}
</style>
