<template>
  <div>
    <div class="nav-header">
      <div class="left-nav">
        <div class="weather">
          <img
            class="weather-img"
            :src="screenDetail.weather_code_url"
            alt=""
          />
          <div class="weather-text">
            <div class="temperature">
              {{ screenDetail.temperature }}
              <span style="font-size: 14px">℃</span>
            </div>
            <div class="condition">{{ screenDetail.weather_condition }}</div>
          </div>
        </div>
        <template v-for="(router, index) in leftNav">
          <router-link
            v-if="router.child && router.child.length == 0"
            :key="index"
            :to="{ path: router.menu_url, query: { token_code: token } }"
          >
            {{ router.right_name }}</router-link
          >

          <el-dropdown v-else :key="index" placement="bottom">
            <div class="parent-router">
              {{ router.right_name }}
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                v-for="(child, cIndex) in router.child"
                :key="cIndex"
              >
                <router-link
                  :to="{ path: child.menu_url, query: { token_code: token } }"
                >
                  {{ child.right_name }}</router-link
                >
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </div>
      <div class="mid-title">
        <el-tooltip
          class="item"
          effect="dark"
          :content="screenDetail.project_name"
          placement="bottom"
        >
          <div class="overflowed">{{ screenDetail.short_name }}</div>
        </el-tooltip>
      </div>
      <div class="right-nav">
        <template v-for="(router, index) in rightNav">
          <router-link
            v-if="router.child && router.child.length == 0"
            :key="index"
            :to="{ path: router.menu_url, query: { token_code: token } }"
          >
            {{ router.right_name }}</router-link
          >

          <el-dropdown v-else :key="index" placement="bottom">
            <div class="parent-router">
              {{ router.right_name }}
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                v-for="(child, cIndex) in router.child"
                :key="cIndex"
              >
                <router-link
                  :to="{ path: child.menu_url, query: { token_code: token } }"
                >
                  {{ child.right_name }}</router-link
                >
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>

        <div class="clock">
          <div class="time">
            {{ time }}
          </div>
          <div class="date">
            <div class="week">{{ weekDay }}</div>
            <div class="day">{{ date }}</div>
          </div>
        </div>
      </div>
    </div>
    <div>
      <router-view></router-view>
    </div>
  </div>
</template>
<script>
import { getBaseInfo } from "@/api/index";
export default {
  name: "Index",
  data() {
    return {
      time: "",
      weekDay: "",
      date: "",
      zoom: 1,
      screenDetail: {},
      leftNav: [],
      rightNav: [],
    };
  },
  methods: {
    getTime() {
      let date = new Date();
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      month = month > 9 ? month : "0" + month;
      day = day < 10 ? "0" + day : day;
      let today = year + "-" + month + "-" + day;
      this.date = today;
      let hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      let min =
        date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      let sec =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      this.time = `${hour}:${min}:${sec}`;

      let str = "星期";
      let weekDay = new Date().getDay();
      switch (weekDay) {
        case 0:
          str += "日";
          break;
        case 1:
          str += "一";
          break;
        case 2:
          str += "二";
          break;
        case 3:
          str += "三";
          break;
        case 4:
          str += "四";
          break;
        case 5:
          str += "五";
          break;
        case 6:
          str += "六";
          break;
      }
      this.weekDay = str;
    },
    bodyScale() {
      var deviceHeight = window.screen.height;
      var scale = deviceHeight / 1220;
      document.body.style.zoom = scale;
      document.styleSheets[document.styleSheets.length - 1].insertRule(
        "canvas:not(.amap-layer):not(.amap-labels) {zoom: " +
          Number(1 / scale).toFixed(2) +
          "!important}"
      );

      document.styleSheets[document.styleSheets.length - 1].insertRule(
        "canvas:not(.amap-layer):not(.amap-labels) {transform: scale(" +
          scale +
          ")!important}"
      );

      document.styleSheets[document.styleSheets.length - 1].insertRule(
        "canvas:not(.amap-layer):not(.amap-labels) {transform-origin: 0 0}"
      );
    },
    jumpBack() {
      window.location.href = window.location.origin + "/pweb/m/";
    },
  },
  computed: {
    token() {
      return localStorage.getItem("token");
    },
  },
  mounted() {
    getBaseInfo().then((res) => {
      this.screenDetail = res.data;
      document.title = res.data.project_name;
      if (window.screen.width < 1800) {
        if (res.data.menu.length >= 3) {
          this.leftNav = res.data.menu.slice(0, 3);
          this.rightNav = res.data.menu.slice(3, res.data.menu.length);
        } else {
          this.leftNav = [...res.data.menu];
        }
      } else {
        if (res.data.menu.length >= 4) {
          this.leftNav = res.data.menu.slice(0, 4);
          this.rightNav = res.data.menu.slice(4, res.data.menu.length);
        } else {
          this.leftNav = [...res.data.menu];
        }
      }
    });
    this.bodyScale();

    this.getTime();
    setInterval(() => {
      this.getTime();
    }, 1000);
  },
};
</script>
<style scoped lang="less">
.nav-header {
  display: flex;
  align-content: center;
  line-height: 70px;
  padding: 0 18px;
  min-height: 78px;
  margin: 0px;
  background: url("@/assets/nav.png") no-repeat;
  background-size: 100% 100%;
  .left-nav {
    width: calc(100% / 3);
    span {
      color: #fff;
      margin-right: 16px;
    }
  }
  .mid-title {
    width: calc(100% / 3);
    font-size: 28px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #fefeff;
    text-align: center;
    letter-spacing: 3px;
  }
  .right-nav {
    width: calc(100% / 3);
    text-align: left;
    .clock {
      float: right;
      width: 124px;
      text-align: center;
      .time {
        text-align: center;
        font-size: 24px;
        color: #22c3ed;
        letter-spacing: 4px;
        line-height: 46px;
      }
      .date {
        font-size: 12px;
        color: #fff;
        display: flex;
        justify-content: space-between;
        line-height: 14px;
      }
    }
  }
}
.parent-router {
  margin-right: 0 !important;
}
a,
.parent-router {
  cursor: pointer;
  min-width: 66px;
  text-align: center;
  display: inline-block;
  text-decoration: none;
  margin-right: 8px;
  color: #80a1c6;
  padding: 9px 12px;
  border: 1px solid #0f5da6;
  border-radius: 4px;
  line-height: 16px;
  font-size: 16px;
  background-image: linear-gradient(#0d3353, #183e75);
  &:last-child {
    margin-right: 0;
  }
  &:hover {
    color: #fff;
    border-color: #27acfb;
    position: relative;
    &::after {
      position: absolute;
      content: "";
      width: 0;
      height: 0;
      bottom: 0px;
      left: 50%;
      transform: translateX(-50%);
      border-right: solid 6px transparent;
      border-left: solid 6px transparent;
      border-bottom: solid 6px #22c3ed;
      filter: drop-shadow(0px -1px 3px rgba(0, 234, 255, 0.8));
    }
  }
}

.router-link-exact-active.router-link-active {
  color: #fff;
  border-color: #27acfb;
  position: relative;
}

.router-link-exact-active.router-link-active::after {
  position: absolute;
  content: "";
  width: 0;
  height: 0;
  bottom: 0px;
  left: 50%;
  transform: translateX(-50%);
  border-right: solid 6px transparent;
  border-left: solid 6px transparent;
  border-bottom: solid 6px #22c3ed;
  filter: drop-shadow(0px -1px 3px rgba(0, 234, 255, 0.8));
}
.overflowed {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  padding: 0 30px;
}
.weather {
  display: inline-block;
  line-height: 24px;
  vertical-align: top;
  margin-right: 20px;
  margin-top: 8px;
  .weather-text {
    color: #fff;
    font-size: 18px;
    display: inline-block;
  }
  .weather-img {
    vertical-align: top;
    width: 50px;
    height: 50px;
    margin-right: 6px;
  }
}
.el-dropdown {
  margin-right: 8px;
}
.el-dropdown-menu {
  padding: 0;
  border: none;
  margin: 0;
  background: none;
  a {
    margin-right: 0;
    width: 80px;
  }
  .el-dropdown-menu__item:focus,
  .el-dropdown-menu__item:not(.is-disabled):hover {
    background: transparent !important;
  }
}
.el-popper[x-placement^="bottom"] .popper__arrow::after {
  display: none;
}
</style>