<template>
  <div class="nav-bar-container">
    <div class="left-panel">
      <vab-remix-icon class="logo" icon-class="logo" />
      <span class="hidden-xs-only">无症状人群胃癌筛查随访数据中心</span>
    </div>
    <div class="userInfo">
      <error-log></error-log>
      <!-- <avatar></avatar> -->
      <div v-if="$route.path != '/auditqualitycontrol'" class="fontSelect">
        <div
          v-for="(item, index) in fontList"
          :key="index"
          class="font"
          @click="changeFont(index)"
        >
          <div class="label">{{ item.label }}</div>
        </div>
      </div>
      <div
        v-show="curSelectedIndex != -1 && $route.path != '/auditqualitycontrol'"
        class="community"
        @click="openModalFlag"
      >
        <img src="~@/assets/img/DataCenter/shift.png" alt />
        {{
          curSelectedIndex != -1
            ? screeningList[curSelectedIndex - 1]
              ? screeningList[curSelectedIndex - 1].title
              : ""
            : ""
        }}
      </div>

      <div class="user">
        <img src="~@/assets/img/DataCenter/user.png" alt />
        <avatar></avatar>
      </div>
      <el-popover
        v-if="messageCount != 0"
        placement="top-start"
        width="300"
        trigger="hover"
        popper-class="message-pop"
      >
        <div class="messageBox">
          <div
            v-for="(item, index) in messageList"
            :key="index"
            class="message-box mb-24"
          >
            <div class="top-box mb-12">
              <span class="blue-dot mt-5 mr-8"></span>
              <span class="top-text">{{ item.createTime }}</span>
            </div>
            <div
              class="main-text ml-8"
              style="cursor: pointer"
              @click="goSearch(item.bizType, item.id)"
            >
              {{ item.content }}
            </div>
          </div>
          <div v-show="loading" v-loading="loading" class="loading"></div>
        </div>
        <template slot="reference">
          <el-badge :value="messageCount" :class-name="'badge'">
            <div class="user">
              <img src="~@/assets/img/DataCenter/message.png" alt />
              消息
            </div>
          </el-badge>
        </template>
      </el-popover>
      <el-badge v-else :value="0" :class-name="'badge'">
        <div class="user">
          <img src="~@/assets/img/DataCenter/message.png" alt />
          消息
        </div>
      </el-badge>
      <div class="user" @click="$router.push('/home')">
        <img src="~@/assets/img/DataCenter/backhome.png" alt />
        <span>返回首页</span>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex"
import { getMessage, setReadMessege } from "@/api/user"
import Message from "@/mixins/getMessage"
import {
  Avatar,
  // Breadcrumb,
  ErrorLog,
  // FullScreenBar,
  // ThemeBar,
} from "@/layouts/components"

export default {
  name: "NavBar",
  components: {
    Avatar,
    // Breadcrumb,
    ErrorLog,
    // FullScreenBar,
    // ThemeBar,
  },
  mixins: [Message],
  props: {
    curSelectedIndex: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      pulse: false,
      isDot: true,
      screeningList: [
        {
          title: "社区筛查",

          src: require("@/assets/img/DataCenter/shequ.png"),
        },
        {
          title: "医院筛查",

          src: require("@/assets/img/DataCenter/yiyuan.png"),
        },
        {
          title: "体检筛查",

          src: require("@/assets/img/DataCenter/tijian.png"),
        },
      ],
      fontList: [
        { fontSize: "-", label: "A-" },
        { fontSize: 14, label: "A" },
        { fontSize: "+", label: "A+" },
      ],
      fontSize: localStorage.getItem("fontSize") - 0 || 14,
    }
  },
  computed: {
    ...mapGetters({
      collapse: "settings/collapse",
      visitedRoutes: "tagsBar/visitedRoutes",
      device: "settings/device",
      routes: "routes/routes",
      feedBackNumber: "user/feedBackNumber",
      getMessageFlag: "user/getMessageFlag",
    }),
  },
  watch: {
    curSelectedIndex: {
      handler(v) {
        this.messageList = []
        this.pageNum = 1
        this.getMessage()
      },
      immediate: true,
    },
    getMessageFlag(v) {
      if (v) {
        this.messageList = []
        this.pageNum = 1
        this.getMessage()
      }
    },
  },
  created() {
    // this.getMessage()
  },
  mounted() {
    if (document.querySelector(".messageBox")) {
      document
        .querySelector(".messageBox")
        .addEventListener("scroll", this.scrolling)
    }
  },
  methods: {
    scrolling() {
      let scrollHeight = document.querySelector(".messageBox").scrollHeight //实际高度
      let scrollTop = document.querySelector(".messageBox").scrollTop //滚动高度
      let height = document.querySelector(".messageBox").clientHeight //滚动高度
      console.log(scrollHeight, scrollTop)
      if (
        scrollTop + height + 10 >= scrollHeight &&
        this.messageList.length < this.messageCount &&
        !this.loading
      ) {
        this.pageNum++
        this.loading = true
        this.getMessage()
      }
    },
    goSearch(val, id) {
      // 消息已读
      setReadMessege(id).then((res) => {
        if (res.code == 1) {
          this.getMessage()
        }
      })
      if (val == "screen") {
        this.$router.push("/screening/index?checkStatus=2")
      }
    },
    getMessage() {
      this.loading = true
      getMessage({
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        patientFrom: this.curSelectedIndex,
      }).then((res) => {
        if (res.code == 1) {
          this.$store.commit("user/setGetMessageFlag", false)
          this.messageCount = res.data.total
          this.messageList = [...this.messageList, ...res.data.records]
          // console.log(res.data.total)
        } else {
          this.messageCount = 0
        }
        this.loading = false
      })
    },
    handleCollapse() {
      this.$store.dispatch("settings/changeCollapse")
    },
    viewMsg() {
      this.$router.push({ name: "Message" })
    },
    async refreshRoute() {
      this.$baseEventBus.$emit("reloadRouterView")
      this.pulse = true
      setTimeout(() => {
        this.pulse = false
      }, 1000)
    },
    openModalFlag() {
      this.$emit("openModalFlag")
    },
    changeFont(i) {
      let fontSize = this.fontList[i].fontSize
      if (fontSize == "-") {
        if (this.fontSize > 12) {
          this.fontSize = this.fontSize - 2
        }
      } else if (fontSize == "+") {
        if (this.fontSize < 18) {
          this.fontSize = this.fontSize + 2
        }
      } else {
        this.fontSize = 14
      }
      // this.fontIndex = i
      this.$store.commit("table/setFontSize", this.fontSize)
      localStorage.setItem("fontSize", this.fontSize)
      console.log(this.fontSize)
    },
  },
}
</script>

<style lang="scss" scoped>
.nav-bar-container {
  position: relative;
  height: 74px;
  overflow: hidden;
  user-select: none;
  background: #4e68ff;
  box-shadow: $base-box-shadow;
  display: flex;
  justify-content: space-between;
  color: #fff;
  border-radius: 0px 0px 4px 4px;
  .left-panel {
    display: flex;
    align-items: center;
    justify-items: center;
    font-size: 24px;
    font-weight: 600;
    height: 74px;
    max-height: 74px;
    letter-spacing: 2px;

    .logo {
      margin-right: 20px;
      margin-left: 40px;
      font-size: 40px;
      vertical-align: middle;
    }
    .fold-unfold {
      margin-left: 10px;
      font-size: 20px;
      color: $base-color-gray;
      cursor: pointer;
    }

    ::v-deep {
      .breadcrumb-container {
        margin-left: 10px;
      }
    }
  }
  .userInfo {
    display: flex;
    align-items: center;
    padding-right: 40px;
    .fontSelect {
      display: flex;
      margin-right: 20px;
      .font {
        width: 40px;
        height: 40px;
        background: #4e68ff;
        border-radius: 4px;
        border: 1px solid #ffffff;
        text-align: center;
        line-height: 40px;
        cursor: pointer;
        margin-left: 20px;
        &:active {
          box-sizing: border-box;
          background: #ffffff;
          height: 40px;
          color: #4e68ff;
          padding: 1px;
          .label {
            height: 36px;
            border: 1px solid #4e68ff;
          }
        }
      }
    }
    .community {
      width: 106px;
      height: 30px;
      white-space: nowrap;
      background: rgba(255, 255, 255, 0.1);
      color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      img {
        margin-right: 4px;
        width: 18px;
        height: 18px;
      }
    }
    .user {
      white-space: nowrap;
      margin-left: 40px;
      height: 30px;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      color: #fff;
      img {
        margin-right: 4px;
        width: 18px;
        height: 18px;
      }
    }
  }
}
.message-box {
  .top-box {
    display: flex;
    flex-direction: row;
    .blue-dot {
      width: 4px;
      height: 4px;
      background: #4b65ff;
      display: block;
      border-radius: 100%;
    }
    .top-text {
      font-size: 14px;
      font-family: AlibabaPuHuiTiR;
      color: #999999;
    }
  }
}
.messageBox {
  padding: 40px 28px 12px;
  height: 380px;
  // background: pink;
  overflow: auto;
  .loading {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
  }
}
// .el-popper {
//   ::v-deep .popper__arrow {
//     display: none;
//   }
// }
</style>
<style lang="scss">
.message-pop {
  padding: 0px !important;
  z-index: 10;

  .popper__arrow::after {
    display: none;
  }
  .popper__arrow {
    display: none;
  }
}
</style>
