<template>
  <div style="height: 100%">
    <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 }}℃</div>
            <div class="condition">
              {{ screenDetail.weather_condition }}
            </div>
          </div>
        </div>
        <template v-for="(router, index) in leftNav">
          <div
            v-if="router.child && router.child.length == 0"
            :key="index"
            class="router-link"
            @click="clearRoute"
          >
            <router-link
              :to="{ path: router.menu_url, query: { token_code: token } }"
            >
              {{ router.right_name }}</router-link
            >
          </div>

          <el-dropdown v-else :key="index" placement="bottom">
            <div :class="['parent-router', router.isActive ? 'active' : '']">
              {{ router.right_name
              }}{{ childRouteName ? `/${childRouteName}` : "" }}
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                v-for="(child, cIndex) in router.child"
                :key="cIndex"
              >
                <div @click="setRoute(router, child)">
                  <router-link
                    :to="{ path: child.menu_url, query: { token_code: token } }"
                  >
                    {{ child.right_name }}</router-link
                  >
                </div>
              </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">
        <div class="invisible"></div>
        <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 } }"
          >
            <span @click="clearRoute">{{
              router.right_name
            }}</span></router-link
          >

          <el-dropdown v-else :key="index" placement="bottom">
            <div :class="['parent-router', childRouteName ? 'active' : '']">
              {{ router.right_name
              }}{{ router.childRoute ? `/${childRouteName}` : "" }}
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                v-for="(child, cIndex) in router.child"
                :key="cIndex"
              >
                <div @click="setRoute(router, child)">
                  <router-link
                    :to="{ path: child.menu_url, query: { token_code: token } }"
                  >
                    {{ child.right_name }}</router-link
                  >
                </div>
              </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 class="route-container">
      <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: [],
      childRouteName: "",
      totalMenu: [],
    };
  },
  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() {
      // window.innerHeight  窗口内高度  //document.getElementsByTagName("html")[0].offsetHeight
      // var windowHeight = window.innerHeight;
      // var documentHeight =
      //   document.getElementsByTagName("html")[0].offsetHeight;
      // console.log(windowHeight - documentHeight);
      var deviceHeight = window.innerHeight;
      var scale = deviceHeight / 1080;
      document.body.style.zoom = scale;
      document.body.style.height = (1 / scale) * 100 + "vh";

      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/";
    },
    setRoute(router, child) {
      router.isActive = true;
      console.log(router, child);
      this.childRouteName = child.right_name;
      this.$forceUpdate();
    },
    clearRoute() {
      this.childRouteName = "";
      this.leftNav = this.leftNav.map((item) => {
        return {
          ...item,
          isActive: false,
        };
      });
      this.rightNav = this.rightNav.map((item) => {
        return {
          ...item,
          isActive: false,
        };
      });
      this.$forceUpdate();
    },
  },
  computed: {
    token() {
      return localStorage.getItem("token");
    },
  },
  mounted() {
    getBaseInfo().then((res) => {
      this.screenDetail = res.data;
      document.title = res.data.project_name;
      let menu = res.data.menu.map((item) => {
        return {
          ...item,
          isActive:
            item.child.filter((item) => {
              return this.$route.path == item.menu_url;
            }).length > 0,
        };
      });
      let selectedMenu = menu.filter((item) => {
        return item.isActive;
      });
      if (selectedMenu.length > 0) {
        this.childRouteName =
          selectedMenu[0].child.filter((item) => {
            return this.$route.path == item.menu_url;
          })[0] &&
          selectedMenu[0].child.filter((item) => {
            return this.$route.path == item.menu_url;
          })[0].right_name;
      }
      if (res.data.menu.length >= 3) {
        this.leftNav = menu.slice(0, 3);
        this.rightNav = menu.slice(3, res.data.menu.length);
      } else {
        this.leftNav = [...menu];
      }
    });
    var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1;
    if (isChrome) {
      this.bodyScale();
    }

    this.getTime();
    setInterval(() => {
      this.getTime();
    }, 1000);
  },
};
</script>
<style scoped lang="less">
.route-container {
  height: calc(100% - 78px);
}
.nav-header {
  display: flex;
  align-content: center;
  line-height: 70px;
  padding: 0 18px;
  height: 78px;
  margin: 0px;
  background: url("@/assets/nav.png") no-repeat;
  background-size: 100% 100%;
  .left-nav {
    width: calc(100% / 3);
    display: flex;
    justify-content: flex-start;
    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 {
  position: relative;
  margin-right: 0 !important;
}
.router-link {
  margin-right: 30px;
  &:last-child {
    margin-right: 0;
  }
}
a,
.parent-router {
  white-space: nowrap;
  cursor: pointer;
  min-width: 66px;
  text-align: center;
  display: inline-block;
  text-decoration: none;
  margin-right: 30px;
  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,
  &.active {
    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: flex;
  flex-wrap: nowrap;
  line-height: 26px;
  vertical-align: top;
  margin-top: 8px;
  margin-right: 50px;
  .weather-text {
    color: #fff;
    display: inline-block;
  }
  .condition {
    font-size: 14px;
  }
  .weather-img {
    vertical-align: top;
    width: 40px;
    height: 40px;
    margin-top: 6px;
    margin-right: 4px;
  }
}
.el-dropdown {
  margin-right: 30px;
}
.el-dropdown-menu {
  padding: 0;
  border: none;
  margin: 0;
  background: none;
  a {
    margin-right: 0;
    width: 86px;
  }
  .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;
}
.invisible {
  height: 100%;
  width: 40px;
  display: inline-block;
  vertical-align: top;
}
.temperature {
  white-space: nowrap;
  font-size: 20px;
}
</style>